KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > util > ClifClassLoader


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF $Name: $
20 *
21 * Contact: clif@objectweb.org
22 */

23
24 package org.objectweb.clif.util;
25
26 import org.objectweb.clif.util.bytearray.ByteArrayURLStreamHandler;
27 import java.net.URL JavaDoc;
28 import java.net.Socket JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.DataInputStream JavaDoc;
31 import java.io.DataOutputStream JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Vector JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36
37 /**
38  *
39  * @author Bruno Dillenseger
40  */

41 public class ClifClassLoader extends ClassLoader JavaDoc
42 {
43     static private ClifClassLoader cl = new ClifClassLoader();
44
45
46     static public ClifClassLoader getClassLoader()
47     {
48         return cl;
49     }
50
51
52     static public void clear()
53     {
54         cl.my_cache.clear();
55         cl = new ClifClassLoader();
56     }
57
58
59     Map JavaDoc my_cache = new HashMap JavaDoc();
60     Socket JavaDoc sock = null;
61     DataInputStream JavaDoc dins;
62     DataOutputStream JavaDoc douts;
63
64
65     private ClifClassLoader()
66     {
67         super();
68     }
69
70
71     public synchronized byte[] getBytes(String JavaDoc name)
72         throws IOException JavaDoc
73     {
74         if (sock == null)
75         {
76             sock = new Socket JavaDoc(
77                 System.getProperty("clif.codeserver.host"),
78                 Integer.parseInt(System.getProperty("clif.codeserver.port")));
79             dins = new DataInputStream JavaDoc(sock.getInputStream());
80             douts = new DataOutputStream JavaDoc(sock.getOutputStream());
81         }
82         byte[] result = null;
83         // try to get the bytes from the cache (in case it has already been downloaded)
84
result = (byte[])my_cache.get(name);
85         if (result == null && sock != null)
86         {
87             douts.writeUTF(name);
88             douts.flush();
89             int length = dins.readInt();
90             if (length >= 0)
91             {
92                 result = new byte[length];
93                 int offset = 0;
94                 while (length > 0)
95                 {
96                     int n = dins.read(result, offset, length);
97                     length -= n;
98                     offset += n;
99                 }
100                 my_cache.put(name, result);
101             }
102         }
103         return result;
104     }
105
106
107     protected synchronized Class JavaDoc findClass(String JavaDoc name)
108         throws ClassNotFoundException JavaDoc
109     {
110         if (name.startsWith("org.objectweb.fractal.julia.generated."))
111         {
112             throw new ClassNotFoundException JavaDoc(name);
113         }
114         Class JavaDoc result = null;
115         String JavaDoc pathname = name.replace('.', '/') + ".class";
116         // first, see if the class can be found using the classpath
117
try
118         {
119             ClassLoader JavaDoc parent = getParent();
120             if (parent == null)
121             {
122                 parent = getSystemClassLoader();
123             }
124             result = parent.loadClass(name);
125         }
126         catch (ClassNotFoundException JavaDoc e)
127         {
128             // second, see if the class has already been defined
129
result = (Class JavaDoc)my_cache.get(name);
130             if (result == null)
131             {
132                 byte[] code = null;
133                 // then, try to get the bytes and define the class
134
try
135                 {
136                     code = getBytes(pathname);
137                 }
138                 catch (IOException JavaDoc ex)
139                 {
140                     throw new ClassNotFoundException JavaDoc(name, ex);
141                 }
142                 if (code != null)
143                 {
144                     result = defineClass(name, code, 0, code.length);
145                     my_cache.put(name, result);
146                 }
147                 else
148                 {
149                     throw new ClassNotFoundException JavaDoc(name);
150                 }
151             }
152         }
153         return result;
154     }
155
156
157     public URL JavaDoc findResource(String JavaDoc name)
158     {
159         try
160         {
161             return new URL JavaDoc(null, "bytearray:" + name, new ByteArrayURLStreamHandler(this));
162         }
163         catch (Exception JavaDoc ex)
164         {
165             ex.printStackTrace();
166             return null;
167         }
168     }
169
170
171     /**
172      * @param name the resource's name
173      * @return an empty or single-value URL enumeration for the named resource
174      * @see #findResource(String)
175      */

176     public Enumeration JavaDoc findResources(String JavaDoc name)
177     {
178         Vector JavaDoc result = new Vector JavaDoc(1);
179         URL JavaDoc url = findResource(name);
180         if (url != null)
181         {
182             result.add(url);
183         }
184         return result.elements();
185     }
186 }
187
Popular Tags