1
0

Changed createLogFromClass method to return null on recoverable failure,

and only throw an exception on unrecoverable failure. This simplifies
the code a fair bit.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@179546 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Kitching
2005-06-02 07:39:39 +00:00
parent 5c06195efb
commit ac7a0f8505

View File

@@ -468,20 +468,9 @@ public class LogFactoryImpl extends LogFactory {
* it will be. * it will be.
*/ */
protected boolean isJdk13LumberjackAvailable() { protected boolean isJdk13LumberjackAvailable() {
return isLogLibraryAvailable(
logDiagnostic("Checking for Jdk13Lumberjack."); "Jdk13Lumberjack",
try { "org.apache.commons.logging.impl.Jdk13LumberjackLogger");
createLogFromClass("org.apache.commons.logging.impl.Jdk13LumberjackLogger",
getClass().getName(),
false);
// No exception means success
logDiagnostic("Found Jdk13Lumberjack.");
return true;
} catch (Throwable t) {
logDiagnostic("Did not find Jdk13Lumberjack.");
return false;
}
} }
@@ -495,19 +484,9 @@ public class LogFactoryImpl extends LogFactory {
* it will be. * it will be.
*/ */
protected boolean isJdk14Available() { protected boolean isJdk14Available() {
return isLogLibraryAvailable(
logDiagnostic("Checking for Jdk14."); "Jdk14",
try { "org.apache.commons.logging.impl.Jdk14Logger");
createLogFromClass("org.apache.commons.logging.impl.Jdk14Logger",
getClass().getName(),
false);
// No exception means success
logDiagnostic("Found Jdk14.");
return true;
} catch (Throwable t) {
logDiagnostic("Did not find Jdk14.");
return false;
}
} }
@@ -518,19 +497,9 @@ public class LogFactoryImpl extends LogFactory {
* it will be. * it will be.
*/ */
protected boolean isLog4JAvailable() { protected boolean isLog4JAvailable() {
return isLogLibraryAvailable(
logDiagnostic("Checking for Log4J"); "Log4J",
try { "org.apache.commons.logging.impl.Log4JLogger");
createLogFromClass("org.apache.commons.logging.impl.Log4JLogger",
getClass().getName(),
false);
// No exception means success
logDiagnostic("Found Log4J.");
return true;
} catch (Throwable t) {
logDiagnostic("Did not find Log4J");
return false;
}
} }
@@ -588,6 +557,33 @@ public class LogFactoryImpl extends LogFactory {
// ------------------------------------------------------ Private Methods // ------------------------------------------------------ Private Methods
/**
* Utility method to check whether a particular logging library is
* present and available for use. Note that this does <i>not</i>
* affect the future behaviour of this class.
*/
private boolean isLogLibraryAvailable(String name, String classname) {
logDiagnostic("Checking for " + name + ".");
try {
Log log = createLogFromClass(
classname,
this.getClass().getName(), // dummy category
false);
if (log == null) {
logDiagnostic("Did not find " + name + ".");
return false;
} else {
logDiagnostic("Found " + name + ".");
return true;
}
} catch(LogConfigurationException e) {
logDiagnostic("Logging system " + name + " is available but not useable.");
return false;
}
}
/** /**
* Attempts to create a Log instance for the given category name. * Attempts to create a Log instance for the given category name.
* Follows the discovery process described in the class javadoc. * Follows the discovery process described in the class javadoc.
@@ -599,6 +595,7 @@ public class LogFactoryImpl extends LogFactory {
* instantiated * instantiated
*/ */
private Log discoverLogImplementation(String logCategory) private Log discoverLogImplementation(String logCategory)
throws LogConfigurationException
{ {
logDiagnostic("Attempting to discover a Log implementation."); logDiagnostic("Attempting to discover a Log implementation.");
@@ -608,101 +605,42 @@ public class LogFactoryImpl extends LogFactory {
String specifiedLogClassName = findUserSpecifiedLogClassName(); String specifiedLogClassName = findUserSpecifiedLogClassName();
if (specifiedLogClassName != null) { if (specifiedLogClassName != null) {
try {
// note: createLogFromClass never returns null.. // note: createLogFromClass never returns null..
result = createLogFromClass(specifiedLogClassName, result = createLogFromClass(specifiedLogClassName,
logCategory, logCategory,
true); true);
return result; if (result == null) {
} catch (LogConfigurationException ex) { throw new LogConfigurationException(
// this type of exception means we've already output "User-specified log class " + specifiedLogClassName
// diagnostics about this issue, etc.; just pass it on + " cannot be found or is not useable.");
throw ex;
} catch (Throwable t) {
// log problem, and throw a LogConfigurationException
// wrapping the Throwable
handleFlawedDiscovery(specifiedLogClassName, null, t);
// handleFlawedDiscovery should have thrown an LCE, but
// in case it didn't we'll throw one. Inability to
// instantiate a user specified class is a fatal error
throw new LogConfigurationException("Unable to instantiate "
+ specifiedLogClassName,
t);
} }
// this if-statement never exits! return result;
} }
// No user specified log; try to discover what's on the classpath // No user specified log; try to discover what's on the classpath
// Try Log4j // Try Log4j
try {
result = createLogFromClass("org.apache.commons.logging.impl.Log4JLogger", result = createLogFromClass("org.apache.commons.logging.impl.Log4JLogger",
logCategory, logCategory,
true); true);
} catch (LogConfigurationException lce) {
// LCE means we had a flawed discovery and already
// output diagnostics; just pass it on
throw (LogConfigurationException) lce;
} catch (Throwable t) {
// Other throwables just mean couldn't load the adapter
// or log4j; continue with discovery
}
if (result == null) { if (result == null) {
// Try JDK 1.4 Logging
try {
result = createLogFromClass("org.apache.commons.logging.impl.Jdk14Logger", result = createLogFromClass("org.apache.commons.logging.impl.Jdk14Logger",
logCategory, logCategory,
true); true);
} catch (LogConfigurationException lce) {
// LCE means we had a flawed discovery and already
// output diagnostics; just pass it on
throw (LogConfigurationException) lce;
} catch (Throwable t) {
// Other throwables just mean couldn't load the adapter
// or j.u.l; continue with discovery
}
} }
if (result == null) { if (result == null) {
// Try Lumberjack
try {
result = createLogFromClass("org.apache.commons.logging.impl.Jdk13LumberjackLogger", result = createLogFromClass("org.apache.commons.logging.impl.Jdk13LumberjackLogger",
logCategory, logCategory,
true); true);
} catch (LogConfigurationException lce) {
// LCE means we had a flawed discovery and already
// output diagnostics; just pass it on
throw (LogConfigurationException) lce;
} catch (Throwable t) {
// Other throwables just mean couldn't load the adapter
// or j.u.l; continue with discovery
}
} }
if (result == null) { if (result == null) {
// Try SimpleLog
try {
result = createLogFromClass("org.apache.commons.logging.impl.SimpleLog", result = createLogFromClass("org.apache.commons.logging.impl.SimpleLog",
logCategory, logCategory,
true); true);
} catch (LogConfigurationException lce) {
// LCE means we had a flawed discovery and already
// output diagnostics; just pass it up
throw (LogConfigurationException) lce;
} catch (Throwable t) {
// Other throwables just mean couldn't load the adapter
}
} }
if (result == null) { if (result == null) {
@@ -768,56 +706,60 @@ public class LogFactoryImpl extends LogFactory {
* be affected by this method call, <code>false</code> * be affected by this method call, <code>false</code>
* otherwise. * otherwise.
* *
* @return an instance of the given class. Will not return * @return an instance of the given class, or null if the logging
* <code>null</code>. * library associated with the specified adapter is not available.
* *
* @throws LinkageError if any linkage provoked by this method fails * @throws LogConfigurationException if there was a serious error with
* @throws ExceptionInInitializerError if any initialization provoked * configuration and the handleFlawedDiscovery method decided this
* by this method fails * problem was fatal.
* @throws ClassNotFoundException if the class cannot be located
* @throws NoClassDefFoundError if <code>logImplClass</code> could be
* loaded but the logging implementation it
* relies on could not be located
* @throws LogConfigurationException if the class was loaded but no suitable
* logger could be created and this object
* is configured to fail in such a
* situation
*/ */
private Log createLogFromClass(String logAdapterClass, private Log createLogFromClass(String logAdapterClass,
String logCategory, String logCategory,
boolean affectState) boolean affectState)
throws Throwable { throws LogConfigurationException {
logDiagnostic("Attempting to instantiate " + logAdapterClass); logDiagnostic("Attempting to instantiate " + logAdapterClass);
Class logClass = loadClass(logAdapterClass); Class logClass = null;
Object[] params = { logCategory }; Object[] params = { logCategory };
Log result = null; Log logAdapter = null;
Constructor constructor = null; Constructor constructor = null;
try { try {
logClass = loadClass(logAdapterClass);
constructor = logClass.getConstructor(logConstructorSignature); constructor = logClass.getConstructor(logConstructorSignature);
result = (Log) constructor.newInstance(params); logAdapter = (Log) constructor.newInstance(params);
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
// We were able to load the adapter but its underlying // We were able to load the adapter but it had references to
// logger library could not be found. This is normal and not // other classes that could not be found. This simply means that
// a "flawed discovery", so just throw the error on // the underlying logger library could not be found.
logDiagnostic("Unable to load logging library used by " String msg = "" + e.getMessage();
+ logAdapterClass); logDiagnostic(
throw e; "The logging library used by "
+ logAdapterClass
+ " is not available: "
+ msg.trim());
return null;
} catch (ExceptionInInitializerError e) {
// A static initializer block or the initializer code associated
// with a static variable on the log adapter class has thrown
// an exception.
//
// We treat this as meaning the adapter's underlying logging
// library could not be found.
String msg = "" + e.getMessage();
logDiagnostic(
"The logging library used by "
+ logAdapterClass
+ " is not available: "
+ msg.trim());
return null;
} catch(Throwable t) { } catch(Throwable t) {
// ExceptionInInitializerError // handleFlawedDiscovery will determine whether this is a fatal
// NoSuchMethodException // problem or not. If it is fatal, then a LogConfigurationException
// InvocationTargetException // will be thrown.
// ClassCastException
// All mean the adapter and underlying logger library were found
// but there was a problem creating an instance.
// This is a "flawed discovery"
handleFlawedDiscovery(logAdapterClass, logClass, t); handleFlawedDiscovery(logAdapterClass, logClass, t);
// handleFlawedDiscovery should have thrown an LCE, but return null;
// in case it didn't we'll throw one
throw new LogConfigurationException(t);
} }
if (affectState) { if (affectState) {
@@ -838,8 +780,7 @@ public class LogFactoryImpl extends LogFactory {
} }
} }
return result; return logAdapter;
} }