org.logicalcobwebs.logging.impl
Class LogFactoryImpl

java.lang.Object
  extended by org.logicalcobwebs.logging.LogFactory
      extended by org.logicalcobwebs.logging.impl.LogFactoryImpl

public class LogFactoryImpl
extends LogFactory

Concrete subclass of LogFactory that implements the following algorithm to dynamically select a logging implementation class to instantiate a wrapper for.

If the selected Log implementation class has a setLogFactory() method that accepts a LogFactory parameter, this method will be called on each newly created instance to identify the associated factory. This makes factory configuration attributes available to the Log instance, if it so desires.

This factory will remember previously created Log instances for the same name, and will return them on repeated requests to the getInstance() method. This implementation ignores any configured attributes.

Version:
$Revision: 1.5 $ $Date: 2003/03/11 00:02:09 $
Author:
Rod Waldhoff, Craig R. McClanahan

Field Summary
private  java.util.Hashtable attributes
          Configuration attributes
private  java.util.Hashtable instances
          The Log instances that have already been created, keyed by logger name.
static java.lang.String LOG_DEFAULT
          The fully qualified name of the default Log implementation.
static java.lang.String LOG_PROPERTY
          The name of the system property identifying our Log implementation class.
protected static java.lang.String LOG_PROPERTY_OLD
          The deprecated system property used for backwards compatibility with the old LogSource class.
private  java.lang.reflect.Constructor logConstructor
          The one-argument constructor of the Log implementation class that will be used to create new instances.
private  java.lang.Class[] logConstructorSignature
          The signature of the Constructor to be used.
private  java.lang.reflect.Method logMethod
          The one-argument setLogFactory method of the selected Log method, if it exists.
private  java.lang.Class[] logMethodSignature
          The signature of the setLogFactory method to be used.
private  LogFactory proxyFactory
           
 
Fields inherited from class org.logicalcobwebs.logging.LogFactory
FACTORY_DEFAULT, FACTORY_PROPERTIES, FACTORY_PROPERTY, SERVICE_ID
 
Constructor Summary
LogFactoryImpl()
          Public no-arguments constructor required by the lookup mechanism.
 
Method Summary
 java.lang.Object getAttribute(java.lang.String name)
          Return the configuration attribute with the specified name (if any), or null if there is no such attribute.
 java.lang.String[] getAttributeNames()
          Return an array containing the names of all currently defined configuration attributes.
 Log getInstance(java.lang.Class clazz)
          Convenience method to derive a name from the specified class and call getInstance(String) with it.
 Log getInstance(java.lang.String name)
          Construct (if necessary) and return a Log instance, using the factory's current set of configuration attributes.
protected  java.lang.reflect.Constructor getLogConstructor()
          Return the Constructor that can be called to instantiate new Log instances.
protected  void guessConfig()
           
protected  boolean isJdk14Available()
          Is JDK 1.4 or later logging available?
protected  boolean isLog4JAvailable()
          Is a Log4J implementation available?
private static java.lang.Class loadClass(java.lang.String name)
          MUST KEEP THIS METHOD PRIVATE
protected  Log newInstance(java.lang.String name)
          Create and return a new Log instance for the specified name.
 void release()
          Release any internal references to previously created Log instances returned by this factory.
 void removeAttribute(java.lang.String name)
          Remove any configuration attribute associated with the specified name.
 void setAttribute(java.lang.String name, java.lang.Object value)
          Set the configuration attribute with the specified name.
 
Methods inherited from class org.logicalcobwebs.logging.LogFactory
getContextClassLoader, getFactory, getLog, getLog, newFactory, releaseAll
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG_DEFAULT

public static final java.lang.String LOG_DEFAULT
The fully qualified name of the default Log implementation.

See Also:
Constant Field Values

LOG_PROPERTY

public static final java.lang.String LOG_PROPERTY
The name of the system property identifying our Log implementation class.

See Also:
Constant Field Values

LOG_PROPERTY_OLD

protected static final java.lang.String LOG_PROPERTY_OLD
The deprecated system property used for backwards compatibility with the old LogSource class.

See Also:
Constant Field Values

attributes

private java.util.Hashtable attributes
Configuration attributes


instances

private java.util.Hashtable instances
The Log instances that have already been created, keyed by logger name.


logConstructor

private java.lang.reflect.Constructor logConstructor
The one-argument constructor of the Log implementation class that will be used to create new instances. This value is initialized by getLogConstructor(), and then returned repeatedly.


proxyFactory

private LogFactory proxyFactory

logConstructorSignature

private java.lang.Class[] logConstructorSignature
The signature of the Constructor to be used.


logMethod

private java.lang.reflect.Method logMethod
The one-argument setLogFactory method of the selected Log method, if it exists.


logMethodSignature

private java.lang.Class[] logMethodSignature
The signature of the setLogFactory method to be used.

Constructor Detail

LogFactoryImpl

public LogFactoryImpl()
Public no-arguments constructor required by the lookup mechanism.

Method Detail

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Return the configuration attribute with the specified name (if any), or null if there is no such attribute.

Specified by:
getAttribute in class LogFactory
Parameters:
name - Name of the attribute to return

getAttributeNames

public java.lang.String[] getAttributeNames()
Return an array containing the names of all currently defined configuration attributes. If there are no such attributes, a zero length array is returned.

Specified by:
getAttributeNames in class LogFactory

getInstance

public Log getInstance(java.lang.Class clazz)
                throws LogConfigurationException
Convenience method to derive a name from the specified class and call getInstance(String) with it.

Specified by:
getInstance in class LogFactory
Parameters:
clazz - Class for which a suitable Log name will be derived
Throws:
LogConfigurationException - if a suitable Log instance cannot be returned

getInstance

public Log getInstance(java.lang.String name)
                throws LogConfigurationException

Construct (if necessary) and return a Log instance, using the factory's current set of configuration attributes.

NOTE - Depending upon the implementation of the LogFactory you are using, the Log instance you are returned may or may not be local to the current application, and may or may not be returned again on a subsequent call with the same name argument.

Specified by:
getInstance in class LogFactory
Parameters:
name - Logical name of the Log instance to be returned (the meaning of this name is only known to the underlying logging implementation that is being wrapped)
Throws:
LogConfigurationException - if a suitable Log instance cannot be returned

release

public void release()
Release any internal references to previously created Log instances returned by this factory. This is useful environments like servlet containers, which implement application reloading by throwing away a ClassLoader. Dangling references to objects in that class loader would prevent garbage collection.

Specified by:
release in class LogFactory

removeAttribute

public void removeAttribute(java.lang.String name)
Remove any configuration attribute associated with the specified name. If there is no such attribute, no action is taken.

Specified by:
removeAttribute in class LogFactory
Parameters:
name - Name of the attribute to remove

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object value)
Set the configuration attribute with the specified name. Calling this with a null value is equivalent to calling removeAttribute(name).

Specified by:
setAttribute in class LogFactory
Parameters:
name - Name of the attribute to set
value - Value of the attribute to set, or null to remove any setting for this attribute

getLogConstructor

protected java.lang.reflect.Constructor getLogConstructor()
                                                   throws LogConfigurationException

Return the Constructor that can be called to instantiate new Log instances.

IMPLEMENTATION NOTE - Race conditions caused by calling this method from more than one thread are ignored, because the same Constructor instance will ultimately be derived in all circumstances.

Throws:
LogConfigurationException - if a suitable constructor cannot be returned

loadClass

private static java.lang.Class loadClass(java.lang.String name)
                                  throws java.lang.ClassNotFoundException
MUST KEEP THIS METHOD PRIVATE

Exposing this method establishes a security violation. This method uses AccessController.doPrivileged().

Load a class, try first the thread class loader, and if it fails use the loader that loaded this class.

Throws:
java.lang.ClassNotFoundException

guessConfig

protected void guessConfig()

isJdk14Available

protected boolean isJdk14Available()
Is JDK 1.4 or later logging available?


isLog4JAvailable

protected boolean isLog4JAvailable()
Is a Log4J implementation available?


newInstance

protected Log newInstance(java.lang.String name)
                   throws LogConfigurationException
Create and return a new Log instance for the specified name.

Parameters:
name - Name of the new logger
Throws:
LogConfigurationException - if a new instance cannot be created