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: */package org.apache.cxf.common.classloader;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.net.URL;
023:
024: /**
025: * This class is extremely useful for loading resources and classes in a fault
026: * tolerant manner that works across different applications servers. Do not
027: * touch this unless you're a grizzled classloading guru veteran who is going to
028: * verify any change on 6 different application servers.
029: */
030: public final class ClassLoaderUtils {
031:
032: private ClassLoaderUtils() {
033: }
034:
035: /**
036: * Load a given resource. <p/> This method will try to load the resource
037: * using the following methods (in order):
038: * <ul>
039: * <li>From Thread.currentThread().getContextClassLoader()
040: * <li>From ClassLoaderUtil.class.getClassLoader()
041: * <li>callingClass.getClassLoader()
042: * </ul>
043: *
044: * @param resourceName The name of the resource to load
045: * @param callingClass The Class object of the calling object
046: */
047: public static URL getResource(String resourceName,
048: Class callingClass) {
049: URL url = Thread.currentThread().getContextClassLoader()
050: .getResource(resourceName);
051:
052: if (url == null) {
053: url = ClassLoaderUtils.class.getClassLoader().getResource(
054: resourceName);
055: }
056:
057: if (url == null) {
058: ClassLoader cl = callingClass.getClassLoader();
059:
060: if (cl != null) {
061: url = cl.getResource(resourceName);
062: }
063: }
064:
065: if (url == null) {
066: url = callingClass.getResource(resourceName);
067: }
068:
069: if ((url == null) && (resourceName != null)
070: && (resourceName.charAt(0) != '/')) {
071: return getResource('/' + resourceName, callingClass);
072: }
073:
074: return url;
075: }
076:
077: /**
078: * This is a convenience method to load a resource as a stream. <p/> The
079: * algorithm used to find the resource is given in getResource()
080: *
081: * @param resourceName The name of the resource to load
082: * @param callingClass The Class object of the calling object
083: */
084: public static InputStream getResourceAsStream(String resourceName,
085: Class callingClass) {
086: URL url = getResource(resourceName, callingClass);
087:
088: try {
089: return (url != null) ? url.openStream() : null;
090: } catch (IOException e) {
091: return null;
092: }
093: }
094:
095: /**
096: * Load a class with a given name. <p/> It will try to load the class in the
097: * following order:
098: * <ul>
099: * <li>From Thread.currentThread().getContextClassLoader()
100: * <li>Using the basic Class.forName()
101: * <li>From ClassLoaderUtil.class.getClassLoader()
102: * <li>From the callingClass.getClassLoader()
103: * </ul>
104: *
105: * @param className The name of the class to load
106: * @param callingClass The Class object of the calling object
107: * @throws ClassNotFoundException If the class cannot be found anywhere.
108: */
109: public static Class loadClass(String className, Class callingClass)
110: throws ClassNotFoundException {
111: try {
112: ClassLoader cl = Thread.currentThread()
113: .getContextClassLoader();
114:
115: if (cl != null) {
116: return cl.loadClass(className);
117: }
118:
119: return loadClass2(className, callingClass);
120: } catch (ClassNotFoundException e) {
121: return loadClass2(className, callingClass);
122: }
123: }
124:
125: private static Class loadClass2(String className, Class callingClass)
126: throws ClassNotFoundException {
127: try {
128: return Class.forName(className);
129: } catch (ClassNotFoundException ex) {
130: try {
131: return ClassLoaderUtils.class.getClassLoader()
132: .loadClass(className);
133: } catch (ClassNotFoundException exc) {
134: return callingClass.getClassLoader().loadClass(
135: className);
136: }
137: }
138: }
139: }
|