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); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值