class ClassLoaderFactory

package org.apache.catalina.startup
2003-03-24

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):
 
  • A set of directories containing unpacked classes (and resources) that should be included in the class loader's repositories.
  • A set of directories containing classes and resources in JAR files. Each readable JAR file discovered in these directories will be added to the class loader's repositories.
  • ClassLoader instance that should become the parent of the new class loader.
* * @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); } }