📄 exceptionmanager.java
字号:
package oracle.otnsamples.AQ.Manager;
/**
* @author Rajat Gupta
* @version 1.0
*
* Name of the Application : ExceptionManager.java
* Development Environment : Oracle 9i JDeveloper
* Creation/Modification History :
*
* Rajat Gupta 15-Jan-2001 Created
*
*/
// Servlet Imports
import javax.servlet.http.HttpServletRequest;
// Util Imports
import java.util.Hashtable;
// Net Imports
import java.net.URL;
// W3C Imports
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.SAXException;
// Oracle XML Imports
import oracle.xml.parser.v2.DOMParser;
import oracle.xml.parser.v2.XMLDocument;
import oracle.xml.parser.v2.XMLParser;
import oracle.xml.parser.v2.XMLParseException;
// IO Imports
import java.io.IOException;
// Oracle XML Schema Imports
import oracle.xml.parser.schema.XSDBuilder;
import oracle.xml.parser.schema.XMLSchema;
// Other files
import oracle.otnsamples.AQ.Helper.CreateURL;
/**
* This is the main class that handles all the exceptions occurring in the
* sample. All the exceptions generated in the sample will throw a new
* exception with some ID which will be propagated to this class. This
* class parses a XML file Exceptions.xml which has the String to be
* displayed for each ID. If an exception is thrown which is not caught
* by the sample, then a default error message will be shown to the user.
* @see Manager.java
*/
public class ExceptionManager implements Manager{
/**
* This is a Hashtable which stores the exception ID and the String
* to be displayed for that ID. This Hashtable is populated by parsing
* the Exceptions.xml
*/
private final Hashtable m_exceptionTable;
/**
* This constructor creates the URL to the Exceptions.xml and
* parses it.
*
* @exception Exception If an unreported exception is thrown
* @see CreateURL.java
*/
public ExceptionManager() throws Exception{
CreateURL createURL = new CreateURL();
URL xmlURL = createURL.getURL("AQConfig/Exceptions.xml");
URL schemaURL = createURL.getURL("AQConfig/Exception.xsd");
m_exceptionTable = parseXML(xmlURL, schemaURL);
}
/**
* This is the main method that does the actual parsing of the XML file.
* It stores the exception ID and its value in a Hashtable which is
* returned.
*
* @exception Exception If an unreported exception is thrown
* @param p_url URL to Exceptions.xml
* @return Exception Hashtable
*/
static Hashtable parseXML(URL p_xmlURL, URL p_schemaURL) throws Exception{
Hashtable exceptionParameters = new Hashtable();
try{
// Define the variables used for parsing
Node childNode, node;
NodeList nodeList;
NamedNodeMap nnm;
// Get an instance of the parser
DOMParser parser = new DOMParser();
XSDBuilder builder = new XSDBuilder();
XMLSchema schemadoc = (XMLSchema)builder.build(p_schemaURL);
parser.setXMLSchema(schemadoc);
// Set various parser options: validation on,
// warnings shown, error stream set to stderr.
parser.setErrorStream(System.err);
// This statement will be used when we write the schema for the XML file
parser.setValidationMode(XMLParser.SCHEMA_VALIDATION);
parser.showWarnings(true);
// Parse the document.
parser.parse(p_xmlURL);
// Obtain the document.
XMLDocument doc = parser.getDocument();
// Get all the Child Nodes in a NodeList
nodeList = doc.getFirstChild().getNextSibling().getNextSibling().getChildNodes();
// Iterate through the nodelist and get the Attribute and value of
// DataSource used to connect to the DataBase. Store these then in
// the Hashtable.
for (int j=0; j<nodeList.getLength() ; j++){
childNode = nodeList.item(j);
nnm = childNode.getAttributes();
node = nnm.item(0);
exceptionParameters.put(node.getNodeValue(),
childNode.getFirstChild().getFirstChild().getNodeValue());
}
return exceptionParameters;
}
catch(XMLParseException p_xmlexp){
p_xmlexp.printStackTrace();
throw new Exception("AQ-1017");
}catch(IOException p_ioexp){
p_ioexp.printStackTrace();
throw new Exception("AQ-1005");
}
catch(SAXException p_saxexp){
p_saxexp.printStackTrace();
throw new Exception("AQ-1008");
}
catch(Exception p_exp){
throw p_exp;
}
}
/**
* This method is called if any exception occurs in the application.
* It gets the corresponding message to be displayed for the ID from the
* Hashtable. If the ID is not present, then a default ID is assigned.
*
* @param p_request HttpServlet Request Object
* @param p_exception Exception thrown
* @exception Exception If an unreported exception is thrown
*/
public void getException(HttpServletRequest p_request, Exception p_exception) throws Exception{
// Get the Exception ID
String id = p_exception.getMessage();
// If ID is null or some other exception is reported then
// assign it a default value
if ((id == null) || (!id.startsWith("AQ"))){
id = "AQ-1012";
}
// Get the Exception String to be displayed
String displayString = (String)m_exceptionTable.get(id);
p_request.setAttribute("Exception", displayString);
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -