001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.fault;
038:
039: import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
040: import com.sun.xml.ws.developer.ServerSideException;
041: import org.w3c.dom.Element;
042: import org.w3c.dom.Node;
043:
044: import javax.xml.bind.JAXBContext;
045: import javax.xml.bind.JAXBException;
046: import javax.xml.bind.Marshaller;
047: import javax.xml.bind.annotation.XmlAttribute;
048: import javax.xml.bind.annotation.XmlElement;
049: import javax.xml.bind.annotation.XmlElementWrapper;
050: import javax.xml.bind.annotation.XmlRootElement;
051: import java.util.ArrayList;
052: import java.util.List;
053:
054: /**
055: * JAXB-bound bean that captures the exception and its call stack.
056: *
057: * <p>
058: * This is used to capture the stack trace of the server side error and
059: * send that over to the client.
060: *
061: * @author Kohsuke Kawaguchi
062: */
063: @XmlRootElement(namespace=ExceptionBean.NS,name=ExceptionBean.LOCAL_NAME)
064: final class ExceptionBean {
065: /**
066: * Converts the given {@link Throwable} into an XML representation
067: * and put that as a DOM tree under the given node.
068: */
069: public static void marshal(Throwable t, Node parent)
070: throws JAXBException {
071: Marshaller m = JAXB_CONTEXT.createMarshaller();
072: m.setProperty("com.sun.xml.bind.namespacePrefixMapper", nsp);
073: m.marshal(new ExceptionBean(t), parent);
074: }
075:
076: /**
077: * Does the reverse operation of {@link #marshal(Throwable, Node)}. Constructs an
078: * {@link Exception} object from the XML.
079: */
080: public static ServerSideException unmarshal(Node xml)
081: throws JAXBException {
082: ExceptionBean e = (ExceptionBean) JAXB_CONTEXT
083: .createUnmarshaller().unmarshal(xml);
084: return e.toException();
085: }
086:
087: @XmlAttribute(name="class")
088: public String className;
089: @XmlElement
090: public String message;
091: @XmlElementWrapper(namespace=NS,name="stackTrace")
092: @XmlElement(namespace=NS,name="frame")
093: public List<StackFrame> stackTrace = new ArrayList<StackFrame>();
094: @XmlElement(namespace=NS,name="cause")
095: public ExceptionBean cause;
096:
097: // so that people noticed this fragment can turn it off
098: @XmlAttribute
099: public String note = "To disable this feature, set "
100: + SOAPFaultBuilder.CAPTURE_STACK_TRACE_PROPERTY
101: + " system property to false";
102:
103: ExceptionBean() {// for JAXB
104: }
105:
106: /**
107: * Creates an {@link ExceptionBean} tree that represents the given {@link Throwable}.
108: */
109: private ExceptionBean(Throwable t) {
110: this .className = t.getClass().getName();
111: this .message = t.getMessage();
112:
113: for (StackTraceElement f : t.getStackTrace()) {
114: stackTrace.add(new StackFrame(f));
115: }
116:
117: Throwable cause = t.getCause();
118: if (t != cause && cause != null)
119: this .cause = new ExceptionBean(cause);
120: }
121:
122: private ServerSideException toException() {
123: ServerSideException e = new ServerSideException(className,
124: message);
125: if (stackTrace != null) {
126: StackTraceElement[] ste = new StackTraceElement[stackTrace
127: .size()];
128: for (int i = 0; i < stackTrace.size(); i++)
129: ste[i] = stackTrace.get(i).toStackTraceElement();
130: e.setStackTrace(ste);
131: }
132: if (cause != null)
133: e.initCause(cause.toException());
134: return e;
135: }
136:
137: /**
138: * Captures one stack frame.
139: */
140: static final class StackFrame {
141: @XmlAttribute(name="class")
142: public String declaringClass;
143: @XmlAttribute(name="method")
144: public String methodName;
145: @XmlAttribute(name="file")
146: public String fileName;
147: @XmlAttribute(name="line")
148: public String lineNumber;
149:
150: StackFrame() {// for JAXB
151: }
152:
153: public StackFrame(StackTraceElement ste) {
154: this .declaringClass = ste.getClassName();
155: this .methodName = ste.getMethodName();
156: this .fileName = ste.getFileName();
157: this .lineNumber = box(ste.getLineNumber());
158: }
159:
160: private String box(int i) {
161: if (i >= 0)
162: return String.valueOf(i);
163: if (i == -2)
164: return "native";
165: return "unknown";
166: }
167:
168: private int unbox(String v) {
169: try {
170: return Integer.parseInt(v);
171: } catch (NumberFormatException e) {
172: if (v.equals("native"))
173: return -2;
174: return -1;
175: }
176: }
177:
178: private StackTraceElement toStackTraceElement() {
179: return new StackTraceElement(declaringClass, methodName,
180: fileName, unbox(lineNumber));
181: }
182: }
183:
184: /**
185: * Checks if the given element is the XML representation of {@link ExceptionBean}.
186: */
187: public static boolean isStackTraceXml(Element n) {
188: return n.getLocalName().equals(LOCAL_NAME)
189: && n.getNamespaceURI().equals(NS);
190: }
191:
192: private static final JAXBContext JAXB_CONTEXT;
193:
194: /**
195: * Namespace URI.
196: */
197: /*package*/static final String NS = "http://jax-ws.dev.java.net/";
198:
199: /*package*/static final String LOCAL_NAME = "exception";
200:
201: static {
202: try {
203: JAXB_CONTEXT = JAXBContext.newInstance(ExceptionBean.class);
204: } catch (JAXBException e) {
205: // this must be a bug in our code
206: throw new Error(e);
207: }
208: }
209:
210: private static final NamespacePrefixMapper nsp = new NamespacePrefixMapper() {
211: public String getPreferredPrefix(String namespaceUri,
212: String suggestion, boolean requirePrefix) {
213: if (namespaceUri.equals(NS))
214: return "";
215: return suggestion;
216: }
217: };
218: }
|