src/share/jaxws_classes/com/sun/xml/internal/ws/fault/ExceptionBean.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.ws.fault;
aoqi@0 27
aoqi@0 28 import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper;
aoqi@0 29 import com.sun.xml.internal.ws.developer.ServerSideException;
aoqi@0 30 import org.w3c.dom.Element;
aoqi@0 31 import org.w3c.dom.Node;
aoqi@0 32
aoqi@0 33 import javax.xml.bind.JAXBContext;
aoqi@0 34 import javax.xml.bind.JAXBException;
aoqi@0 35 import javax.xml.bind.Marshaller;
aoqi@0 36 import javax.xml.bind.PropertyException;
aoqi@0 37 import javax.xml.bind.annotation.XmlAttribute;
aoqi@0 38 import javax.xml.bind.annotation.XmlElement;
aoqi@0 39 import javax.xml.bind.annotation.XmlElementWrapper;
aoqi@0 40 import javax.xml.bind.annotation.XmlRootElement;
aoqi@0 41 import java.util.ArrayList;
aoqi@0 42 import java.util.List;
aoqi@0 43
aoqi@0 44 /**
aoqi@0 45 * JAXB-bound bean that captures the exception and its call stack.
aoqi@0 46 *
aoqi@0 47 * <p>
aoqi@0 48 * This is used to capture the stack trace of the server side error and
aoqi@0 49 * send that over to the client.
aoqi@0 50 *
aoqi@0 51 * @author Kohsuke Kawaguchi
aoqi@0 52 */
aoqi@0 53 @XmlRootElement(namespace=ExceptionBean.NS,name=ExceptionBean.LOCAL_NAME)
aoqi@0 54 final class ExceptionBean {
aoqi@0 55 /**
aoqi@0 56 * Converts the given {@link Throwable} into an XML representation
aoqi@0 57 * and put that as a DOM tree under the given node.
aoqi@0 58 */
aoqi@0 59 public static void marshal( Throwable t, Node parent ) throws JAXBException {
aoqi@0 60 Marshaller m = JAXB_CONTEXT.createMarshaller();
aoqi@0 61 try {
aoqi@0 62 m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",nsp);
aoqi@0 63 } catch (PropertyException pe) {}
aoqi@0 64 m.marshal(new ExceptionBean(t), parent );
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 /**
aoqi@0 68 * Does the reverse operation of {@link #marshal(Throwable, Node)}. Constructs an
aoqi@0 69 * {@link Exception} object from the XML.
aoqi@0 70 */
aoqi@0 71 public static ServerSideException unmarshal( Node xml ) throws JAXBException {
aoqi@0 72 ExceptionBean e = (ExceptionBean) JAXB_CONTEXT.createUnmarshaller().unmarshal(xml);
aoqi@0 73 return e.toException();
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 @XmlAttribute(name="class")
aoqi@0 77 public String className;
aoqi@0 78 @XmlElement
aoqi@0 79 public String message;
aoqi@0 80 @XmlElementWrapper(namespace=NS,name="stackTrace")
aoqi@0 81 @XmlElement(namespace=NS,name="frame")
aoqi@0 82 public List<StackFrame> stackTrace = new ArrayList<StackFrame>();
aoqi@0 83 @XmlElement(namespace=NS,name="cause")
aoqi@0 84 public ExceptionBean cause;
aoqi@0 85
aoqi@0 86 // so that people noticed this fragment can turn it off
aoqi@0 87 @XmlAttribute
aoqi@0 88 public String note = "To disable this feature, set "+SOAPFaultBuilder.CAPTURE_STACK_TRACE_PROPERTY+" system property to false";
aoqi@0 89
aoqi@0 90 ExceptionBean() {// for JAXB
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 /**
aoqi@0 94 * Creates an {@link ExceptionBean} tree that represents the given {@link Throwable}.
aoqi@0 95 */
aoqi@0 96 private ExceptionBean(Throwable t) {
aoqi@0 97 this.className = t.getClass().getName();
aoqi@0 98 this.message = t.getMessage();
aoqi@0 99
aoqi@0 100 for (StackTraceElement f : t.getStackTrace()) {
aoqi@0 101 stackTrace.add(new StackFrame(f));
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 Throwable cause = t.getCause();
aoqi@0 105 if(t!=cause && cause!=null)
aoqi@0 106 this.cause = new ExceptionBean(cause);
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 private ServerSideException toException() {
aoqi@0 110 ServerSideException e = new ServerSideException(className,message);
aoqi@0 111 if(stackTrace!=null) {
aoqi@0 112 StackTraceElement[] ste = new StackTraceElement[stackTrace.size()];
aoqi@0 113 for( int i=0; i<stackTrace.size(); i++ )
aoqi@0 114 ste[i] = stackTrace.get(i).toStackTraceElement();
aoqi@0 115 e.setStackTrace(ste);
aoqi@0 116 }
aoqi@0 117 if(cause!=null)
aoqi@0 118 e.initCause(cause.toException());
aoqi@0 119 return e;
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 /**
aoqi@0 123 * Captures one stack frame.
aoqi@0 124 */
aoqi@0 125 static final class StackFrame {
aoqi@0 126 @XmlAttribute(name="class")
aoqi@0 127 public String declaringClass;
aoqi@0 128 @XmlAttribute(name="method")
aoqi@0 129 public String methodName;
aoqi@0 130 @XmlAttribute(name="file")
aoqi@0 131 public String fileName;
aoqi@0 132 @XmlAttribute(name="line")
aoqi@0 133 public String lineNumber;
aoqi@0 134
aoqi@0 135 StackFrame() {// for JAXB
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 public StackFrame(StackTraceElement ste) {
aoqi@0 139 this.declaringClass = ste.getClassName();
aoqi@0 140 this.methodName = ste.getMethodName();
aoqi@0 141 this.fileName = ste.getFileName();
aoqi@0 142 this.lineNumber = box(ste.getLineNumber());
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 private String box(int i) {
aoqi@0 146 if(i>=0) return String.valueOf(i);
aoqi@0 147 if(i==-2) return "native";
aoqi@0 148 return "unknown";
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 private int unbox(String v) {
aoqi@0 152 try {
aoqi@0 153 return Integer.parseInt(v);
aoqi@0 154 } catch (NumberFormatException e) {
aoqi@0 155 if ("native".equals(v)) {
aoqi@0 156 return -2;
aoqi@0 157 }
aoqi@0 158 return -1;
aoqi@0 159 }
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 private StackTraceElement toStackTraceElement() {
aoqi@0 163 return new StackTraceElement(declaringClass,methodName,fileName,unbox(lineNumber));
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 /**
aoqi@0 168 * Checks if the given element is the XML representation of {@link ExceptionBean}.
aoqi@0 169 */
aoqi@0 170 public static boolean isStackTraceXml(Element n) {
aoqi@0 171 return LOCAL_NAME.equals(n.getLocalName()) && NS.equals(n.getNamespaceURI());
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 private static final JAXBContext JAXB_CONTEXT;
aoqi@0 175
aoqi@0 176 /**
aoqi@0 177 * Namespace URI.
aoqi@0 178 */
aoqi@0 179 /*package*/ static final String NS = "http://jax-ws.dev.java.net/";
aoqi@0 180
aoqi@0 181 /*package*/ static final String LOCAL_NAME = "exception";
aoqi@0 182
aoqi@0 183 static {
aoqi@0 184 try {
aoqi@0 185 JAXB_CONTEXT = JAXBContext.newInstance(ExceptionBean.class);
aoqi@0 186 } catch (JAXBException e) {
aoqi@0 187 // this must be a bug in our code
aoqi@0 188 throw new Error(e);
aoqi@0 189 }
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 private static final NamespacePrefixMapper nsp = new NamespacePrefixMapper() {
aoqi@0 193 public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
aoqi@0 194 if (NS.equals(namespaceUri)) {
aoqi@0 195 return "";
aoqi@0 196 }
aoqi@0 197 return suggestion;
aoqi@0 198 }
aoqi@0 199 };
aoqi@0 200 }

mercurial