costin      02/02/14 13:09:19

  Modified:    logging/src/java/org/apache/commons/logging LogFactory.java
  Log:
  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 ).
  
  Revision  Changes    Path
  1.4       +85 -23    
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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- LogFactory.java   14 Feb 2002 03:48:44 -0000      1.3
  +++ LogFactory.java   14 Feb 2002 21:09:19 -0000      1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/LogFactory.java,v
 1.3 2002/02/14 03:48:44 craigmcc Exp $
  - * $Revision: 1.3 $
  - * $Date: 2002/02/14 03:48:44 $
  + * $Header: 
/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.4 $
  + * $Date: 2002/02/14 21:09:19 $
    *
    * ====================================================================
    *
  @@ -64,6 +64,8 @@
   
   import java.io.InputStream;
   import java.io.IOException;
  +import java.io.BufferedReader;
  +import java.io.InputStreamReader;
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
   import java.util.Enumeration;
  @@ -82,7 +84,7 @@
    *
    * @author Craig R. McClanahan
    * @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 {
  @@ -105,6 +107,13 @@
       protected static final String FACTORY_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
  @@ -255,7 +264,7 @@
           if (factory != null) {
               return (factory);
           }
  -
  +        
           // First, try the system property
           try {
               String factoryClass = System.getProperty(FACTORY_PROPERTY);
  @@ -266,37 +275,90 @@
               ;
           }
   
  -        // Second, try a properties file
  -        if (factory == null) {
  +        // Second, try to find a service by using the JDK1.3 jar
  +        // 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 stream =
  -                    classLoader.getResourceAsStream(FACTORY_PROPERTIES);
  -                if (stream != null) {
  -                    Properties props = new Properties();
  -                    props.load(stream);
  -                    stream.close();
  -                    String factoryClass = props.getProperty(FACTORY_PROPERTY);
  +                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 {
  +            InputStream stream =
  +                classLoader.getResourceAsStream(FACTORY_PROPERTIES);
  +            if (stream != null) {
  +                props = new Properties();
  +                props.load(stream);
  +                stream.close();
  +                String factoryClass = props.getProperty(FACTORY_PROPERTY);
  +                if( factory==null ) {
                       if (factoryClass == null) {
                           factoryClass = FACTORY_DEFAULT;
                       }
                       factory = newFactory(factoryClass, classLoader);
  -                    Enumeration names = props.propertyNames();
  -                    while (names.hasMoreElements()) {
  -                        String name = (String) names.nextElement();
  -                        String value = props.getProperty(name);
  -                        factory.setAttribute(name, value);
  -                    }
                   }
  -            } catch (IOException e) {
  -            } catch (SecurityException e) {
               }
  +            // the properties will be set at the end.
  +        } catch (IOException e) {
  +        } catch (SecurityException e) {
           }
   
  -        // Third, try the fallback implementation class
  +        // Fourth, try the fallback implementation class
           if (factory == null) {
               factory = newFactory(FACTORY_DEFAULT, classLoader);
           }
   
  +        if( props!=null ) {
  +            Enumeration names = props.propertyNames();
  +            while (names.hasMoreElements()) {
  +                String name = (String) names.nextElement();
  +                String value = props.getProperty(name);
  +                factory.setAttribute(name, value);
  +            }
  +        }
  +        
           // Cache and return the new factory instance
           factories.put(classLoader, factory);
           return (factory);
  
  
  

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

Reply via email to