View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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 net.sf.ehcache.util;
18  
19  import java.lang.reflect.Constructor;
20  import java.lang.reflect.InvocationTargetException;
21  
22  import net.sf.ehcache.CacheException;
23  
24  /***
25   * Keeps all classloading in ehcache consistent.
26   *
27   * @author Greg Luck
28   * @version $Id: ClassLoaderUtil.html 13146 2011-08-01 17:12:39Z oletizi $
29   */
30  public final class ClassLoaderUtil {
31  
32      /***
33       * Utility class.
34       */
35      private ClassLoaderUtil() {
36          //noop
37      }
38  
39      /***
40       * Gets the <code>ClassLoader</code> that all classes in ehcache, and extensions, should
41       * use for classloading. All ClassLoading in ehcache should use this one. This is the only
42       * thing that seems to work for all of the class loading situations found in the wild.
43       * @return the thread context class loader.
44       */
45      public static ClassLoader getStandardClassLoader() {
46          return Thread.currentThread().getContextClassLoader();
47      }
48  
49      /***
50       * Gets a fallback <code>ClassLoader</code> that all classes in ehcache, and extensions,
51       * should use for classloading. This is used if the context class loader does not work.
52       * @return the <code>ClassLoaderUtil.class.getClassLoader();</code>
53       */
54      public static ClassLoader getFallbackClassLoader() {
55          return ClassLoaderUtil.class.getClassLoader();
56      }
57  
58      /***
59       * Creates a new class instance. Logs errors along the way. Classes are loaded using the
60       * ehcache standard classloader.
61       *
62       * @param className a fully qualified class name
63       * @return the newly created instance
64       * @throws CacheException if instance cannot be created due to a missing class or exception
65       */
66      public static Object createNewInstance(String className) throws CacheException {
67          return createNewInstance(className, new Class[0], new Object[0]);
68      }
69     
70      /***
71       * Creates a new class instance and passes args to the constructor call. Logs errors along the way. 
72       * Classes are loaded using the ehcache standard classloader.
73       *
74       * @param className a fully qualified class name
75       * @param argTypes Types for constructor argument parameters
76       * @param args Values for constructor argument parameters 
77       * @return the newly created instance
78       * @throws CacheException if instance cannot be created due to a missing class or exception
79       */
80      public static Object createNewInstance(String className, Class[] argTypes, Object[] args) throws CacheException {
81          Class clazz;
82          Object newInstance;
83          try {
84              clazz = loadClass(className);
85          } catch (ClassNotFoundException e) {
86              throw new CacheException("Unable to load class " + className +
87                      ". Initial cause was " + e.getMessage(), e);
88          }
89  
90          try {
91              Constructor constructor = clazz.getConstructor(argTypes);
92              newInstance = constructor.newInstance(args);
93          } catch (IllegalAccessException e) {
94              throw new CacheException("Unable to load class " + className +
95                      ". Initial cause was " + e.getMessage(), e);
96          } catch (InstantiationException e) {
97              throw new CacheException("Unable to load class " + className +
98                      ". Initial cause was " + e.getMessage(), e);
99          } catch (NoSuchMethodException e) {
100             throw new CacheException("Unable to load class " + className +
101                     ". Initial cause was " + e.getMessage(), e);
102         } catch (SecurityException e) {
103             throw new CacheException("Unable to load class " + className +
104                     ". Initial cause was " + e.getMessage(), e);
105         } catch (IllegalArgumentException e) {
106             throw new CacheException("Unable to load class " + className +
107                     ". Initial cause was " + e.getMessage(), e);
108         } catch (InvocationTargetException e) {
109             throw new CacheException("Unable to load class " + className +
110                     ". Initial cause was " + e.getCause().getMessage(), e.getCause());
111         }
112         return newInstance;
113     }
114 
115     /***
116      * Load the given class by name
117      *
118      * @param className a fully qualified class name
119      * @return Class the loaded class
120      * @throws ClassNotFoundException if the class cannot be loaded
121      * @since 1.7
122      */
123     public static Class loadClass(String className) throws ClassNotFoundException {
124         Class clazz;
125         try {
126             clazz = Class.forName(className, true, getStandardClassLoader());
127         } catch (ClassNotFoundException e) {
128             //try fallback
129             clazz = Class.forName(className, true, getFallbackClassLoader());
130         }
131 
132         return clazz;
133     }
134 
135 
136 
137 }