rdonkin     2004/11/10 14:59:04

  Modified:    logging  build.xml
               logging/optional/src/test/org/apache/commons/logging
                        TestAll.java
               logging/src/java/org/apache/commons/logging LogFactory.java
  Log:
  LogFactory's Hashtable implementation (used to store LogFactoryImpl by 
classloader) can now be subclassed. This will default to WeakHashtable when 
this is present on the classpath, Hashtable otherwise. The implementation class 
can be specified by a system property. Based on a contribution by Brian 
Stansberry.
  
  Revision  Changes    Path
  1.48      +24 -2     jakarta-commons/logging/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/logging/build.xml,v
  retrieving revision 1.47
  retrieving revision 1.48
  diff -u -r1.47 -r1.48
  --- build.xml 4 Nov 2004 22:59:02 -0000       1.47
  +++ build.xml 10 Nov 2004 22:59:04 -0000      1.48
  @@ -462,7 +462,7 @@
   
   
     <target name="test"
  -       depends="log4j12-warning, 
compile.tests,test.jdk14,test.log4j,test.simple,test.avalon,test.log4j12"
  +       depends="test.alt-hashtable, log4j12-warning, 
compile.tests,test.jdk14,test.log4j,test.simple,test.avalon,test.log4j12"
               if="test.entry"
      description="Run all unit test cases">
         <java    classname="${test.runner}" fork="yes"
  @@ -921,5 +921,27 @@
   
     </target>
   
  +
  +
  +  <target name="test.alt-hashtable" depends="compile.tests"
  +   description="Tests for hashtable substitution">
  +
  +    <echo message="Hashtable substitution Tests"/>
  +    <java classname="${test.runner}" fork="yes" 
failonerror="${test.failonerror}">
  +      <arg value="org.apache.commons.logging.AltHashtableTest"/>
  +      <classpath refid="test.classpath"/>
  +      <sysproperty key="org.apache.commons.logging.LogFactory.HashtableImpl"
  +                 value="org.apache.commons.logging.AltHashtable"/>
  +    </java>
  +    
  +    <echo message="Bad property test"/>
  +    <java classname="${test.runner}" fork="yes" 
failonerror="${test.failonerror}">
  +      <arg value="org.apache.commons.logging.BadHashtablePropertyTest"/>
  +      <classpath refid="test.classpath"/>
  +      <sysproperty key="org.apache.commons.logging.LogFactory.HashtableImpl"
  +                 value="org.apache.commons.logging.bad.BogusHashTable"/>
  +    </java>
  +    
  +  </target>
   
   </project>
  
  
  
  1.2       +4 -1      
jakarta-commons/logging/optional/src/test/org/apache/commons/logging/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/logging/optional/src/test/org/apache/commons/logging/TestAll.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestAll.java      4 Nov 2004 23:03:59 -0000       1.1
  +++ TestAll.java      10 Nov 2004 22:59:04 -0000      1.2
  @@ -19,6 +19,7 @@
   
   import junit.framework.*;
   import org.apache.commons.logging.impl.MemoryLogTest;
  +import org.apache.commons.logging.impl.WeakHashtableTest;
   
   /**
     * <p> The build script calls just one <code>TestSuite</code> - this one!
  @@ -38,6 +39,8 @@
           TestSuite suite = new TestSuite();
           
           suite.addTest(MemoryLogTest.suite());
  +        suite.addTestSuite(WeakHashtableTest.class);
  +        suite.addTestSuite(LogFactoryTest.class);
           
           return suite;
       }
  
  
  
  1.28      +59 -2     
jakarta-commons/logging/src/java/org/apache/commons/logging/LogFactory.java
  
  Index: LogFactory.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/LogFactory.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- LogFactory.java   6 Jun 2004 21:15:12 -0000       1.27
  +++ LogFactory.java   10 Nov 2004 22:59:04 -0000      1.28
  @@ -79,6 +79,42 @@
       protected static final String SERVICE_ID =
           "META-INF/services/org.apache.commons.logging.LogFactory";
   
  +    /**
  +     * <p>Setting this system property value allows the 
<code>Hashtable</code> used to store
  +     * classloaders to be substituted by an alternative implementation.
  +     * </p>
  +     * <p>
  +     * <strong>Note:</strong> <code>LogFactory</code> will print:
  +     * <code><pre>
  +     * [ERROR] LogFactory: Load of custom hashtable failed</em>
  +     * </code></pre>
  +     * to system error and then continue using a standard Hashtable.
  +     * </p>
  +     * <p>
  +     * <strong>Usage:</strong> Set this property when Java is invoked
  +     * and <code>LogFactory</code> will attempt to load a new instance 
  +     * of the given implementation class.
  +     * For example, running the following ant scriplet:
  +     * <code><pre>
  +     *  &lt;java classname="${test.runner}" fork="yes" 
failonerror="${test.failonerror}"&gt;
  +     *     ...
  +     *     &lt;sysproperty 
  +     *        key="org.apache.commons.logging.LogFactory.HashtableImpl"
  +     *        value="org.apache.commons.logging.AltHashtable"/&gt;
  +     *  &lt;/java&gt;
  +     * </pre></code>
  +     * will mean that <code>LogFactory</code> will load an instance of
  +     * <code>org.apache.commons.logging.AltHashtable</code>.
  +     * </p>
  +     * <p>
  +     * A typical use case is to allow a custom
  +     * Hashtable implementation using weak references to be substituted.
  +     * This will allow classloaders to be garbage collected without
  +     * the need to release them (on 1.3+ JVMs only, of course ;)
  +     * </p>
  +     */
  +    public static final String HASHTABLE_IMPLEMENTATION_PROPERTY =
  +        "org.apache.commons.logging.LogFactory.HashtableImpl";
   
       // ----------------------------------------------------------- 
Constructors
   
  @@ -181,7 +217,28 @@
        * The previously constructed <code>LogFactory</code> instances, keyed by
        * the <code>ClassLoader</code> with which it was created.
        */
  -    protected static Hashtable factories = new Hashtable();
  +    protected static Hashtable factories = createFactoryStore();
  +    
  +    private static final Hashtable createFactoryStore() {
  +        Hashtable result = null;
  +        String storeImplementationClass 
  +            = System.getProperty(HASHTABLE_IMPLEMENTATION_PROPERTY);
  +        if (storeImplementationClass == null) {
  +            storeImplementationClass = 
"org.apache.commons.logging.impl.WeakHashtable";
  +        }
  +        try {
  +            Class implementationClass = 
Class.forName(storeImplementationClass);
  +            result = (Hashtable) implementationClass.newInstance();
  +            
  +        } catch (Exception e) {
  +            // ignore
  +            System.err.println("[ERROR] LogFactory: Load of custom hashtable 
failed");
  +        }
  +        if (result == null) {
  +            result = new Hashtable();
  +        }
  +        return result;
  +    }
   
   
       // --------------------------------------------------------- Static 
Methods
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to