PluginClassLoader.java 4.52 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: 2993 $
 * $Date: 2005-10-24 18:11:33 -0300 (Mon, 24 Oct 2005) $
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
19 20
 */

21
package org.jivesoftware.openfire.container;
22 23 24 25 26 27 28

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

29 30 31 32
import org.jivesoftware.openfire.XMPPServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

33 34 35 36 37 38 39 40 41 42 43
/**
 * ClassLoader for plugins. It searches the plugin directory for classes
 * and JAR files, then constructs a class loader for the resources found.
 * Resources are loaded as follows:<ul>
 * <p/>
 * <li>Any JAR files in the <tt>lib</tt> will be added to the classpath.
 * <li>Any files in the classes directory will be added to the classpath.
 * </ul>
 *
 * @author Derek DeMoro
 */
44
public class PluginClassLoader extends URLClassLoader {
45

46 47
	private static final Logger Log = LoggerFactory.getLogger(PluginClassLoader.class);

48 49
    public PluginClassLoader() {
        super(new URL[] {}, findParentClassLoader());
50 51 52
    }

    /**
53
     * Adds a directory to the class loader.
54 55
     *
     * @param directory the directory.
56 57
     * @param developmentMode true if the plugin is running in development mode. This
     *      resolves classloader conflicts between the deployed plugin
58
     * and development classes.
59
     */
60
    public void addDirectory(File directory, boolean developmentMode) {
61
        try {
62
            // Add classes directory to classpath.
63 64
            File classesDir = new File(directory, "classes");
            if (classesDir.exists()) {
65
                addURL(classesDir.toURL());
66
            }
67

68 69 70
            // Add i18n directory to classpath.
            File databaseDir = new File(directory, "database");
            if(databaseDir.exists()){
71
                addURL(databaseDir.toURL());
72 73
            }

74 75 76
            // Add i18n directory to classpath.
            File i18nDir = new File(directory, "i18n");
            if(i18nDir.exists()){
77
                addURL(i18nDir.toURL());
78 79
            }

80 81 82
            // Add web directory to classpath.
            File webDir = new File(directory, "web");
            if(webDir.exists()){
83
                addURL(webDir.toURL());
84 85
            }

86
            // Add lib directory to classpath.
87 88 89 90 91 92 93 94 95
            File libDir = new File(directory, "lib");
            File[] jars = libDir.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()) {
96 97 98
                        if (developmentMode) {
                            // Do not add plugin-pluginName.jar to classpath.
                            if (!jars[i].getName().equals("plugin-" + directory.getName() + ".jar")) {
99
                                addURL(jars[i].toURL());
100 101 102
                            }
                        }
                        else {
103
                            addURL(jars[i].toURL());
104
                        }
105 106 107 108 109
                    }
                }
            }
        }
        catch (MalformedURLException mue) {
110
            Log.error(mue.getMessage(), mue);
111 112 113
        }
    }

114 115
    public void addURLFile(URL file) {
        addURL(file);
116 117 118 119 120 121 122
    }

    /**
     * Locates the best parent class loader based on context.
     *
     * @return the best parent classloader to use.
     */
123
    private static ClassLoader findParentClassLoader() {
124
        ClassLoader parent = XMPPServer.class.getClassLoader();
125
        if (parent == null) {
126
            parent = PluginClassLoader.class.getClassLoader();
127 128 129 130 131 132 133
        }
        if (parent == null) {
            parent = ClassLoader.getSystemClassLoader();
        }
        return parent;
    }
}