KickJava   Java API By Example, From Geeks To Geeks.

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


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
7 package org.jfox.ioc.classloader;
8
9 import java.io.File JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.net.URLClassLoader JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.WeakHashMap JavaDoc;
16
17 import org.jfox.ioc.Constants;
18 import org.jfox.ioc.logger.Logger;
19 import org.jfox.ioc.util.FileFilters;
20 import org.jfox.ioc.util.Files;
21
22 /**
23  * ioc container's class loader repository
24  *
25  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
26  */

27
28 public class ClassLoaderRepository extends URLClassLoader JavaDoc {
29     private Logger logger = Logger.getLogger(ClassLoaderRepository.class
30                                              .getName());
31
32     /**
33      * class name => class object 使用WeakHashMap保证在Class没有引用之后,可以被垃圾回收器回收
34      */

35     private Map JavaDoc classes = new WeakHashMap JavaDoc();
36
37     private final static String JavaDoc COMMON_LIBS = System.getProperty(Constants.JFOXCO_COMMON_LIBS,
38                                                                  System.getProperty(Constants.JFOXCO_HOME) + "/common");
39
40     /**
41      * 记录注册的各模块ClassLoader
42      */

43     private List JavaDoc<LocalClassLoader> localCLs = new ArrayList JavaDoc<LocalClassLoader>();
44
45     /**
46      * 单点锁,所有的ClassLoader都必须获得这个锁才能装载类
47      * 获得锁之前,处于wait装载,释放 synchronize 锁,
48      * 因为拥有 ReentranetLock 的线程可能需要使用该 ClassLoader 来装载类
49      */

50 // private ReentrantLock lock = new ReentrantLock();
51

52     public ClassLoaderRepository(URL JavaDoc[] urls, ClassLoader JavaDoc parent) {
53         super(urls, parent);
54         loadExternalLibs();
55     }
56
57     /**
58      * 装载 external 目录下的 jar
59      */

60     private void loadExternalLibs() {
61         File JavaDoc libDir = new File JavaDoc(COMMON_LIBS);
62         if(!libDir.exists() || libDir.isFile())
63             return;
64         List JavaDoc jarFiles = Files.listFiles(libDir, FileFilters
65                                                 .suffixFileFilter(new String JavaDoc[]{"jar", "zip"}));
66         for(int i = 0; i < jarFiles.size(); i++) {
67             File JavaDoc file = (File JavaDoc) jarFiles.get(i);
68             try {
69                 addURL(file.toURL());
70             }
71             catch(Exception JavaDoc e) {
72                 e.printStackTrace();
73             }
74         }
75     }
76
77     public URL JavaDoc[] getURLs() {
78         return super.getURLs();
79     }
80
81     synchronized void addLocalCL(LocalClassLoader localCL) {
82         if(!localCLs.contains(localCL)) {
83             localCLs.add(localCL);
84         }
85     }
86
87     public void removeLocalCL(LocalClassLoader localCL) {
88         localCLs.remove(localCL);
89     }
90
91     /**
92      * 添加 URL 到 Classpath 搜索路径
93      *
94      * @param url
95      */

96     public void addURL(URL JavaDoc url) {
97         logger.debug("add class path " + url.toString());
98         super.addURL(url);
99     }
100
101     /**
102      *
103      * @param name
104      * @return
105      * @throws ClassNotFoundException
106      */

107 // public synchronized Class loadClass(String name) throws ClassNotFoundException {
108

109 // }
110

111     /**
112      * 因为 ClassLoaderRepository 是 LocalClassLoader 的 parent ClassLoader
113      * LocalClassLoader 会直接调这个方法,所以必须要覆盖
114      * 首先从 ClassLoaderRepository 的classpath路径中装载类,如果失败, 装载缓存的 LocalClassLoader
115      * 已经装载的类
116      * @param name
117      * @param resolve
118      * @return
119      * @throws ClassNotFoundException
120      */

121     protected synchronized Class JavaDoc<?> loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
122         try {
123             return super.loadClass(name,resolve);
124         }
125         catch(ClassNotFoundException JavaDoc e) {
126             try {
127                 return loadClassCached(name);
128             }
129             catch(ClassNotFoundException JavaDoc e1) {
130
131             }
132         }
133         throw new ClassNotFoundException JavaDoc(name);
134
135     }
136
137     /**
138      * 装载系统类以及 common libs 中的类
139      *
140      */

141 /*
142     synchronized Class loadClassSuper(String name) throws ClassNotFoundException {
143         return super.loadClass(name);
144     }
145 */

146
147     synchronized void cacheClass(Class JavaDoc clz) {
148         if(!classes.containsKey(clz.getName())) {
149             logger.debug("cache " + clz + ", classloader is "
150                          + clz.getClassLoader());
151             classes.put(clz.getName(), clz);
152         }
153     }
154
155     /**
156      * 装载由LocalClassLoader Cache的类
157      *
158      * @param name
159      * @return
160      * @throws ClassNotFoundException
161      */

162     synchronized Class JavaDoc loadClassCached(String JavaDoc name) throws ClassNotFoundException JavaDoc {
163         if(!classes.containsKey(name)) {
164             throw new ClassNotFoundException JavaDoc(name);
165         }
166         return (Class JavaDoc) classes.get(name);
167     }
168
169
170     /**
171      * 尝试遍历 LocalClassLoader 装载类
172      */

173 /*
174     private synchronized Class loadClassWidely(String name) throws ClassNotFoundException {
175         for(int i = 0; i < localCLs.size(); i++) {
176             LocalClassLoader loader = localCLs.get(i);
177             try {
178                 Class clz = loader.loadClass(name);
179                 return clz;
180             }
181             catch(ClassNotFoundException e) {
182
183             }
184         }
185         throw new ClassNotFoundException(name);
186     }
187 */

188
189     /**
190      * 尝试遍历 LocalClassLoader 装载类
191      */

192 /*
193     synchronized Class loadClassWidelyWithout(String name, LocalClassLoader localCL)
194             throws ClassNotFoundException {
195         for(int i = 0; i < localCLs.size(); i++) {
196             LocalClassLoader loader = localCLs.get(i);
197             if(loader != localCL) {
198                 try {
199                     Class clz = loader.loadClassLocal(name);
200                     return clz;
201                 }
202                 catch(ClassNotFoundException e) {
203
204                 }
205             }
206         }
207         throw new ClassNotFoundException(name);
208     }
209 */

210
211 /*
212     public boolean tryLock(){
213         return lock.tryLock();
214     }
215
216     public void lock(){
217         lock.lock();
218     }
219
220     public void unlock(){
221         lock.unlock();
222     }
223
224     public ReentrantLock getLock(){
225         return lock;
226     }
227
228     public void notifyAllCL(){
229         for(int i = 0; i < localCLs.size(); i++) {
230             LocalClassLoader loader = localCLs.get(i);
231             synchronized(loader){
232                 loader.notifyAll();
233             }
234         }
235     }
236 */

237
238     public static void main(String JavaDoc[] args) {
239
240     }
241 }
Popular Tags