blob: 011197790c5126e3c16484bde8719020c7b599ba [file] [log] [blame]
Richard S. Hallddf2e142009-09-30 17:03:45 +00001/*
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +00002 * 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 */
Richard S. Hall44002cf2009-02-11 21:47:26 +000019package org.apache.felix.log;
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000020
21/**
22 * Implementation dependent exception class used to avoid references to any
23 * bundle defined exception class, which might prevent an uninstalled bundle
24 * from being garbage collected. This exception maintains the class of the
25 * original exception (as part of the message), the message (appended to the
26 * class name) and the stack trace of both the exception thrown and any
27 * embedded exceptions.
28 */
Richard S. Hallddf2e142009-09-30 17:03:45 +000029final class LogException extends Exception
30{
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000031 /** The class name of the original exception. */
32 private final String m_className;
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000033 /** The message from the original exception. */
34 private final String m_message;
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000035 /** The localized message from the original exception. */
36 private final String m_localizedMessage;
37
38 /**
39 * Create a new instance.
40 * @param exception the original exception.
41 */
Richard S. Hallddf2e142009-09-30 17:03:45 +000042 private LogException(final Throwable exception)
43 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000044 m_className = exception.getClass().getName();
45 m_message = exception.getMessage();
46 m_localizedMessage = exception.getLocalizedMessage();
47 setStackTrace(exception.getStackTrace());
48
49 Throwable cause = exception.getCause();
Richard S. Hallddf2e142009-09-30 17:03:45 +000050 if (cause != null)
51 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000052 cause = getException(cause);
53 initCause(cause);
54 }
55 }
56
57 /**
58 * Returns the message associated with the exception. The message
59 * will be the class name of the original exception followed by the
60 * message of the original exception.
61 * @return the message associated with the exception
62 */
Richard S. Hallddf2e142009-09-30 17:03:45 +000063 public String getMessage()
64 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000065 return m_className + ": " + m_message;
66 }
67
68 /**
69 * Returns the localized message associated with the exception. The
70 * localized message will be the class name of the original exception
71 * followed by the localized message of the original exception.
72 * @return the localized message associated with the exception
73 */
Richard S. Hallddf2e142009-09-30 17:03:45 +000074 public String getLocalizedMessage()
75 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000076 return m_className + ": " + m_localizedMessage;
77 }
78
79 /** The prefix that identifies classes from the "java" namespace. */
80 private static final String JAVA_PACKAGE_PREFIX = "java.";
81
82 /**
83 * Returns the exception to store in the {@link LogEntry}.
84 * @param exception the exception that was originally thrown.
85 * @return the exception to store in the {@link LogEntry}
86 */
Richard S. Hallddf2e142009-09-30 17:03:45 +000087 static Throwable getException(final Throwable exception)
88 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000089 Throwable result = null;
90
Richard S. Hallddf2e142009-09-30 17:03:45 +000091 if (exception != null)
92 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000093 String className = exception.getClass().getName();
Richard S. Hallddf2e142009-09-30 17:03:45 +000094 if (exception.getCause() == null && className.startsWith(JAVA_PACKAGE_PREFIX))
95 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +000096 result = exception;
Richard S. Hallddf2e142009-09-30 17:03:45 +000097 }
98 else
99 {
Richard S. Hall0b8e3ba2006-10-25 13:26:32 +0000100 result = new LogException(exception);
101 }
102 }
103
104 return result;
105 }
Richard S. Hallddf2e142009-09-30 17:03:45 +0000106}