Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3472300
  • 博文数量: 1450
  • 博客积分: 11163
  • 博客等级: 上将
  • 技术积分: 11101
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-25 14:40
文章分类

全部博文(1450)

文章存档

2017年(5)

2014年(2)

2013年(3)

2012年(35)

2011年(39)

2010年(88)

2009年(395)

2008年(382)

2007年(241)

2006年(246)

2005年(14)

分类: 系统运维

2008-11-27 08:55:37

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); } }
阅读(594) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~