1 /*
2 * $Id: ClassUtils.java 471754 2006-11-06 14:55:09Z husted $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.struts.chain.commands.util;
22
23
24 /**
25 * <p>Utility methods to load application classes and create instances.</p>
26 *
27 * @version $Rev: 471754 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
28 * $
29 */
30 public final class ClassUtils {
31 // ---------------------------------------------------------- Static Methods
32
33 /**
34 * <p>Return the <code>Class</code> object for the specified fully
35 * qualified class name, from this web application's class loader.
36 *
37 * @param className Fully qualified class name
38 * @throws ClassNotFoundException if the specified class cannot be loaded
39 */
40 public static Class getApplicationClass(String className)
41 throws ClassNotFoundException {
42 if (className == null) {
43 throw new NullPointerException(
44 "getApplicationClass called with null className");
45 }
46
47 ClassLoader classLoader =
48 Thread.currentThread().getContextClassLoader();
49
50 if (classLoader == null) {
51 classLoader = ClassUtils.class.getClassLoader();
52 }
53
54 return (classLoader.loadClass(className));
55 }
56
57 /**
58 * <p>Return a new instance of the specified fully qualified class name,
59 * after loading the class (if necessary) from this web application's
60 * class loader.</p>
61 *
62 * @param className Fully qualified class name
63 * @throws ClassNotFoundException if the specified class cannot be loaded
64 * @throws IllegalAccessException if this class is not concrete
65 * @throws InstantiationException if this class has no zero-arguments
66 * constructor
67 */
68 public static Object getApplicationInstance(String className)
69 throws ClassNotFoundException, IllegalAccessException,
70 InstantiationException {
71 return (getApplicationClass(className).newInstance());
72 }
73 }