1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.struts.chain.commands.util;
17
18
19 /***
20 * <p>Utility methods to load application classes and create instances.</p>
21 *
22 * @version $Rev: 421119 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
23 * $
24 */
25 public final class ClassUtils {
26
27
28 /***
29 * <p>Return the <code>Class</code> object for the specified fully
30 * qualified class name, from this web application's class loader.
31 *
32 * @param className Fully qualified class name
33 * @throws ClassNotFoundException if the specified class cannot be loaded
34 */
35 public static Class getApplicationClass(String className)
36 throws ClassNotFoundException {
37 if (className == null) {
38 throw new NullPointerException(
39 "getApplicationClass called with null className");
40 }
41
42 ClassLoader classLoader =
43 Thread.currentThread().getContextClassLoader();
44
45 if (classLoader == null) {
46 classLoader = ClassUtils.class.getClassLoader();
47 }
48
49 return (classLoader.loadClass(className));
50 }
51
52 /***
53 * <p>Return a new instance of the specified fully qualified class name,
54 * after loading the class (if necessary) from this web application's
55 * class loader.</p>
56 *
57 * @param className Fully qualified class name
58 * @throws ClassNotFoundException if the specified class cannot be loaded
59 * @throws IllegalAccessException if this class is not concrete
60 * @throws InstantiationException if this class has no zero-arguments
61 * constructor
62 */
63 public static Object getApplicationInstance(String className)
64 throws ClassNotFoundException, IllegalAccessException,
65 InstantiationException {
66 return (getApplicationClass(className).newInstance());
67 }
68 }