Source code: com/memoire/fu/FuClassLoaderDedicated.java
1 /**
2 * @modification $Date: 2002/12/16 18:56:26 $
3 * @statut unstable
4 * @file FuClassLoaderDedicated.java
5 * @version 0.36
6 * @author Guillaume Desnoix
7 * @email guillaume@desnoix.com
8 * @license GNU General Public License 2 (GPL2)
9 * @copyright 1998-2001 Guillaume Desnoix
10 */
11
12 package com.memoire.fu;
13
14 import com.memoire.fu.*;
15
16
17 import java.io.*;
18 import java.net.*;
19 import java.util.*;
20
21 /**
22 * Very unstable and buggy. Don't use.
23 */
24 public class FuClassLoaderDedicated
25 extends ClassLoader
26 {
27 private static final FuHashtableFast global_=new FuHashtableFast(501);
28
29 private FuHashtableFast local_=new FuHashtableFast(11);
30 private Class only_;
31
32 public FuClassLoaderDedicated()
33 {
34 only_=null;
35 }
36
37 public final synchronized Class get(String _name)
38 {
39 String s=_name.intern();
40 Class r=(Class)global_.get(s);
41 if(r==null) r=(Class)local_.get(s);
42 return r;
43 }
44
45 public final synchronized void putLocal(Class _class)
46 {
47 local_.put(_class.getName().intern(),_class);
48 }
49
50 public static final synchronized void putGlobal(Class _class)
51 {
52 global_.put(_class.getName().intern(),_class);
53 }
54
55 public final synchronized Class[] list()
56 {
57 Class[] r=new Class[local_.size()];
58
59 Enumeration e=local_.elements();
60 int i=0;
61 while(e.hasMoreElements())
62 {
63 r[i]=(Class)e.nextElement();
64 //System.err.println(" - Fu:list "+r[i]);
65 i++;
66 }
67
68 return r;
69 }
70
71 public Class loadClass(String _name)
72 throws ClassNotFoundException
73 {
74 return loadClass(_name,true);
75 }
76
77 public Class loadClass
78 (String _name, boolean _resolve)
79 throws ClassNotFoundException
80 {
81 Class r=get(_name);
82
83 if(r==null)
84 {
85 /*
86 if(only_!=null)
87 {
88 //System.err.println("SUBLOADER");
89 r=Class.forName(_name);
90 putGlobal(r);
91 //r=load(_name,list());
92 //putLocal(r);
93 }
94 else
95 */
96
97 if(!_name.startsWith("java"))
98 {
99 try
100 {
101 String n=_name.replace('.','/')+".class";
102 //System.err.println("N="+n);
103
104 InputStream in=null;
105 //System.err.println("IN="+in);
106
107 /*
108 if(in==null)
109 {
110 try { in=new FileInputStream(n); }
111 catch(Exception ex) { }
112 }
113 */
114
115 String classpath=System.getProperty("java.class.path");
116 //System.err.println(classpath);
117 StringTokenizer st=new StringTokenizer(classpath,":");
118
119 while(st.hasMoreTokens())
120 {
121 String t=st.nextToken();
122 if(in==null)
123 {
124 try
125 {
126 File d=new File(t);
127 if(d.isDirectory())
128 {
129 File f=new File(d,n);
130 if(f.exists())
131 {
132 //System.err.println("Fu:file="+f);
133 System.err.println("FuClassLoaderDedicated:"+f);
134 in=new FileInputStream(f);
135 }
136 }
137 }
138 catch(Exception ex) { }
139 }
140 }
141
142 /*
143 if(in==null)
144 {
145 try
146 {
147 URL url=new URL("http://localhost/repository/"+n);
148 System.err.println("Fu:url="+url);
149 in=url.openStream();
150 }
151 catch(Exception ex) { }
152 }
153 if(in==null)
154 {
155 try
156 {
157 URL url=new URL("http://www.memoire.com/repository/"+n);
158 System.err.println("Fu:url="+url);
159 in=url.openStream();
160 }
161 catch(Exception ex) { }
162 }
163 */
164
165 ByteArrayOutputStream out=new ByteArrayOutputStream();
166 while(in.available()>0) out.write(in.read());
167 byte[] data=out.toByteArray();
168 r=defineClass(_name,data,0,data.length);
169
170 if(only_==null) only_=r;
171 putLocal(r);
172 if(_resolve) resolveClass(r);
173 //System.err.println(" CL="+r.getClassLoader());
174
175 //System.err.println("Fu:loading "+_name);
176 //System.err.println("CLASSES: "+local_.size()+"/"+global_.size());
177 //list();
178 }
179 catch(Throwable th)
180 { r=null; }
181 }
182 }
183
184 if(r==null)
185 {
186 /*
187 ClassLoader cl=base_.getClassLoader();
188 System.err.println("CL="+cl);
189 if(cl!=null) r=cl.loadClass(_name);
190 else
191 */
192 r=Class.forName(_name);
193 putGlobal(r);
194 }
195
196 return r;
197 }
198
199 public String toString()
200 {
201 return "FuClassLoaderDedicated("+(only_!=null ? only_.getName() : "null")+")";
202 }
203
204 public static final Class load(String _name,Class[] _exclude)
205 throws ClassNotFoundException
206 {
207 FuClassLoaderDedicated cl=new FuClassLoaderDedicated();
208 for(int i=0;i<_exclude.length;i++) cl.putLocal(_exclude[i]);
209 return cl.loadClass(_name);
210 }
211
212 public static final Class load(String _name,Class _exclude)
213 throws ClassNotFoundException
214 {
215 return load(_name,new Class[] { _exclude });
216 }
217
218 public static final Class load(String _name)
219 throws ClassNotFoundException
220 {
221 return load(_name,new Class[] { });
222 }
223
224 /*
225 public static void main(String[] _args)
226 {
227 AglCode.langage("Java");
228 AglCode.langage("Lisp");
229 AglCode.langage("UmlHierarchie");
230 System.gc();
231 }
232 */
233 }