1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
58
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
72
73
74
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
109
110
111 return Class.forName(clazz);
112 }
113 }