View Javadoc

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  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      // ---------------------------------------------------------- Static Methods
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  }