1
0

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.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@139056 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Burrell Donkin
2004-11-10 23:00:47 +00:00
parent 23e71e6d58
commit 7a03f06584
9 changed files with 746 additions and 5 deletions

View File

@@ -42,7 +42,7 @@ import java.util.Properties;
* @author Craig R. McClanahan
* @author Costin Manolache
* @author Richard A. Sitze
* @version $Revision: 1.27 $ $Date: 2004/06/06 21:15:12 $
* @version $Revision: 1.28 $ $Date: 2004/11/10 22:59:04 $
*/
public abstract class LogFactory {
@@ -79,6 +79,42 @@ public abstract class LogFactory {
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 @@ public abstract class LogFactory {
* 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

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.logging;
import java.util.Hashtable;
public class AltHashtable extends Hashtable {
public static Object lastKey;
public static Object lastValue;
public Object put(Object key, Object value) {
lastKey = key;
lastValue = value;
return super.put(key, value);
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.logging;
import junit.framework.*;
public class AltHashtableTest extends TestCase {
public AltHashtableTest(String testName) {
super(testName);
}
public void testType() {
assertTrue(LogFactory.factories instanceof AltHashtable);
}
public void testPutCalled() throws Exception {
AltHashtable.lastKey = null;
AltHashtable.lastValue = null;
ClassLoader classLoader = new ClassLoader() {};
Thread thread = new Thread(
new Runnable() {
public void run() {
LogFactory.getLog(AltHashtableTest.class);
}
}
);
thread.setContextClassLoader(classLoader);
thread.start();
thread.join();
assertEquals(classLoader, AltHashtable.lastKey);
assertNotNull(AltHashtable.lastValue);
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.logging;
import junit.framework.*;
import java.util.Hashtable;
/**
* Tests behaviour when the property is misconfigured.
*/
public class BadHashtablePropertyTest extends TestCase {
public BadHashtablePropertyTest(String testName) {
super(testName);
}
public void testType() {
assertTrue(LogFactory.factories instanceof Hashtable);
}
public void testPutCalled() throws Exception {
Log log = LogFactory.getLog(BadHashtablePropertyTest.class);
}
}