JiveModuleLoader.java 4.31 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1
/**
Matt Tucker's avatar
Matt Tucker committed
2 3 4 5
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
package org.jivesoftware.messenger.container.spi;

import org.jivesoftware.messenger.container.Module;
import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>This class extends the current classpath with plug-in jars and classes
 * and makes it simple to load up modules from that extended classpath.</p>
 * <p>This tool will add any jars in the <tt>lib</tt> directory
 * (if it exists), and the <tt>classes</tt> directory (if it exists) to
 * the classpath.</p>
 * <p>JiveModuleLoader also sets the ccontext classloader for loaded
 * modules to a classloader with extended classpath and sets the parent
 * classloader to the 'best' available according to the following ranked
 * choices:</p>
 * <ol>
 * <li>The current thread's current context class loader</li>
 * <li>The class loader for this class</li>
 * <li>The system class loader</li>
 * </ol>
 *
 * @author Iain Shigeoka
 * @author Derek DeMoro
 */
public class JiveModuleLoader {

    /**
     * The class loader used by this pseudo loader *
     */
    private URLClassLoader classLoader;

    /**
     * Create a classloader for the given root directory.
     * We locate all the relevant URLs then create a standard URLClassLoader.
     *
     * @param dir The directory to search for other classes
     * @throws java.lang.SecurityException    If the created class loader violates existing security constraints
     * @throws java.net.MalformedURLException If a located resource name doesn't properly convert to a URL
     */
    public JiveModuleLoader(String dir) throws MalformedURLException, SecurityException {
        final List list = new ArrayList();
        File classes = new File(dir + File.separator + "classes" + File.separator);
        if (classes.exists()) {
            list.add(classes.toURL());
        }
        File lib = new File(dir + File.separator + "lib" + File.separator);
        File[] jars = lib.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".jar") || name.endsWith(".zip");
            }
        });
        if (jars != null) {
            for (int i = 0; i < jars.length; i++) {
                if (jars[i] != null && jars[i].isFile()) {
                    list.add(jars[i].toURL());
                }
            }
        }
        Iterator urls = list.iterator();
        URL[] urlArray = new URL[list.size()];
        for (int i = 0; urls.hasNext(); i++) {
            urlArray[i] = (URL)urls.next();
        }
        classLoader = new URLClassLoader(urlArray, findParentClassLoader());
        Thread.currentThread().setContextClassLoader(classLoader);
    }

    /**
     * Load a class using this class loader.
     *
     * @param className The fully qualified name of the class to load
     * @return The module object loaded
91 92 93 94
     * @throws ClassNotFoundException if the class could not be loaded by this class loader.
     * @throws IllegalAccessException if the class constructor was private or protected.
     * @throws InstantiationException if the class could not be instantiated (initialization error).
     * @throws SecurityException if the custom class loader not allowed.
Matt Tucker's avatar
Matt Tucker committed
95 96
     */
    public Module loadModule(String className) throws ClassNotFoundException, IllegalAccessException,
97
            InstantiationException, SecurityException
Matt Tucker's avatar
Matt Tucker committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    {
        Class moduleClass = classLoader.loadClass(className);
        Module mod = (Module)moduleClass.newInstance();
        return mod;
    }

    /**
     * Locates the best class loader based on context (see class description).
     *
     * @return The best parent classloader to use
     */
    private ClassLoader findParentClassLoader() {
        ClassLoader parent = Thread.currentThread().getContextClassLoader();
        if (parent == null) {
            parent = this.getClass().getClassLoader();
        }
        if (parent == null) {
            parent = ClassLoader.getSystemClassLoader();
        }
        return parent;
    }
119
}