Revision 41788 trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/plugins/PluginClassLoader.java
PluginClassLoader.java | ||
---|---|---|
180 | 180 |
} |
181 | 181 |
|
182 | 182 |
protected Class singleLoadClass(String name) throws ClassNotFoundException { |
183 |
// Buscamos en las clases de las librer�as del plugin |
|
184 |
Class<?> c = findLoadedClass(name); |
|
185 |
|
|
186 |
if (c != null) { |
|
187 |
return c; |
|
188 |
} |
|
189 |
|
|
190 |
logger.debug("Searching class '{}' in {}", new Object[] {name, this.toString()}); |
|
191 |
try { |
|
192 |
ZipFile jar = (ZipFile) clasesJar.get(name); |
|
193 |
|
|
194 |
//No esta en ningun jar |
|
195 |
if (jar == null) { |
|
196 |
//Buscamos en el directorio de clases |
|
197 |
String classFileName = baseDir + "/classes/" + |
|
198 |
name.replace('.', '/') + ".class"; |
|
199 |
File f = new File(classFileName); |
|
200 |
if (f.exists()){ |
|
201 |
byte[] data = loadClassData(f); |
|
202 |
c = defineClass(name, data, 0, data.length); |
|
203 |
logger.debug("Found class {} in classes-folder of plugin {}", |
|
204 |
new Object[] {name, baseDir}); |
|
205 |
|
|
206 |
} else { |
|
207 |
//Buscamos en los otros plugins |
|
208 |
Set<String>pluginsVisiteds = new HashSet<String>(); |
|
209 |
c = this.singleLoadClassFromOthersPlugins(pluginsVisiteds, name); |
|
210 |
} |
|
211 |
} else { |
|
212 |
String fileName = name.replace('.', '/') + ".class"; |
|
213 |
ZipEntry classFile = jar.getEntry(fileName); |
|
214 |
byte[] data = loadClassData(classFile, |
|
215 |
jar.getInputStream(classFile)); |
|
216 |
|
|
217 |
c = defineClass(name, data, 0, data.length); |
|
218 |
|
|
219 |
logger.debug("Found class {} in jar {} of plugin {}", |
|
220 |
new Object[] { name, jar.getName(), baseDir} ); |
|
221 |
} |
|
222 |
|
|
223 |
if (c == null) { |
|
224 |
throw new ClassNotFoundException(name); |
|
225 |
} |
|
226 |
|
|
227 |
return c; |
|
228 |
} catch (IOException e) { |
|
229 |
throw new ClassNotFoundException(Messages.getString( |
|
230 |
"PluginClassLoader.Error_reading_file") + name); |
|
231 |
} |
|
183 |
Class<?> c; |
|
184 |
Set<String>pluginsVisiteds = new HashSet<String>(); |
|
185 |
c = this.singleLoadClass(pluginsVisiteds, name); |
|
186 |
return c; |
|
232 | 187 |
} |
233 | 188 |
|
234 | 189 |
/** |
... | ... | |
385 | 340 |
*/ |
386 | 341 |
public URL getResource(String res) { |
387 | 342 |
URL ret = null; |
388 |
// |
|
389 |
// Primero buscamos en el directorio del plugin. |
|
390 |
try { |
|
391 |
logger.debug("Plugin {}. Searching resource '{}'", res, this.getPluginName()); |
|
392 |
List<String> resource = new ArrayList<String>(); |
|
393 |
StringTokenizer st = new StringTokenizer(res, "\\/"); |
|
394 |
while ( st.hasMoreTokens() ) { |
|
395 |
String token = st.nextToken(); |
|
396 |
resource.add(token); |
|
397 |
} |
|
398 |
ret = getResource(baseDir, resource); |
|
399 |
if ( ret != null ) { |
|
400 |
return ret; |
|
401 |
} |
|
402 |
} catch (Exception e) { |
|
403 |
logger.warn("Plugin {}. Error getting resource '{}'.", new Object[]{this.getPluginName(), res}, e); |
|
404 |
} |
|
405 | 343 |
|
406 |
// |
|
407 |
// Luego en los plugins de los que depende |
|
408 | 344 |
Set<String>pluginsVisiteds = new HashSet<String>(); |
409 |
ret = this.getResourceFromOthersPlugins(pluginsVisiteds, res); |
|
410 |
|
|
411 |
// |
|
412 |
// Por ultimo en el class loader padre, se supone que es el del sistema. |
|
413 |
try { |
|
414 |
ret = super.getResource(res); |
|
415 |
} catch (Exception e) { |
|
416 |
logger.warn("Plugin {}. Error getting resource '{}' in parent classloader'", new Object[]{this.getPluginName(), res}, e); |
|
417 |
} |
|
418 |
|
|
419 |
if ( ret == null ) { |
|
420 |
logger.debug("Plugin {}. Resource '{}' not found.", new Object[]{this.getPluginName(), res}); |
|
421 |
} |
|
345 |
ret = this.getResource(pluginsVisiteds, res); |
|
422 | 346 |
return ret; |
423 | 347 |
} |
424 | 348 |
|
... | ... | |
505 | 429 |
} |
506 | 430 |
} |
507 | 431 |
|
508 |
private Class singleLoadClassFromOthersPlugins(Set<String> pluginsVisiteds, String name) throws ClassNotFoundException { |
|
509 |
Class<?> c=null; |
|
432 |
private Class singleLoadClass(Set<String> pluginsVisiteds, String name) throws ClassNotFoundException { |
|
510 | 433 |
|
511 | 434 |
if ( pluginsVisiteds.contains(this.getPluginName()) ) { |
512 | 435 |
return null; |
513 | 436 |
} |
514 | 437 |
pluginsVisiteds.add(this.getPluginName()); |
515 | 438 |
|
516 |
logger.debug("Searching in depends pluginLoaders"); |
|
517 |
for ( int i = 0; i < pluginLoaders.size(); i++ ) { |
|
518 |
c = null; |
|
519 |
if ( pluginLoaders.get(i) != null ) { |
|
520 |
try { |
|
521 |
c = pluginLoaders.get(i).singleLoadClass(name); |
|
522 |
} catch (ClassNotFoundException e) { |
|
523 |
// Si no la encontramos en el primer plugin, capturamos la exceptcion |
|
524 |
// porque es probable que la encontremos en el resto de plugins. |
|
439 |
// Buscamos en las clases de las librer�as del plugin |
|
440 |
Class<?> c = findLoadedClass(name); |
|
441 |
|
|
442 |
if (c != null) { |
|
443 |
return c; |
|
444 |
} |
|
445 |
|
|
446 |
logger.debug("Searching class '{}' in {}", new Object[] {name, this.toString()}); |
|
447 |
try { |
|
448 |
ZipFile jar = (ZipFile) clasesJar.get(name); |
|
449 |
|
|
450 |
//No esta en ningun jar |
|
451 |
if (jar == null) { |
|
452 |
//Buscamos en el directorio de clases |
|
453 |
String classFileName = baseDir + "/classes/" + |
|
454 |
name.replace('.', '/') + ".class"; |
|
455 |
File f = new File(classFileName); |
|
456 |
if (f.exists()){ |
|
457 |
byte[] data = loadClassData(f); |
|
458 |
c = defineClass(name, data, 0, data.length); |
|
459 |
logger.debug("Found class {} in classes-folder of plugin {}", |
|
460 |
new Object[] {name, baseDir}); |
|
461 |
|
|
462 |
} else { |
|
463 |
//Buscamos en los otros plugins |
|
464 |
for ( int i = 0; i < pluginLoaders.size(); i++ ) { |
|
465 |
c = null; |
|
466 |
if ( pluginLoaders.get(i) != null ) { |
|
467 |
try { |
|
468 |
c = pluginLoaders.get(i).singleLoadClass(pluginsVisiteds, name); |
|
469 |
} catch (ClassNotFoundException e) { |
|
470 |
// Si no la encontramos en el primer plugin, capturamos la exceptcion |
|
471 |
// porque es probable que la encontremos en el resto de plugins. |
|
472 |
} |
|
473 |
} |
|
474 |
if ( c != null ) { |
|
475 |
return c; |
|
476 |
} |
|
477 |
} |
|
525 | 478 |
} |
479 |
} else { |
|
480 |
String fileName = name.replace('.', '/') + ".class"; |
|
481 |
ZipEntry classFile = jar.getEntry(fileName); |
|
482 |
byte[] data = loadClassData(classFile, |
|
483 |
jar.getInputStream(classFile)); |
|
484 |
|
|
485 |
c = defineClass(name, data, 0, data.length); |
|
486 |
|
|
487 |
logger.debug("Found class {} in jar {} of plugin {}", |
|
488 |
new Object[] { name, jar.getName(), baseDir} ); |
|
526 | 489 |
} |
527 |
if ( c != null ) { |
|
528 |
return c; |
|
490 |
|
|
491 |
if (c == null) { |
|
492 |
throw new ClassNotFoundException(name); |
|
529 | 493 |
} |
530 |
} |
|
531 | 494 |
|
532 |
return null; |
|
495 |
return c; |
|
496 |
} catch (IOException e) { |
|
497 |
throw new ClassNotFoundException(Messages.getString( |
|
498 |
"PluginClassLoader.Error_reading_file") + name); |
|
499 |
} |
|
533 | 500 |
} |
534 | 501 |
|
535 |
|
|
536 |
private URL getResourceFromOthersPlugins(Set<String>pluginsVisiteds, String res) { |
|
502 |
/** |
|
503 |
* Este metodo busca en este class loader y en el de los plugins |
|
504 |
* de los que depende. Se cerciora de que no se queda |
|
505 |
* bucleado cuando hay una relacion recursiva entre los plugins. |
|
506 |
* |
|
507 |
* @param pluginsVisiteds, set con los plugins que va visitando para evitar |
|
508 |
* que se quede bucleado cuando hay referencia ciclicas entre plugins. |
|
509 |
* @param res |
|
510 |
* |
|
511 |
* @return |
|
512 |
*/ |
|
513 |
private URL getResource(Set<String> pluginsVisiteds, String res) { |
|
537 | 514 |
URL ret = null; |
538 | 515 |
|
539 |
if( pluginsVisiteds.contains(this.getPluginName()) ) { |
|
516 |
if ( pluginsVisiteds.contains(this.getPluginName()) ) {
|
|
540 | 517 |
return null; |
541 | 518 |
} |
542 | 519 |
pluginsVisiteds.add(this.getPluginName()); |
543 | 520 |
|
521 |
// |
|
522 |
// Primero buscamos en el directorio del plugin. |
|
523 |
try { |
|
524 |
logger.debug("Plugin {}. Searching resource '{}'", res, this.getPluginName()); |
|
525 |
List<String> resource = new ArrayList<String>(); |
|
526 |
StringTokenizer st = new StringTokenizer(res, "\\/"); |
|
527 |
while ( st.hasMoreTokens() ) { |
|
528 |
String token = st.nextToken(); |
|
529 |
resource.add(token); |
|
530 |
} |
|
531 |
ret = getResource(baseDir, resource); |
|
532 |
if ( ret != null ) { |
|
533 |
return ret; |
|
534 |
} |
|
535 |
} catch (Exception e) { |
|
536 |
logger.warn("Plugin {}. Error getting resource '{}'.", new Object[]{this.getPluginName(), res}, e); |
|
537 |
} |
|
538 |
|
|
544 | 539 |
logger.debug("Plugin {}. Searching in depends pluginLoaders", this.getPluginName()); |
545 | 540 |
for ( int i = 0; i < this.pluginLoaders.size(); i++ ) { |
546 | 541 |
PluginClassLoader pluginClassLoader = pluginLoaders.get(i); |
547 | 542 |
if ( pluginClassLoader != null ) { |
548 | 543 |
try { |
549 | 544 |
pluginsVisiteds.add(pluginClassLoader.getPluginName()); |
550 |
ret = pluginClassLoader.getResourceFromOthersPlugins(pluginsVisiteds, res);
|
|
545 |
ret = pluginClassLoader.getResource(pluginsVisiteds, res); |
|
551 | 546 |
if ( ret != null ) { |
552 | 547 |
logger.trace("Plugin {}. Found resource '{}' in plugin '{}'.", |
553 | 548 |
new Object[]{ |
... | ... | |
560 | 555 |
} |
561 | 556 |
} |
562 | 557 |
} |
563 |
return null; |
|
558 |
|
|
559 |
if ( ret == null ) { |
|
560 |
// |
|
561 |
// Por ultimo en el class loader padre, se supone que es el del sistema. |
|
562 |
try { |
|
563 |
ret = super.getResource(res); |
|
564 |
} catch (Exception e) { |
|
565 |
logger.warn("Plugin {}. Error getting resource '{}' in parent classloader'", new Object[]{this.getPluginName(), res}, e); |
|
566 |
} |
|
567 |
if ( ret == null ) { |
|
568 |
logger.debug("Plugin {}. Resource '{}' not found.", new Object[]{this.getPluginName(), res}); |
|
569 |
} |
|
570 |
} |
|
571 |
return ret; |
|
564 | 572 |
} |
573 |
|
|
565 | 574 |
} |
Also available in: Unified diff