KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > classloader > LocalClassLoader


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6 package org.jfox.ioc.classloader;
7
8 import java.net.URL JavaDoc;
9 import java.net.URLClassLoader JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Arrays JavaDoc;
12 import java.util.List JavaDoc;
13
14 /**
15  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
16  */

17
18 public class LocalClassLoader extends URLClassLoader JavaDoc {
19     /**
20      * global Class ClassLoaderRepository
21      */

22     private ClassLoaderRepository repo;
23
24     // 用来装载模块 lib 下的包,作为私有类,不共享给其它 module
25
private PrivateURLClassLoader privateCL = null;
26
27     /**
28      * the jars in the urls can be load by Repository
29      *
30      * @param urls
31      */

32     public LocalClassLoader(URL JavaDoc[] urls, ClassLoaderRepository repo) {
33         super(urls, repo);
34         privateCL = new PrivateURLClassLoader(new URL JavaDoc[0], ClassLoader
35                                                           .getSystemClassLoader());
36         this.repo = repo;
37         repo.addLocalCL(this);
38     }
39
40     public Class JavaDoc<?> loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
41         try {
42             Class JavaDoc c = super.loadClass(name);
43             // 只有使用LocalClassLoader装载的类才缓存,有可能是 SystemClassLoader 装载的
44
if(c.getClassLoader() == this) {
45                 repo.cacheClass(c);
46             }
47             return c;
48
49         }
50         catch(ClassNotFoundException JavaDoc e2) {
51             try {
52                 return privateCL.loadClass(name);
53             }
54             catch(ClassNotFoundException JavaDoc e4) {
55
56             }
57         }
58         throw new ClassNotFoundException JavaDoc(name);
59
60     }
61
62     /**
63      * 成功装载,交给 repo 缓存; 装载失败,查找 repo 缓存 最后失败,就抛出异常
64      *
65      * @param name
66      * @param resolve
67      * @return
68      * @throws ClassNotFoundException
69      */

70 // protected synchronized Class loadClass(String name, boolean resolve)
71
// throws ClassNotFoundException {
72

73 // try {
74
//装载 common lib 中的类
75
// return repo.loadClassSuper(name);
76
// return getParent().loadClass(name);
77
// }
78
// catch(ClassNotFoundException e0) {
79
// }
80

81     /*
82     * 不能实现 findClass,否则会出现一个类被多次装载的错误,因为: 通过 privateCL装载的类没有缓存在
83     * LocalClassLoader 里,这样每次下载load这样的类的时候, 还是会调用 findClass 来查找,由于findClass
84     * 没有使用缓存装载功能(findLoadedClass(name)), 所以 privateCL 会重新 find 该类,但是已经装载了,会报错
85     * protected Class findClass(String name) throws ClassNotFoundException {
86     * try { return super.findClass(name); } catch(ClassNotFoundException e) {
87     * try { // 使用私有装载器,不给repo缓存 return privateCL.findClass(name); }
88     * catch(ClassNotFoundException e1) { } } throw new
89     * ClassNotFoundException(name);
90     * }
91     */

92
93     /**
94      * 添加类路径到privateClassLoader中, LocalClassLoader的类路径通过构造器参数传入
95      *
96      * @param url
97      */

98     public void addURL(URL JavaDoc url) {
99         privateCL.addURL(url);
100     }
101
102     public URL JavaDoc getResource(String JavaDoc name) {
103         URL JavaDoc url = super.getResource(name);
104         if(url == null) {
105             url = repo.getResource(name);
106         }
107         return url;
108
109     }
110
111     /**
112      * jsper 编译的时候,需要用到 urls,所以必须加入repo 的urls
113      *
114      * @return
115      */

116     public URL JavaDoc[] getURLs() {
117         List JavaDoc urls = new ArrayList JavaDoc();
118         urls.addAll(Arrays.asList(repo.getURLs()));
119         urls.addAll(Arrays.asList(super.getURLs()));
120         return (URL JavaDoc[]) urls.toArray(new URL JavaDoc[0]);
121     }
122
123
124     public String JavaDoc toString() {
125         return "LocalClassLoader" + Arrays.asList(getURLs());
126     }
127 }
128
129
Popular Tags