1
0

[LOGGING-144] Do not swallow certain Errors anymore, like ThreadDeath and VirtualMachineError.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/logging/trunk@1449064 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart
2013-02-22 14:49:22 +00:00
parent d6a182ebc2
commit e986065b7a
2 changed files with 34 additions and 6 deletions

View File

@@ -333,6 +333,8 @@ public abstract class LogFactory {
Class implementationClass = Class.forName(storeImplementationClass); Class implementationClass = Class.forName(storeImplementationClass);
result = (Hashtable) implementationClass.newInstance(); result = (Hashtable) implementationClass.newInstance();
} catch (Throwable t) { } catch (Throwable t) {
handleThrowable(t); // may re-throw t
// ignore // ignore
if (!WEAK_HASHTABLE_CLASSNAME.equals(storeImplementationClass)) { if (!WEAK_HASHTABLE_CLASSNAME.equals(storeImplementationClass)) {
// if the user's trying to set up a custom implementation, give a clue // if the user's trying to set up a custom implementation, give a clue
@@ -362,6 +364,28 @@ public abstract class LogFactory {
return src.trim(); return src.trim();
} }
/**
* Checks whether the supplied Throwable is one that needs to be
* re-thrown and ignores all others.
*
* The following errors are re-thrown:
* <ul>
* <li>ThreadDeath</li>
* <li>VirtualMachineError</li>
* </ul>
*
* @param t the Throwable to check
*/
protected static void handleThrowable(Throwable t) {
if (t instanceof ThreadDeath) {
throw (ThreadDeath) t;
}
if (t instanceof VirtualMachineError) {
throw (VirtualMachineError) t;
}
// All other instances of Throwable will be silently ignored
}
/** /**
* Construct (if necessary) and return a <code>LogFactory</code> * Construct (if necessary) and return a <code>LogFactory</code>
* instance, using the following ordered lookup procedure to determine * instance, using the following ordered lookup procedure to determine
@@ -1337,7 +1361,7 @@ public abstract class LogFactory {
if (stream != null) { if (stream != null) {
try { try {
stream.close(); stream.close();
} catch (Throwable t) { } catch (IOException e) {
// ignore exception; this should not happen // ignore exception; this should not happen
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic("Unable to close stream for URL " + url); logDiagnostic("Unable to close stream for URL " + url);

View File

@@ -425,7 +425,7 @@ public class LogFactoryImpl extends LogFactory {
} else { } else {
classLoaderName = objectId(classLoader); classLoaderName = objectId(classLoader);
} }
} catch(SecurityException e) { } catch (SecurityException e) {
classLoaderName = "UNKNOWN"; classLoaderName = "UNKNOWN";
} }
diagnosticPrefix = "[LogFactoryImpl@" + System.identityHashCode(this) + " from " + classLoaderName + "] "; diagnosticPrefix = "[LogFactoryImpl@" + System.identityHashCode(this) + " from " + classLoaderName + "] ";
@@ -565,6 +565,7 @@ public class LogFactoryImpl extends LogFactory {
Throwable c = e.getTargetException(); Throwable c = e.getTargetException();
throw new LogConfigurationException(c == null ? e : c); throw new LogConfigurationException(c == null ? e : c);
} catch (Throwable t) { } catch (Throwable t) {
handleThrowable(t); // may re-throw t
// A problem occurred invoking the Constructor or Method // A problem occurred invoking the Constructor or Method
// previously discovered // previously discovered
throw new LogConfigurationException(t); throw new LogConfigurationException(t);
@@ -635,7 +636,7 @@ public class LogFactoryImpl extends LogFactory {
return cl.getParent(); return cl.getParent();
} }
}); });
} catch(SecurityException ex) { } catch (SecurityException ex) {
logDiagnostic("[SECURITY] Unable to obtain parent classloader"); logDiagnostic("[SECURITY] Unable to obtain parent classloader");
return null; return null;
} }
@@ -668,7 +669,7 @@ public class LogFactoryImpl extends LogFactory {
} }
return true; return true;
} }
} catch(LogConfigurationException e) { } catch (LogConfigurationException e) {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic("Logging system '" + name + "' is available but not useable."); logDiagnostic("Logging system '" + name + "' is available but not useable.");
} }
@@ -1067,11 +1068,12 @@ public class LogFactoryImpl extends LogFactory {
"' is unable to initialize itself when loaded via classloader " + objectId(currentCL) + "' is unable to initialize itself when loaded via classloader " + objectId(currentCL) +
": " + msg.trim()); ": " + msg.trim());
break; break;
} catch(LogConfigurationException e) { } catch (LogConfigurationException e) {
// call to handleFlawedHierarchy above must have thrown // call to handleFlawedHierarchy above must have thrown
// a LogConfigurationException, so just throw it on // a LogConfigurationException, so just throw it on
throw e; throw e;
} catch(Throwable t) { } catch (Throwable t) {
handleThrowable(t); // may re-throw t
// handleFlawedDiscovery will determine whether this is a fatal // handleFlawedDiscovery will determine whether this is a fatal
// problem or not. If it is fatal, then a LogConfigurationException // problem or not. If it is fatal, then a LogConfigurationException
// will be thrown. // will be thrown.
@@ -1097,6 +1099,7 @@ public class LogFactoryImpl extends LogFactory {
this.logMethod = logAdapterClass.getMethod("setLogFactory", logMethodSignature); this.logMethod = logAdapterClass.getMethod("setLogFactory", logMethodSignature);
logDiagnostic("Found method setLogFactory(LogFactory) in '" + logAdapterClassName + "'"); logDiagnostic("Found method setLogFactory(LogFactory) in '" + logAdapterClassName + "'");
} catch (Throwable t) { } catch (Throwable t) {
handleThrowable(t); // may re-throw t
this.logMethod = null; this.logMethod = null;
logDiagnostic("[INFO] '" + logAdapterClassName + "' from classloader " + objectId(currentCL) + logDiagnostic("[INFO] '" + logAdapterClassName + "' from classloader " + objectId(currentCL) +
" does not declare optional method " + "setLogFactory(LogFactory)"); " does not declare optional method " + "setLogFactory(LogFactory)");
@@ -1337,6 +1340,7 @@ public class LogFactoryImpl extends LogFactory {
objectId(badClassLoader) + ". It is bound to a Log interface which is not" + objectId(badClassLoader) + ". It is bound to a Log interface which is not" +
" the one loaded from classloader " + objectId(logInterfaceClassLoader)); " the one loaded from classloader " + objectId(logInterfaceClassLoader));
} catch (Throwable t) { } catch (Throwable t) {
handleThrowable(t); // may re-throw t
logDiagnostic("Error while trying to output diagnostics about" + " bad class '" + badClass + "'"); logDiagnostic("Error while trying to output diagnostics about" + " bad class '" + badClass + "'");
} }
} }