001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.util;
021:
022: import org.apache.axis2.java.security.AccessController;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import java.net.URL;
028: import java.security.PrivilegedAction;
029: import java.io.InputStream;
030:
031: /**
032: * Loads resources (or images) from various sources.
033: */
034: public class Loader {
035: private static final Log log = LogFactory.getLog(Loader.class);
036:
037: /**
038: * Searches for <code>resource</code> in different
039: * places. The search order is as follows:
040: * <ol>
041: * <p><li>Search for <code>resource</code> using the thread context
042: * class loader under Java2. If that fails, search for
043: * <code>resource</code> using the class loader that loaded this
044: * class (<code>Loader</code>).
045: * <p><li>Try one last time with
046: * <code>ClassLoader.getSystemResource(resource)</code>, that is is
047: * using the system class loader in JDK 1.2 and virtual machine's
048: * built-in class loader in JDK 1.1.
049: * </ol>
050: * <p/>
051: *
052: * @param resource
053: * @return Returns URL
054: */
055: static public URL getResource(String resource) {
056: ClassLoader classLoader = null;
057: URL url = null;
058: try {
059: // We could not find resource. Ler us now try with the
060: // classloader that loaded this class.
061: classLoader = getTCL();
062: if (classLoader != null) {
063: log.debug("Trying to find [" + resource + "] using "
064: + classLoader + " class loader.");
065: url = classLoader.getResource(resource);
066: if (url != null) {
067: return url;
068: }
069: }
070: } catch (Throwable t) {
071: log
072: .warn(
073: "Caught Exception while in Loader.getResource. This may be innocuous.",
074: t);
075: }
076:
077: // Last ditch attempt: get the resource from the class path. It
078: // may be the case that clazz was loaded by the Extentsion class
079: // loader which the parent of the system class loader. Hence the
080: // code below.
081: log.debug("Trying to find [" + resource
082: + "] using ClassLoader.getSystemResource().");
083: return ClassLoader.getSystemResource(resource);
084: }
085:
086: /**
087: * Gets the resource with the specified class loader.
088: *
089: * @param loader
090: * @param resource
091: * @return Returns URL.
092: * @throws ClassNotFoundException
093: */
094: static public URL getResource(ClassLoader loader, String resource)
095: throws ClassNotFoundException {
096: URL url = null;
097: try {
098: if (loader != null) {
099: log.debug("Trying to find [" + resource + "] using "
100: + loader + " class loader.");
101: url = loader.getResource(resource);
102: if (url != null) {
103: return url;
104: }
105: }
106: } catch (Throwable t) {
107: log
108: .warn(
109: "Caught Exception while in Loader.getResource. This may be innocuous.",
110: t);
111: }
112: return getResource(resource);
113: }
114:
115: /**
116: * Searches for <code>resource</code> in different
117: * places. The search order is as follows:
118: * <ol>
119: * <p><li>Search for <code>resource</code> using the thread context
120: * class loader under Java2. If that fails, search for
121: * <code>resource</code> using the class loader that loaded this
122: * class (<code>Loader</code>).
123: * <p><li>Try one last time with
124: * <code>ClassLoader.getSystemResourceAsStream(resource)</code>, that is is
125: * using the system class loader in JDK 1.2 and virtual machine's
126: * built-in class loader in JDK 1.1.
127: * </ol>
128: * <p/>
129: *
130: * @param resource
131: * @return Returns URL
132: */
133: static public InputStream getResourceAsStream(String resource) {
134: ClassLoader classLoader = null;
135: try {
136: // Let's try the Thread Context Class Loader
137: classLoader = getTCL();
138: if (classLoader != null) {
139: log.debug("Trying to find [" + resource + "] using "
140: + classLoader + " class loader.");
141: InputStream is = classLoader
142: .getResourceAsStream(resource);
143: if (is != null) {
144: return is;
145: }
146: }
147: } catch (Throwable t) {
148: log
149: .warn(
150: "Caught Exception while in Loader.getResourceAsStream. This may be innocuous.",
151: t);
152: }
153:
154: try {
155: // We could not find resource. Ler us now try with the
156: // classloader that loaded this class.
157: classLoader = Loader.class.getClassLoader();
158: if (classLoader != null) {
159: log.debug("Trying to find [" + resource + "] using "
160: + classLoader + " class loader.");
161: InputStream is = classLoader
162: .getResourceAsStream(resource);
163: if (is != null) {
164: return is;
165: }
166: }
167: } catch (Throwable t) {
168: log
169: .warn(
170: "Caught Exception while in Loader.getResourceAsStream. This may be innocuous.",
171: t);
172: }
173:
174: // Last ditch attempt: get the resource from the class path. It
175: // may be the case that clazz was loaded by the Extentsion class
176: // loader which the parent of the system class loader. Hence the
177: // code below.
178: log.debug("Trying to find [" + resource
179: + "] using ClassLoader.getSystemResourceAsStream().");
180: return ClassLoader.getSystemResourceAsStream(resource);
181: }
182:
183: /**
184: * Gets the resource with the specified class loader.
185: *
186: * @param loader
187: * @param resource
188: * @return Returns URL.
189: * @throws ClassNotFoundException
190: */
191: static public InputStream getResourceAsStream(ClassLoader loader,
192: String resource) throws ClassNotFoundException {
193: try {
194: if (loader != null) {
195: log.debug("Trying to find [" + resource + "] using "
196: + loader + " class loader.");
197: InputStream is = loader.getResourceAsStream(resource);
198: if (is != null) {
199: return is;
200: }
201: }
202: } catch (Throwable t) {
203: log
204: .warn(
205: "Caught Exception while in Loader.getResource. This may be innocuous.",
206: t);
207: }
208: return getResourceAsStream(resource);
209: }
210:
211: /**
212: * Gets the thread context class loader.
213: *
214: * @return Returns ClassLoader.
215: * @throws IllegalAccessException
216: * @throws InvocationTargetException
217: */
218: static public ClassLoader getTCL() throws IllegalAccessException,
219: InvocationTargetException {
220: return (ClassLoader) AccessController
221: .doPrivileged(new PrivilegedAction() {
222: public Object run() {
223: return Thread.currentThread()
224: .getContextClassLoader();
225: }
226: });
227: }
228:
229: /**
230: * Loads the specified classloader and then falls back to the loadClass.
231: *
232: * @param loader
233: * @param clazz
234: * @return Returns Class.
235: * @throws ClassNotFoundException
236: */
237: static public Class loadClass(ClassLoader loader, String clazz)
238: throws ClassNotFoundException {
239: try {
240: if (loader != null) {
241: Class c = loader.loadClass(clazz);
242: if (c != null) {
243: return c;
244: }
245: }
246: } catch (UnsupportedClassVersionError e) {
247: log.error(e.getMessage(), e);
248: throw e;
249: } catch (Throwable e) {
250: log.debug(e);
251: }
252: return loadClass(clazz);
253: }
254:
255: /**
256: * If running under JDK 1.2, loads the specified class using the
257: * <code>Thread</code> <code>contextClassLoader</code> . If that
258: * fails, try Class.forname.
259: * <p/>
260: *
261: * @param clazz
262: * @return Returns Class.
263: * @throws ClassNotFoundException
264: */
265: static public Class loadClass(String clazz)
266: throws ClassNotFoundException {
267: try {
268: ClassLoader tcl = getTCL();
269:
270: if (tcl != null) {
271: Class c = tcl.loadClass(clazz);
272: if (c != null) {
273: return c;
274: }
275: }
276: } catch (UnsupportedClassVersionError e) {
277: log.error(e.getMessage(), e);
278: throw e;
279: } catch (Throwable e) {
280: log.debug(e);
281: }
282: // we reached here because tcl was null or because of a
283: // security exception, or because clazz could not be loaded...
284: // In any case we now try one more time
285: return Class.forName(clazz);
286: }
287: }
|