ClassUtils.java 2.46 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4
/**
 * $Revision$
 * $Date$
 *
5
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
6
 *
7
 * This software is published under the terms of the GNU Public License (GPL),
8 9
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
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
package org.jivesoftware.util;

Bill Lynch's avatar
Bill Lynch committed
14 15
import java.io.InputStream;

Matt Tucker's avatar
Matt Tucker committed
16
/**
Bill Lynch's avatar
Bill Lynch committed
17
 * A utility class to assist with loading classes or resources by name. Many application servers use
Matt Tucker's avatar
Matt Tucker committed
18 19 20 21
 * custom classloaders, which will break uses of:
 * <pre>
 *    Class.forName(className);
 * </pre>
Bill Lynch's avatar
Bill Lynch committed
22 23 24
 *
 * This utility attempts to load the class or resource using a number of different mechanisms to
 * work around this problem.
Matt Tucker's avatar
Matt Tucker committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 *
 * @author Matt Tucker
 */
public class ClassUtils {

    private static ClassUtils instance = new ClassUtils();

    /**
     * Loads the class with the specified name.
     *
     * @param className the name of the class
     * @return the resulting <code>Class</code> object
     * @throws ClassNotFoundException if the class was not found
     */
    public static Class forName(String className) throws ClassNotFoundException {
        return instance.loadClass(className);
    }

Bill Lynch's avatar
Bill Lynch committed
43 44 45 46 47 48 49 50
    /**
     * Loads the given resource as a stream.
     *
     * @param name the name of the resource that exists in the classpath.
     * @return the resource as an input stream or <tt>null</tt> if the resource was not found.
     */
    public static InputStream getResourceAsStream(String name) {
        return instance.loadResource(name);
Matt Tucker's avatar
Matt Tucker committed
51 52
    }

Bill Lynch's avatar
Bill Lynch committed
53 54 55 56 57
    /**
     * Not instantiatable.
     */
    private ClassUtils() {}

Matt Tucker's avatar
Matt Tucker committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    public Class loadClass(String className) throws ClassNotFoundException {
        Class theClass = null;
        try {
            theClass = Class.forName(className);
        }
        catch (ClassNotFoundException e1) {
            try {
                theClass = Thread.currentThread().getContextClassLoader().loadClass(className);
            }
            catch (ClassNotFoundException e2) {
                theClass = getClass().getClassLoader().loadClass(className);
            }
        }
        return theClass;
    }
Bill Lynch's avatar
Bill Lynch committed
73 74 75 76 77 78 79 80 81 82 83

    private InputStream loadResource(String name) {
        InputStream in = getClass().getResourceAsStream(name);
        if (in == null) {
            in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
            if (in == null) {
                in = getClass().getClassLoader().getResourceAsStream(name);
            }
        }
        return in;
    }
Matt Tucker's avatar
Matt Tucker committed
84
}