1
0

Formatting.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/logging/trunk@1432675 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart
2013-01-13 17:53:30 +00:00
parent 70a06f8947
commit cf973028d8
5 changed files with 142 additions and 206 deletions

View File

@@ -145,18 +145,16 @@ public abstract class LogFactory {
private static final String diagnosticPrefix; private static final String diagnosticPrefix;
/** /**
* <p>Setting this system property * Setting this system property
* (<code>org.apache.commons.logging.LogFactory.HashtableImpl</code>) * (<code>org.apache.commons.logging.LogFactory.HashtableImpl</code>)
* value allows the <code>Hashtable</code> used to store * value allows the <code>Hashtable</code> used to store
* classloaders to be substituted by an alternative implementation. * classloaders to be substituted by an alternative implementation.
* </p>
* <p> * <p>
* <strong>Note:</strong> <code>LogFactory</code> will print: * <strong>Note:</strong> <code>LogFactory</code> will print:
* <code><pre> * <code><pre>
* [ERROR] LogFactory: Load of custom hashtable failed</em> * [ERROR] LogFactory: Load of custom hashtable failed</em>
* </pre></code> * </pre></code>
* to system error and then continue using a standard Hashtable. * to system error and then continue using a standard Hashtable.
* </p>
* <p> * <p>
* <strong>Usage:</strong> Set this property when Java is invoked * <strong>Usage:</strong> Set this property when Java is invoked
* and <code>LogFactory</code> will attempt to load a new instance * and <code>LogFactory</code> will attempt to load a new instance
@@ -172,18 +170,16 @@ public abstract class LogFactory {
* </pre></code> * </pre></code>
* will mean that <code>LogFactory</code> will load an instance of * will mean that <code>LogFactory</code> will load an instance of
* <code>org.apache.commons.logging.AltHashtable</code>. * <code>org.apache.commons.logging.AltHashtable</code>.
* </p>
* <p> * <p>
* A typical use case is to allow a custom * A typical use case is to allow a custom
* Hashtable implementation using weak references to be substituted. * Hashtable implementation using weak references to be substituted.
* This will allow classloaders to be garbage collected without * This will allow classloaders to be garbage collected without
* the need to release them (on 1.3+ JVMs only, of course ;) * the need to release them (on 1.3+ JVMs only, of course ;).
* </p>
*/ */
public static final String HASHTABLE_IMPLEMENTATION_PROPERTY = public static final String HASHTABLE_IMPLEMENTATION_PROPERTY =
"org.apache.commons.logging.LogFactory.HashtableImpl"; "org.apache.commons.logging.LogFactory.HashtableImpl";
/** Name used to load the weak hashtable implementation by names */ /** Name used to load the weak hashtable implementation by names. */
private static final String WEAK_HASHTABLE_CLASSNAME = private static final String WEAK_HASHTABLE_CLASSNAME =
"org.apache.commons.logging.impl.WeakHashtable"; "org.apache.commons.logging.impl.WeakHashtable";
@@ -226,7 +222,6 @@ public abstract class LogFactory {
* call <code>getInstance(String)</code> with it. * call <code>getInstance(String)</code> with it.
* *
* @param clazz Class for which a suitable Log name will be derived * @param clazz Class for which a suitable Log name will be derived
*
* @throws LogConfigurationException if a suitable <code>Log</code> * @throws LogConfigurationException if a suitable <code>Log</code>
* instance cannot be returned * instance cannot be returned
*/ */
@@ -246,7 +241,6 @@ public abstract class LogFactory {
* @param name Logical name of the <code>Log</code> instance to be * @param name Logical name of the <code>Log</code> instance to be
* returned (the meaning of this name is only known to the underlying * returned (the meaning of this name is only known to the underlying
* logging implementation that is being wrapped) * logging implementation that is being wrapped)
*
* @throws LogConfigurationException if a suitable <code>Log</code> * @throws LogConfigurationException if a suitable <code>Log</code>
* instance cannot be returned * instance cannot be returned
*/ */
@@ -491,7 +485,8 @@ public abstract class LogFactory {
// the specified class wasn't castable to this LogFactory type. // the specified class wasn't castable to this LogFactory type.
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic("[LOOKUP] An exception occurred while trying to create an" + logDiagnostic("[LOOKUP] An exception occurred while trying to create an" +
" instance of the custom factory class" + ": [" + trim(e.getMessage()) + " instance of the custom factory class" + ": [" +
trim(e.getMessage()) +
"] as specified by a system property."); "] as specified by a system property.");
} }
throw e; throw e;
@@ -509,8 +504,7 @@ public abstract class LogFactory {
"] to define the LogFactory subclass to use..."); "] to define the LogFactory subclass to use...");
} }
try { try {
InputStream is = getResourceAsStream(contextClassLoader, final InputStream is = getResourceAsStream(contextClassLoader, SERVICE_ID);
SERVICE_ID);
if( is != null ) { if( is != null ) {
// This code is needed by EBCDIC and other strange systems. // This code is needed by EBCDIC and other strange systems.
@@ -525,10 +519,10 @@ public abstract class LogFactory {
String factoryClassName = rd.readLine(); String factoryClassName = rd.readLine();
rd.close(); rd.close();
if (factoryClassName != null && if (factoryClassName != null && ! "".equals(factoryClassName)) {
! "".equals(factoryClassName)) {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic("[LOOKUP] Creating an instance of LogFactory class " + factoryClassName + logDiagnostic("[LOOKUP] Creating an instance of LogFactory class " +
factoryClassName +
" as specified by file '" + SERVICE_ID + " as specified by file '" + SERVICE_ID +
"' which was present in the path of the context classloader."); "' which was present in the path of the context classloader.");
} }
@@ -546,10 +540,10 @@ public abstract class LogFactory {
// continue to find a compatible class. // continue to find a compatible class.
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic(
"[LOOKUP] A security exception occurred while trying to create an" "[LOOKUP] A security exception occurred while trying to create an" +
+ " instance of the custom factory class" " instance of the custom factory class" +
+ ": [" + trim(ex.getMessage()) ": [" + trim(ex.getMessage()) +
+ "]. Trying alternative implementations..."); "]. Trying alternative implementations...");
} }
// ignore // ignore
} }
@@ -561,31 +555,26 @@ public abstract class LogFactory {
if (props != null) { if (props != null) {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic(
"[LOOKUP] Looking in properties file for entry with key '" "[LOOKUP] Looking in properties file for entry with key '" + FACTORY_PROPERTY +
+ FACTORY_PROPERTY "' to define the LogFactory subclass to use...");
+ "' to define the LogFactory subclass to use...");
} }
String factoryClass = props.getProperty(FACTORY_PROPERTY); String factoryClass = props.getProperty(FACTORY_PROPERTY);
if (factoryClass != null) { if (factoryClass != null) {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic(
"[LOOKUP] Properties file specifies LogFactory subclass '" "[LOOKUP] Properties file specifies LogFactory subclass '" + factoryClass + "'");
+ factoryClass + "'");
} }
factory = newFactory(factoryClass, baseClassLoader, contextClassLoader); factory = newFactory(factoryClass, baseClassLoader, contextClassLoader);
// TODO: think about whether we need to handle exceptions from newFactory // TODO: think about whether we need to handle exceptions from newFactory
} else { } else {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("[LOOKUP] Properties file has no entry specifying LogFactory subclass.");
"[LOOKUP] Properties file has no entry specifying LogFactory subclass.");
} }
} }
} else { } else {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("[LOOKUP] No properties file available to determine" + " LogFactory subclass from..");
"[LOOKUP] No properties file available to determine"
+ " LogFactory subclass from..");
} }
} }
} }
@@ -595,9 +584,9 @@ public abstract class LogFactory {
if (factory == null) { if (factory == null) {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic(
"[LOOKUP] Loading the default LogFactory implementation '" + FACTORY_DEFAULT "[LOOKUP] Loading the default LogFactory implementation '" + FACTORY_DEFAULT +
+ "' via the same classloader that loaded this LogFactory" "' via the same classloader that loaded this LogFactory" +
+ " class (ie not looking in the context classloader)."); " class (ie not looking in the context classloader).");
} }
// Note: unlike the above code which can try to load custom LogFactory // Note: unlike the above code which can try to load custom LogFactory
@@ -618,7 +607,7 @@ public abstract class LogFactory {
*/ */
cacheFactory(contextClassLoader, factory); cacheFactory(contextClassLoader, factory);
if( props!=null ) { if (props != null) {
Enumeration names = props.propertyNames(); Enumeration names = props.propertyNames();
while (names.hasMoreElements()) { while (names.hasMoreElements()) {
String name = (String) names.nextElement(); String name = (String) names.nextElement();
@@ -636,15 +625,11 @@ public abstract class LogFactory {
* having to care about factories. * having to care about factories.
* *
* @param clazz Class from which a log name will be derived * @param clazz Class from which a log name will be derived
*
* @throws LogConfigurationException if a suitable <code>Log</code> * @throws LogConfigurationException if a suitable <code>Log</code>
* instance cannot be returned * instance cannot be returned
*/ */
public static Log getLog(Class clazz) public static Log getLog(Class clazz) throws LogConfigurationException {
throws LogConfigurationException {
return getFactory().getInstance(clazz); return getFactory().getInstance(clazz);
} }
/** /**
@@ -654,15 +639,11 @@ public abstract class LogFactory {
* @param name Logical name of the <code>Log</code> instance to be * @param name Logical name of the <code>Log</code> instance to be
* returned (the meaning of this name is only known to the underlying * returned (the meaning of this name is only known to the underlying
* logging implementation that is being wrapped) * logging implementation that is being wrapped)
*
* @throws LogConfigurationException if a suitable <code>Log</code> * @throws LogConfigurationException if a suitable <code>Log</code>
* instance cannot be returned * instance cannot be returned
*/ */
public static Log getLog(String name) public static Log getLog(String name) throws LogConfigurationException {
throws LogConfigurationException {
return getFactory().getInstance(name); return getFactory().getInstance(name);
} }
/** /**
@@ -674,7 +655,6 @@ public abstract class LogFactory {
* @param classLoader ClassLoader for which to release the LogFactory * @param classLoader ClassLoader for which to release the LogFactory
*/ */
public static void release(ClassLoader classLoader) { public static void release(ClassLoader classLoader) {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic("Releasing factory for classloader " + objectId(classLoader)); logDiagnostic("Releasing factory for classloader " + objectId(classLoader));
} }
@@ -687,14 +667,13 @@ public abstract class LogFactory {
nullClassLoaderFactory = null; nullClassLoaderFactory = null;
} }
} else { } else {
LogFactory factory = (LogFactory) factories.get(classLoader); final LogFactory factory = (LogFactory) factories.get(classLoader);
if (factory != null) { if (factory != null) {
factory.release(); factory.release();
factories.remove(classLoader); factories.remove(classLoader);
} }
} }
} }
} }
/** /**
@@ -712,7 +691,7 @@ public abstract class LogFactory {
// factories is not final and could be replaced in this block. // factories is not final and could be replaced in this block.
final Hashtable factories = LogFactory.factories; final Hashtable factories = LogFactory.factories;
synchronized (factories) { synchronized (factories) {
Enumeration elements = factories.elements(); final Enumeration elements = factories.elements();
while (elements.hasMoreElements()) { while (elements.hasMoreElements()) {
LogFactory element = (LogFactory) elements.nextElement(); LogFactory element = (LogFactory) elements.nextElement();
element.release(); element.release();
@@ -760,9 +739,8 @@ public abstract class LogFactory {
return clazz.getClassLoader(); return clazz.getClassLoader();
} catch(SecurityException ex) { } catch(SecurityException ex) {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("Unable to get classloader for class '" + clazz +
"Unable to get classloader for class '" + clazz "' due to security restrictions - " + ex.getMessage());
+ "' due to security restrictions - " + ex.getMessage());
} }
throw ex; throw ex;
} }
@@ -781,17 +759,13 @@ public abstract class LogFactory {
* support for that. * support for that.
* *
* @return the context classloader associated with the current thread, * @return the context classloader associated with the current thread,
* or null if security doesn't allow it. * or null if security doesn't allow it.
*
* @throws LogConfigurationException if there was some weird error while * @throws LogConfigurationException if there was some weird error while
* attempting to get the context classloader. * attempting to get the context classloader.
*
* @throws SecurityException if the current java security policy doesn't * @throws SecurityException if the current java security policy doesn't
* allow this class to access the context classloader. * allow this class to access the context classloader.
*/ */
protected static ClassLoader getContextClassLoader() protected static ClassLoader getContextClassLoader() throws LogConfigurationException {
throws LogConfigurationException {
return directGetContextClassLoader(); return directGetContextClassLoader();
} }
@@ -805,13 +779,11 @@ public abstract class LogFactory {
* allowed. * allowed.
* *
* @return the context classloader associated with the current thread, * @return the context classloader associated with the current thread,
* or null if security doesn't allow it. * or null if security doesn't allow it.
*
* @throws LogConfigurationException if there was some weird error while * @throws LogConfigurationException if there was some weird error while
* attempting to get the context classloader. * attempting to get the context classloader.
*
* @throws SecurityException if the current java security policy doesn't * @throws SecurityException if the current java security policy doesn't
* allow this class to access the context classloader. * allow this class to access the context classloader.
*/ */
private static ClassLoader getContextClassLoaderInternal() throws LogConfigurationException { private static ClassLoader getContextClassLoaderInternal() throws LogConfigurationException {
return (ClassLoader)AccessController.doPrivileged( return (ClassLoader)AccessController.doPrivileged(
@@ -823,8 +795,7 @@ public abstract class LogFactory {
} }
/** /**
* Return the thread context class loader if available; otherwise return * Return the thread context class loader if available; otherwise return null.
* null.
* <p> * <p>
* Most/all code should call getContextClassLoaderInternal rather than * Most/all code should call getContextClassLoaderInternal rather than
* calling this method directly. * calling this method directly.
@@ -836,22 +807,19 @@ public abstract class LogFactory {
* this method is called every time LogFactory.getLogger() is called, * this method is called every time LogFactory.getLogger() is called,
* and we don't want too much output generated here. * and we don't want too much output generated here.
* *
* @exception LogConfigurationException if a suitable class loader * @throws LogConfigurationException if a suitable class loader
* cannot be identified. * cannot be identified.
* * @throws SecurityException if the java security policy forbids
* @exception SecurityException if the java security policy forbids * access to the context classloader from one of the classes in the
* access to the context classloader from one of the classes in the * current call stack.
* current call stack.
* @since 1.1 * @since 1.1
*/ */
protected static ClassLoader directGetContextClassLoader() protected static ClassLoader directGetContextClassLoader() throws LogConfigurationException {
throws LogConfigurationException
{
ClassLoader classLoader = null; ClassLoader classLoader = null;
try { try {
// Are we running on a JDK 1.2 or later system? // Are we running on a JDK 1.2 or later system?
Method method = Thread.class.getMethod("getContextClassLoader", (Class[]) null); final Method method = Thread.class.getMethod("getContextClassLoader", (Class[]) null);
// Get the thread context class loader (if there is one) // Get the thread context class loader (if there is one)
try { try {
@@ -881,8 +849,7 @@ public abstract class LogFactory {
} else { } else {
// Capture 'e.getTargetException()' exception for details // Capture 'e.getTargetException()' exception for details
// alternate: log 'e.getTargetException()', and pass back 'e'. // alternate: log 'e.getTargetException()', and pass back 'e'.
throw new LogConfigurationException throw new LogConfigurationException("Unexpected InvocationTargetException", e.getTargetException());
("Unexpected InvocationTargetException", e.getTargetException());
} }
} }
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
@@ -917,11 +884,10 @@ public abstract class LogFactory {
* placed in the bootclasspath. * placed in the bootclasspath.
* *
* @return the factory associated with the specified classloader if * @return the factory associated with the specified classloader if
* one has previously been created, or null if this is the first time * one has previously been created, or null if this is the first time
* we have seen this particular classloader. * we have seen this particular classloader.
*/ */
private static LogFactory getCachedFactory(ClassLoader contextClassLoader) private static LogFactory getCachedFactory(ClassLoader contextClassLoader) {
{
if (contextClassLoader == null) { if (contextClassLoader == null) {
// We have to handle this specially, as factories is a Hashtable // We have to handle this specially, as factories is a Hashtable
// and those don't accept null as a key value. // and those don't accept null as a key value.
@@ -939,12 +905,10 @@ public abstract class LogFactory {
* cached Log objects). * cached Log objects).
* *
* @param classLoader should be the current context classloader. Note that * @param classLoader should be the current context classloader. Note that
* this can be null under some circumstances; this is ok. * this can be null under some circumstances; this is ok.
*
* @param factory should be the factory to cache. This should never be null. * @param factory should be the factory to cache. This should never be null.
*/ */
private static void cacheFactory(ClassLoader classLoader, LogFactory factory) private static void cacheFactory(ClassLoader classLoader, LogFactory factory) {
{
// Ideally we would assert(factory != null) here. However reporting // Ideally we would assert(factory != null) here. However reporting
// errors from within a logging implementation is a little tricky! // errors from within a logging implementation is a little tricky!
@@ -991,14 +955,13 @@ public abstract class LogFactory {
* do not have to move the custom LogFactory subclass; that is ok as * do not have to move the custom LogFactory subclass; that is ok as
* long as the only LogFactory class it can find to bind to is in the * long as the only LogFactory class it can find to bind to is in the
* parent classloader. * parent classloader.
* <p> *
* @param factoryClass Fully qualified name of the <code>LogFactory</code> * @param factoryClass Fully qualified name of the <code>LogFactory</code>
* implementation class * implementation class
* @param classLoader ClassLoader from which to load this class * @param classLoader ClassLoader from which to load this class
* @param contextClassLoader is the context that this new factory will * @param contextClassLoader is the context that this new factory will
* manage logging for. * manage logging for.
* * @throws LogConfigurationException if a suitable instance
* @exception LogConfigurationException if a suitable instance
* cannot be created * cannot be created
* @since 1.1 * @since 1.1
*/ */
@@ -1054,12 +1017,10 @@ public abstract class LogFactory {
* Implements the operations described in the javadoc for newFactory. * Implements the operations described in the javadoc for newFactory.
* *
* @param factoryClass * @param factoryClass
*
* @param classLoader used to load the specified factory class. This is * @param classLoader used to load the specified factory class. This is
* expected to be either the TCCL or the classloader which loaded this * expected to be either the TCCL or the classloader which loaded this
* class. Note that the classloader which loaded this class might be * class. Note that the classloader which loaded this class might be
* "null" (ie the bootloader) for embedded systems. * "null" (ie the bootloader) for embedded systems.
*
* @return either a LogFactory object or a LogConfigurationException object. * @return either a LogFactory object or a LogConfigurationException object.
* @since 1.1 * @since 1.1
*/ */
@@ -1077,9 +1038,8 @@ public abstract class LogFactory {
logFactoryClass = classLoader.loadClass(factoryClass); logFactoryClass = classLoader.loadClass(factoryClass);
if (LogFactory.class.isAssignableFrom(logFactoryClass)) { if (LogFactory.class.isAssignableFrom(logFactoryClass)) {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("Loaded class " + logFactoryClass.getName() +
"Loaded class " + logFactoryClass.getName() " from classloader " + objectId(classLoader));
+ " from classloader " + objectId(classLoader));
} }
} else { } else {
// //
@@ -1094,11 +1054,10 @@ public abstract class LogFactory {
// ClassLoader hierarchy. // ClassLoader hierarchy.
// //
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("Factory class " + logFactoryClass.getName() +
"Factory class " + logFactoryClass.getName() " loaded from classloader " + objectId(logFactoryClass.getClassLoader()) +
+ " loaded from classloader " + objectId(logFactoryClass.getClassLoader()) " does not extend '" + LogFactory.class.getName() +
+ " does not extend '" + LogFactory.class.getName() "' as loaded by this classloader.");
+ "' as loaded by this classloader.");
logHierarchy("[BAD CL TREE] ", classLoader); logHierarchy("[BAD CL TREE] ", classLoader);
} }
} }
@@ -1109,9 +1068,8 @@ public abstract class LogFactory {
if (classLoader == thisClassLoader) { if (classLoader == thisClassLoader) {
// Nothing more to try, onwards. // Nothing more to try, onwards.
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("Unable to locate any class called '" + factoryClass +
"Unable to locate any class called '" + factoryClass "' via classloader " + objectId(classLoader));
+ "' via classloader " + objectId(classLoader));
} }
throw ex; throw ex;
} }
@@ -1120,11 +1078,9 @@ public abstract class LogFactory {
if (classLoader == thisClassLoader) { if (classLoader == thisClassLoader) {
// Nothing more to try, onwards. // Nothing more to try, onwards.
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("Class '" + factoryClass + "' cannot be loaded" +
"Class '" + factoryClass + "' cannot be loaded" " via classloader " + objectId(classLoader) +
+ " via classloader " + objectId(classLoader) " - it depends on some other class that cannot be found.");
+ " - it depends on some other class that cannot"
+ " be found.");
} }
throw e; throw e;
} }
@@ -1143,27 +1099,31 @@ public abstract class LogFactory {
// has been specified. Several well known containers use this mechanism to adapt JCL // has been specified. Several well known containers use this mechanism to adapt JCL
// to their native logging system. // to their native logging system.
// //
String msg = final StringBuffer msg = new StringBuffer();
"The application has specified that a custom LogFactory implementation should be used but " + msg.append("The application has specified that a custom LogFactory implementation ");
"Class '" + factoryClass + "' cannot be converted to '" msg.append("should be used but Class '");
+ LogFactory.class.getName() + "'. "; msg.append(factoryClass);
msg.append("' cannot be converted to '");
msg.append(LogFactory.class.getName());
msg.append("'. ");
if (implementsLogFactory) { if (implementsLogFactory) {
msg = msg + "The conflict is caused by the presence of multiple LogFactory classes in incompatible classloaders. " + msg.append("The conflict is caused by the presence of multiple LogFactory classes ");
"Background can be found in http://commons.apache.org/logging/tech.html. " + msg.append("in incompatible classloaders. ");
"If you have not explicitly specified a custom LogFactory then it is likely that " + msg.append("Background can be found in http://commons.apache.org/logging/tech.html. ");
"the container has set one without your knowledge. " + msg.append("If you have not explicitly specified a custom LogFactory then it is likely ");
"In this case, consider using the commons-logging-adapters.jar file or " + msg.append("that the container has set one without your knowledge. ");
"specifying the standard LogFactory from the command line. "; msg.append("In this case, consider using the commons-logging-adapters.jar file or ");
msg.append("specifying the standard LogFactory from the command line. ");
} else { } else {
msg = msg + "Please check the custom implementation. "; msg.append("Please check the custom implementation. ");
} }
msg = msg + "Help can be found @http://commons.apache.org/logging/troubleshooting.html."; msg.append("Help can be found @http://commons.apache.org/logging/troubleshooting.html.");
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic(msg); logDiagnostic(msg.toString());
} }
throw new ClassCastException(msg); throw new ClassCastException(msg.toString());
} }
// Ignore exception, continue. Presumably the classloader was the // Ignore exception, continue. Presumably the classloader was the
@@ -1198,10 +1158,8 @@ public abstract class LogFactory {
// Warning: must typecast here & allow exception // Warning: must typecast here & allow exception
// to be generated/caught & recast properly. // to be generated/caught & recast properly.
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("Unable to load factory class via classloader " + objectId(classLoader) +
"Unable to load factory class via classloader " " - trying the classloader associated with this LogFactory.");
+ objectId(classLoader)
+ " - trying the classloader associated with this LogFactory.");
} }
logFactoryClass = Class.forName(factoryClass); logFactoryClass = Class.forName(factoryClass);
return (LogFactory) logFactoryClass.newInstance(); return (LogFactory) logFactoryClass.newInstance();
@@ -1212,9 +1170,8 @@ public abstract class LogFactory {
} }
if (logFactoryClass != null && !LogFactory.class.isAssignableFrom(logFactoryClass)) { if (logFactoryClass != null && !LogFactory.class.isAssignableFrom(logFactoryClass)) {
return new LogConfigurationException( return new LogConfigurationException(
"The chosen LogFactory implementation does not extend LogFactory." "The chosen LogFactory implementation does not extend LogFactory." +
+ " Please check your configuration.", " Please check your configuration.", e);
e);
} }
return new LogConfigurationException(e); return new LogConfigurationException(e);
} }
@@ -1245,11 +1202,11 @@ public abstract class LogFactory {
= Class.forName("org.apache.commons.logging.LogFactory", false, logFactoryClassLoader); = Class.forName("org.apache.commons.logging.LogFactory", false, logFactoryClassLoader);
implementsLogFactory = factoryFromCustomLoader.isAssignableFrom(logFactoryClass); implementsLogFactory = factoryFromCustomLoader.isAssignableFrom(logFactoryClass);
if (implementsLogFactory) { if (implementsLogFactory) {
logDiagnostic("[CUSTOM LOG FACTORY] " + logFactoryClass.getName() logDiagnostic("[CUSTOM LOG FACTORY] " + logFactoryClass.getName() +
+ " implements LogFactory but was loaded by an incompatible classloader."); " implements LogFactory but was loaded by an incompatible classloader.");
} else { } else {
logDiagnostic("[CUSTOM LOG FACTORY] " + logFactoryClass.getName() logDiagnostic("[CUSTOM LOG FACTORY] " + logFactoryClass.getName() +
+ " does not implement LogFactory."); " does not implement LogFactory.");
} }
} }
} catch (SecurityException e) { } catch (SecurityException e) {
@@ -1259,8 +1216,7 @@ public abstract class LogFactory {
// Consider running less securely whilst debugging this issue. // Consider running less securely whilst debugging this issue.
// //
logDiagnostic("[CUSTOM LOG FACTORY] SecurityException thrown whilst trying to determine whether " + logDiagnostic("[CUSTOM LOG FACTORY] SecurityException thrown whilst trying to determine whether " +
"the compatibility was caused by a classloader conflict: " "the compatibility was caused by a classloader conflict: " + e.getMessage());
+ e.getMessage());
} catch (LinkageError e) { } catch (LinkageError e) {
// //
// This should be an unusual circumstance. // This should be an unusual circumstance.
@@ -1269,8 +1225,7 @@ public abstract class LogFactory {
// Time for a clean rebuild? // Time for a clean rebuild?
// //
logDiagnostic("[CUSTOM LOG FACTORY] LinkageError thrown whilst trying to determine whether " + logDiagnostic("[CUSTOM LOG FACTORY] LinkageError thrown whilst trying to determine whether " +
"the compatibility was caused by a classloader conflict: " "the compatibility was caused by a classloader conflict: " + e.getMessage());
+ e.getMessage());
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
// //
// LogFactory cannot be loaded by the classloader which loaded the custom factory implementation. // LogFactory cannot be loaded by the classloader which loaded the custom factory implementation.
@@ -1279,8 +1234,8 @@ public abstract class LogFactory {
// Running with diagnostics on should give information about the classloaders used // Running with diagnostics on should give information about the classloaders used
// to load the custom factory. // to load the custom factory.
// //
logDiagnostic("[CUSTOM LOG FACTORY] LogFactory class cannot be loaded by classloader which loaded the " + logDiagnostic("[CUSTOM LOG FACTORY] LogFactory class cannot be loaded by classloader which loaded " +
"custom LogFactory implementation. Is the custom factory in the right classloader?"); "the custom LogFactory implementation. Is the custom factory in the right classloader?");
} }
} }
return implementsLogFactory; return implementsLogFactory;
@@ -1441,9 +1396,8 @@ public abstract class LogFactory {
} }
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("[LOOKUP] Properties file found at '" + url + "'" +
"[LOOKUP] Properties file found at '" + url + "'" " with priority " + priority);
+ " with priority " + priority);
} }
} else { } else {
String newPriorityStr = newProps.getProperty(PRIORITY_KEY); String newPriorityStr = newProps.getProperty(PRIORITY_KEY);
@@ -1454,11 +1408,10 @@ public abstract class LogFactory {
if (newPriority > priority) { if (newPriority > priority) {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("[LOOKUP] Properties file at '" + url + "'" +
"[LOOKUP] Properties file at '" + url + "'" " with priority " + newPriority +
+ " with priority " + newPriority " overrides file at '" + propsUrl + "'" +
+ " overrides file at '" + propsUrl + "'" " with priority " + priority);
+ " with priority " + priority);
} }
propsUrl = url; propsUrl = url;
@@ -1466,11 +1419,10 @@ public abstract class LogFactory {
priority = newPriority; priority = newPriority;
} else { } else {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic( logDiagnostic("[LOOKUP] Properties file at '" + url + "'" +
"[LOOKUP] Properties file at '" + url + "'" " with priority " + newPriority +
+ " with priority " + newPriority " does not override file at '" + propsUrl + "'" +
+ " does not override file at '" + propsUrl + "'" " with priority " + priority);
+ " with priority " + priority);
} }
} }
} }
@@ -1639,8 +1591,7 @@ public abstract class LogFactory {
classLoader = getClassLoader(clazz); classLoader = getClassLoader(clazz);
} catch(SecurityException ex) { } catch(SecurityException ex) {
// not much useful diagnostics we can print here! // not much useful diagnostics we can print here!
logDiagnostic( logDiagnostic("[ENV] Security forbids determining the classloader for " + className);
"[ENV] Security forbids determining the classloader for " + className);
return; return;
} }
@@ -1672,7 +1623,7 @@ public abstract class LogFactory {
return; return;
} }
if (classLoader != null) { if (classLoader != null) {
StringBuffer buf = new StringBuffer(prefix + "ClassLoader tree:"); final StringBuffer buf = new StringBuffer(prefix + "ClassLoader tree:");
for(;;) { for(;;) {
buf.append(objectId(classLoader)); buf.append(objectId(classLoader));
if (classLoader == systemClassLoader) { if (classLoader == systemClassLoader) {

View File

@@ -81,7 +81,7 @@ public class LogSource {
// Is JDK 1.4 Logging Available? // Is JDK 1.4 Logging Available?
try { try {
jdk14IsAvailable = null != Class.forName("java.util.logging.Logger") && jdk14IsAvailable = null != Class.forName("java.util.logging.Logger") &&
null != Class.forName("org.apache.commons.logging.impl.Jdk14Logger"); null != Class.forName("org.apache.commons.logging.impl.Jdk14Logger");
} catch (Throwable t) { } catch (Throwable t) {
jdk14IsAvailable = false; jdk14IsAvailable = false;
} }
@@ -100,8 +100,7 @@ public class LogSource {
setLogImplementation(name); setLogImplementation(name);
} catch (Throwable t) { } catch (Throwable t) {
try { try {
setLogImplementation setLogImplementation("org.apache.commons.logging.impl.NoOpLog");
("org.apache.commons.logging.impl.NoOpLog");
} catch (Throwable u) { } catch (Throwable u) {
// ignored // ignored
} }
@@ -109,19 +108,15 @@ public class LogSource {
} else { } else {
try { try {
if (log4jIsAvailable) { if (log4jIsAvailable) {
setLogImplementation setLogImplementation("org.apache.commons.logging.impl.Log4JLogger");
("org.apache.commons.logging.impl.Log4JLogger");
} else if (jdk14IsAvailable) { } else if (jdk14IsAvailable) {
setLogImplementation setLogImplementation("org.apache.commons.logging.impl.Jdk14Logger");
("org.apache.commons.logging.impl.Jdk14Logger");
} else { } else {
setLogImplementation setLogImplementation("org.apache.commons.logging.impl.NoOpLog");
("org.apache.commons.logging.impl.NoOpLog");
} }
} catch (Throwable t) { } catch (Throwable t) {
try { try {
setLogImplementation setLogImplementation("org.apache.commons.logging.impl.NoOpLog");
("org.apache.commons.logging.impl.NoOpLog");
} catch (Throwable u) { } catch (Throwable u) {
// ignored // ignored
} }
@@ -144,10 +139,8 @@ public class LogSource {
* and provide a constructor that takes a single {@link String} argument * and provide a constructor that takes a single {@link String} argument
* (containing the name of the log). * (containing the name of the log).
*/ */
static public void setLogImplementation(String classname) throws static public void setLogImplementation(String classname)
LinkageError, throws LinkageError, NoSuchMethodException, SecurityException, ClassNotFoundException {
NoSuchMethodException, SecurityException,
ClassNotFoundException {
try { try {
Class logclass = Class.forName(classname); Class logclass = Class.forName(classname);
Class[] argtypes = new Class[1]; Class[] argtypes = new Class[1];
@@ -163,9 +156,8 @@ public class LogSource {
* The given class must implement {@link Log}, and provide a constructor * The given class must implement {@link Log}, and provide a constructor
* that takes a single {@link String} argument (containing the name of the log). * that takes a single {@link String} argument (containing the name of the log).
*/ */
static public void setLogImplementation(Class logclass) throws static public void setLogImplementation(Class logclass)
LinkageError, ExceptionInInitializerError, throws LinkageError, ExceptionInInitializerError, NoSuchMethodException, SecurityException {
NoSuchMethodException, SecurityException {
Class[] argtypes = new Class[1]; Class[] argtypes = new Class[1];
argtypes[0] = "".getClass(); argtypes[0] = "".getClass();
logImplctor = logclass.getConstructor(argtypes); logImplctor = logclass.getConstructor(argtypes);
@@ -204,7 +196,6 @@ public class LogSource {
* @param name the log name (or category) * @param name the log name (or category)
*/ */
static public Log makeNewLogInstance(String name) { static public Log makeNewLogInstance(String name) {
Log log; Log log;
try { try {
Object[] args = { name }; Object[] args = { name };
@@ -216,7 +207,6 @@ public class LogSource {
log = new NoOpLog(name); log = new NoOpLog(name);
} }
return log; return log;
} }
/** /**

View File

@@ -73,17 +73,17 @@ public class Jdk14Logger implements Log, Serializable {
Logger logger = getLogger(); Logger logger = getLogger();
if (logger.isLoggable(level)) { if (logger.isLoggable(level)) {
// Hack (?) to get the stack trace. // Hack (?) to get the stack trace.
Throwable dummyException=new Throwable(); Throwable dummyException = new Throwable();
StackTraceElement locations[]=dummyException.getStackTrace(); StackTraceElement locations[] = dummyException.getStackTrace();
// Caller will be the third element // Caller will be the third element
String cname="unknown"; String cname = "unknown";
String method="unknown"; String method = "unknown";
if( locations!=null && locations.length >2 ) { if( locations != null && locations.length > 2 ) {
StackTraceElement caller=locations[2]; StackTraceElement caller=locations[2];
cname=caller.getClassName(); cname=caller.getClassName();
method=caller.getMethodName(); method=caller.getMethodName();
} }
if( ex==null ) { if( ex == null ) {
logger.logp( level, cname, method, msg ); logger.logp( level, cname, method, msg );
} else { } else {
logger.logp( level, cname, method, msg, ex ); logger.logp( level, cname, method, msg, ex );

View File

@@ -117,7 +117,7 @@ public class Log4JLogger implements Log, Serializable {
"Warning - null logger in constructor; possible log4j misconfiguration."); "Warning - null logger in constructor; possible log4j misconfiguration.");
} }
this.name = logger.getName(); this.name = logger.getName();
this.logger=logger; this.logger = logger;
} }
/** /**

View File

@@ -167,9 +167,9 @@ public class SimpleLog implements Log, Serializable {
} }
} }
showLogName = getBooleanProperty( systemPrefix + "showlogname", showLogName); showLogName = getBooleanProperty(systemPrefix + "showlogname", showLogName);
showShortName = getBooleanProperty( systemPrefix + "showShortLogname", showShortName); showShortName = getBooleanProperty(systemPrefix + "showShortLogname", showShortName);
showDateTime = getBooleanProperty( systemPrefix + "showdatetime", showDateTime); showDateTime = getBooleanProperty(systemPrefix + "showdatetime", showDateTime);
if(showDateTime) { if(showDateTime) {
dateTimeFormat = getStringProperty(systemPrefix + "dateTimeFormat", dateTimeFormat = getStringProperty(systemPrefix + "dateTimeFormat",
@@ -201,7 +201,6 @@ public class SimpleLog implements Log, Serializable {
* @param name log name * @param name log name
*/ */
public SimpleLog(String name) { public SimpleLog(String name) {
logName = name; logName = name;
// Set initial log level // Set initial log level
@@ -239,20 +238,17 @@ public class SimpleLog implements Log, Serializable {
} else if("off".equalsIgnoreCase(lvl)) { } else if("off".equalsIgnoreCase(lvl)) {
setLevel(SimpleLog.LOG_LEVEL_OFF); setLevel(SimpleLog.LOG_LEVEL_OFF);
} }
} }
// -------------------------------------------------------- Properties // -------------------------------------------------------- Properties
/** /**
* <p> Set logging level. </p> * Set logging level.
* *
* @param currentLogLevel new logging level * @param currentLogLevel new logging level
*/ */
public void setLevel(int currentLogLevel) { public void setLevel(int currentLogLevel) {
this.currentLogLevel = currentLogLevel; this.currentLogLevel = currentLogLevel;
} }
/** /**
@@ -276,11 +272,11 @@ public class SimpleLog implements Log, Serializable {
*/ */
protected void log(int type, Object message, Throwable t) { protected void log(int type, Object message, Throwable t) {
// Use a string buffer for better performance // Use a string buffer for better performance
StringBuffer buf = new StringBuffer(); final StringBuffer buf = new StringBuffer();
// Append date-time if so configured // Append date-time if so configured
if(showDateTime) { if(showDateTime) {
Date now = new Date(); final Date now = new Date();
String dateText; String dateText;
synchronized(dateFormatter) { synchronized(dateFormatter) {
dateText = dateFormatter.format(now); dateText = dateFormatter.format(now);
@@ -300,10 +296,10 @@ public class SimpleLog implements Log, Serializable {
} }
// Append the name of the log instance if so configured // Append the name of the log instance if so configured
if( showShortName) { if(showShortName) {
if( shortLogName==null ) { if(shortLogName == null) {
// Cut all but the last component of the name for both styles // Cut all but the last component of the name for both styles
String slName = logName.substring(logName.lastIndexOf(".") + 1); final String slName = logName.substring(logName.lastIndexOf(".") + 1);
shortLogName = slName.substring(slName.lastIndexOf("/") + 1); shortLogName = slName.substring(slName.lastIndexOf("/") + 1);
} }
buf.append(String.valueOf(shortLogName)).append(" - "); buf.append(String.valueOf(shortLogName)).append(" - ");
@@ -320,8 +316,8 @@ public class SimpleLog implements Log, Serializable {
buf.append(t.toString()); buf.append(t.toString());
buf.append(">"); buf.append(">");
java.io.StringWriter sw= new java.io.StringWriter(1024); final java.io.StringWriter sw = new java.io.StringWriter(1024);
java.io.PrintWriter pw= new java.io.PrintWriter(sw); final java.io.PrintWriter pw = new java.io.PrintWriter(sw);
t.printStackTrace(pw); t.printStackTrace(pw);
pw.close(); pw.close();
buf.append(sw.toString()); buf.append(sw.toString());
@@ -332,9 +328,9 @@ public class SimpleLog implements Log, Serializable {
} }
/** /**
* <p>Write the content of the message accumulated in the specified * Write the content of the message accumulated in the specified
* <code>StringBuffer</code> to the appropriate output destination. The * <code>StringBuffer</code> to the appropriate output destination. The
* default implementation writes to <code>System.err</code>.</p> * default implementation writes to <code>System.err</code>.
* *
* @param buffer A <code>StringBuffer</code> containing the accumulated * @param buffer A <code>StringBuffer</code> containing the accumulated
* text to be logged * text to be logged
@@ -589,8 +585,7 @@ public class SimpleLog implements Log, Serializable {
try { try {
// Are we running on a JDK 1.2 or later system? // Are we running on a JDK 1.2 or later system?
Method method = Thread.class.getMethod("getContextClassLoader", final Method method = Thread.class.getMethod("getContextClassLoader", (Class[]) null);
(Class[]) null);
// Get the thread context class loader (if there is one) // Get the thread context class loader (if there is one)
try { try {