📄 xmlblasterexception.java
字号:
/*------------------------------------------------------------------------------Name: XmlBlasterException.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE fileComment: Basic xmlBlaster exception.------------------------------------------------------------------------------*/package org.xmlBlaster.util;import java.io.StringWriter;import java.io.PrintWriter;import java.io.ByteArrayOutputStream;import java.text.MessageFormat;import java.util.logging.Logger;import org.xmlBlaster.util.def.ErrorCode;import org.xmlBlaster.util.Timestamp;import org.xmlBlaster.util.Global;import org.xmlBlaster.util.def.Constants;/** * The basic exception handling class for xmlBlaster. * <p> * This exception will be thrown in remote RMI calls as well. * </p> * <p> * The getMessage() method returns a configurable formatted string * here is an example how to configure the format in your xmlBlaster.properties: * <pre> * XmlBlasterException.logFormat=XmlBlasterException errorCode=[{0}] node=[{1}] location=[{2}] message=[{4} : {8}] * XmlBlasterException.logFormat.internal= XmlBlasterException errorCode=[{0}] node=[{1}] location=[{2}]\nmessage={4} : {8}\nversionInfo={5}\nstackTrace={7} * XmlBlasterException.logFormat.resource= defaults to XmlBlasterException.logFormat * XmlBlasterException.logFormat.communication= defaults to XmlBlasterException.logFormat * XmlBlasterException.logFormat.user= defaults to XmlBlasterException.logFormat * XmlBlasterException.logFormat.transaction= defaults to XmlBlasterException.logFormat * XmlBlasterException.logFormat.legacy= defaults to XmlBlasterException.logFormat * </pre> * where the replacements are: * <pre> * {0} = errorCodeStr * {1} = node * {2} = location * {3} = isServerSide // exception thrown from server or from client? * {4} = message * {5} = versionInfo * {6} = timestamp * {7} = stackTrace * {8} = embeddedMessage * {9} = errorCode.getDescription() * // {10} = transactionInfo IBM's JDK MakeFormat only supports 9 digits * // {11} = lang IBM's JDK MakeFormat only supports 9 digits * </pre> * <p> * You can register your own exception handler which intercepts all XmlBlasterException creations * and for example do a shutdown on certain ErrorCodes</p> * <pre> * java -Dorg.xmlBlaster.util.I_XmlBlasterExceptionHandler=MyHandler org.xmlBlaster.Main * </pre> * @author "Marcel Ruff" <xmlBlaster@marcelruff.info> * @since 0.8+ with extended attributes * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/admin.errorcodes.html">The admin.errorcodes requirement</a> * @see org.xmlBlaster.test.classtest.XmlBlasterExceptionTest */public class XmlBlasterException extends Exception implements java.io.Serializable{ private static Logger log = Logger.getLogger(XmlBlasterException.class.getName()); private static final long serialVersionUID = -973794183539996697L; private static I_XmlBlasterExceptionHandler exceptionHandler; transient private final Global glob; transient private ErrorCode errorCodeEnum; private String errorCodeStr; private final String node; private String location; private final String lang; private final String versionInfo; transient private Timestamp timestamp; private final long timestampNanos; private final String stackTrace; private boolean isServerSide; transient private final Throwable cause; // Since JDK 1.4 this is available in Throwable, we keep it here to support older JDK versions private String embeddedMessage; private final String transactionInfo; private final static String DEFAULT_LOGFORMAT = "XmlBlasterException errorCode=[{0}] serverSideException={3} node=[{1}] location=[{2}] message=[{4} : {8}]"; private final static String DEFAULT_LOGFORMAT_INTERNAL = "XmlBlasterException serverSideException={3} node=[{1}] location=[{2}]\n" + "{8}\n" + "stackTrace={7}\n" + "versionInfo={5}\n" + "errorCode description={9}\n"; private String logFormatInternal; private final String logFormatResource; private final String logFormatCommunication; private final String logFormatUser; //private final String logFormatTransaction; private final String logFormatLegacy; private final String logFormat; static { String cname = null; try { cname = System.getProperty("org.xmlBlaster.util.I_XmlBlasterExceptionHandler"); if (cname != null) { try { Class clz = ClassLoader.getSystemClassLoader().loadClass(cname); exceptionHandler = (I_XmlBlasterExceptionHandler) clz.newInstance(); } catch (ClassNotFoundException ex) { Class clz = Thread.currentThread().getContextClassLoader().loadClass(cname); exceptionHandler = (I_XmlBlasterExceptionHandler) clz.newInstance(); } } } catch (Exception ex) { System.err.println("Could not load I_XmlBlasterExceptionHandler \"" + cname + "\""); ex.printStackTrace(); } } /** * The errorCodeEnum.getDescription() is used as error message. */ public XmlBlasterException(Global glob, ErrorCode errorCodeEnum, String location) { this(glob, errorCodeEnum, location, (String)null, (Throwable)null); } public XmlBlasterException(Global glob, ErrorCode errorCodeEnum, String location, String message) { this(glob, errorCodeEnum, location, message, (Throwable)null); } public XmlBlasterException(Global glob, ErrorCode errorCodeEnum, String location, String message, Throwable cause) { this(glob, errorCodeEnum, (String)null, location, (String)null, message, (String)null, (Timestamp)null, (String)null, (String)null, (String)null, (glob==null)?true:glob.isServerSide(), cause); } /** * For internal use: Deserializing and exception creation from CORBA XmlBlasterException */ public XmlBlasterException(Global glob, ErrorCode errorCodeEnum, String node, String location, String lang, String message, String versionInfo, Timestamp timestamp, String stackTrace, String embeddedMessage, String transcationInfo, boolean isServerSide) { this(glob, errorCodeEnum, node, location, lang, message, versionInfo, timestamp, stackTrace, embeddedMessage, transcationInfo, isServerSide, (Throwable)null); } private XmlBlasterException(Global glob, ErrorCode errorCodeEnum, String node, String location, String lang, String message, String versionInfo, Timestamp timestamp, String stackTrace, String embeddedMessage, String transcationInfo, boolean isServerSide, Throwable cause) { //super(message, cause); // JDK 1.4 only super((message == null || message.length() < 1) ? errorCodeEnum.getLongDescription() : message); this.glob = (glob == null) ? Global.instance() : glob; this.logFormat = this.glob.getProperty().get("XmlBlasterException.logFormat", DEFAULT_LOGFORMAT); this.logFormatInternal = this.glob.getProperty().get("XmlBlasterException.logFormat.internal", DEFAULT_LOGFORMAT_INTERNAL); this.logFormatResource = this.glob.getProperty().get("XmlBlasterException.logFormat.resource", this.logFormat); this.logFormatCommunication = this.glob.getProperty().get("XmlBlasterException.logFormat.communication", this.logFormat); this.logFormatUser = this.glob.getProperty().get("XmlBlasterException.logFormat.user", this.logFormat); //this.logFormatTransaction = this.glob.getProperty().get("XmlBlasterException.logFormat.transaction", this.logFormat); this.logFormatLegacy = this.glob.getProperty().get("XmlBlasterException.logFormat.legacy", this.logFormat); this.errorCodeEnum = (errorCodeEnum == null) ? ErrorCode.INTERNAL_UNKNOWN : errorCodeEnum; this.errorCodeStr = this.errorCodeEnum.getErrorCode(); this.node = (node == null) ? this.glob.getId() : node; this.location = location; this.lang = (lang == null) ? "en" : lang; // System.getProperty("user.language"); this.versionInfo = (versionInfo == null) ? createVersionInfo() : versionInfo; this.timestamp = (timestamp == null) ? new Timestamp() : timestamp; this.timestampNanos = this.timestamp.getTimestamp(); this.cause = cause; this.stackTrace = (stackTrace == null) ? createStackTrace() : stackTrace; String causeStr = ""; if (this.cause != null) { if (this.cause instanceof XmlBlasterException) { causeStr = ((XmlBlasterException)this.cause).getMessage(); } else { causeStr = this.cause.toString(); } } this.embeddedMessage = (embeddedMessage == null) ? causeStr : embeddedMessage; // cause.toString() is <classname>:getMessage() this.transactionInfo = (transcationInfo == null) ? "<transaction/>" : transcationInfo; this.isServerSide = isServerSide; I_XmlBlasterExceptionHandler eh = exceptionHandler; if (eh != null) eh.newException(this); } public final void changeErrorCode(ErrorCode errorCodeEnum) { if (this.embeddedMessage == null || this.embeddedMessage.length() < 1) { this.embeddedMessage = "Original erroCode=" + this.errorCodeStr; } this.errorCodeEnum = (errorCodeEnum == null) ? ErrorCode.INTERNAL_UNKNOWN : errorCodeEnum; this.errorCodeStr = this.errorCodeEnum.getErrorCode(); } public final Global getGlobal() { return this.glob; } /** * @return The error code enumeration object, is never null */ public final ErrorCode getErrorCode() { if (this.errorCodeEnum == null) { try { this.errorCodeEnum = ErrorCode.toErrorCode(this.errorCodeStr); } catch (IllegalArgumentException e) { this.errorCodeEnum = ErrorCode.INTERNAL_UNKNOWN; } } return this.errorCodeEnum; } public final boolean isErrorCode(ErrorCode code) { return this.errorCodeEnum == code; } public final String getErrorCodeStr() { return this.errorCodeStr; } public final String getNode() { return this.node; } public final String getLocation() { return this.location; } /** Overwrite the location */ public final void setLocation(String location) { this.location = location; } public final String getLang() { return this.lang; } /** * Configurable with property <i>XmlBlasterException.logFormat</i>, * <i>XmlBlasterException.logFormat.internal</i> <i>XmlBlasterException.logFormat.resource</i> etc. * @return e.g. errorCode + ": " + getMessage() + ": " + getEmbeddedMessage() */ public String getMessage() { Object[] arguments = { (errorCodeStr==null) ? "" : errorCodeStr, // {0} (node==null) ? "" : node, // {1} (location==null) ? "" : location, // {2} new Boolean(isServerSide()), // {3} getRawMessage(), // {4} (versionInfo==null) ? "" : versionInfo, // {5} (timestamp==null) ? "" : timestamp.toString(), // {6} (stackTrace==null) ? "" : stackTrace, // {7} (embeddedMessage==null) ? "" : embeddedMessage, // {8} (errorCodeEnum==null) ? "" : errorCodeEnum.getUrl() // {9} // NOTE: IBM JDK 1.3 can't handle {} greater 9! //(errorCodeEnum==null) ? "" : errorCodeEnum.getLongDescription(), // {9} //(transactionInfo==null) ? "" : transactionInfo, // {10} //(lang==null) ? "" : lang, // {11} }; boolean handleAsInternal = this.cause != null && ( this.cause instanceof XmlBlasterException && ((XmlBlasterException)this.cause).isInternal() || this.cause instanceof NullPointerException || this.cause instanceof IllegalArgumentException ||
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -