KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > ClassUtils


1 /**
2  * $RCSfile: ClassUtils.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2004/11/09 18:37:52 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11 package org.jivesoftware.util;
12
13 import java.io.InputStream JavaDoc;
14
15 /**
16  * A utility class to assist with loading classes or resources by name. Many application servers use
17  * custom classloaders, which will break uses of:
18  * <pre>
19  * Class.forName(className);
20  * </pre>
21  *
22  * This utility attempts to load the class or resource using a number of different mechanisms to
23  * work around this problem.
24  *
25  * @author Matt Tucker
26  */

27 public class ClassUtils {
28
29     private static ClassUtils instance = new ClassUtils();
30
31     /**
32      * Loads the class with the specified name.
33      *
34      * @param className the name of the class
35      * @return the resulting <code>Class</code> object
36      * @throws ClassNotFoundException if the class was not found
37      */

38     public static Class JavaDoc forName(String JavaDoc className) throws ClassNotFoundException JavaDoc {
39         return instance.loadClass(className);
40     }
41
42     /**
43      * Loads the given resource as a stream.
44      *
45      * @param name the name of the resource that exists in the classpath.
46      * @return the resource as an input stream or <tt>null</tt> if the resource was not found.
47      */

48     public static InputStream JavaDoc getResourceAsStream(String JavaDoc name) {
49         return instance.loadResource(name);
50     }
51
52     /**
53      * Not instantiatable.
54      */

55     private ClassUtils() {}
56
57     public Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
58         Class JavaDoc theClass = null;
59         try {
60             theClass = Class.forName(className);
61         }
62         catch (ClassNotFoundException JavaDoc e1) {
63             try {
64                 theClass = Thread.currentThread().getContextClassLoader().loadClass(className);
65             }
66             catch (ClassNotFoundException JavaDoc e2) {
67                 theClass = getClass().getClassLoader().loadClass(className);
68             }
69         }
70         return theClass;
71     }
72
73     private InputStream JavaDoc loadResource(String JavaDoc name) {
74         InputStream JavaDoc in = getClass().getResourceAsStream(name);
75         if (in == null) {
76             in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
77             if (in == null) {
78                 in = getClass().getClassLoader().getResourceAsStream(name);
79             }
80         }
81         return in;
82     }
83 }
84
Popular Tags