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/discovery/src/java/org/apache/commons/discovery/tools ManagedProperties.java
Date Fri, 16 Aug 2002 16:48:25 GMT
rsitze      2002/08/16 09:48:25

  Modified:    discovery/src/java/org/apache/commons/discovery/load
                        ClassLoaderUtils.java
               discovery/src/java/org/apache/commons/discovery/tools
                        ManagedProperties.java
  Log:
  Beginning to think about security... this will probably all change down the road, but here
tis.. for now.
  
  Revision  Changes    Path
  1.6       +114 -93   jakarta-commons/discovery/src/java/org/apache/commons/discovery/load/ClassLoaderUtils.java
  
  Index: ClassLoaderUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/load/ClassLoaderUtils.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ClassLoaderUtils.java	14 Aug 2002 21:46:21 -0000	1.5
  +++ ClassLoaderUtils.java	16 Aug 2002 16:48:25 -0000	1.6
  @@ -62,9 +62,13 @@
   package org.apache.commons.discovery.load;
   
   import java.io.InputStream;
  +
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
   
  +import java.security.AccessController;
  +import java.security.PrivilegedAction;
  +
   import org.apache.commons.discovery.DiscoveryException;
   
   
  @@ -265,20 +269,27 @@
        * as </code>thisClassLoader</code> or if <code>classLoader</code>
        * is an ancestor of </code>thisClassLoader</code>.
        */
  -    public static final boolean wouldUseClassLoader(ClassLoader thisClassLoader,
  -                                                    ClassLoader classLoader) {
  +    public static final boolean wouldUseClassLoader(final ClassLoader thisClassLoader,
  +                                                    final ClassLoader classLoader) {
           /* bootstrap classloader, at root of all trees! */
           if (classLoader == null)
               return true;
  -        
  -        while (thisClassLoader != null) {
  -            if (thisClassLoader == classLoader) {
  -                return true;
  -            }
  -            thisClassLoader = thisClassLoader.getParent();
  -        }
  -        
  -        return false;
  +                        
  +        return ((Boolean)AccessController.doPrivileged(
  +                new PrivilegedAction() {
  +                    public Object run() {
  +                        for(ClassLoader walker = thisClassLoader;
  +                            walker != null;
  +                            walker = walker.getParent())
  +                        {
  +                            if (walker == classLoader) {
  +                                return Boolean.TRUE;
  +                            }
  +                        }
  +                        
  +                        return Boolean.FALSE;
  +                    }
  +            })).booleanValue();
       }
   
       /**
  @@ -360,53 +371,58 @@
       public static ClassLoader getThreadContextClassLoader()
           throws DiscoveryException
       {
  -        ClassLoader classLoader = null;
  -        
  -        try {
  -            // Are we running on a JDK 1.2 or later system?
  -            Method method = Thread.class.getMethod("getContextClassLoader", null);
  -    
  -            // Get the thread context class loader (if there is one)
  -            try {
  -                classLoader = (ClassLoader)method.invoke(Thread.currentThread(), null);
  -            } catch (IllegalAccessException e) {
  -                throw new DiscoveryException("Unexpected IllegalAccessException", e);
  -            } catch (InvocationTargetException e) {
  -                /**
  -                 * InvocationTargetException is thrown by 'invoke' when
  -                 * the method being invoked (Thread.getContextClassLoader)
  -                 * throws an exception.
  -                 * 
  -                 * Thread.getContextClassLoader() throws SecurityException
  -                 * when the context class loader isn't an ancestor of the
  -                 * calling class's class loader, or if security permissions
  -                 * are restricted.
  -                 * 
  -                 * In the first case (the context class loader isn't an
  -                 * ancestor of the calling class's class loader), we want
  -                 * to ignore and keep going.  We cannot help but also ignore
  -                 * the second case (restricted security permissions) with
  -                 * the logic below, but other calls elsewhere (to obtain
  -                 * a class loader) will re-trigger this exception where
  -                 * we can make a distinction.
  -                 */
  -                if (e.getTargetException() instanceof SecurityException) {
  -                    classLoader = null;  // ignore
  -                } else {
  -                    // Capture 'e.getTargetException()' exception for details
  -                    // alternate: log 'e.getTargetException()', and pass back 'e'.
  -                    throw new DiscoveryException
  -                        ("Unexpected InvocationTargetException",
  -                         e.getTargetException());
  +        return (ClassLoader)
  +            AccessController.doPrivileged(new PrivilegedAction() {
  +                public Object run() {
  +                    ClassLoader classLoader = null;
  +                    
  +                    try {
  +                        // Are we running on a JDK 1.2 or later system?
  +                        Method method = Thread.class.getMethod("getContextClassLoader",
null);
  +                
  +                        // Get the thread context class loader (if there is one)
  +                        try {
  +                            classLoader = (ClassLoader)method.invoke(Thread.currentThread(),
null);
  +                        } catch (IllegalAccessException e) {
  +                            throw new DiscoveryException("Unexpected IllegalAccessException",
e);
  +                        } catch (InvocationTargetException e) {
  +                            /**
  +                             * InvocationTargetException is thrown by 'invoke' when
  +                             * the method being invoked (Thread.getContextClassLoader)
  +                             * throws an exception.
  +                             * 
  +                             * Thread.getContextClassLoader() throws SecurityException
  +                             * when the context class loader isn't an ancestor of the
  +                             * calling class's class loader, or if security permissions
  +                             * are restricted.
  +                             * 
  +                             * In the first case (the context class loader isn't an
  +                             * ancestor of the calling class's class loader), we want
  +                             * to ignore and keep going.  We cannot help but also ignore
  +                             * the second case (restricted security permissions) with
  +                             * the logic below, but other calls elsewhere (to obtain
  +                             * a class loader) will re-trigger this exception where
  +                             * we can make a distinction.
  +                             */
  +                            if (e.getTargetException() instanceof SecurityException) {
  +                                classLoader = null;  // ignore
  +                            } else {
  +                                // Capture 'e.getTargetException()' exception for details
  +                                // alternate: log 'e.getTargetException()', and pass back
'e'.
  +                                throw new DiscoveryException
  +                                    ("Unexpected InvocationTargetException",
  +                                     e.getTargetException());
  +                            }
  +                        }
  +                    } catch (NoSuchMethodException e) {
  +                        // Assume we are running on JDK 1.1
  +                        classLoader = null;
  +                    }
  +                    
  +                    // Return the selected class loader
  +                    return classLoader;
                   }
  -            }
  -        } catch (NoSuchMethodException e) {
  -            // Assume we are running on JDK 1.1
  -            classLoader = null;
  -        }
  -    
  -        // Return the selected class loader
  -        return classLoader;
  +            });
       }
       
       /**
  @@ -444,42 +460,47 @@
       private static ClassLoader findSystemClassLoader()
           throws DiscoveryException
       {
  -        ClassLoader classLoader = null;
  -        
  -        try {
  -            // Are we running on a JDK 1.2 or later system?
  -            Method method = ClassLoader.class.getMethod("getSystemClassLoader", null);
  -    
  -            // Get the system class loader (if there is one)
  -            try {
  -                classLoader = (ClassLoader)method.invoke(null, null);
  -            } catch (IllegalAccessException e) {
  -                throw new DiscoveryException("Unexpected IllegalAccessException", e);
  -            } catch (InvocationTargetException e) {
  -                /**
  -                 * InvocationTargetException is thrown by 'invoke' when
  -                 * the method being invoked (ClassLoader.getSystemClassLoader)
  -                 * throws an exception.
  -                 * 
  -                 * ClassLoader.getSystemClassLoader() throws SecurityException
  -                 * if security permissions are restricted.
  -                 */
  -                if (e.getTargetException() instanceof SecurityException) {
  -                    classLoader = null;  // ignore
  -                } else {
  -                    // Capture 'e.getTargetException()' exception for details
  -                    // alternate: log 'e.getTargetException()', and pass back 'e'.
  -                    throw new DiscoveryException
  -                        ("Unexpected InvocationTargetException",
  -                         e.getTargetException());
  +        return (ClassLoader)
  +            AccessController.doPrivileged(new PrivilegedAction() {
  +                public Object run() {
  +                    ClassLoader classLoader = null;
  +        
  +                    try {
  +                        // Are we running on a JDK 1.2 or later system?
  +                        Method method = ClassLoader.class.getMethod("getSystemClassLoader",
null);
  +                
  +                        // Get the system class loader (if there is one)
  +                        try {
  +                            classLoader = (ClassLoader)method.invoke(null, null);
  +                        } catch (IllegalAccessException e) {
  +                            throw new DiscoveryException("Unexpected IllegalAccessException",
e);
  +                        } catch (InvocationTargetException e) {
  +                            /**
  +                             * InvocationTargetException is thrown by 'invoke' when
  +                             * the method being invoked (ClassLoader.getSystemClassLoader)
  +                             * throws an exception.
  +                             * 
  +                             * ClassLoader.getSystemClassLoader() throws SecurityException
  +                             * if security permissions are restricted.
  +                             */
  +                            if (e.getTargetException() instanceof SecurityException) {
  +                                classLoader = null;  // ignore
  +                            } else {
  +                                // Capture 'e.getTargetException()' exception for details
  +                                // alternate: log 'e.getTargetException()', and pass back
'e'.
  +                                throw new DiscoveryException
  +                                    ("Unexpected InvocationTargetException",
  +                                     e.getTargetException());
  +                            }
  +                        }
  +                    } catch (NoSuchMethodException e) {
  +                        // Assume we are running on JDK 1.1
  +                        classLoader = new PsuedoSystemClassLoader();
  +                    }
  +                
  +                    // Return the selected class loader
  +                    return classLoader;
                   }
  -            }
  -        } catch (NoSuchMethodException e) {
  -            // Assume we are running on JDK 1.1
  -            classLoader = new PsuedoSystemClassLoader();
  -        }
  -    
  -        // Return the selected class loader
  -        return classLoader;
  +            });
       }
   }
  
  
  
  1.2       +18 -3     jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/ManagedProperties.java
  
  Index: ManagedProperties.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/discovery/src/java/org/apache/commons/discovery/tools/ManagedProperties.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ManagedProperties.java	14 Aug 2002 21:46:22 -0000	1.1
  +++ ManagedProperties.java	16 Aug 2002 16:48:25 -0000	1.2
  @@ -61,6 +61,12 @@
   import java.util.Map;
   import java.util.Properties;
   
  +import java.security.AccessController;
  +import java.security.PrivilegedAction;
  +
  +import org.apache.commons.discovery.load.ClassLoaderUtils;
  +
  +
   
   /**
    * <p>This class may disappear in the future, or be moved to another project..
  @@ -277,7 +283,8 @@
               }
   
               if (classLoader == null) break;
  -            classLoader = classLoader.getParent();
  +            
  +            classLoader = getParent(classLoader);
           }
           
           return allProps.keys();
  @@ -334,7 +341,7 @@
                * then get up-tree value.
                */
               if (classLoader != null) {
  -                value = getValueProperty(classLoader.getParent(), propertyName);
  +                value = getValueProperty(getParent(classLoader), propertyName);
               }
               
               if (value == null  ||  value.isDefault) {
  @@ -357,6 +364,14 @@
       }
       
       private static final ClassLoader getThreadContextClassLoader() {
  -        return Thread.currentThread().getContextClassLoader();
  +        return ClassLoaderUtils.getThreadContextClassLoader();
  +    }
  +
  +    private static final ClassLoader getParent(final ClassLoader classLoader) {
  +        return (ClassLoader)AccessController.doPrivileged(new PrivilegedAction() {
  +                    public Object run() {
  +                        return classLoader.getParent();
  +                    }
  +                });
       }
   }
  
  
  

--
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