一、前言
关于grizzly-utils源码包中com.sun.grizzly.util.ClassLoaderUtil类加载器工具类,创建本地路径加载器对象createClassloader、createURLClassLoader网络加载器对象等。
二、源码说明
package com.sun.grizzly.util;@b@@b@import java.io.File;@b@import java.io.FilenameFilter;@b@import java.io.IOException;@b@import java.net.URI;@b@import java.net.URL;@b@import java.net.URLClassLoader;@b@import java.util.logging.Level;@b@import java.util.logging.Logger;@b@@b@public class ClassLoaderUtil@b@{@b@ @b@ public static ClassLoader createClassloader(File libDir, ClassLoader cl)@b@ throws IOException@b@ {@b@ URLClassLoader urlClassloader = null;@b@ if ((libDir.exists()) && @b@ (libDir.isDirectory())) {@b@ String[] jars = libDir.list(new FilenameFilter() {@b@ public boolean accept(File dir, String name) {@b@ return ((name.endsWith(".jar")) || (name.endsWith(".zip")));@b@ }@b@@b@ });@b@ URL[] urls = new URL[jars.length];@b@ for (int i = 0; i < jars.length; ++i) {@b@ String path = new File(libDir.getName() + File.separator + jars[i]).getCanonicalFile().toURI().toURL().toString();@b@@b@ urls[i] = new URL(path);@b@ }@b@ urlClassloader = new URLClassLoader(urls, cl);@b@ }@b@@b@ return urlClassloader;@b@ }@b@@b@ public static URLClassLoader createURLClassLoader(String dirPath)@b@ throws IOException@b@ {@b@ String path;@b@ URL appRoot;@b@ URL classesURL;@b@ URL[] urls;@b@ int i;@b@ if ((!(dirPath.endsWith(File.separator))) && (!(dirPath.endsWith(".war"))) && (!(dirPath.endsWith(".jar"))))@b@ {@b@ dirPath = dirPath + File.separator;@b@ }@b@@b@ String separator = (java.lang.System.getProperty("os.name").toLowerCase().startsWith("win")) ? "/" : "//";@b@@b@ if ((dirPath != null) && (((dirPath.endsWith(".war")) || (dirPath.endsWith(".jar")))))@b@ {@b@ File file = new File(dirPath);@b@ appRoot = new URL("jar:file:" + separator + file.getCanonicalPath().replace('\\', '/') + "!/");@b@@b@ classesURL = new URL("jar:file:" + separator + file.getCanonicalPath().replace('\\', '/') + "!/WEB-INF/classes/");@b@@b@ path = ExpandJar.expand(appRoot);@b@ }@b@ else@b@ {@b@ path = dirPath;@b@ classesURL = new URL("file://" + path + "WEB-INF/classes/");@b@ appRoot = new URL("file://" + path);@b@ }@b@@b@ String absolutePath = new File(path).getAbsolutePath();@b@@b@ File libFiles = new File(absolutePath + File.separator + "WEB-INF" + File.separator + "lib");@b@ int arraySize = 4;@b@@b@ if ((libFiles.exists()) && (libFiles.isDirectory())) {@b@ urls = new URL[libFiles.listFiles().length + arraySize];@b@ for (i = 0; i < libFiles.listFiles().length; ++i)@b@ urls[i] = new URL("jar:file:" + separator + libFiles.listFiles()[i].toString().replace('\\', '/') + "!/");@b@ }@b@ else {@b@ urls = new URL[arraySize];@b@ }@b@@b@ urls[(urls.length - 1)] = classesURL;@b@ urls[(urls.length - 2)] = appRoot;@b@ urls[(urls.length - 3)] = new URL("file://" + path + "/WEB-INF/classes/");@b@ urls[(urls.length - 4)] = new URL("file://" + path);@b@@b@ return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());@b@ }@b@@b@ public static URLClassLoader createURLClassLoader(String location, ClassLoader parent)@b@ throws IOException@b@ {@b@ ClassLoader loader = Thread.currentThread().getContextClassLoader();@b@ Thread.currentThread().setContextClassLoader(parent);@b@ try {@b@ URLClassLoader localURLClassLoader = createURLClassLoader(location);@b@@b@ return localURLClassLoader; } finally { Thread.currentThread().setContextClassLoader(loader);@b@ }@b@ }@b@@b@ public static Object load(String clazzName)@b@ {@b@ return load(clazzName, Thread.currentThread().getContextClassLoader());@b@ }@b@@b@ public static Object load(String clazzName, ClassLoader classLoader)@b@ {@b@ Class className;@b@ try@b@ {@b@ className = Class.forName(clazzName, true, classLoader);@b@ return className.newInstance();@b@ } catch (Throwable t) {@b@ LoggerUtils.getLogger().log(Level.SEVERE, "Unable to load class " + clazzName, t);@b@ }@b@@b@ return null;@b@ }@b@}