一、前言
关于grizzly-module源码包中com.sun.grizzly.util.ClassLoaderUtil类加载工具类,通过java.io.FilenameFilter对指定路径识别jar、zip程序包后,在经过URLClassLoader加载初始化。
二、代码示例
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.URL;@b@import java.net.URLClassLoader;@b@@b@public class ClassLoaderUtil@b@{@b@ public static final 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@ {@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().toURL().toString();@b@@b@ urls[i] = new URL(path);@b@ }@b@ urlClassloader = new URLClassLoader(urls, cl);@b@ }@b@@b@ return urlClassloader;@b@ }@b@}