FileDocCategorySizeDatePackage
ClassLoaderUtils.javaAPI DocGlassfish v2 API12298Tue May 22 22:17:14 BST 2007com.sun.enterprise.loader

ClassLoaderUtils

public class ClassLoaderUtils extends Object
Contains utility methods to create class loaders.
author
Nazrul Islam
since
JDK1.4

Fields Summary
private static Logger
_logger
logger for this class
private static final URL[]
EMPTY_URL_ARRAY
Constructors Summary
Methods Summary
public static java.lang.ClassLoadergetClassLoader(java.io.File[] dirs, java.io.File[] jarDirs, java.lang.ClassLoader parent)
Returns a new class loader based on the given directory paths and the jar files & zip files found under jar directory.

param
dirs array of directory path names
param
jarDirs array of path name to directories that contains JAR & ZIP files.
param
parent parent class loader for the new class loader
return
a new class loader based on the urls from the given params
throws
IOException if an i/o error while constructing the urls


                                                                                                                               
           
                

        URLClassLoader loader  = null;
        URL[] urls             = getUrls(dirs, jarDirs);

        if (urls != null) {
            if (parent != null) {
                loader = new URLClassLoader(urls, parent); 
            } else {
                loader = new URLClassLoader(urls);
            }
        }

        return loader;
    
public static java.util.ListgetUrlList(java.io.File[] dirs, java.io.File[] jarDirs)
Returns a list of urls that contains ..
i. all the valid directories from the given directory (dirs) array
ii. all jar files from the given directory (jarDirs) array
iii. all zip files from the given directory (jarDirs) array
This is similar to getUrls(File[], File[])

param
dirs array of directory path names
param
jarDirs array of path name to directories that contains JAR & ZIP files.
return
a list of urls that contains all the valid dirs, *.jar & *.zip; the obj representing the paths are of type java.lang.String. It returns an empty list if no valid dir, jar or zip present.
throws
IOException if an i/o error while constructing the urls
see
#getUrls(File[], File[]);

        return getUrlList(dirs, jarDirs, false); 
    
public static java.util.ListgetUrlList(java.io.File[] dirs, java.io.File[] jarDirs, boolean ignoreZip)
Returns a list of urls that contains ..
i. all the valid directories from the given directory (dirs)
array
ii. all jar files from the given directory (jarDirs) array
iii. all zip files from the given directory (jarDirs) array if
not ignoring zip file (ignoreZip is false).

param
dirs array of directory path names
param
jarDirs array of path name to directories that contains JAR & ZIP files.
return
a list of urls that contains all the valid dirs, *.jar & *.zip if not ignoring zip file, the obj representing the paths are of type java.lang.String. It returns an empty list if no valid dir, jar or zip present.
throws
IOException if an i/o error while constructing the urls


        List list   = new ArrayList();

        // adds all directories
        if (dirs != null) {
            for (int i=0; i<dirs.length; i++) {
                File dir = dirs[i];
                if (dir.isDirectory() || dir.canRead()) {
                    list.add( dir.getCanonicalPath() );

                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.log(Level.FINE, 
                            "Adding directory to class path:" 
                            + dir.getCanonicalPath());
                    }
                }
            }
        } 

        // adds all the jars
        if (jarDirs != null) {
            for (int i=0; i<jarDirs.length; i++) {
                File jarDir =  jarDirs[i];

                if (jarDir.isDirectory() || jarDir.canRead()) {
                    File[] files = jarDir.listFiles();

                    for (int j=0; j<files.length; j++) {
                        File jar = files[j];

                        if ( FileUtils.isJar(jar) || 
                            (!ignoreZip && FileUtils.isZip(jar)) ) {
                            list.add( jar.getCanonicalPath() );

                            if (_logger.isLoggable(Level.FINE)) {
                                _logger.log(Level.FINE, 
                                    "Adding jar to class path:" 
                                    + jar.getCanonicalPath());
                            }
                        }
                    }
                }
            }
        }

        return list;
    
public static java.net.URL[]getUrls(java.io.File[] dirs, java.io.File[] jarDirs)
Returns an array of urls that contains ..
i. all the valid directories from the given directory (dirs) array
ii. all jar files from the given directory (jarDirs) array
iii. all zip files from the given directory (jarDirs) array

param
dirs array of directory path names
param
jarDirs array of path name to directories that contains JAR & ZIP files.
return
an array of urls that contains all the valid dirs, *.jar & *.zip
throws
IOException if an i/o error while constructing the urls


        URL[] urls  = null;
        List list   = new ArrayList();

        // adds all directories
        if (dirs != null) {
            for (int i=0; i<dirs.length; i++) {
                File dir = dirs[i];
                if (dir.isDirectory() || dir.canRead()) {
                    URL url = dir.toURI().toURL();
                    list.add(url);

                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.log(Level.FINE, 
                            "Adding directory to class path:" + url.toString());
                    }
                }
            }
        } 

        // adds all the jars
        if (jarDirs != null) {
            for (int i=0; i<jarDirs.length; i++) {
                File jarDir =  jarDirs[i];

                if (jarDir.isDirectory() || jarDir.canRead()) {
                    File[] files = jarDir.listFiles();

                    for (int j=0; j<files.length; j++) {
                        File jar = files[j];

                        if ( FileUtils.isJar(jar) || FileUtils.isZip(jar) ) {
                            list.add(jar.toURI().toURL());

                            if (_logger.isLoggable(Level.FINE)) {
                                _logger.log(Level.FINE, 
                                    "Adding jar to class path:" + jar.toURL());
                            }
                        }
                    }
                }
            }
        }

        // converts the list to an array
        if (list.size() > 0) {
            urls = new URL[list.size()];
            urls = (URL[]) list.toArray(urls);
        }

        return urls;
    
public static java.net.URL[]getUrlsFromClasspath(java.lang.String classpath)
get URL[] from classpath catches exception for wrong files

    
                  
         
        final List<URL> urls  = new ArrayList<URL>();
        
        if (classpath == null) {
            return EMPTY_URL_ARRAY;
        }
        
        // tokenize classpath
        final StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
        while (st.hasMoreTokens()) {
            try {
                File f = new File(st.nextToken());
                urls.add(f.toURI().toURL());
            } catch(Exception e) {
                  _logger.log(Level.WARNING,
				  			"loader.unexpected_error_while_creating_urls",e);
            }
        }
         
        // converts the list to an array
        URL[] ret;
        if (urls.size() > 0) {
            ret = urls.toArray(new URL[]{});
        } else {
            ret = new URL[0];
        }

        return ret;
     
public static voidmain(java.lang.String[] args)
Unit test code.


        try {
            URL[] urls = getUrls(new File[] {new File(args[0])}, 
                     new File[] {new File(args[1])});
            for (int i=0; i<urls.length; i++) {
                System.out.println(urls[i]);
            }

            URLClassLoader loader = (URLClassLoader) 
                    getClassLoader(new File[] {new File(args[0])},
                        new File[] {new File(args[1])}, null);

            //Class c = Class.forName(args[2], true, loader);
            Class c = loader.loadClass(args[2]);
            System.out.println("Loaded: " + c.getName());
            System.out.println("Loaded class has the following methods...");
            java.lang.reflect.Method[] m = c.getDeclaredMethods();
            for (int i=0; i<m.length; i++) {
                System.out.println(m[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }