KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > AxisFault


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis ;
18
19 import java.io.PrintStream JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
28 import javax.xml.parsers.ParserConfigurationException JavaDoc;
29 import javax.xml.rpc.soap.SOAPFaultException JavaDoc;
30
31 import org.apache.axis.components.logger.LogFactory;
32 import org.apache.axis.encoding.SerializationContext;
33 import org.apache.axis.message.SOAPEnvelope;
34 import org.apache.axis.message.SOAPFault;
35 import org.apache.axis.message.SOAPHeaderElement;
36 import org.apache.axis.soap.SOAPConstants;
37 import org.apache.axis.utils.JavaUtils;
38 import org.apache.axis.utils.XMLUtils;
39 import org.apache.axis.utils.NetworkUtils;
40 import org.apache.commons.logging.Log;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Element JavaDoc;
43 import org.w3c.dom.Text JavaDoc;
44
45 /**
46  * An exception which maps cleanly to a SOAP fault.
47  * This is a base class for exceptions which are mapped to faults.
48  * SOAP faults contain
49  * <ol>
50  * <li>A fault string
51  * <li>A fault code
52  * <li>A fault actor
53  * <li>Fault details; an xml tree of fault specific stuff
54  * </ol>
55  * @author Doug Davis (dug@us.ibm.com)
56  * @author James Snell (jasnell@us.ibm.com)
57  * @author Steve Loughran
58  */

59
60 public class AxisFault extends java.rmi.RemoteException JavaDoc {
61     /**
62      * The <code>Log</code> used by this class for all logging.
63      */

64     protected static Log log =
65         LogFactory.getLog(AxisFault.class.getName());
66
67     protected QName JavaDoc faultCode ;
68     /** SOAP1.2 addition: subcodes of faults; a Vector of QNames */
69     protected Vector JavaDoc faultSubCode ;
70     protected String JavaDoc faultString = "";
71     protected String JavaDoc faultActor ;
72     protected Vector JavaDoc faultDetails ; // vector of Element's
73
protected String JavaDoc faultNode ;
74
75     /** SOAP headers which should be serialized with the Fault. */
76     protected ArrayList JavaDoc faultHeaders = null;
77
78     /**
79      * Make an AxisFault based on a passed Exception. If the Exception is
80      * already an AxisFault, simply use that. Otherwise, wrap it in an
81      * AxisFault. If the Exception is an InvocationTargetException (which
82      * already wraps another Exception), get the wrapped Exception out from
83      * there and use that instead of the passed one.
84      *
85      * @param e the <code>Exception</code> to build a fault for
86      * @return an <code>AxisFault</code> representing <code>e</code>
87      */

88     public static AxisFault makeFault(Exception JavaDoc e)
89     {
90         if (e instanceof InvocationTargetException JavaDoc) {
91             Throwable JavaDoc t = ((InvocationTargetException JavaDoc)e).getTargetException();
92             if (t instanceof Exception JavaDoc) {
93                 e = (Exception JavaDoc)t;
94             }
95         }
96
97         if (e instanceof AxisFault) {
98             return (AxisFault)e;
99         }
100
101         return new AxisFault(e);
102     }
103
104     /**
105      * Make a fault in the <code>Constants.NS_URI_AXIS</code> namespace.
106      *
107      * @param code fault code which will be passed into the Axis namespace
108      * @param faultString fault string
109      * @param actor fault actor
110      * @param details details; if null the current stack trace and classname is
111      * inserted into the details.
112      */

113     public AxisFault(String JavaDoc code, String JavaDoc faultString,
114                      String JavaDoc actor, Element[] details) {
115         this(new QName JavaDoc(Constants.NS_URI_AXIS, code),
116                 faultString, actor, details);
117     }
118
119     /**
120      * Make a fault in any namespace.
121      *
122      * @param code fault code which will be passed into the Axis namespace
123      * @param faultString fault string
124      * @param actor fault actor
125      * @param details details; if null the current stack trace and classname is
126      * inserted into the details.
127      */

128     public AxisFault(QName JavaDoc code, String JavaDoc faultString,
129                      String JavaDoc actor, Element[] details) {
130         super (faultString);
131         setFaultCode( code );
132         setFaultString( faultString );
133         setFaultActor( actor );
134         setFaultDetail( details );
135         if (details == null) {
136             initFromException(this);
137         }
138     }
139
140     /**
141      * Make a fault in any namespace.
142      *
143      * @param code fault code which will be passed into the Axis namespace
144      * @param subcodes fault subcodes which will be pased into the Axis namespace
145      * @param faultString fault string
146      * @param actor fault actor, same as fault role in SOAP 1.2
147      * @param node which node caused the fault on the SOAP path
148      * @param details details; if null the current stack trace and classname is
149      * inserted into the details.
150      * @since axis1.1
151      */

152     public AxisFault(QName JavaDoc code, QName JavaDoc[] subcodes, String JavaDoc faultString,
153                      String JavaDoc actor, String JavaDoc node, Element[] details) {
154         super (faultString);
155         setFaultCode( code );
156         if (subcodes != null) {
157             for (int i = 0; i < subcodes.length; i++) {
158                 addFaultSubCode( subcodes[i] );
159             }
160         }
161         setFaultString( faultString );
162         setFaultActor( actor );
163         setFaultNode( node );
164         setFaultDetail( details );
165         if (details == null) {
166             initFromException(this);
167         }
168     }
169
170     // fixme: docs says private, access says protected
171
/**
172      * Wrap an AxisFault around an existing Exception. This is private
173      * to force everyone to use makeFault() above, which sanity-checks us.
174      *
175      * @param target the target <code>Exception</code>
176      */

177     protected AxisFault(Exception JavaDoc target) {
178         super ("", target);
179         // ? SOAP 1.2 or 1.1 ?
180
setFaultCodeAsString( Constants.FAULT_SERVER_USER );
181         initFromException(target);
182
183         // if the target is a JAX-RPC SOAPFaultException init
184
// AxisFault with the values from the SOAPFaultException
185
if ( target instanceof SOAPFaultException JavaDoc ) {
186             //strip out the hostname as we want any new one
187
removeHostname();
188             initFromSOAPFaultException((SOAPFaultException JavaDoc) target);
189             //but if they left it out, add it
190
addHostnameIfNeeded();
191         }
192
193     }
194
195     /**
196      * create a simple axis fault from the message. Classname and stack trace
197      * go into the fault details.
198      * @param message
199      */

200     public AxisFault(String JavaDoc message)
201     {
202         super (message);
203         setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL);
204         setFaultString(message);
205         initFromException(this);
206     }
207
208     /**
209      * No-arg constructor for building one from an XML stream.
210      */

211     public AxisFault()
212     {
213         super();
214         setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL);
215         initFromException(this);
216     }
217
218     /**
219      * create a fault from any throwable;
220      * When faulting a throwable (as opposed to an exception),
221      * stack trace information does not go into the fault.
222      * @param message any extra text to with the fault
223      * @param t whatever is to be turned into a fault
224      */

225     public AxisFault (String JavaDoc message, Throwable JavaDoc t)
226     {
227         super (message, t);
228         setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL);
229         setFaultString(getMessage());
230         addHostnameIfNeeded();
231     }
232
233     /**
234      * fill in soap fault details from the exception, unless
235      * this object already has a stack trace in its details. Which, given
236      * the way this private method is invoked, is a pretty hard situation to ever achieve.
237      * This method adds classname of the exception and the stack trace.
238      * @param target what went wrong
239      */

240     private void initFromException(Exception JavaDoc target)
241     {
242         //look for old stack trace
243
Element oldStackTrace = lookupFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE);
244         if (oldStackTrace != null) {
245             // todo: Should we replace it or just let it be?
246
return;
247         }
248
249         // Set the exception message (if any) as the fault string
250
setFaultString( target.toString() );
251
252
253         // Put the exception class into the AXIS SPECIFIC HACK
254
// "exceptionName" element in the details. This allows
255
// us to get back a correct Java Exception class on the other side
256
// (assuming they have it available).
257
// NOTE: This hack is obsolete! We now serialize exception data
258
// and the other side uses *that* QName to figure out what exception
259
// to use, because the class name may be completly different on the
260
// client.
261
if ((target instanceof AxisFault) &&
262             (target.getClass() != AxisFault.class)) {
263           addFaultDetail(Constants.QNAME_FAULTDETAIL_EXCEPTIONNAME,
264                     target.getClass().getName());
265         }
266
267         //add stack trace
268
if (target == this) {
269             // only add stack trace. JavaUtils.stackToString() call would
270
// include dumpToString() info which is already sent as different
271
// elements of this fault.
272
addFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE,
273                            getPlainStackTrace());
274         } else {
275             addFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE,
276                            JavaUtils.stackToString(target));
277         }
278
279         //add the hostname
280
addHostnameIfNeeded();
281     }
282     
283     /**
284      * Initiates the AxisFault with the values from a SOAPFaultException
285      * @param fault SOAPFaultException
286      */

287     private void initFromSOAPFaultException(SOAPFaultException JavaDoc fault) {
288         
289         // faultcode
290
if ( fault.getFaultCode() != null ) {
291             setFaultCode(fault.getFaultCode());
292         }
293         
294         // faultstring
295
if ( fault.getFaultString() != null ) {
296             setFaultString(fault.getFaultString());
297         }
298         
299         // actor
300
if ( fault.getFaultActor() != null ) {
301             setFaultActor(fault.getFaultActor());
302         }
303
304         if ( null == fault.getDetail() ) {
305             return;
306         }
307         
308         // We get an Iterator but we need a List
309
Vector JavaDoc details = new Vector JavaDoc();
310         Iterator JavaDoc detailIter = fault.getDetail().getChildElements();
311         while (detailIter.hasNext()) {
312             details.add( detailIter.next());
313         }
314         
315         // Convert the List in an Array an return the array
316
setFaultDetail( XMLUtils.asElementArray(details));
317     }
318
319     /**
320      * Init the fault details data structure; does nothing
321      * if this exists already.
322      */

323     private void initFaultDetails() {
324         if (faultDetails == null) {
325             faultDetails = new Vector JavaDoc();
326         }
327     }
328
329     /**
330      * Clear the fault details list.
331      */

332     public void clearFaultDetails() {
333         faultDetails=null;
334     }
335
336     /**
337      * Dump the fault info to the log at debug level.
338      */

339     public void dump() {
340         log.debug(dumpToString());
341     }
342
343
344     /**
345      * turn the fault and details into a string, with XML escaping.
346      * subclassers: for security (cross-site-scripting) reasons,
347      * escape everything that could contain caller-supplied data.
348      * @return stringified fault details
349      */

350     public String JavaDoc dumpToString()
351     {
352         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("AxisFault");
353         buf.append(JavaUtils.LS);
354         buf.append(" faultCode: ");
355         buf.append(XMLUtils.xmlEncodeString(faultCode.toString()));
356         buf.append(JavaUtils.LS);
357         buf.append(" faultSubcode: ");
358         if (faultSubCode != null) {
359             for (int i = 0; i < faultSubCode.size(); i++) {
360                 buf.append(JavaUtils.LS);
361                 buf.append(faultSubCode.elementAt(i).toString());
362             }
363         }
364         buf.append(JavaUtils.LS);
365         buf.append(" faultString: ");
366         buf.append(XMLUtils.xmlEncodeString(faultString));
367         buf.append(JavaUtils.LS);
368         buf.append(" faultActor: ");
369         buf.append(XMLUtils.xmlEncodeString(faultActor));
370         buf.append(JavaUtils.LS);
371         buf.append(" faultNode: ");
372         buf.append(XMLUtils.xmlEncodeString(faultNode));
373         buf.append(JavaUtils.LS);
374         buf.append(" faultDetail: ");
375         if (faultDetails != null) {
376             for (int i=0; i < faultDetails.size(); i++) {
377                 Element e = (Element) faultDetails.get(i);
378                 buf.append(JavaUtils.LS);
379                 buf.append("\t{");
380                 buf.append(null == e.getNamespaceURI() ? "" : e.getNamespaceURI());
381                 buf.append("}");
382                 buf.append(null == e.getLocalName() ? "" : e.getLocalName());
383                 buf.append(":");
384                 buf.append(XMLUtils.getInnerXMLString(e));
385             }
386         }
387         buf.append(JavaUtils.LS);
388         return buf.toString();
389     }
390
391     /**
392      * Set the fault code.
393      *
394      * @param code a new fault code
395      */

396     public void setFaultCode(QName JavaDoc code) {
397         faultCode = code ;
398     }
399
400     /**
401      * Set the fault code (as a String).
402      *
403      * @param code a new fault code
404      * @deprecated expect to see this go away after 1.1, use
405      * setFaultCodeAsString instead!
406      */

407
408     public void setFaultCode(String JavaDoc code) {
409         setFaultCodeAsString(code);
410     }
411
412     /**
413      * set a fault code string that is turned into a qname
414      * in the SOAP 1.1 or 1.2 namespace, depending on the current context
415      * @param code fault code
416      */

417     public void setFaultCodeAsString(String JavaDoc code) {
418         SOAPConstants soapConstants = MessageContext.getCurrentContext() == null ?
419                                         SOAPConstants.SOAP11_CONSTANTS :
420                                         MessageContext.getCurrentContext().getSOAPConstants();
421
422         faultCode = new QName JavaDoc(soapConstants.getEnvelopeURI(), code);
423     }
424
425     /**
426      * Get the fault code <code>QName</code>.
427      *
428      * @return fault code QName or null if there is none yet.
429      */

430     public QName JavaDoc getFaultCode() {
431         return( faultCode );
432     }
433
434     /**
435      * Add a fault sub-code with the local name <code>code</code> and namespace
436      * <code>Constants.NS_URI_AXIS</code>.
437      * This is new in SOAP 1.2, ignored in SOAP 1.1
438      *
439      * @param code the local name of the code to add
440      * @since axis1.1
441      */

442     public void addFaultSubCodeAsString(String JavaDoc code) {
443         initFaultSubCodes();
444         faultSubCode.add(new QName JavaDoc(Constants.NS_URI_AXIS, code));
445     }
446
447     /**
448      * Do whatever is needed to create the fault subcodes
449      * data structure, if it is needed.
450      */

451     protected void initFaultSubCodes() {
452         if (faultSubCode == null) {
453             faultSubCode = new Vector JavaDoc();
454         }
455     }
456
457     /**
458      * Add a fault sub-code.
459      * This is new in SOAP 1.2, ignored in SOAP 1.1.
460      *
461      * @param code the <code>QName</code> of the fault sub-code to add
462      * @since axis1.1
463      */

464     public void addFaultSubCode(QName JavaDoc code) {
465         initFaultSubCodes();
466         faultSubCode.add(code);
467     }
468
469     /**
470      * Clear all fault sub-codes.
471      * This is new in SOAP 1.2, ignored in SOAP 1.1.
472      *
473      * @since axis1.1
474      */

475     public void clearFaultSubCodes() {
476         faultSubCode = null;
477     }
478
479     /**
480      * get the fault subcode list; only used in SOAP 1.2
481      * @since axis1.1
482      * @return null for no subcodes, or a QName array
483      */

484     public QName JavaDoc[] getFaultSubCodes() {
485         if (faultSubCode == null) {
486             return null;
487         }
488         QName JavaDoc[] q = new QName JavaDoc[faultSubCode.size()];
489         return (QName JavaDoc[])faultSubCode.toArray(q);
490     }
491
492
493     /**
494      * Set a fault string.
495      * @param str new fault string; null is turned into ""
496      */

497     public void setFaultString(String JavaDoc str) {
498         if (str != null) {
499             faultString = str ;
500         } else {
501             faultString = "";
502         }
503     }
504
505     /**
506      * Get the fault string; this will never be null but may be the
507      * empty string.
508      *
509      * @return a fault string
510      */

511     public String JavaDoc getFaultString() {
512         return( faultString );
513     }
514
515     /**
516      * This is SOAP 1.2 equivalent of {@link #setFaultString(java.lang.String)}.
517      *
518      * @param str the fault reason as a <code>String</code>
519      * @since axis1.1
520      */

521     public void setFaultReason(String JavaDoc str) {
522         setFaultString(str);
523     }
524
525     /**
526      * This is SOAP 1.2 equivalent of {@link #getFaultString()}.
527      * @since axis1.1
528      * @return the fault <code>String</code>
529      */

530     public String JavaDoc getFaultReason() {
531         return getFaultString();
532     }
533
534     /**
535      * Set the fault actor.
536      *
537      * @param actor fault actor
538      */

539     public void setFaultActor(String JavaDoc actor) {
540         faultActor = actor ;
541     }
542
543     /**
544      * get the fault actor
545      * @return actor or null
546      */

547     public String JavaDoc getFaultActor() {
548         return( faultActor );
549     }
550
551     /**
552      * This is SOAP 1.2 equivalent of {@link #getFaultActor()}.
553      * @since axis1.1
554      * @return the name of the fault actor
555      */

556     public String JavaDoc getFaultRole() {
557         return getFaultActor();
558     }
559
560     // fixme: both faultRole and faultActor refer to the other one - can we
561
// break the circularity here?
562
/**
563      * This is SOAP 1.2 equivalent of {@link #setFaultActor(java.lang.String)}.
564      * @since axis1.1
565      */

566     public void setFaultRole(String JavaDoc role) {
567         setFaultActor(role);
568     }
569
570     /**
571      * Get the fault node.
572      *
573      * This is new in SOAP 1.2
574      * @since axis1.1
575      * @return
576      */

577     public String JavaDoc getFaultNode() {
578         return( faultNode );
579     }
580
581     /**
582      * Set the fault node.
583      *
584      * This is new in SOAP 1.2.
585      *
586      * @param node a <code>String</code> representing the fault node
587      * @since axis1.1
588      */

589     public void setFaultNode(String JavaDoc node) {
590         faultNode = node;
591     }
592
593     /**
594      * Set the fault detail element to the arrary of details.
595      *
596      * @param details list of detail elements, can be null
597      */

598     public void setFaultDetail(Element[] details) {
599         if ( details == null ) {
600             faultDetails=null;
601             return ;
602         }
603         faultDetails = new Vector JavaDoc( details.length );
604         for ( int loop = 0 ; loop < details.length ; loop++ ) {
605             faultDetails.add( details[loop] );
606         }
607     }
608
609     /**
610      * set the fault details to a string element.
611      * @param details XML fragment
612      */

613     public void setFaultDetailString(String JavaDoc details) {
614         clearFaultDetails();
615         addFaultDetailString(details);
616     }
617
618     /**
619      * add a string tag to the fault details.
620      * @param detail XML fragment
621      */

622     public void addFaultDetailString(String JavaDoc detail) {
623         initFaultDetails();
624         try {
625             Document JavaDoc doc = XMLUtils.newDocument();
626             Element element = doc.createElement("string");
627             Text JavaDoc text = doc.createTextNode(detail);
628             element.appendChild(text);
629             faultDetails.add(element);
630         } catch (ParserConfigurationException JavaDoc e) {
631             // This should not occur
632
throw new InternalException(e);
633         }
634     }
635
636     /**
637      * Append an element to the fault detail list.
638      *
639      * @param detail the new element to add
640      * @since Axis1.1
641      */

642     public void addFaultDetail(Element detail) {
643         initFaultDetails();
644         faultDetails.add(detail);
645     }
646
647     /**
648      * Create an element of the given qname and add it to the details.
649      *
650      * @param qname qname of the element
651      * @param body string to use as body
652      */

653     public void addFaultDetail(QName JavaDoc qname,String JavaDoc body) {
654         Element detail = XMLUtils.StringToElement(qname.getNamespaceURI(),
655                 qname.getLocalPart(),
656                 body);
657
658         addFaultDetail(detail);
659     }
660
661     // fixme: should we be returning null for none or a zero length array?
662
/**
663      * Get all the fault details.
664      *
665      * @return an array of fault details, or null for none
666      */

667     public Element[] getFaultDetails() {
668         if (faultDetails == null) {
669             return null;
670         }
671         Element result[] = new Element[faultDetails.size()];
672         for (int i=0; i<result.length; i++) {
673             result[i] = (Element) faultDetails.elementAt(i);
674         }
675         return result;
676     }
677
678     /**
679      * Find a fault detail element by its qname.
680      * @param qname name of the node to look for
681      * @return the matching element or null
682      * @since axis1.1
683      */

684     public Element lookupFaultDetail(QName JavaDoc qname) {
685         if (faultDetails != null) {
686             //extract details from the qname. the empty namespace is represented
687
//by the empty string
688
String JavaDoc searchNamespace = qname.getNamespaceURI();
689             String JavaDoc searchLocalpart = qname.getLocalPart();
690             //now spin through the elements, seeking a match
691
Iterator JavaDoc it=faultDetails.iterator();
692             while (it.hasNext()) {
693                 Element e = (Element) it.next();
694                 String JavaDoc localpart= e.getLocalName();
695                 if(localpart==null) {
696                     localpart=e.getNodeName();
697                 }
698                 String JavaDoc namespace= e.getNamespaceURI();
699                 if(namespace==null) {
700                     namespace="";
701                 }
702                 //we match on matching namespace and local part; empty namespace
703
//in an element may be null, which matches QName's ""
704
if(searchNamespace.equals(namespace)
705                     && searchLocalpart.equals(localpart)) {
706                     return e;
707                 }
708             }
709         }
710         return null;
711     }
712
713     /**
714      * Find and remove a specified fault detail element.
715      *
716      * @param qname qualified name of detail
717      * @return true if it was found and removed, false otherwise
718      * @since axis1.1
719      */

720     public boolean removeFaultDetail(QName JavaDoc qname) {
721         Element elt=lookupFaultDetail(qname);
722         if(elt==null) {
723             return false;
724         } else {
725             return faultDetails.remove(elt);
726         }
727     }
728
729     /**
730      * Add this fault and any needed headers to the output context.
731      *
732      * @param context
733      * @throws Exception
734      */

735     public void output(SerializationContext context) throws Exception JavaDoc {
736
737         SOAPConstants soapConstants = Constants.DEFAULT_SOAP_VERSION;
738         if (context.getMessageContext() != null) {
739             soapConstants = context.getMessageContext().getSOAPConstants();
740         }
741
742         SOAPEnvelope envelope = new SOAPEnvelope(soapConstants);
743
744         SOAPFault fault = new SOAPFault(this);
745         envelope.addBodyElement(fault);
746
747         // add any headers we need
748
if (faultHeaders != null) {
749             for (Iterator JavaDoc i = faultHeaders.iterator(); i.hasNext();) {
750                 SOAPHeaderElement header = (SOAPHeaderElement) i.next();
751                 envelope.addHeader(header);
752             }
753         }
754
755         envelope.output(context);
756     }
757
758     /**
759      * Stringify this fault as the current fault string.
760      *
761      * @return the fault string, possibly the empty string, but never null
762      */

763     public String JavaDoc toString() {
764         return faultString;
765     }
766
767     /**
768      * Gets the stack trace as a string.
769      */

770     private String JavaDoc getPlainStackTrace() {
771         StringWriter JavaDoc sw = new StringWriter JavaDoc(512);
772         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
773         super.printStackTrace(pw);
774         pw.close();
775         return sw.toString();
776     }
777
778     /**
779      * The override of the base class method prints out the
780      * fault info before the stack trace.
781      *
782      * @param ps where to print
783      */

784     public void printStackTrace(PrintStream JavaDoc ps) {
785         ps.println(dumpToString());
786         super.printStackTrace(ps);
787     }
788
789     /**
790      * The override of the base class method prints out the
791      * fault info before the stack trace.
792      *
793      * @param pw where to print
794      */

795     public void printStackTrace(java.io.PrintWriter JavaDoc pw) {
796         pw.println(dumpToString());
797         super.printStackTrace(pw);
798     }
799
800     /**
801      * Add a SOAP header which should be serialized along with the
802      * fault.
803      *
804      * @param header a SOAPHeaderElement containing some fault-relevant stuff
805      */

806     public void addHeader(SOAPHeaderElement header) {
807         if (faultHeaders == null) {
808             faultHeaders = new ArrayList JavaDoc();
809         }
810         faultHeaders.add(header);
811     }
812
813     /**
814      * Get the SOAP headers associated with this fault.
815      *
816      * @return an ArrayList containing any headers associated with this fault
817      */

818     public ArrayList JavaDoc getHeaders() {
819         return faultHeaders;
820     }
821
822     /**
823      * Clear all fault headers.
824      */

825     public void clearHeaders() {
826         faultHeaders = null;
827     }
828
829
830     /**
831      * Writes any exception data to the faultDetails.
832      *
833      * This can be overridden (and is) by emitted exception clases.
834      * The base implementation will attempt to serialize exception data the
835      * fault was created from an Exception and a type mapping is found for it.
836      *
837      * @param qname the <code>QName</code> to write this under
838      * @param context the <code>SerializationContext</code> to write this fault
839      * to
840      * @throws java.io.IOException if we can't write ourselves for any reason
841      */

842     public void writeDetails(QName JavaDoc qname, SerializationContext context)
843             throws java.io.IOException JavaDoc {
844         Object JavaDoc detailObject = this.detail;
845         if (detailObject == null) {
846             return;
847         }
848
849         boolean haveSerializer = false;
850         try {
851             if (context.getTypeMapping().getSerializer(detailObject.getClass()) != null) {
852                 haveSerializer = true;
853             }
854         } catch (Exception JavaDoc e) {
855             // swallow this exception, it means that we don't know how to serialize
856
// the details.
857
}
858         if (haveSerializer) {
859             boolean oldMR = context.getDoMultiRefs();
860             context.setDoMultiRefs(false);
861             context.serialize(qname, null, detailObject);
862             context.setDoMultiRefs(oldMR);
863         }
864     }
865
866     /**
867      * add the hostname of the current system. This is very useful for
868      * locating faults on a cluster.
869      * @since Axis1.2
870      */

871     public void addHostnameIfNeeded() {
872         //look for an existing declaration
873
if(lookupFaultDetail(Constants.QNAME_FAULTDETAIL_HOSTNAME)!=null) {
874             //and do nothing if it exists
875
return;
876         }
877         addHostname(NetworkUtils.getLocalHostname());
878     }
879
880     /**
881      * add the hostname string. If one already exists, remove it.
882      * @param hostname string name of a host
883      * @since Axis1.2
884      */

885     public void addHostname(String JavaDoc hostname) {
886         //add the hostname
887
removeHostname();
888         addFaultDetail(Constants.QNAME_FAULTDETAIL_HOSTNAME,
889                 hostname);
890     }
891
892     /**
893      * strip out the hostname on a message. This
894      * is useful for security reasons.
895      */

896     public void removeHostname() {
897         removeFaultDetail(Constants.QNAME_FAULTDETAIL_HOSTNAME);
898     }
899 }
900
Popular Tags