View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * Returns the stack trace as string.
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       * Returns the stack trace as DOM node.
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  }