Revision 2291 trunk/frameworks/_fwAndami/src/com/iver/andami/plugins/PluginClassLoader.java
PluginClassLoader.java | ||
---|---|---|
40 | 40 |
*/ |
41 | 41 |
package com.iver.andami.plugins; |
42 | 42 |
|
43 |
import com.iver.andami.messages.Messages; |
|
44 |
|
|
45 |
import org.apache.log4j.Logger; |
|
46 |
|
|
43 | 47 |
import java.io.DataInputStream; |
44 | 48 |
import java.io.File; |
49 |
import java.io.FileInputStream; |
|
50 |
import java.io.FileNotFoundException; |
|
45 | 51 |
import java.io.IOException; |
46 | 52 |
import java.io.InputStream; |
53 |
|
|
47 | 54 |
import java.net.MalformedURLException; |
48 | 55 |
import java.net.URL; |
49 | 56 |
import java.net.URLClassLoader; |
57 |
|
|
50 | 58 |
import java.security.AllPermission; |
51 | 59 |
import java.security.CodeSource; |
52 | 60 |
import java.security.PermissionCollection; |
61 |
|
|
53 | 62 |
import java.util.ArrayList; |
54 | 63 |
import java.util.Enumeration; |
55 | 64 |
import java.util.Hashtable; |
... | ... | |
60 | 69 |
import java.util.zip.ZipException; |
61 | 70 |
import java.util.zip.ZipFile; |
62 | 71 |
|
63 |
import org.apache.log4j.Logger; |
|
64 | 72 |
|
65 |
import com.iver.andami.messages.Messages; |
|
66 |
|
|
67 |
|
|
68 | 73 |
/** |
69 | 74 |
* Class loader que carga las clases pedidas por los plugins de manera que |
70 | 75 |
* primero busca en el classpath, luego busca en el directorio del propio |
... | ... | |
96 | 101 |
* @param cl ClassLoader padre del classLoader, al que se le pedir? |
97 | 102 |
* resolver las clases antes de utilizar el algoritmo propio |
98 | 103 |
* @param pluginLoaders DOCUMENT ME! |
104 |
* |
|
99 | 105 |
* @throws IOException |
100 |
* @throws IOException |
|
101 | 106 |
*/ |
102 | 107 |
public PluginClassLoader(URL[] jars, String baseDir, ClassLoader cl, |
103 | 108 |
PluginClassLoader[] pluginLoaders) throws IOException { |
... | ... | |
128 | 133 |
if (clasesJar.get(fileName) != null) { |
129 | 134 |
throw new JarException(Messages.getString( |
130 | 135 |
"PluginClassLoader.Dos_clases_con_el_mismo_nombre_en_el_plugin" + |
131 |
": " + fileName + " en " + jarFiles[i].getName() + " y en " + ((ZipFile)clasesJar.get(fileName)).getName())); |
|
136 |
": " + fileName + " en " + |
|
137 |
jarFiles[i].getName() + " y en " + |
|
138 |
((ZipFile) clasesJar.get(fileName)).getName())); |
|
132 | 139 |
} |
133 | 140 |
|
134 | 141 |
clasesJar.put(fileName, jarFiles[i]); |
135 | 142 |
} |
136 | 143 |
} catch (ZipException e) { |
137 |
throw new IOException(e.getMessage() + " Jar: " + jars[i].getPath()+": " + jarFiles[i]); |
|
144 |
throw new IOException(e.getMessage() + " Jar: " + |
|
145 |
jars[i].getPath() + ": " + jarFiles[i]); |
|
138 | 146 |
} catch (IOException e) { |
139 | 147 |
throw e; |
140 |
}
|
|
148 |
}
|
|
141 | 149 |
} |
142 | 150 |
} |
143 | 151 |
|
... | ... | |
145 | 153 |
* DOCUMENT ME! |
146 | 154 |
* |
147 | 155 |
* @param name DOCUMENT ME! |
148 |
* @param resolve DOCUMENT ME! |
|
149 | 156 |
* |
150 | 157 |
* @return DOCUMENT ME! |
151 | 158 |
* |
152 | 159 |
* @throws ClassNotFoundException DOCUMENT ME! |
153 | 160 |
*/ |
154 |
protected Class singleLoadClass(String name) |
|
155 |
throws ClassNotFoundException { |
|
161 |
protected Class singleLoadClass(String name) throws ClassNotFoundException { |
|
156 | 162 |
// Buscamos en las clases de las librer?as del plugin |
157 | 163 |
Class c = findLoadedClass(name); |
158 |
if (c != null){ |
|
159 |
return c; |
|
160 |
} |
|
161 | 164 |
|
165 |
if (c != null) { |
|
166 |
return c; |
|
167 |
} |
|
168 |
|
|
162 | 169 |
try { |
163 | 170 |
ZipFile jar = (ZipFile) clasesJar.get(name); |
164 | 171 |
|
172 |
//No est? en ning?n jar |
|
165 | 173 |
if (jar == null) { |
166 |
for (int i = 0; i < pluginLoaders.length; i++) { |
|
167 |
c = pluginLoaders[i].singleLoadClass(name); |
|
174 |
//Buscamos en el directorio de clases |
|
175 |
String classFileName = baseDir + "/classes/" + |
|
176 |
name.replace('.', '/') + ".class"; |
|
177 |
File f = new File(classFileName); |
|
178 |
if (f.exists()){ |
|
179 |
byte[] data = loadClassData(f); |
|
180 |
c = defineClass(name, data, 0, data.length); |
|
181 |
}else{ |
|
182 |
//Buscamos en los otros plugins |
|
183 |
for (int i = 0; i < pluginLoaders.length; i++) { |
|
184 |
c = pluginLoaders[i].singleLoadClass(name); |
|
168 | 185 |
|
169 |
if (c != null) { |
|
170 |
break; |
|
186 |
if (c != null) { |
|
187 |
break; |
|
188 |
} |
|
171 | 189 |
} |
172 | 190 |
} |
173 | 191 |
} else { |
... | ... | |
251 | 269 |
} |
252 | 270 |
|
253 | 271 |
/** |
272 |
* Gets the bytes of a File |
|
273 |
* |
|
274 |
* @param file File |
|
275 |
* |
|
276 |
* @return bytes of file |
|
277 |
* |
|
278 |
* @throws IOException If the operation fails |
|
279 |
*/ |
|
280 |
private byte[] loadClassData(File file) throws IOException { |
|
281 |
InputStream is = new FileInputStream(file); |
|
282 |
|
|
283 |
// Get the size of the file |
|
284 |
long length = file.length(); |
|
285 |
|
|
286 |
// You cannot create an array using a long type. |
|
287 |
// It needs to be an int type. |
|
288 |
// Before converting to an int type, check |
|
289 |
// to ensure that file is not larger than Integer.MAX_VALUE. |
|
290 |
if (length > Integer.MAX_VALUE) { |
|
291 |
// File is too large |
|
292 |
} |
|
293 |
|
|
294 |
// Create the byte array to hold the data |
|
295 |
byte[] bytes = new byte[(int) length]; |
|
296 |
|
|
297 |
// Read in the bytes |
|
298 |
int offset = 0; |
|
299 |
int numRead = 0; |
|
300 |
|
|
301 |
while ((offset < bytes.length) && |
|
302 |
((numRead = is.read(bytes, offset, bytes.length - offset)) >= 0)) { |
|
303 |
offset += numRead; |
|
304 |
} |
|
305 |
|
|
306 |
// Ensure all the bytes have been read in |
|
307 |
if (offset < bytes.length) { |
|
308 |
throw new IOException("Could not completely read file " + |
|
309 |
file.getName()); |
|
310 |
} |
|
311 |
|
|
312 |
// Close the input stream and return bytes |
|
313 |
is.close(); |
|
314 |
|
|
315 |
return bytes; |
|
316 |
} |
|
317 |
|
|
318 |
/** |
|
254 | 319 |
* Obtiene los recursos tomando como la raiz el directorio base del plugin. |
255 | 320 |
* Si no se encuentra el recurso ah? se invoca a getResource del |
256 | 321 |
* classloader padre, que buscar? en el jar de la aplicaci?n. Si ah? |
... | ... | |
324 | 389 |
* @return |
325 | 390 |
*/ |
326 | 391 |
public String getPluginName() { |
327 |
String ret = baseDir.getAbsolutePath().substring(baseDir.getAbsolutePath().lastIndexOf(File.separatorChar) + |
|
392 |
String ret = baseDir.getAbsolutePath().substring(baseDir.getAbsolutePath() |
|
393 |
.lastIndexOf(File.separatorChar) + |
|
328 | 394 |
1); |
329 | 395 |
|
330 | 396 |
return ret; |
Also available in: Unified diff