View Javadoc

1   package org.codehaus.xfire.util;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.net.URL;
6   
7   /***
8    * This class is extremely useful for loading resources and classes in a fault tolerant manner
9    * that works across different applications servers. Do not touch this unless you're a grizzled classloading
10   * guru veteran who is going to verify any change on 6 different application servers.
11   */
12  public class ClassLoaderUtils
13  {
14      /***
15       * Load a given resource.
16       * <p/>
17       * This method will try to load the resource using the following methods (in order):
18       * <ul>
19       * <li>From Thread.currentThread().getContextClassLoader()
20       * <li>From ClassLoaderUtil.class.getClassLoader()
21       * <li>callingClass.getClassLoader()
22       * </ul>
23       *
24       * @param resourceName The name of the resource to load
25       * @param callingClass The Class object of the calling object
26       */
27      public static URL getResource(String resourceName, Class callingClass)
28      {
29          URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
30      
31          if(url == null)
32          {
33              url = ClassLoaderUtils.class.getClassLoader().getResource(resourceName);
34          }
35      
36          if(url == null)
37          {
38              ClassLoader cl = callingClass.getClassLoader();
39        
40              if(cl != null)
41              {
42                  url = cl.getResource(resourceName);
43              }
44          }
45      
46          if((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/'))
47          {
48              return getResource('/' + resourceName, callingClass);
49          }
50      
51          return url;
52      }
53    
54      /***
55       * This is a convenience method to load a resource as a stream.
56       * <p/>
57       * The algorithm used to find the resource is given in getResource()
58       *
59       * @param resourceName The name of the resource to load
60       * @param callingClass The Class object of the calling object
61       */
62      public static InputStream getResourceAsStream(String resourceName, Class callingClass)
63      {
64          URL url = getResource(resourceName, callingClass);
65      
66          try
67          {
68              return (url != null) ? url.openStream() : null;
69          }
70          catch(IOException e)
71          {
72              return null;
73          }
74      }
75    
76      /***
77       * Load a class with a given name.
78       * <p/>
79       * It will try to load the class in the following order:
80       * <ul>
81       * <li>From Thread.currentThread().getContextClassLoader()
82       * <li>Using the basic Class.forName()
83       * <li>From ClassLoaderUtil.class.getClassLoader()
84       * <li>From the callingClass.getClassLoader()
85       * </ul>
86       *
87       * @param className The name of the class to load
88       * @param callingClass The Class object of the calling object
89       * @throws ClassNotFoundException If the class cannot be found anywhere.
90       */
91      public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException
92      {
93          try
94          {
95              return Thread.currentThread().getContextClassLoader().loadClass(className);
96          }
97          catch(ClassNotFoundException e)
98          {
99              try
100             {
101                 return Class.forName(className);
102             }
103             catch(ClassNotFoundException ex)
104             {
105                 try
106                 {
107                     return ClassLoaderUtils.class.getClassLoader().loadClass(className);
108                 }
109                 catch(ClassNotFoundException exc)
110                 {
111                     return callingClass.getClassLoader().loadClass(className);
112                 }
113             }
114         }
115     }
116 }