class ClassLoaderFactory

轉自:http://www.huihoo.org/apache/tomcat/heavyz/ClassLoaderFactory.java.html

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); } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章