Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 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 | */ |
Richard S. Hall | 44002cf | 2009-02-11 21:47:26 +0000 | [diff] [blame] | 19 | package org.apache.felix.log; |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 20 | |
| 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. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 29 | final class LogException extends Exception |
| 30 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 31 | /** The class name of the original exception. */ |
| 32 | private final String m_className; |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 33 | /** The message from the original exception. */ |
| 34 | private final String m_message; |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 35 | /** 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. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 42 | private LogException(final Throwable exception) |
| 43 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 44 | 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. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 50 | if (cause != null) |
| 51 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 52 | 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. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 63 | public String getMessage() |
| 64 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 65 | 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. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 74 | public String getLocalizedMessage() |
| 75 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 76 | 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. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 87 | static Throwable getException(final Throwable exception) |
| 88 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 89 | Throwable result = null; |
| 90 | |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 91 | if (exception != null) |
| 92 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 93 | String className = exception.getClass().getName(); |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 94 | if (exception.getCause() == null && className.startsWith(JAVA_PACKAGE_PREFIX)) |
| 95 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 96 | result = exception; |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 97 | } |
| 98 | else |
| 99 | { |
Richard S. Hall | 0b8e3ba | 2006-10-25 13:26:32 +0000 | [diff] [blame] | 100 | result = new LogException(exception); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return result; |
| 105 | } |
Richard S. Hall | ddf2e14 | 2009-09-30 17:03:45 +0000 | [diff] [blame] | 106 | } |