📄 exceptioninfocache.java
字号:
package org.expframework.exceptionhandler;
import java.util.HashMap;
import org.expframework.data.ExceptionInfoDTO;
import org.expframework.xml.XmlParser;
import org.expframework.xml.XmlUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* This is a singleton which acts as a cache for <code>ExceptionInfoDTO</code>
* objects.
*
* <p>
* <b>Overview: </b>
* <p>
* It reads a file called <code>exceptionInfo.xml</code> which contains a
* mapping of the exception type with message key and context indicator. It
* parses the <code>exceptionInfo.xml</code> file and creates
* <code>ExceptionInfoDTO</code> objects out of that. The sample XML file will
* be something like this.
*
* <pre>
* <?xml version='1.0'?>
* <exceptioninfo>
* <exception name="UniqueConstraintException">
* <messagecode>errorcode.uniqueconstraint</message>
* <contextind>true</contextindi>
* </exception>
* <exception name="XmlParseException">
* <messagecode>errorcode.xmlparse</message>
* <contextind>false</contextindi>
* </exception>
* </exceptionInfo>
* </pre>
*
* Above exceptioninfo.xml is based on following DTD
*
* <pre>
* <!ELEMENT exceptioninfo(exception+) >
* <!ELEMENT exception(messagecode, contextind?, confirmationind ?, loggingtype? >
* <!ATTLIST exception name CDATA #REQUIRED>
* <!ELEMENT messagecode (#PCDATA)>
* <!ELEMENT contextind (#PCDATA)>
* <!ELEMENT confirmationind (#PCDATA)>
* <!ELEMENT loggingtype (#PCDATA)>
* </pre>
*
* In above DTD, contextind, confirmationind and loggingtype tags are
* optional.While creating ExceptionInfoDTO objects, following values are taken
* as default for these tags if not specified in the XML.
*
* <pre>
* contextind ---> false
* confirmationind --> false
* loggingtype --> error (other possible values are "warning", "info" and "nologging" if specified).
* </pre>
*
* It's mandatory to specify "nologging" as logging type for confirmation
* exception (confirmationind = true) in <code>exceptioninfo.xml</code>.
*
* @author ShriKant
*
* @version 1.0
*/
public class ExceptionInfoCache {
private static final ExceptionInfoCache SINGLETON = new ExceptionInfoCache();
private HashMap ecache = new HashMap();
/**
* It's a private constructor for singleton instance.
*/
private ExceptionInfoCache() {
try {
Document doc = XmlParser.parse("/expinfo.xml");
Element[] elements = XmlUtil.getElements("exception", doc);
if (elements != null && elements.length > 0) {
for (int i = 0; i < elements.length; i++) {
Element exElement = elements[i];
addException(exElement);
}
}
} catch (Throwable t) {
ecache = null;
}
}
private void addException(Element exElement) {
String exName = exElement.getAttribute("name");
String context = XmlUtil.getTagValue(exElement, "contextind");
String messageCode = XmlUtil.getTagValue(exElement, "messagecode");
boolean contextInd = false;
boolean confirmationInd = false;
if (context != null)
contextInd = Boolean.valueOf(context).booleanValue();
String confirmation = XmlUtil.getTagValue(exElement, "confirmationind");
if (confirmation != null)
confirmationInd = Boolean.valueOf(confirmation).booleanValue();
String loggingType = XmlUtil.getTagValue(exElement, "loggingtype");
ExceptionInfoDTO exInfo = new ExceptionInfoDTO(messageCode, contextInd,
confirmationInd, loggingType);
ecache.put(exName, exInfo);
}
/**
* Gets the singleton instance of <code>ExceptionInfoCache</code> class.
*
* @return <code>ExceptionInfoCache</code> instance
*
*/
public static ExceptionInfoCache getInstance() {
return SINGLETON;
}
/**
* Based on exception type passed it returns the
* <code>ExceptionInfoDTO</code> object from cache.
*
* @param errorId
* error id String.
* @return <code>ExceptionInfoDTO</code>
*
*/
public ExceptionInfoDTO getExceptionInfo(String errorId) {
if (ecache == null)
return null;
return (ExceptionInfoDTO) ecache.get(errorId);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -