1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.server.shared;
20
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23 import org.w3c.dom.Node;
24
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30
31
32 public class ExceptionHelper
33 {
34 public static final String STACK_TRACE_PROPERTY = "org.apache.chemistry.opencmis.stacktrace.disable";
35 private static final boolean sendStackTrace;
36
37 static
38 {
39 sendStackTrace = System.getProperty( STACK_TRACE_PROPERTY ) == null;
40 }
41
42 private ExceptionHelper( )
43 {
44 }
45
46
47
48
49 public static String getStacktraceAsString( Throwable t )
50 {
51 if ( !sendStackTrace || ( t == null ) )
52 {
53 return null;
54 }
55
56 StringWriter sw = new StringWriter( );
57 PrintWriter pw = new PrintWriter( sw );
58
59 t.printStackTrace( pw );
60
61 return sw.toString( );
62 }
63
64
65
66
67 public static Node getStacktraceAsNode( Throwable t )
68 {
69 try
70 {
71 String st = getStacktraceAsString( t );
72
73 if ( st != null )
74 {
75 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance( );
76 DocumentBuilder docBuilder = dbfac.newDocumentBuilder( );
77 Document doc = docBuilder.newDocument( );
78
79 Element node = doc.createElementNS( "http://chemistry.apache.org/opencmis/exception", "stacktrace" );
80 doc.appendChild( node );
81
82 node.appendChild( doc.createTextNode( st ) );
83
84 return node;
85 }
86 }
87 catch ( Exception e )
88 {
89 }
90
91 return null;
92 }
93 }