6382N/A/*
6382N/A * reserved comment block
6382N/A * DO NOT REMOVE OR ALTER!
6382N/A */
6382N/A/**
6382N/A * Licensed to the Apache Software Foundation (ASF) under one
6382N/A * or more contributor license agreements. See the NOTICE file
6382N/A * distributed with this work for additional information
6382N/A * regarding copyright ownership. The ASF licenses this file
6382N/A * to you under the Apache License, Version 2.0 (the
6382N/A * "License"); you may not use this file except in compliance
6382N/A * with the License. You may obtain a copy of the License at
6382N/A *
6382N/A * http://www.apache.org/licenses/LICENSE-2.0
6382N/A *
6382N/A * Unless required by applicable law or agreed to in writing,
6382N/A * software distributed under the License is distributed on an
6382N/A * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6382N/A * KIND, either express or implied. See the License for the
6382N/A * specific language governing permissions and limitations
6382N/A * under the License.
6382N/A */
6382N/A
6382N/Apackage com.sun.org.apache.xml.internal.security.transforms;
6382N/A
6382N/Aimport java.io.IOException;
6382N/Aimport java.io.InputStream;
6382N/Aimport java.net.URL;
6382N/Aimport java.util.ArrayList;
6382N/Aimport java.util.Enumeration;
6382N/Aimport java.util.List;
6382N/A
6382N/A/**
6382N/A * This class is extremely useful for loading resources and classes in a fault
6382N/A * tolerant manner that works across different applications servers. Do not
6382N/A * touch this unless you're a grizzled classloading guru veteran who is going to
6382N/A * verify any change on 6 different application servers.
6382N/A */
6382N/A// NOTE! This is a duplicate of utils.ClassLoaderUtils with public
6382N/A// modifiers changed to package-private. Make sure to integrate any future
6382N/A// changes to utils.ClassLoaderUtils to this file.
6382N/Afinal class ClassLoaderUtils {
6382N/A
6382N/A /** {@link org.apache.commons.logging} logging facility */
6382N/A private static final java.util.logging.Logger log =
6382N/A java.util.logging.Logger.getLogger(ClassLoaderUtils.class.getName());
6382N/A
6382N/A private ClassLoaderUtils() {
6382N/A }
6382N/A
6382N/A /**
6382N/A * Load a given resource. <p/> This method will try to load the resource
6382N/A * using the following methods (in order):
6382N/A * <ul>
6382N/A * <li>From Thread.currentThread().getContextClassLoader()
6382N/A * <li>From ClassLoaderUtil.class.getClassLoader()
6382N/A * <li>callingClass.getClassLoader()
6382N/A * </ul>
6382N/A *
6382N/A * @param resourceName The name of the resource to load
6382N/A * @param callingClass The Class object of the calling object
6382N/A */
6382N/A static URL getResource(String resourceName, Class<?> callingClass) {
6382N/A URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
6382N/A if (url == null && resourceName.startsWith("/")) {
6382N/A //certain classloaders need it without the leading /
6382N/A url =
6382N/A Thread.currentThread().getContextClassLoader().getResource(
6382N/A resourceName.substring(1)
6382N/A );
6382N/A }
6382N/A
6382N/A ClassLoader cluClassloader = ClassLoaderUtils.class.getClassLoader();
6382N/A if (cluClassloader == null) {
6382N/A cluClassloader = ClassLoader.getSystemClassLoader();
6382N/A }
6382N/A if (url == null) {
6382N/A url = cluClassloader.getResource(resourceName);
6382N/A }
6382N/A if (url == null && resourceName.startsWith("/")) {
6382N/A //certain classloaders need it without the leading /
6382N/A url = cluClassloader.getResource(resourceName.substring(1));
6382N/A }
6382N/A
6382N/A if (url == null) {
6382N/A ClassLoader cl = callingClass.getClassLoader();
6382N/A
6382N/A if (cl != null) {
6382N/A url = cl.getResource(resourceName);
6382N/A }
6382N/A }
6382N/A
6382N/A if (url == null) {
6382N/A url = callingClass.getResource(resourceName);
6382N/A }
6382N/A
6382N/A if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) {
6382N/A return getResource('/' + resourceName, callingClass);
6382N/A }
6382N/A
6382N/A return url;
6382N/A }
6382N/A
6382N/A /**
6382N/A * Load a given resources. <p/> This method will try to load the resources
6382N/A * using the following methods (in order):
6382N/A * <ul>
6382N/A * <li>From Thread.currentThread().getContextClassLoader()
6382N/A * <li>From ClassLoaderUtil.class.getClassLoader()
6382N/A * <li>callingClass.getClassLoader()
6382N/A * </ul>
6382N/A *
6382N/A * @param resourceName The name of the resource to load
6382N/A * @param callingClass The Class object of the calling object
6382N/A */
6382N/A static List<URL> getResources(String resourceName, Class<?> callingClass) {
6382N/A List<URL> ret = new ArrayList<URL>();
6382N/A Enumeration<URL> urls = new Enumeration<URL>() {
6382N/A public boolean hasMoreElements() {
6382N/A return false;
6382N/A }
6382N/A public URL nextElement() {
6382N/A return null;
6382N/A }
6382N/A
6382N/A };
6382N/A try {
6382N/A urls = Thread.currentThread().getContextClassLoader().getResources(resourceName);
6382N/A } catch (IOException e) {
6382N/A if (log.isLoggable(java.util.logging.Level.FINE)) {
6382N/A log.log(java.util.logging.Level.FINE, e.getMessage(), e);
6382N/A }
6382N/A //ignore
6382N/A }
6382N/A if (!urls.hasMoreElements() && resourceName.startsWith("/")) {
6382N/A //certain classloaders need it without the leading /
6382N/A try {
6382N/A urls =
6382N/A Thread.currentThread().getContextClassLoader().getResources(
6382N/A resourceName.substring(1)
6382N/A );
6382N/A } catch (IOException e) {
6382N/A if (log.isLoggable(java.util.logging.Level.FINE)) {
6382N/A log.log(java.util.logging.Level.FINE, e.getMessage(), e);
6382N/A }
6382N/A // ignore
6382N/A }
6382N/A }
6382N/A
6382N/A ClassLoader cluClassloader = ClassLoaderUtils.class.getClassLoader();
6382N/A if (cluClassloader == null) {
6382N/A cluClassloader = ClassLoader.getSystemClassLoader();
6382N/A }
6382N/A if (!urls.hasMoreElements()) {
6382N/A try {
6382N/A urls = cluClassloader.getResources(resourceName);
6382N/A } catch (IOException e) {
6382N/A if (log.isLoggable(java.util.logging.Level.FINE)) {
6382N/A log.log(java.util.logging.Level.FINE, e.getMessage(), e);
6382N/A }
6382N/A // ignore
6382N/A }
6382N/A }
6382N/A if (!urls.hasMoreElements() && resourceName.startsWith("/")) {
6382N/A //certain classloaders need it without the leading /
6382N/A try {
6382N/A urls = cluClassloader.getResources(resourceName.substring(1));
6382N/A } catch (IOException e) {
6382N/A if (log.isLoggable(java.util.logging.Level.FINE)) {
6382N/A log.log(java.util.logging.Level.FINE, e.getMessage(), e);
6382N/A }
6382N/A // ignore
6382N/A }
6382N/A }
6382N/A
6382N/A if (!urls.hasMoreElements()) {
6382N/A ClassLoader cl = callingClass.getClassLoader();
6382N/A
6382N/A if (cl != null) {
6382N/A try {
6382N/A urls = cl.getResources(resourceName);
6382N/A } catch (IOException e) {
6382N/A if (log.isLoggable(java.util.logging.Level.FINE)) {
6382N/A log.log(java.util.logging.Level.FINE, e.getMessage(), e);
6382N/A }
6382N/A // ignore
6382N/A }
6382N/A }
6382N/A }
6382N/A
6382N/A if (!urls.hasMoreElements()) {
6382N/A URL url = callingClass.getResource(resourceName);
6382N/A if (url != null) {
6382N/A ret.add(url);
6382N/A }
6382N/A }
6382N/A while (urls.hasMoreElements()) {
6382N/A ret.add(urls.nextElement());
6382N/A }
6382N/A
6382N/A
6382N/A if (ret.isEmpty() && (resourceName != null) && (resourceName.charAt(0) != '/')) {
6382N/A return getResources('/' + resourceName, callingClass);
6382N/A }
6382N/A return ret;
6382N/A }
6382N/A
6382N/A
6382N/A /**
6382N/A * This is a convenience method to load a resource as a stream. <p/> The
6382N/A * algorithm used to find the resource is given in getResource()
6382N/A *
6382N/A * @param resourceName The name of the resource to load
6382N/A * @param callingClass The Class object of the calling object
6382N/A */
6382N/A static InputStream getResourceAsStream(String resourceName, Class<?> callingClass) {
6382N/A URL url = getResource(resourceName, callingClass);
6382N/A
6382N/A try {
6382N/A return (url != null) ? url.openStream() : null;
6382N/A } catch (IOException e) {
6382N/A if (log.isLoggable(java.util.logging.Level.FINE)) {
6382N/A log.log(java.util.logging.Level.FINE, e.getMessage(), e);
6382N/A }
6382N/A return null;
6382N/A }
6382N/A }
6382N/A
6382N/A /**
6382N/A * Load a class with a given name. <p/> It will try to load the class in the
6382N/A * following order:
6382N/A * <ul>
6382N/A * <li>From Thread.currentThread().getContextClassLoader()
6382N/A * <li>Using the basic Class.forName()
6382N/A * <li>From ClassLoaderUtil.class.getClassLoader()
6382N/A * <li>From the callingClass.getClassLoader()
6382N/A * </ul>
6382N/A *
6382N/A * @param className The name of the class to load
6382N/A * @param callingClass The Class object of the calling object
6382N/A * @throws ClassNotFoundException If the class cannot be found anywhere.
6382N/A */
6382N/A static Class<?> loadClass(String className, Class<?> callingClass)
6382N/A throws ClassNotFoundException {
6382N/A try {
6382N/A ClassLoader cl = Thread.currentThread().getContextClassLoader();
6382N/A
6382N/A if (cl != null) {
6382N/A return cl.loadClass(className);
6382N/A }
6382N/A } catch (ClassNotFoundException e) {
6382N/A if (log.isLoggable(java.util.logging.Level.FINE)) {
6382N/A log.log(java.util.logging.Level.FINE, e.getMessage(), e);
6382N/A }
6382N/A //ignore
6382N/A }
6382N/A return loadClass2(className, callingClass);
6382N/A }
6382N/A
6382N/A private static Class<?> loadClass2(String className, Class<?> callingClass)
6382N/A throws ClassNotFoundException {
6382N/A try {
6382N/A return Class.forName(className);
6382N/A } catch (ClassNotFoundException ex) {
6382N/A try {
6382N/A if (ClassLoaderUtils.class.getClassLoader() != null) {
6382N/A return ClassLoaderUtils.class.getClassLoader().loadClass(className);
6382N/A }
6382N/A } catch (ClassNotFoundException exc) {
6382N/A if (callingClass != null && callingClass.getClassLoader() != null) {
6382N/A return callingClass.getClassLoader().loadClass(className);
6382N/A }
6382N/A }
6382N/A if (log.isLoggable(java.util.logging.Level.FINE)) {
6382N/A log.log(java.util.logging.Level.FINE, ex.getMessage(), ex);
6382N/A }
6382N/A throw ex;
6382N/A }
6382N/A }
6382N/A}