KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > chain > util > ClassUtils


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.struts.chain.util;
18
19 /**
20  * <p>Utility methods to load application classes and create instances.</p>
21  *
22  * @author Craig R. McClanahan
23  * @version $Rev: 54933 $ $Date: 2004-10-16 18:04:52 +0100 (Sat, 16 Oct 2004) $
24  */

25
26 public final class ClassUtils {
27
28
29     // ---------------------------------------------------------- Static Methods
30

31
32     /**
33      * <p>Return the <code>Class</code> object for the specified fully
34      * qualified class name, from this web application's class loader.
35      *
36      * @param className Fully qualified class name
37      *
38      * @exception ClassNotFoundException if the specified class cannot
39      * be loaded
40      */

41     public static Class JavaDoc getApplicationClass(String JavaDoc className)
42         throws ClassNotFoundException JavaDoc {
43
44         if (className == null) {
45             throw new NullPointerException JavaDoc("getApplicationClass called with null className");
46         }
47
48         ClassLoader JavaDoc classLoader =
49             Thread.currentThread().getContextClassLoader();
50         if (classLoader == null) {
51             classLoader = ClassUtils.class.getClassLoader();
52         }
53         return (classLoader.loadClass(className));
54
55     }
56
57
58     /**
59      * <p>Return a new instance of the specified fully qualified class name,
60      * after loading the class (if necessary) from this web application's
61      * class loader.</p>
62      *
63      * @param className Fully qualified class name
64      *
65      * @exception ClassNotFoundException if the specified class cannot
66      * be loaded
67      * @exception IllegalAccessException if this class is not concrete
68      * @exception InstantiationException if this class has no zero-arguments
69      * constructor
70      */

71     public static Object JavaDoc getApplicationInstance(String JavaDoc className)
72         throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc,
73                InstantiationException JavaDoc {
74
75         return (getApplicationClass(className).newInstance());
76
77     }
78
79
80
81 }
82
Popular Tags