1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/fault/ExceptionBean.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,196 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.fault; 1.30 + 1.31 +import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper; 1.32 +import com.sun.xml.internal.ws.developer.ServerSideException; 1.33 +import org.w3c.dom.Element; 1.34 +import org.w3c.dom.Node; 1.35 + 1.36 +import javax.xml.bind.JAXBContext; 1.37 +import javax.xml.bind.JAXBException; 1.38 +import javax.xml.bind.Marshaller; 1.39 +import javax.xml.bind.PropertyException; 1.40 +import javax.xml.bind.annotation.XmlAttribute; 1.41 +import javax.xml.bind.annotation.XmlElement; 1.42 +import javax.xml.bind.annotation.XmlElementWrapper; 1.43 +import javax.xml.bind.annotation.XmlRootElement; 1.44 +import java.util.ArrayList; 1.45 +import java.util.List; 1.46 + 1.47 +/** 1.48 + * JAXB-bound bean that captures the exception and its call stack. 1.49 + * 1.50 + * <p> 1.51 + * This is used to capture the stack trace of the server side error and 1.52 + * send that over to the client. 1.53 + * 1.54 + * @author Kohsuke Kawaguchi 1.55 + */ 1.56 +@XmlRootElement(namespace=ExceptionBean.NS,name=ExceptionBean.LOCAL_NAME) 1.57 +final class ExceptionBean { 1.58 + /** 1.59 + * Converts the given {@link Throwable} into an XML representation 1.60 + * and put that as a DOM tree under the given node. 1.61 + */ 1.62 + public static void marshal( Throwable t, Node parent ) throws JAXBException { 1.63 + Marshaller m = JAXB_CONTEXT.createMarshaller(); 1.64 + try { 1.65 + m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",nsp); 1.66 + } catch (PropertyException pe) {} 1.67 + m.marshal(new ExceptionBean(t), parent ); 1.68 + } 1.69 + 1.70 + /** 1.71 + * Does the reverse operation of {@link #marshal(Throwable, Node)}. Constructs an 1.72 + * {@link Exception} object from the XML. 1.73 + */ 1.74 + public static ServerSideException unmarshal( Node xml ) throws JAXBException { 1.75 + ExceptionBean e = (ExceptionBean) JAXB_CONTEXT.createUnmarshaller().unmarshal(xml); 1.76 + return e.toException(); 1.77 + } 1.78 + 1.79 + @XmlAttribute(name="class") 1.80 + public String className; 1.81 + @XmlElement 1.82 + public String message; 1.83 + @XmlElementWrapper(namespace=NS,name="stackTrace") 1.84 + @XmlElement(namespace=NS,name="frame") 1.85 + public List<StackFrame> stackTrace = new ArrayList<StackFrame>(); 1.86 + @XmlElement(namespace=NS,name="cause") 1.87 + public ExceptionBean cause; 1.88 + 1.89 + // so that people noticed this fragment can turn it off 1.90 + @XmlAttribute 1.91 + public String note = "To disable this feature, set "+SOAPFaultBuilder.CAPTURE_STACK_TRACE_PROPERTY+" system property to false"; 1.92 + 1.93 + ExceptionBean() {// for JAXB 1.94 + } 1.95 + 1.96 + /** 1.97 + * Creates an {@link ExceptionBean} tree that represents the given {@link Throwable}. 1.98 + */ 1.99 + private ExceptionBean(Throwable t) { 1.100 + this.className = t.getClass().getName(); 1.101 + this.message = t.getMessage(); 1.102 + 1.103 + for (StackTraceElement f : t.getStackTrace()) { 1.104 + stackTrace.add(new StackFrame(f)); 1.105 + } 1.106 + 1.107 + Throwable cause = t.getCause(); 1.108 + if(t!=cause && cause!=null) 1.109 + this.cause = new ExceptionBean(cause); 1.110 + } 1.111 + 1.112 + private ServerSideException toException() { 1.113 + ServerSideException e = new ServerSideException(className,message); 1.114 + if(stackTrace!=null) { 1.115 + StackTraceElement[] ste = new StackTraceElement[stackTrace.size()]; 1.116 + for( int i=0; i<stackTrace.size(); i++ ) 1.117 + ste[i] = stackTrace.get(i).toStackTraceElement(); 1.118 + e.setStackTrace(ste); 1.119 + } 1.120 + if(cause!=null) 1.121 + e.initCause(cause.toException()); 1.122 + return e; 1.123 + } 1.124 + 1.125 + /** 1.126 + * Captures one stack frame. 1.127 + */ 1.128 + static final class StackFrame { 1.129 + @XmlAttribute(name="class") 1.130 + public String declaringClass; 1.131 + @XmlAttribute(name="method") 1.132 + public String methodName; 1.133 + @XmlAttribute(name="file") 1.134 + public String fileName; 1.135 + @XmlAttribute(name="line") 1.136 + public String lineNumber; 1.137 + 1.138 + StackFrame() {// for JAXB 1.139 + } 1.140 + 1.141 + public StackFrame(StackTraceElement ste) { 1.142 + this.declaringClass = ste.getClassName(); 1.143 + this.methodName = ste.getMethodName(); 1.144 + this.fileName = ste.getFileName(); 1.145 + this.lineNumber = box(ste.getLineNumber()); 1.146 + } 1.147 + 1.148 + private String box(int i) { 1.149 + if(i>=0) return String.valueOf(i); 1.150 + if(i==-2) return "native"; 1.151 + return "unknown"; 1.152 + } 1.153 + 1.154 + private int unbox(String v) { 1.155 + try { 1.156 + return Integer.parseInt(v); 1.157 + } catch (NumberFormatException e) { 1.158 + if(v.equals("native")) return -2; 1.159 + return -1; 1.160 + } 1.161 + } 1.162 + 1.163 + private StackTraceElement toStackTraceElement() { 1.164 + return new StackTraceElement(declaringClass,methodName,fileName,unbox(lineNumber)); 1.165 + } 1.166 + } 1.167 + 1.168 + /** 1.169 + * Checks if the given element is the XML representation of {@link ExceptionBean}. 1.170 + */ 1.171 + public static boolean isStackTraceXml(Element n) { 1.172 + return n.getLocalName().equals(LOCAL_NAME) && n.getNamespaceURI().equals(NS); 1.173 + } 1.174 + 1.175 + private static final JAXBContext JAXB_CONTEXT; 1.176 + 1.177 + /** 1.178 + * Namespace URI. 1.179 + */ 1.180 + /*package*/ static final String NS = "http://jax-ws.dev.java.net/"; 1.181 + 1.182 + /*package*/ static final String LOCAL_NAME = "exception"; 1.183 + 1.184 + static { 1.185 + try { 1.186 + JAXB_CONTEXT = JAXBContext.newInstance(ExceptionBean.class); 1.187 + } catch (JAXBException e) { 1.188 + // this must be a bug in our code 1.189 + throw new Error(e); 1.190 + } 1.191 + } 1.192 + 1.193 + private static final NamespacePrefixMapper nsp = new NamespacePrefixMapper() { 1.194 + public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { 1.195 + if(namespaceUri.equals(NS)) return ""; 1.196 + return suggestion; 1.197 + } 1.198 + }; 1.199 +}