JiveClassLoader.java 1.99 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 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
package org.jivesoftware.messenger.starter;
Matt Tucker's avatar
Matt Tucker committed
13 14 15 16 17 18 19 20

import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
Matt Tucker's avatar
Matt Tucker committed
21 22 23 24
 * A simple classloader to extend the classpath to
 * include all jars in a lib directory.<p>
 *
 * The new classpath includes all <tt>*.jar</tt> and <tt>*.zip</tt>
Matt Tucker's avatar
Matt Tucker committed
25 26
 * archives (zip is commonly used in packaging JDBC drivers). The extended
 * classpath is used for both the initial server startup, as well as loading
Matt Tucker's avatar
Matt Tucker committed
27
 * plug-in support jars.
Matt Tucker's avatar
Matt Tucker committed
28 29 30 31 32
 *
 * @author Derek DeMoro
 * @author Iain Shigeoka
 */
class JiveClassLoader extends URLClassLoader {
Matt Tucker's avatar
Matt Tucker committed
33

Matt Tucker's avatar
Matt Tucker committed
34
    /**
Matt Tucker's avatar
Matt Tucker committed
35
     * Constructs the classloader.
Matt Tucker's avatar
Matt Tucker committed
36
     *
Matt Tucker's avatar
Matt Tucker committed
37 38 39
     * @param parent the parent class loader (or null for none).
     * @param libDir the directory to load jar files from.
     * @throws java.net.MalformedURLException if the libDir path is not valid.
Matt Tucker's avatar
Matt Tucker committed
40 41
     */
    JiveClassLoader(ClassLoader parent, File libDir) throws MalformedURLException {
Matt Tucker's avatar
Matt Tucker committed
42
        super(new URL[] { libDir.toURL() }, parent);
Matt Tucker's avatar
Matt Tucker committed
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

        File[] jars = libDir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                boolean accept = false;
                String smallName = name.toLowerCase();
                if (smallName.endsWith(".jar")) {
                    accept = true;
                }
                else if (smallName.endsWith(".zip")) {
                    accept = true;
                }
                return accept;
            }
        });

        // Do nothing if no jar or zip files were found
        if (jars == null) {
            return;
        }

        for (int i = 0; i < jars.length; i++) {
            if (jars[i].isFile()) {
                addURL(jars[i].toURL());
            }
        }
    }
}