一、前言
关于apache的fediz-core源码包中的org.apache.cxf.fediz.core.util.ClassLoaderUtils类加载器工具类,通过URLClassLoader加载URL链接加载资源getURLClassLoader、获取getResources类的URL资源集及加载loadClass自定类资源等。
二、源码说明
package org.apache.cxf.fediz.core.util;@b@@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.net.URL;@b@import java.net.URLClassLoader;@b@import java.security.AccessController;@b@import java.security.PrivilegedAction;@b@import java.util.ArrayList;@b@import java.util.Enumeration;@b@import java.util.List;@b@@b@public final class ClassLoaderUtils@b@{@b@ public static ClassLoaderHolder setThreadContextClassloader(ClassLoader newLoader)@b@ {@b@ return ((ClassLoaderHolder)AccessController.doPrivileged(new PrivilegedAction(newLoader) {@b@ public ClassLoaderUtils.ClassLoaderHolder run() {@b@ ClassLoader l = Thread.currentThread().getContextClassLoader();@b@ Thread.currentThread().setContextClassLoader(this.val$newLoader);@b@ return new ClassLoaderUtils.ClassLoaderHolder(l);@b@ }@b@ }));@b@ }@b@@b@ public static ClassLoader getURLClassLoader(URL[] urls, ClassLoader parent)@b@ {@b@ return ((ClassLoader)AccessController.doPrivileged(new PrivilegedAction(urls, parent) {@b@ public ClassLoader run() {@b@ return new URLClassLoader(this.val$urls, this.val$parent);@b@ }@b@ }));@b@ }@b@@b@ public static ClassLoader getURLClassLoader(List<URL> urlList, ClassLoader parent)@b@ {@b@ return getURLClassLoader((URL[])urlList.toArray(new URL[urlList.size()]), parent);@b@ }@b@@b@ public static URL getResource(String resourceName, Class<?> callingClass)@b@ {@b@ URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);@b@ if ((url == null) && (resourceName.startsWith("/")))@b@ {@b@ url = Thread.currentThread().getContextClassLoader().getResource(resourceName.substring(1));@b@ }@b@@b@ ClassLoader cluClassloader = ClassLoaderUtils.class.getClassLoader();@b@ if (cluClassloader == null)@b@ cluClassloader = ClassLoader.getSystemClassLoader();@b@@b@ if (url == null)@b@ url = cluClassloader.getResource(resourceName);@b@@b@ if ((url == null) && (resourceName.startsWith("/")))@b@ {@b@ url = cluClassloader.getResource(resourceName.substring(1));@b@ }@b@@b@ if (url == null) {@b@ ClassLoader cl = callingClass.getClassLoader();@b@@b@ if (cl != null)@b@ url = cl.getResource(resourceName);@b@@b@ }@b@@b@ if (url == null) {@b@ url = callingClass.getResource(resourceName);@b@ }@b@@b@ if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) {@b@ return getResource('/' + resourceName, callingClass);@b@ }@b@@b@ return url;@b@ }@b@@b@ public static List<URL> getResources(String resourceName, Class<?> callingClass)@b@ {@b@ List ret = new ArrayList();@b@ Enumeration urls = new Enumeration() {@b@ public boolean hasMoreElements() {@b@ return false; }@b@@b@ public URL nextElement() {@b@ return null;@b@ }@b@ };@b@ try@b@ {@b@ urls = Thread.currentThread().getContextClassLoader().getResources(resourceName);@b@ }@b@ catch (IOException e)@b@ {@b@ }@b@ if ((!(urls.hasMoreElements())) && (resourceName.startsWith("/")))@b@ try@b@ {@b@ urls = Thread.currentThread().getContextClassLoader().getResources(resourceName.substring(1));@b@ }@b@ catch (IOException e)@b@ {@b@ }@b@@b@@b@ ClassLoader cluClassloader = ClassLoaderUtils.class.getClassLoader();@b@ if (cluClassloader == null)@b@ cluClassloader = ClassLoader.getSystemClassLoader();@b@@b@ if (!(urls.hasMoreElements()))@b@ try {@b@ urls = cluClassloader.getResources(resourceName);@b@ }@b@ catch (IOException e)@b@ {@b@ }@b@ if ((!(urls.hasMoreElements())) && (resourceName.startsWith("/")))@b@ try@b@ {@b@ urls = cluClassloader.getResources(resourceName.substring(1));@b@ }@b@ catch (IOException e)@b@ {@b@ }@b@@b@ if (!(urls.hasMoreElements())) {@b@ ClassLoader cl = callingClass.getClassLoader();@b@@b@ if (cl != null)@b@ try {@b@ urls = cl.getResources(resourceName);@b@ }@b@ catch (IOException e)@b@ {@b@ }@b@ }@b@@b@ if (!(urls.hasMoreElements())) {@b@ URL url = callingClass.getResource(resourceName);@b@ if (url != null)@b@ ret.add(url);@b@ }@b@@b@ while (urls.hasMoreElements()) {@b@ ret.add(urls.nextElement());@b@ }@b@@b@ if ((ret.isEmpty()) && (resourceName != null) && (resourceName.charAt(0) != '/'))@b@ return getResources('/' + resourceName, callingClass);@b@@b@ return ret;@b@ }@b@@b@ public static InputStream getResourceAsStream(String resourceName, Class<?> callingClass)@b@ {@b@ URL url = getResource(resourceName, callingClass);@b@ try@b@ {@b@ return ((url != null) ? url.openStream() : null); } catch (IOException e) {@b@ }@b@ return null;@b@ }@b@@b@ public static Class<?> loadClass(String className, Class<?> callingClass)@b@ throws ClassNotFoundException@b@ {@b@ ClassLoader cl;@b@ try@b@ {@b@ cl = Thread.currentThread().getContextClassLoader();@b@@b@ if (cl != null)@b@ return cl.loadClass(className);@b@ }@b@ catch (ClassNotFoundException e)@b@ {@b@ }@b@ return loadClass2(className, callingClass); }@b@@b@ public static <T> Class<? extends T> loadClass(String className, Class<?> callingClass, Class<T> type) throws ClassNotFoundException {@b@ ClassLoader cl;@b@ try {@b@ cl = Thread.currentThread().getContextClassLoader();@b@@b@ if (cl != null)@b@ return cl.loadClass(className).asSubclass(type);@b@ }@b@ catch (ClassNotFoundException e)@b@ {@b@ }@b@ return loadClass2(className, callingClass).asSubclass(type);@b@ }@b@@b@ private static Class<?> loadClass2(String className, Class<?> callingClass) throws ClassNotFoundException {@b@ try {@b@ return Class.forName(className);@b@ } catch (ClassNotFoundException ex) {@b@ try {@b@ if (ClassLoaderUtils.class.getClassLoader() != null)@b@ return ClassLoaderUtils.class.getClassLoader().loadClass(className);@b@ }@b@ catch (ClassNotFoundException exc) {@b@ if ((callingClass != null) && (callingClass.getClassLoader() != null))@b@ return callingClass.getClassLoader().loadClass(className);@b@ }@b@@b@ throw ex;@b@ }@b@ }@b@@b@ public static class ClassLoaderHolder@b@ {@b@ ClassLoader loader;@b@@b@ ClassLoaderHolder(ClassLoader c)@b@ {@b@ this.loader = c;@b@ }@b@@b@ public void reset() {@b@ ClassLoaderUtils.setThreadContextClassloader(this.loader);@b@ }@b@ }@b@}