分类: 系统运维
2008-11-27 08:55:37
package org.apache.catalina.startup;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.apache.catalina.loader.StandardClassLoader;
/**
* Utility class for building class loaders for Catalina. The factory
* method requires the following parameters in order to build a new class
* loader (with suitable defaults in all cases):
*
* @author Craig R. McClanahan
* @version $Revision: 1.8 $ $Date: 2002/02/17 08:26:02 $
*/
public final class ClassLoaderFactory {
/**
* Debugging detail level for processing the startup.
*/
private static int debug = 0;
/**
* Return the debugging detail level.
*/
public static int getDebug() {
return (debug);
}
/**
* 设置DEBUG级别
*/
public static void setDebug(int newDebug) {
debug = newDebug;
}
/**
* 该类是一个静态类,用来创建和返回ClassLoader对象(实际上是StandardClassLoader对象)
* 它将根据设置和参数返回apache定置的ClassLoader对象,以完成自己的类载入
*
* @param unpacked 类路径CLASSPATH的数组
* @param packed 含有JAR文件的类路径
* @param parent 父ClassLoader对象。当一个ClassLoader对象无法完成类载入时,它将请求父对象帮助
*
* @exception Exception if an error occurs constructing the class loader
*/
public static ClassLoader createClassLoader(File unpacked[],
File packed[],
ClassLoader parent)
throws Exception {
if (debug >= 1)
log("Creating new class loader");
// list里将被填入所有需要附加到CLASSPATH上去的文件名
ArrayList list = new ArrayList();
// Add unpacked directories
if (unpacked != null) {
for (int i = 0; i < unpacked.length; i++) {
File file = unpacked[i];
if (!file.isDirectory() || !file.exists() || !file.canRead())
continue;
if (debug >= 1)
log(" Including directory " + file.getAbsolutePath());
URL url = new URL("file", null,
file.getCanonicalPath() + File.separator);
list.add(url.toString());
}
}
// Add packed directory JAR files
if (packed != null) {
for (int i = 0; i < packed.length; i++) {
File directory = packed[i];
if (!directory.isDirectory() || !directory.exists() ||
!directory.canRead())
continue;
String filenames[] = directory.list();
for (int j = 0; j < filenames.length; j++) {
String filename = filenames[j].toLowerCase();
if (!filename.endsWith(".jar"))
continue;
File file = new File(directory, filenames[j]);
if (debug >= 1)
log(" Including jar file " + file.getAbsolutePath());
URL url = new URL("file", null,
file.getCanonicalPath());
list.add(url.toString());
}
}
}
// 填好了list
// 创建StandardClassLoader对象,并返回
String array[] = (String[]) list.toArray(new String[list.size()]);
StandardClassLoader classLoader = null;
if (parent == null)
classLoader = new StandardClassLoader(array);
else
classLoader = new StandardClassLoader(array, parent);
classLoader.setDelegate(true);
return (classLoader);
}
/**
* 输出日志
*/
private static void log(String message) {
System.out.print("ClassLoaderFactory: ");
System.out.println(message);
}
/**
* 输出日志和异常信息
*/
private static void log(String message, Throwable exception) {
log(message);
exception.printStackTrace(System.out);
}
}
ClassLoader
instance that should become the parent of
the new class loader.