ClassUtils.java 2.42 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
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 11 12
 */
package org.jivesoftware.util;

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

Matt Tucker's avatar
Matt Tucker committed
15
/**
Bill Lynch's avatar
Bill Lynch committed
16
 * A utility class to assist with loading classes or resources by name. Many application servers use
Matt Tucker's avatar
Matt Tucker committed
17 18 19 20
 * custom classloaders, which will break uses of:
 * <pre>
 *    Class.forName(className);
 * </pre>
Bill Lynch's avatar
Bill Lynch committed
21 22 23
 *
 * 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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * @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
42 43 44 45 46 47 48 49
    /**
     * 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
50 51
    }

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

Matt Tucker's avatar
Matt Tucker committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    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
72 73 74 75 76 77 78 79 80 81 82

    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
83
}