1
0

Add the standard ( ? ) JDK1.3 'service provider' mechanism. It's usually better

to use the standards ( if it makes sense ), and that's something that
works well enough ( crimson, xerces, xalan, saxon, etc).

Changed the code that deals with 'properties'-based setup. The propertis
will be set on the factory regardless of the discovery mechanism.
This may be arguable, but at least it's symetrical and permits the
application to pass the information to the logger implementation in
all cases. Given that the properties are read by the class loader,
each application can have it's own settings  (even if
the admin sets a JVM-wide default by using a system property ).


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138862 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Costin Manolache
2002-02-14 21:09:19 +00:00
parent 2ffc19f7df
commit 6f50fad399

View File

@@ -1,7 +1,7 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/LogFactory.java,v 1.3 2002/02/14 03:48:44 craigmcc Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//logging/src/java/org/apache/commons/logging/LogFactory.java,v 1.4 2002/02/14 21:09:19 costin Exp $
* $Revision: 1.3 $ * $Revision: 1.4 $
* $Date: 2002/02/14 03:48:44 $ * $Date: 2002/02/14 21:09:19 $
* *
* ==================================================================== * ====================================================================
* *
@@ -64,6 +64,8 @@ package org.apache.commons.logging;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Enumeration; import java.util.Enumeration;
@@ -82,7 +84,7 @@ import java.util.Properties;
* *
* @author Craig R. McClanahan * @author Craig R. McClanahan
* @author Costin Manolache * @author Costin Manolache
* @version $Revision: 1.3 $ $Date: 2002/02/14 03:48:44 $ * @version $Revision: 1.4 $ $Date: 2002/02/14 21:09:19 $
*/ */
public abstract class LogFactory { public abstract class LogFactory {
@@ -105,6 +107,13 @@ public abstract class LogFactory {
protected static final String FACTORY_PROPERTIES = protected static final String FACTORY_PROPERTIES =
"commons-logging.properties"; "commons-logging.properties";
/**
* JDK1.3+ 'Service Provider' specification
* ( http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html )
*/
protected static final String SERVICE_ID =
"META-INF/services/org.apache.commons.logging.LogFactory";
/** /**
* The name of the property used to identify the LogFactory implementation * The name of the property used to identify the LogFactory implementation
@@ -266,20 +275,82 @@ public abstract class LogFactory {
; ;
} }
// Second, try a properties file // Second, try to find a service by using the JDK1.3 jar
if (factory == null) { // discovery mechanism. This will allow users to plug a logger
// by just placing it in the lib/ directory of the webapp ( or in
// CLASSPATH or equivalent ). This is similar with the second
// step, except that it uses the (standard?) jdk1.3 location in the jar.
if( factory==null ) {
try {
InputStream is=null;
if (classLoader == null) {
is=ClassLoader.getSystemResourceAsStream( SERVICE_ID );
} else {
is=classLoader.getResourceAsStream( SERVICE_ID );
}
if( is != null ) {
// This code is needed by EBCDIC and other strange systems.
// It's a fix for bugs reported in xerces
BufferedReader rd;
try {
rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
} catch (java.io.UnsupportedEncodingException e) {
rd = new BufferedReader(new InputStreamReader(is));
}
String factoryClassName = rd.readLine();
rd.close();
if (factoryClassName != null &&
! "".equals(factoryClassName)) {
factory= newFactory( factoryClassName, classLoader );
}
}
} catch( Exception ex ) {
;
}
}
Properties props=null;
// Third try a properties file.
// If the properties file exists, it'll be read and the properties
// used. IMHO ( costin ) System property and JDK1.3 jar service
// should be enough for detecting the class name. The properties
// should be used to set the attributes ( which may be specific to
// the webapp, even if a default logger is set at JVM level by a
// system property )
try { try {
InputStream stream = InputStream stream =
classLoader.getResourceAsStream(FACTORY_PROPERTIES); classLoader.getResourceAsStream(FACTORY_PROPERTIES);
if (stream != null) { if (stream != null) {
Properties props = new Properties(); props = new Properties();
props.load(stream); props.load(stream);
stream.close(); stream.close();
String factoryClass = props.getProperty(FACTORY_PROPERTY); String factoryClass = props.getProperty(FACTORY_PROPERTY);
if( factory==null ) {
if (factoryClass == null) { if (factoryClass == null) {
factoryClass = FACTORY_DEFAULT; factoryClass = FACTORY_DEFAULT;
} }
factory = newFactory(factoryClass, classLoader); factory = newFactory(factoryClass, classLoader);
}
}
// the properties will be set at the end.
} catch (IOException e) {
} catch (SecurityException e) {
}
// Fourth, try the fallback implementation class
if (factory == null) {
factory = newFactory(FACTORY_DEFAULT, classLoader);
}
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();
@@ -287,15 +358,6 @@ public abstract class LogFactory {
factory.setAttribute(name, value); factory.setAttribute(name, value);
} }
} }
} catch (IOException e) {
} catch (SecurityException e) {
}
}
// Third, try the fallback implementation class
if (factory == null) {
factory = newFactory(FACTORY_DEFAULT, classLoader);
}
// Cache and return the new factory instance // Cache and return the new factory instance
factories.put(classLoader, factory); factories.put(classLoader, factory);