View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
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 net.sf.ehcache.CacheException;
20  
21  /**
22   * Keeps all classloading in ehcache consistent.
23   *
24   * @author Greg Luck
25   * @version $Id: ClassLoaderUtil.java 512 2007-07-10 09:18:45Z gregluck $
26   */
27  public final class ClassLoaderUtil {
28  
29      /**
30       * Utility class.
31       */
32      private ClassLoaderUtil() {
33          //noop
34      }
35  
36      /**
37       * Gets the <code>ClassLoader</code> that all classes in ehcache, and extensions, should
38       * use for classloading. All ClassLoading in ehcache should use this one. This is the only
39       * thing that seems to work for all of the class loading situations found in the wild.
40       * @return the thread context class loader.
41       */
42      public static ClassLoader getStandardClassLoader() {
43          return Thread.currentThread().getContextClassLoader();
44      }
45  
46      /**
47       * Gets a fallback <code>ClassLoader</code> that all classes in ehcache, and extensions,
48       * should use for classloading. This is used if the context class loader does not work.
49       * @return the <code>ClassLoaderUtil.class.getClassLoader();</code>
50       */
51      public static ClassLoader getFallbackClassLoader() {
52          return ClassLoaderUtil.class.getClassLoader();
53      }
54  
55      /**
56       * Creates a new class instance. Logs errors along the way. Classes are loaded using the
57       * ehcache standard classloader.
58       *
59       * @param className a fully qualified class name
60       * @return null if the instance cannot be loaded
61       */
62      public static Object createNewInstance(String className) throws CacheException {
63          Class clazz;
64          Object newInstance;
65          try {
66              clazz = Class.forName(className, true, getStandardClassLoader());
67          } catch (ClassNotFoundException e) {
68              //try fallback
69              try {
70                  clazz = Class.forName(className, true, getFallbackClassLoader());
71              } catch (ClassNotFoundException ex) {
72                  throw new CacheException("Unable to load class " + className +
73                          ". Initial cause was " + e.getMessage(), e);
74              }
75          }
76  
77          try {
78              newInstance = clazz.newInstance();
79          } catch (IllegalAccessException e) {
80              throw new CacheException("Unable to load class " + className +
81                      ". Initial cause was " + e.getMessage(), e);
82          } catch (InstantiationException e) {
83              throw new CacheException("Unable to load class " + className +
84                      ". Initial cause was " + e.getMessage(), e);
85          }
86          return newInstance;
87      }
88  
89  
90  }