This project has retired. For details please refer to its
Attic page.
ExceptionHelper xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */19package org.apache.chemistry.opencmis.server.shared;
2021import java.io.PrintWriter;
22import java.io.StringWriter;
2324import javax.xml.parsers.DocumentBuilder;
25import javax.xml.parsers.DocumentBuilderFactory;
2627import org.w3c.dom.Document;
28import org.w3c.dom.Element;
29import org.w3c.dom.Node;
3031publicclassExceptionHelper {
3233publicstaticfinal String STACK_TRACE_PROPERTY = "org.apache.chemistry.opencmis.stacktrace.disable";
3435privatestaticfinalboolean sendStackTrace;
3637static {
38 sendStackTrace = System.getProperty(STACK_TRACE_PROPERTY) == null;
39 }
4041privateExceptionHelper() {
42 }
4344/**45 * Returns the stack trace as string.46 */47publicstatic String getStacktraceAsString(Throwable t) {
48if (!sendStackTrace || t == null) {
49returnnull;
50 }
5152 StringWriter sw = new StringWriter();
53 PrintWriter pw = new PrintWriter(sw);
5455 t.printStackTrace(pw);
5657return sw.toString();
58 }
5960/**61 * Returns the stack trace as DOM node.62 */63publicstatic Node getStacktraceAsNode(Throwable t) {
64try {
65 String st = getStacktraceAsString(t);
66if (st != null) {
67 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
68 DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
69 Document doc = docBuilder.newDocument();
7071 Element node = doc.createElementNS("http://chemistry.apache.org/opencmis/exception", "stacktrace");
72 doc.appendChild(node);
7374 node.appendChild(doc.createTextNode(st));
7576return node;
77 }
78 } catch (Exception e) {
79 }
8081returnnull;
82 }
83 }