All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.ws.security.util.Loader Maven / Gradle / Ivy

Go to download

The Apache WSS4J project provides a Java implementation of the primary security standards for Web Services, namely the OASIS Web Services Security (WS-Security) specifications from the OASIS Web Services Security TC.

There is a newer version: 1.6.19
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.ws.security.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;

/**
 * Load resources (or images) from various sources.
 * 

* * @author Davanum Srinivas ([email protected]). */ public class Loader { private static Log log = LogFactory.getLog(Loader.class.getName()); /** * This method will search for resource in different * places. The search order is as follows: *

    *

  1. Search for resource using the thread context * class loader under Java2. *

  2. Try one last time with * ClassLoader.getSystemResource(resource), that is is * using the system class loader in JDK 1.2 and virtual machine's * built-in class loader in JDK 1.1. *
*

* * @param resource * @return TODO */ public static URL getResource(String resource) { ClassLoader classLoader = null; URL url = null; try { classLoader = getTCL(); if (classLoader != null) { log.debug("Trying to find [" + resource + "] using " + classLoader + " class loader."); url = classLoader.getResource(resource); if (url != null) { return url; } } } catch (Throwable t) { log.warn("Caught Exception while in Loader.getResource. This may be innocuous.", t); } // Last ditch attempt: get the resource from the class path. It // may be the case that clazz was loaded by the Extension class // loader which the parent of the system class loader. Hence the // code below. log.debug("Trying to find [" + resource + "] using ClassLoader.getSystemResource()."); return ClassLoader.getSystemResource(resource); } /** * This method will search for resource in different * places. The search order is as follows: *

    *

  1. Search for resource using the supplied class loader. * If that fails, search for resource using the thread context * class loader. *

  2. Try one last time with * ClassLoader.getSystemResource(resource), that is is * using the system class loader in JDK 1.2 and virtual machine's * built-in class loader in JDK 1.1. *
*

* * @param resource * @return TODO */ public static URL getResource(ClassLoader loader, String resource) { URL url = null; try { if (loader != null) { log.debug("Trying to find [" + resource + "] using " + loader + " class loader."); url = loader.getResource(resource); if (url != null) { return url; } } } catch (Throwable t) { log.warn("Caught Exception while in Loader.getResource. This may be innocuous.", t); } return getResource(resource); } /** * Get the Thread context class loader. *

* * @return the Thread context class loader * @throws IllegalAccessException * @throws InvocationTargetException */ public static ClassLoader getTCL() throws IllegalAccessException, InvocationTargetException { return (ClassLoader)AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return Thread.currentThread().getContextClassLoader(); } }); } /** * Get the class loader of the class argument *

* * @return the class loader of the argument */ public static ClassLoader getClassLoader(final Class clazz) { return (ClassLoader)AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return clazz.getClassLoader(); } }); } /** * Try the specified classloader and then fall back to the loadClass *

* * @param loader * @param clazz * @return Class * @throws ClassNotFoundException */ public static Class loadClass(ClassLoader loader, String clazz) throws ClassNotFoundException { try { if (loader != null) { Class c = loader.loadClass(clazz); if (c != null) { return c; } } } catch (Throwable e) { log.warn(e.getMessage(), e); } return loadClass(clazz, true); } /** * If running under JDK 1.2 load the specified class using the * Thread contextClassLoader if that * fails try Class.forname. *

* * @param clazz * @return TODO * @throws ClassNotFoundException */ public static Class loadClass(String clazz) throws ClassNotFoundException { return loadClass(clazz, true); } public static Class loadClass(String clazz, boolean warn) throws ClassNotFoundException { try { ClassLoader tcl = getTCL(); if (tcl != null) { Class c = tcl.loadClass(clazz); if (c != null) { return c; } } } catch (Throwable e) { if (warn) { log.warn(e.getMessage(), e); } else { log.debug(e.getMessage(), e); } } // we reached here because tcl was null or because of a // security exception, or because clazz could not be loaded... // In any case we now try one more time return Class.forName(clazz); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy