View Javadoc

1   /***************************************************************************************
2    * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package org.codehaus.aspectwerkz.util;
9   
10  import java.io.InputStream;
11  import java.net.URL;
12  
13  /***
14   * Utility methods dealing with the context class loader. Fail-over is provided to the default class loader.
15   *
16   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
17   */
18  public final class ContextClassLoader {
19  
20      /***
21       * Loads a class starting from the given class loader (can be null, then use default class loader)
22       *
23       * @param loader
24       * @param name   of class to load
25       * @return
26       * @throws ClassNotFoundException
27       */
28      public static Class forName(final ClassLoader loader, final String name) throws ClassNotFoundException {
29          Class klass = null;
30          if (loader != null) {
31              klass = Class.forName(name, false, loader);
32          } else {
33              klass = Class.forName(name, false, ClassLoader.getSystemClassLoader());
34          }
35          return klass;
36      }
37  
38  
39      /***
40       * Loads a class from the context class loader or, if that fails, from the default class loader.
41       *
42       * @param name is the name of the class to load.
43       * @return a <code>Class</code> object.
44       * @throws ClassNotFoundException if the class was not found.
45       */
46      public static Class forName(final String name) throws ClassNotFoundException {
47          Class cls = null;
48          try {
49              cls = Class.forName(name, false, Thread.currentThread().getContextClassLoader());
50          } catch (Exception e) {
51              cls = Class.forName(name);
52          }
53          return cls;
54      }
55  
56      /***
57       * Loads a resource from the context class loader or, if that fails, from the default class loader.
58       *
59       * @param name is the name of the resource to load.
60       * @return a <code>URL</code> object.
61       */
62      public static URL loadResource(final String name) {
63          try {
64              return Thread.currentThread().getContextClassLoader().getResource(name);
65          } catch (Exception e) {
66              return ClassLoader.class.getClassLoader().getResource(name);
67          }
68      }
69  
70  //    /***
71  //     * Loads a resource from the context class loader or, if that fails, from the default class loader, as stream
72  //     *
73  //     * @param name is the name of the resource to load.
74  //     * @return a <code>InputStream</code> object.
75  //     */
76  //    public static InputStream getResourceAsStream(final String name) {
77  //        InputStream stream = null;
78  //        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
79  //        if (contextClassLoader != null) {
80  //            stream = contextClassLoader.getResourceAsStream(name);
81  //        }
82  //        if (stream == null) {
83  //            ClassLoader classLoader = ClassLoader.class.getClassLoader();
84  //            if (classLoader != null) {
85  //                stream = classLoader.getResourceAsStream(name);
86  //            }
87  //        }
88  //        return stream;
89  //    }
90  
91      /***
92       * Returns the context class loader.
93       *
94       * @return the context class loader
95       */
96      public static ClassLoader getLoader() {
97          ClassLoader loader = Thread.currentThread().getContextClassLoader();
98          if (loader == null) {
99              loader = ClassLoader.class.getClassLoader();
100         }
101         return loader;
102     }
103 }