View Javadoc

1   /*
2    * Copyright  2003-2004 The Apache Software Foundation.
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  
18  package org.apache.ws.security.util;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import java.lang.reflect.InvocationTargetException;
24  import java.net.URL;
25  
26  /***
27   * Load resources (or images) from various sources.
28   * <p/>
29   *
30   * @author Davanum Srinivas (dims@yahoo.com).
31   */
32  public class Loader {
33      private static Log log = LogFactory.getLog(Loader.class.getName());
34  
35      /***
36       * This method will search for <code>resource</code> in different
37       * places. The rearch order is as follows:
38       * <ol>
39       * <p><li>Search for <code>resource</code> using the thread context
40       * class loader under Java2. If that fails, search for
41       * <code>resource</code> using the class loader that loaded this
42       * class (<code>Loader</code>).
43       * <p><li>Try one last time with
44       * <code>ClassLoader.getSystemResource(resource)</code>, that is is
45       * using the system class loader in JDK 1.2 and virtual machine's
46       * built-in class loader in JDK 1.1.
47       * </ol>
48       * <p/>
49       *
50       * @param resource
51       * @return
52       */
53      static public URL getResource(String resource) {
54          ClassLoader classLoader = null;
55          URL url = null;
56          try {
57              // We could not find resource. Ler us now try with the
58              // classloader that loaded this class.
59              classLoader = Loader.class.getClassLoader();
60              if (classLoader != null) {
61                  log.debug("Trying to find [" + resource + "] using " + classLoader + " class loader.");
62                  url = classLoader.getResource(resource);
63                  if (url != null) {
64                      return url;
65                  }
66              }
67          } catch (Throwable t) {
68              log.warn("Caught Exception while in Loader.getResource. This may be innocuous.", t);
69          }
70      
71          // Last ditch attempt: get the resource from the class path. It
72          // may be the case that clazz was loaded by the Extentsion class
73          // loader which the parent of the system class loader. Hence the
74          // code below.
75          log.debug("Trying to find [" + resource + "] using ClassLoader.getSystemResource().");
76          return ClassLoader.getSystemResource(resource);
77      }
78  
79      /***
80       * Get the Thread context class loader.
81       * <p/>
82       *
83       * @return
84       * @throws IllegalAccessException
85       * @throws InvocationTargetException
86       */
87      private static ClassLoader getTCL() throws IllegalAccessException, InvocationTargetException {
88          return Thread.currentThread().getContextClassLoader();
89      }
90  
91      /***
92       * If running under JDK 1.2 load the specified class using the
93       * <code>Thread</code> <code>contextClassLoader</code> if that
94       * fails try Class.forname.
95       * <p/>
96       *
97       * @param clazz
98       * @return
99       * @throws ClassNotFoundException
100      */
101     static public Class loadClass(String clazz) throws ClassNotFoundException {
102         try {
103             Class c = getTCL().loadClass(clazz);
104             if (c != null)
105                 return c;
106         } catch (Throwable e) {
107         }
108         // we reached here because tcl was null or because of a
109         // security exception, or because clazz could not be loaded...
110         // In any case we now try one more time
111         return Class.forName(clazz);
112     }
113 }