View Javadoc
1   /*
2    * $Id$
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  import java.lang.reflect.InvocationTargetException;
24  
25  /**
26   * <p>Utility methods to load application classes and create instances.</p>
27   *
28   * @version $Rev$ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
29   *          $
30   */
31  public final class ClassUtils {
32      // ---------------------------------------------------------- Static Methods
33  
34      /**
35       * <p>Return the <code>Class</code> object for the specified fully
36       * qualified class name, from this web application's class loader.
37       *
38       * @param className Fully qualified class name
39       * @throws ClassNotFoundException if the specified class cannot be loaded
40       */
41      public static Class<?> getApplicationClass(String className)
42          throws ClassNotFoundException {
43          if (className == null) {
44              throw new NullPointerException(
45                  "getApplicationClass called with null className");
46          }
47  
48          ClassLoader classLoader =
49              Thread.currentThread().getContextClassLoader();
50  
51          if (classLoader == null) {
52              classLoader = ClassUtils.class.getClassLoader();
53          }
54  
55          return (classLoader.loadClass(className));
56      }
57  
58      /**
59       * 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.
62       *
63       * @param className Fully qualified class name
64       *
65       * @throws InstantiationException if this class has no zero-arguments
66       *                                constructor
67       * @throws IllegalAccessException if this class is not concrete
68       * @throws IllegalArgumentException if the number of actual and formal
69       *                                  parameters differ
70       * @throws InvocationTargetException if the underlying constructor
71       *                                   throws an exception
72       * @throws NoSuchMethodException if a matching method is not found
73       * @throws SecurityException if there is a security-exception
74       * @throws ClassNotFoundException if the specified class cannot be loaded
75       */
76      public static Object getApplicationInstance(String className)
77          throws InstantiationException, IllegalAccessException,
78              IllegalArgumentException, InvocationTargetException,
79              NoSuchMethodException, SecurityException, ClassNotFoundException {
80  
81          return getApplicationClass(className).getDeclaredConstructor().newInstance();
82      }
83  }