commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rsi...@apache.org
Subject cvs commit: jakarta-commons/logging/src/java/org/apache/commons/logging/impl LogFactoryImpl.java
Date Fri, 30 Aug 2002 03:23:34 GMT
rsitze      2002/08/29 20:23:34

  Modified:    logging/src/java/org/apache/commons/logging LogFactory.java
               logging/src/java/org/apache/commons/logging/impl
                        LogFactoryImpl.java
  Log:
  Correct 2 problems:
  1. getContextClassLoader is priviledged, protect for J2EE environs.
  2. getContextClassLoader can & does return null.  Code wasn't
      checking for this properly.
  
  Revision  Changes    Path
  1.12      +18 -13    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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- LogFactory.java	12 Aug 2002 21:01:07 -0000	1.11
  +++ LogFactory.java	30 Aug 2002 03:23:34 -0000	1.12
  @@ -62,16 +62,17 @@
   package org.apache.commons.logging;
   
   
  -import java.io.InputStream;
  -import java.io.IOException;
   import java.io.BufferedReader;
  +import java.io.IOException;
  +import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
  +import java.security.AccessController;
  +import java.security.PrivilegedAction;
   import java.util.Enumeration;
   import java.util.Hashtable;
   import java.util.Properties;
  -import java.lang.SecurityException;
   
   
   /**
  @@ -258,7 +259,13 @@
       public static LogFactory getFactory() throws LogConfigurationException {
   
           // Identify the class loader we will be using
  -        ClassLoader contextClassLoader = getContextClassLoader();
  +        ClassLoader contextClassLoader =
  +            (ClassLoader)AccessController.doPrivileged(
  +                new PrivilegedAction() {
  +                    public Object run() {
  +                        return getContextClassLoader();
  +                    }
  +                });
   
           // Return any previously registered factory for this class loader
           LogFactory factory = getCachedFactory(contextClassLoader);
  @@ -323,8 +330,9 @@
           // system property )
   
           try {
  -            InputStream stream =
  -                contextClassLoader.getResourceAsStream(FACTORY_PROPERTIES);
  +            InputStream stream = (contextClassLoader == null
  +                                 ? ClassLoader.getSystemResourceAsStream( FACTORY_PROPERTIES
)
  +                                 : contextClassLoader.getResourceAsStream( FACTORY_PROPERTIES
));
               if (stream != null) {
                   props = new Properties();
                   props.load(stream);
  @@ -497,9 +505,6 @@
           
           if (contextClassLoader != null)
               factory = (LogFactory) factories.get(contextClassLoader);
  -            
  -        if (factory==null)
  -            factory = (LogFactory) factories.get(LogFactory.class.getClassLoader());
           
           return factory;
       }
  
  
  
  1.14      +40 -19    jakarta-commons/logging/src/java/org/apache/commons/logging/impl/LogFactoryImpl.java
  
  Index: LogFactoryImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/impl/LogFactoryImpl.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- LogFactoryImpl.java	9 Aug 2002 18:47:34 -0000	1.13
  +++ LogFactoryImpl.java	30 Aug 2002 03:23:34 -0000	1.14
  @@ -64,13 +64,15 @@
   
   import java.lang.reflect.Constructor;
   import java.lang.reflect.Method;
  +import java.security.AccessController;
  +import java.security.PrivilegedAction;
   import java.util.Enumeration;
   import java.util.Hashtable;
   import java.util.Vector;
  +
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogConfigurationException;
   import org.apache.commons.logging.LogFactory;
  -import org.apache.commons.logging.LogSource;
   
   
   /**
  @@ -435,23 +437,42 @@
   
       }
   
  -    /** Load a class, try first the thread class loader, and
  -        if it fails use the loader that loaded this class
  -    */
  -    static Class loadClass( String name )
  +    /**
  +     * <p>** MUST KEEP THIS METHOD PRIVATE **
  +     * </p>
  +     * 
  +     * <p>This method uses <code>AccessController.doPrivileged()</code>.
  +     * </p>
  +     * 
  +     * Load a class, try first the thread class loader, and
  +     * if it fails use the loader that loaded this class.
  +     */  
  +    private static Class loadClass( final String name )
           throws ClassNotFoundException
       {
  -        ClassLoader threadCL = getContextClassLoader();
  -        
  -        if (threadCL != null) {
  -            try {
  -                return threadCL.loadClass(name);
  -            } catch( ClassNotFoundException ex ) {
  -                return Class.forName( name );
  -            }
  -        }
  +        Object result = AccessController.doPrivileged(
  +            new PrivilegedAction() {
  +                public Object run() {
  +                    ClassLoader threadCL = getContextClassLoader();
  +                    if (threadCL != null) {
  +                        try {
  +                            return threadCL.loadClass(name);
  +                        } catch( ClassNotFoundException ex ) {
  +                            // ignore
  +                        }
  +                    }
  +                    try {
  +                        return Class.forName( name );
  +                    } catch (ClassNotFoundException e) {
  +                        return e;
  +                    }
  +                }
  +            });
  +
  +        if (result instanceof Class)
  +            return (Class)result;
   
  -        return null;
  +        throw (ClassNotFoundException)result;
       }
   
       protected void guessConfig() {
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message