FileDocCategorySizeDatePackage
ClassUtils.javaAPI DocApache Axis 1.411545Sat Apr 22 18:57:26 BST 2006org.apache.axis.utils

ClassUtils

public final class ClassUtils extends Object
Utility methods for Class Loading.
author
Davanum Srinvas (dims@yahoo.com)
author
Matthew Pocock (matthew_pocock@yahoo.co.uk)

Fields Summary
private static ClassLoader
defaultClassLoader
default class loader
private static Hashtable
classloaders
hash table of class loaders
Constructors Summary
Methods Summary
public static java.lang.ClassLoadercreateClassLoader(java.lang.String classpath, java.lang.ClassLoader parent)
Creates a new ClassLoader from a classpath specification and a parent class loader. The classpath string will be split using the system path seperator character (e.g. : or ;), just as the java system-wide class path is processed.

param
classpath the classpath String
param
parent the parent ClassLoader, or null if the default is to be used
throws
SecurityException if you don't have privilages to create class loaders
throws
IllegalArgumentException if your classpath string is silly

        String[] names = StringUtils.split(classpath, System.getProperty("path.separator").charAt(0));

        URL[] urls = new URL[names.length];
        try {
            for(int i = 0; i < urls.length; i++)
                urls[i] = new File(names[i]).toURL();
        }
        catch (MalformedURLException e) {
          // I don't think this is possible, so I'm throwing this as an
          // un-checked exception
          throw (IllegalArgumentException) new IllegalArgumentException(
                  "Unable to parse classpath: " + classpath);
        }

        return new URLClassLoader(urls, parent);
    
public static java.lang.ClassforName(java.lang.String className)
Use this method instead of Class.forName

param
className Class name
return
java class
throws
ClassNotFoundException if the class is not found

        return loadClass(className);
    
public static java.lang.ClassforName(java.lang.String _className, boolean init, java.lang.ClassLoader _loader)
Use this method instead of Class.forName (String className, boolean init, ClassLoader loader)

param
_className Class name
param
init initialize the class
param
_loader class loader
return
java class
throws
ClassNotFoundException if the class is not found

        
        // Create final vars for doPrivileged block
        final String className = _className;
        final ClassLoader loader = _loader;
        try {
            // Get the class within a doPrivleged block
            Object ret = 
                AccessController.doPrivileged(
                    new PrivilegedAction() {
                        public Object run() {
                            try {
                                return Class.forName(className, true, loader);
                            } catch (Throwable e) {
                                return e;
                            }
                        }
                    });
            // If the class was located, return it.  Otherwise throw exception
            if (ret instanceof Class) {
                return (Class) ret;
            } else if (ret instanceof ClassNotFoundException) {
                throw (ClassNotFoundException) ret;
            } else {
                throw new ClassNotFoundException(_className);
            }
        } catch (ClassNotFoundException cnfe) {
            return loadClass(className);
        }
    
public static java.lang.ClassLoadergetClassLoader(java.lang.String className)
Obtain the ClassLoader (if any) associated with the given className.

param
className the name of a class
return
class loader

        if (className == null) {
            return null;
        }
        return (ClassLoader) classloaders.get(className);
    
public static java.lang.ClassLoadergetDefaultClassLoader()

        return defaultClassLoader;
    
public static java.io.InputStreamgetResourceAsStream(java.lang.Class clazz, java.lang.String resource)
Get an input stream from a named resource. Tries
  1. the classloader that loaded "clazz" first,
  2. the system classloader
  3. the class "clazz" itself

param
clazz class to use in the lookups
param
resource resource string to look for
return
input stream if found, or null

        InputStream myInputStream = null;

        if(clazz.getClassLoader()!=null) {
            // Try the class loader that loaded this class.
            myInputStream = clazz.getClassLoader().getResourceAsStream(resource);
        } else {
            // Try the system class loader.
            myInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(resource);
        }
        if (myInputStream == null && Thread.currentThread().getContextClassLoader() != null) {
            // try the context class loader.
            myInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
        }
        if (myInputStream == null) {
            // if not found in classpath fall back to default
            myInputStream = clazz.getResourceAsStream(resource);
        }
        return myInputStream;
    
public static java.io.InputStreamgetResourceAsStream(java.lang.Class clazz, java.lang.String resource, boolean checkThreadContextFirst)
Get an input stream from a named resource. Tries
  1. the classloader that loaded "clazz" first,
  2. the system classloader
  3. the class "clazz" itself

param
clazz class to use in the lookups
param
resource resource string to look for
param
checkThreadContextFirst check the thread context first?
return
input stream if found, or null

        InputStream myInputStream = null;

        if (checkThreadContextFirst &&
                Thread.currentThread().getContextClassLoader() != null) {
            // try the context class loader.
            myInputStream =
                    Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream(resource);
        }
        if (myInputStream == null) {
            // if not found in context class loader fall back to default
            myInputStream = getResourceAsStream(clazz, resource);
        }
        return myInputStream;
    
private static java.lang.ClassloadClass(java.lang.String _className)
Loads the class from the context class loader and then falls back to getDefaultClassLoader().forName

param
_className Class name
return
java class
throws
ClassNotFoundException if the class is not found

        // Create final vars for doPrivileged block
        final String className = _className;

        // Get the class within a doPrivleged block
        Object ret = 
            AccessController.doPrivileged(
                    new PrivilegedAction() {
                        public Object run() {
                            try {
                                // Check if the class is a registered class then
                                // use the classloader for that class.
                                ClassLoader classLoader = getClassLoader(className);
                                return Class.forName(className, true, classLoader);
                            } catch (ClassNotFoundException cnfe) {
                            } catch (SecurityException cnfe) {
                            }
                            
                            try {
                                // Try the context class loader
                                ClassLoader classLoader =
                                    Thread.currentThread().getContextClassLoader();
                                return Class.forName(className, true, classLoader);
                            } catch (ClassNotFoundException cnfe2) {
                                try {
                                    // Try the classloader that loaded this class.
                                    ClassLoader classLoader =
                                        ClassUtils.class.getClassLoader();
                                    return Class.forName(className, true, classLoader);
                                } catch (ClassNotFoundException cnfe3) {
                                    // Try the default class loader.
                                    try {
                                        return defaultClassLoader.loadClass(
                                                className);
                                    } catch (Throwable e) {
                                        // Still not found, return exception
                                        return e;
                                    }
                                }
                            } 
                        }
                    });

        // If the class was located, return it.  Otherwise throw exception
        if (ret instanceof Class) {
            return (Class) ret;
        } else if (ret instanceof ClassNotFoundException) {
            throw (ClassNotFoundException) ret;
        } else {
            throw new ClassNotFoundException(_className);
        }
    
public static voidremoveClassLoader(java.lang.String className)
Deregister the ClassLoader for a given className.

param
className the name of a class

        classloaders.remove(className);
    
public static voidsetClassLoader(java.lang.String className, java.lang.ClassLoader loader)
Set the ClassLoader associated with the given className. If either the class name or the loader are null, no action is performed.

param
className the name of a class
param
loader the ClassLoader for the class

        if (className != null && loader != null)
            classloaders.put(className, loader);
    
public static voidsetDefaultClassLoader(java.lang.ClassLoader loader)
Set the default ClassLoader. If loader is null, the default loader is not changed.

param
loader the new default ClassLoader


                              
         
      if (loader != null)
          defaultClassLoader = loader;