📄 xmlblasterexception.java
字号:
/** * Serialize the complete exception */ public byte[] toByteArr() { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(1024); PrintWriter out = new PrintWriter(byteOut); out.write(getErrorCodeStr()); out.write(0); out.write(getNode()); out.write(0); out.write(getLocation()); out.write(0); out.write(getLang()); out.write(0); out.write(getRawMessage()); out.write(0); out.write(getVersionInfo()); out.write(0); out.write(getTimestamp().toString()); out.write(0); out.write(getStackTraceStr()); out.write(0); out.write(getEmbeddedMessage()); out.write(0); out.write(getTransactionInfo()); out.write(0); out.write(""+isServerSide()); out.write(0); out.flush(); byte[] result = byteOut.toByteArray(); return result; } public static XmlBlasterException parseByteArr(Global glob, byte[] data) { return parseByteArr(glob, data, ErrorCode.INTERNAL_UNKNOWN); } /** * Serialize the complete exception. * Take care when changing!!! * Is used e.g. in CallbackServerUnparsed.c and XmlScriptInterpreter.java */ public static XmlBlasterException parseByteArr(Global glob, byte[] data, ErrorCode fallback) { if (data == null) return new XmlBlasterException(glob, ErrorCode.INTERNAL_UNKNOWN, "XmlBlasterException", "Can't parse given serial XmlBlasterException data"); int start = 0; int end = start; String errorCodeStr = null; String node = null; String location = null; String lang = null; String message = null; String versionInfo = null; String timestampStr = null; String stackTrace = null; String embeddedMessage = null; String transactionInfo = null; Boolean exceptionFromServer = new Boolean(true); try { for (end=start; end<data.length; end++) if (data[end] == 0) break; errorCodeStr = new String(data, start, end-start); start = end+1; for (end=start; end<data.length; end++) if (data[end] == 0) break; node = new String(data, start, end-start); start = end+1; for (end=start; end<data.length; end++) if (data[end] == 0) break; location = new String(data, start, end-start); start = end+1; for (end=start; end<data.length; end++) if (data[end] == 0) break; lang = new String(data, start, end-start); start = end+1; for (end=start; end<data.length; end++) if (data[end] == 0) break; message = new String(data, start, end-start); start = end+1; for (end=start; end<data.length; end++) if (data[end] == 0) break; versionInfo = new String(data, start, end-start); start = end+1; for (end=start; end<data.length; end++) if (data[end] == 0) break; timestampStr = new String(data, start, end-start); start = end+1; for (end=start; end<data.length; end++) if (data[end] == 0) break; stackTrace = new String(data, start, end-start); start = end+1; for (end=start; end<data.length; end++) if (data[end] == 0) break; embeddedMessage = new String(data, start, end-start); start = end+1; for (end=start; end<data.length; end++) if (data[end] == 0) break; transactionInfo = new String(data, start, end-start); start = end+1; for (end=start; end<data.length; end++) if (data[end] == 0) break; exceptionFromServer = new Boolean(new String(data, start, end-start)); } catch (java.lang.StringIndexOutOfBoundsException e) { log.severe("Receiving invalid format for XmlBlasterException in '" + new String(data) + "'"); } ErrorCode errorCode = (fallback == null) ? ErrorCode.INTERNAL_UNKNOWN : fallback; try { errorCode = ErrorCode.toErrorCode(errorCodeStr); } catch (Throwable e) { log.severe("Receiving invalid errorCode in XmlBlasterException in '" + new String(data) + "', handling it as " + errorCode.toString()); message = "Can't parse XmlBlasterException in method parseByteArr(). original message is '" + new String(data) + "'"; } Timestamp ti = new Timestamp(); try { ti = Timestamp.valueOf(timestampStr); } catch (Throwable e) { log.fine("Receiving invalid timestamp in XmlBlasterException in '" + new String(data) + "'"); } return new XmlBlasterException(glob, errorCode, node, location, lang, message, versionInfo, ti, stackTrace, embeddedMessage, transactionInfo, exceptionFromServer.booleanValue()); } /** * If throwable is of type XmlBlasterException it is just casted (and location/message are ignored) * else if throwable is one if IllegalArgumentException, NullpointerException or OutOfMemoryError * it is converted to an XmlBlasterException with corresponding ErrorCode * otherwise the ErrorCode is INTERNAL_UNKNOWN * @param location null if not of interest * @param message null if not of interest * @param throwable Any exception type you can think of * @return An exception of type XmlBlasterException */ public static XmlBlasterException convert(Global glob, String location, String message, Throwable throwable) { return convert(glob, ErrorCode.INTERNAL_UNKNOWN, location, message, throwable); } /** * @param errorCodeEnum is the fallback error code */ public static XmlBlasterException convert(Global glob, ErrorCode errorCodeEnum, String location, String message, Throwable throwable) { if (throwable instanceof XmlBlasterException) { return (XmlBlasterException)throwable; } else if (throwable instanceof NullPointerException) { return new XmlBlasterException(glob, ErrorCode.INTERNAL_NULLPOINTER, location, message, throwable); } else if (throwable instanceof IllegalArgumentException) { return new XmlBlasterException(glob, ErrorCode.INTERNAL_ILLEGALARGUMENT, location, message, throwable); } else if (throwable instanceof ArrayIndexOutOfBoundsException) { return new XmlBlasterException(glob, ErrorCode.INTERNAL_UNKNOWN, location, message, throwable); } else if (throwable instanceof StringIndexOutOfBoundsException) { return new XmlBlasterException(glob, ErrorCode.INTERNAL_UNKNOWN, location, message, throwable); } else if (throwable instanceof ClassCastException) { return new XmlBlasterException(glob, ErrorCode.INTERNAL_UNKNOWN, location, message, throwable); } else if (throwable instanceof OutOfMemoryError) { return new XmlBlasterException(glob, ErrorCode.RESOURCE_OUTOFMEMORY, location, message, throwable); } else { return new XmlBlasterException(glob, errorCodeEnum, location, message, throwable); } } /** * Overwrite the formatting of internal logs * (the env property -XmlBlasterException.logFormat.internal) */ public void setLogFormatInternal(String logFormatInternal) { this.logFormatInternal = logFormatInternal; } public static XmlBlasterException tranformCallbackException(XmlBlasterException e) { // TODO: Marcel: For the time being the client has the chance // to force requeueing by sending a USER_UPDATE_HOLDBACK which will lead // to a COMMUNICATION exception behaviour if (ErrorCode.USER_UPDATE_HOLDBACK.toString().equals(e.getErrorCode().toString())) { // Will set dispatcherActive==false XmlBlasterException ret = new XmlBlasterException(e.getGlobal(), ErrorCode.COMMUNICATION_USER_HOLDBACK, e.getEmbeddedMessage()); ret.isServerSide(e.isServerSide()); return ret; } // WE ONLY ACCEPT ErrorCode.USER... FROM CLIENTS ! if (e.isUser()) return e; // and server side communication problems (how to assure if from server?) //if (e.isCommunication() && e.isServerSide()) //it can also be thrown by client side, if for example the client side SOCKET callback server is marked shutdown if (e.isCommunication()) return e; // The SOCKET protocol plugin throws this when a client has shutdown its callback server //if (xmlBlasterException.getErrorCode() == ErrorCode.COMMUNICATION_NOCONNECTION_CALLBACKSERVER_NOTAVAILABLE) // throw xmlBlasterException; return new XmlBlasterException(e.getGlobal(), ErrorCode.USER_UPDATE_ERROR, e.getLocation(), e.getRawMessage(), e); } /** * java org.xmlBlaster.util.XmlBlasterException */ public static void main(String[] args) { Global glob = new Global(args); XmlBlasterException e = new XmlBlasterException(glob, ErrorCode.RESOURCE_OVERFLOW_QUEUE_BYTES, "LOC", "Bla bla"); System.out.println(e.toXml()); byte[] serial = e.toByteArr(); System.out.println("\n" + new String(serial)); XmlBlasterException back = parseByteArr(glob, serial); System.out.println("BACK\n" + back.toXml()); System.out.println("\ngetMessage:\n" + back.getMessage()); e = new XmlBlasterException(glob, ErrorCode.INTERNAL_UNKNOWN, "LOC", "Bla bla"); System.out.println("\ngetMessage:\n" + e.getMessage()); e = XmlBlasterException.convert(glob, null, null, new IllegalArgumentException("wrong args")); System.out.println("\ngetMessage:\n" + e.getMessage()); } /** * @return Returns the exceptionHandler. */ public static I_XmlBlasterExceptionHandler getExceptionHandler() { return exceptionHandler; } /** * @param exceptionHandler The exceptionHandler to set. */ public static void setExceptionHandler( I_XmlBlasterExceptionHandler exceptionHandler) { XmlBlasterException.exceptionHandler = exceptionHandler; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -