1 19 package org.java.plugin.standard; 20 21 import java.io.BufferedOutputStream ; 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.security.AccessController ; 31 import java.security.CodeSource ; 32 import java.security.PrivilegedAction ; 33 import java.security.ProtectionDomain ; 34 import java.util.Arrays ; 35 import java.util.Collections ; 36 import java.util.Enumeration ; 37 import java.util.HashMap ; 38 import java.util.HashSet ; 39 import java.util.Iterator ; 40 import java.util.LinkedList ; 41 import java.util.List ; 42 import java.util.Map ; 43 import java.util.Set ; 44 45 import org.apache.commons.logging.Log; 46 import org.apache.commons.logging.LogFactory; 47 import org.java.plugin.PathResolver; 48 import org.java.plugin.PluginClassLoader; 49 import org.java.plugin.PluginManager; 50 import org.java.plugin.registry.Library; 51 import org.java.plugin.registry.PluginDescriptor; 52 import org.java.plugin.registry.PluginPrerequisite; 53 import org.java.plugin.registry.PluginRegistry; 54 import org.java.plugin.util.IoUtil; 55 56 60 public class StandardPluginClassLoader extends PluginClassLoader { 61 static Log log = LogFactory.getLog(StandardPluginClassLoader.class); 62 63 private static File libCacheFolder; 64 private static boolean libCacheFolderInitialized = false; 65 66 private static URL getClassBaseUrl(final Class cls) { 67 ProtectionDomain pd = cls.getProtectionDomain(); 68 if (pd != null) { 69 CodeSource cs = pd.getCodeSource(); 70 if (cs != null) { 71 return cs.getLocation(); 72 } 73 } 74 return null; 75 } 76 77 private static URL [] getUrls(final PluginManager manager, 78 final PluginDescriptor descr) { 79 List result = new LinkedList (); 80 for (Iterator it = descr.getLibraries().iterator(); it.hasNext();) { 81 Library lib = (Library) it.next(); 82 if (!lib.isCodeLibrary()) { 83 continue; 84 } 85 result.add(manager.getPathResolver().resolvePath(lib, 86 lib.getPath())); 87 } 88 if (log.isDebugEnabled()) { 89 StringBuffer buf = new StringBuffer (); 90 buf.append("Code URL's populated for plug-in " + descr + ":\r\n"); for (Iterator it = result.iterator(); it.hasNext();) { 93 buf.append("\t"); buf.append(it.next()); 95 buf.append("\r\n"); } 97 log.debug(buf.toString()); 98 } 99 return (URL []) result.toArray(new URL [result.size()]); 100 } 101 102 private static URL [] getUrls(final PluginManager manager, 103 final PluginDescriptor descr, final URL [] existingUrls) { 104 List urls = Arrays.asList(existingUrls); 105 List result = new LinkedList (); 106 for (Iterator it = descr.getLibraries().iterator(); it.hasNext();) { 107 Library lib = (Library) it.next(); 108 if (!lib.isCodeLibrary()) { 109 continue; 110 } 111 URL url = manager.getPathResolver().resolvePath(lib, lib.getPath()); 112 if (!urls.contains(url)) { 113 result.add(url); 114 } 115 } 116 return (URL []) result.toArray(new URL [result.size()]); 117 } 118 119 private static File getLibCacheFolder() { 120 if (libCacheFolder != null) { 121 return libCacheFolderInitialized ? libCacheFolder : null; 122 } 123 synchronized (StandardPluginClassLoader.class) { 124 libCacheFolder = new File (System.getProperty("java.io.tmpdir"), System.currentTimeMillis() + ".jpf-lib-cache"); log.debug("libraries cache folder is " + libCacheFolder); File lockFile = new File (libCacheFolder, "lock"); if (lockFile.exists()) { 129 log.error("can't initialize libraries cache folder " + libCacheFolder + " as lock file indicates that it" + " is owned by another JPF instance"); return null; 133 } 134 if (libCacheFolder.exists()) { 135 IoUtil.emptyFolder(libCacheFolder); 137 } else { 138 libCacheFolder.mkdirs(); 139 } 140 try { 141 if (!lockFile.createNewFile()) { 142 log.error("can\'t create lock file in JPF libraries cache" + " folder " + libCacheFolder); return null; 145 } 146 } catch (IOException ioe) { 147 log.error("can\'t create lock file in JPF libraries cache" + " folder " + libCacheFolder, ioe); return null; 150 } 151 lockFile.deleteOnExit(); 152 libCacheFolder.deleteOnExit(); 153 libCacheFolderInitialized = true; 154 } 155 return libCacheFolder; 156 } 157 158 private PluginDescriptor[] publicImports; 159 private PluginDescriptor[] privateImports; 160 private PluginDescriptor[] reverseLookups; 161 private PluginResourceLoader resourceLoader; 162 private Map resourceFilters; private Map libraryCache; private boolean probeParentLoaderLast; 165 166 174 public StandardPluginClassLoader(final PluginManager aManager, 175 final PluginDescriptor descr, final ClassLoader parent) { 176 super(aManager, descr, getUrls(aManager, descr), parent); 177 collectImports(); 178 resourceLoader = PluginResourceLoader.get(aManager, descr); 179 collectFilters(); 180 libraryCache = new HashMap (); 181 } 182 183 protected void collectImports() { 184 Map publicImportsMap = new HashMap (); Map privateImportsMap = new HashMap (); PluginRegistry registry = getPluginDescriptor().getRegistry(); 188 for (Iterator it = getPluginDescriptor().getPrerequisites().iterator(); 189 it.hasNext();) { 190 PluginPrerequisite pre = (PluginPrerequisite) it.next(); 191 if (!pre.matches()) { 192 continue; 193 } 194 PluginDescriptor preDescr = 195 registry.getPluginDescriptor(pre.getPluginId()); 196 if (pre.isExported()) { 197 publicImportsMap.put(preDescr.getId(), preDescr); 198 } else { 199 privateImportsMap.put(preDescr.getId(), preDescr); 200 } 201 } 202 publicImports = (PluginDescriptor[]) publicImportsMap.values().toArray( 203 new PluginDescriptor[publicImportsMap.size()]); 204 privateImports = 205 (PluginDescriptor[]) privateImportsMap.values().toArray( 206 new PluginDescriptor[privateImportsMap.size()]); 207 Map reverseLookupsMap = new HashMap (); for (Iterator it = registry.getPluginDescriptors().iterator(); 210 it.hasNext();) { 211 PluginDescriptor descr = (PluginDescriptor) it.next(); 212 if (descr.equals(getPluginDescriptor()) 213 || publicImportsMap.containsKey(descr.getId()) 214 || privateImportsMap.containsKey(descr.getId())) { 215 continue; 216 } 217 for (Iterator it2 = descr.getPrerequisites().iterator(); 218 it2.hasNext();) { 219 PluginPrerequisite pre = (PluginPrerequisite) it2.next(); 220 if (!pre.getPluginId().equals(getPluginDescriptor().getId()) 221 || !pre.isReverseLookup()) { 222 continue; 223 } 224 if (!pre.matches()) { 225 continue; 226 } 227 reverseLookupsMap.put(descr.getId(), descr); 228 break; 229 } 230 } 231 reverseLookups = 232 (PluginDescriptor[]) reverseLookupsMap.values().toArray( 233 new PluginDescriptor[reverseLookupsMap.size()]); 234 } 235 236 protected void collectFilters() { 237 if (resourceFilters == null) { 238 resourceFilters = new HashMap (); 239 } else { 240 resourceFilters.clear(); 241 } 242 for (Iterator it = getPluginDescriptor().getLibraries().iterator(); 243 it.hasNext();) { 244 Library lib = (Library) it.next(); 245 resourceFilters.put( 246 getPluginManager().getPathResolver().resolvePath(lib, 247 lib.getPath()).toExternalForm(), 248 new ResourceFilter(lib)); 249 } 250 } 251 252 255 protected void pluginsSetChanged() { 256 URL [] newUrls = getUrls(getPluginManager(), getPluginDescriptor(), 257 getURLs()); 258 for (int i = 0; i < newUrls.length; i++) { 259 addURL(newUrls[i]); 260 } 261 if (log.isDebugEnabled()) { 262 StringBuffer buf = new StringBuffer (); 263 buf.append("New code URL's populated for plug-in " + getPluginDescriptor() + ":\r\n"); for (int i = 0; i < newUrls.length; i++) { 266 buf.append("\t"); buf.append(newUrls[i]); 268 buf.append("\r\n"); } 270 log.debug(buf.toString()); 271 } 272 collectImports(); 273 resourceLoader = 275 PluginResourceLoader.get(getPluginManager(), getPluginDescriptor()); 276 collectFilters(); 277 for (Iterator it = libraryCache.entrySet().iterator(); it.hasNext();) { 278 if (((Map.Entry ) it.next()).getValue() == null) { 279 it.remove(); 280 } 281 } 282 } 283 284 287 protected void dispose() { 288 for (Iterator it = libraryCache.values().iterator(); it.hasNext();) { 289 ((File ) it.next()).delete(); 290 } 291 libraryCache.clear(); 292 resourceFilters.clear(); 293 privateImports = null; 294 publicImports = null; 295 resourceLoader = null; 296 } 297 298 protected void setProbeParentLoaderLast(final boolean value) { 299 probeParentLoaderLast = value; 300 } 301 302 305 protected Class loadClass(final String name, final boolean resolve) 306 throws ClassNotFoundException { 307 309 Class result; 310 if (probeParentLoaderLast) { 311 try { 312 result = loadClass(name, resolve, this, null); 313 } catch (ClassNotFoundException cnfe) { 314 result = getParent().loadClass(name); 315 } 316 if (result == null) { 317 result = getParent().loadClass(name); 318 } 319 } else { 320 try { 321 result = getParent().loadClass(name); 322 } catch (ClassNotFoundException cnfe) { 323 result = loadClass(name, resolve, this, null); 324 } 325 } 326 if (result != null) { 327 return result; 328 } 329 throw new ClassNotFoundException (name); 330 } 331 332 protected Class loadClass(final String name, final boolean resolve, 333 final StandardPluginClassLoader requestor, final Set seenPlugins) 334 throws ClassNotFoundException { 335 337 Set seen = seenPlugins; 338 if ((seen != null) && seen.contains(getPluginDescriptor().getId())) { 339 return null; 340 } 341 if ((this != requestor) 342 && !getPluginManager().isPluginActivated(getPluginDescriptor()) 343 && !getPluginManager().isPluginActivating( 344 getPluginDescriptor())) { 345 String msg = "can't load class " + name + ", plug-in " + getPluginDescriptor() + " is not activated yet"; log.warn(msg); 348 throw new ClassNotFoundException (msg); 349 } 350 Class result = null; 351 synchronized (this) { 352 result = findLoadedClass(name); 353 if (result != null) { 354 if (log.isDebugEnabled()) { 355 log.debug("loadClass(...): found loaded class, class=" + result + ", this=" + this + ", requestor=" + requestor); } 359 checkClassVisibility(result, requestor); 360 363 return result; } 365 try { 366 synchronized (getClass()) { 367 result = findClass(name); 368 } 369 } catch (LinkageError le) { 370 if (log.isDebugEnabled()) { 371 log.debug("loadClass(...): class loading failed," + " name=" + name + ", this=" + this + ", requestor=" + requestor, le); } 375 throw le; 376 } catch (ClassNotFoundException cnfe) { 377 } 379 if (result != null) { 380 if (log.isDebugEnabled()) { 381 log.debug("loadClass(...): found class, class=" + result + ", this=" + this + ", requestor=" + requestor); } 385 checkClassVisibility(result, requestor); 386 if (resolve) { 387 resolveClass(result); 388 } 389 return result; } 391 } 392 if (seen == null) { 393 seen = new HashSet (); 394 } 395 if (log.isDebugEnabled()) { 396 log.debug("loadClass(...): class not found, name=" + name + ", this=" + this + ", requestor=" + requestor); } 400 seen.add(getPluginDescriptor().getId()); 401 for (int i = 0; i < publicImports.length; i++) { 402 if (seen.contains(publicImports[i].getId())) { 403 continue; 404 } 405 result = ((StandardPluginClassLoader) getPluginManager() 406 .getPluginClassLoader(publicImports[i])).loadClass( 407 name, resolve, requestor, seen); 408 if (result != null) { 409 412 break; } 414 } 415 if ((this == requestor) && (result == null)) { 416 for (int i = 0; i < privateImports.length; i++) { 417 if (seen.contains(privateImports[i].getId())) { 418 continue; 419 } 420 result = ((StandardPluginClassLoader) getPluginManager() 421 .getPluginClassLoader(privateImports[i])).loadClass( 422 name, resolve, requestor, seen); 423 if (result != null) { 424 427 break; } 429 } 430 } 431 if ((this == requestor) && (result == null)) { 432 for (int i = 0; i < reverseLookups.length; i++) { 433 PluginDescriptor descr = reverseLookups[i]; 434 if (seen.contains(descr.getId())) { 435 continue; 436 } 437 if (!getPluginManager().isPluginActivated(descr) 438 && !getPluginManager().isPluginActivating(descr)) { 439 continue; 440 } 441 result = ((StandardPluginClassLoader) getPluginManager() 442 .getPluginClassLoader(descr)).loadClass( 443 name, resolve, requestor, seen); 444 if (result != null) { 445 448 break; } 451 } 452 } 453 return result; 454 } 455 456 protected void checkClassVisibility(final Class cls, 457 final StandardPluginClassLoader requestor) 458 throws ClassNotFoundException { 459 462 if (this == requestor) { 463 return; 464 } 465 URL lib = getClassBaseUrl(cls); 466 if (lib == null) { 467 return; } 469 ClassLoader loader = cls.getClassLoader(); 470 if (!(loader instanceof StandardPluginClassLoader)) { 471 return; 472 } 473 if (loader != this) { 474 ((StandardPluginClassLoader) loader).checkClassVisibility(cls, 475 requestor); 476 } else { 477 ResourceFilter filter = 478 (ResourceFilter) resourceFilters.get(lib.toExternalForm()); 479 if (filter == null) { 480 log.warn("class not visible, no class filter found, lib=" + lib + ", class=" + cls + ", this=" + this + ", requestor=" + requestor); throw new ClassNotFoundException ("class " + cls.getName() + " is not visible for plug-in " + requestor.getPluginDescriptor().getId() 486 + ", no filter found for library " + lib); } 488 if (!filter.isClassVisible(cls.getName())) { 489 log.warn("class not visible, lib=" + lib + ", class=" + cls + ", this=" + this + ", requestor=" + requestor); throw new ClassNotFoundException ("class " + cls.getName() + " is not visible for plug-in " + requestor.getPluginDescriptor().getId()); 495 } 496 } 497 } 498 499 502 protected String findLibrary(final String name) { 503 if ((name == null) || "".equals(name.trim())) { return null; 505 } 506 if (log.isDebugEnabled()) { 507 log.debug("findLibrary(String): name=" + name + ", this=" + this); } 510 String libname = System.mapLibraryName(name); 511 String result = null; 512 PathResolver pathResolver = getPluginManager().getPathResolver(); 513 for (Iterator it = getPluginDescriptor().getLibraries().iterator(); 514 it.hasNext();) { 515 Library lib = (Library) it.next(); 516 if (lib.isCodeLibrary()) { 517 continue; 518 } 519 URL libUrl = pathResolver.resolvePath(lib, lib.getPath() + libname); 520 if (log.isDebugEnabled()) { 521 log.debug("findLibrary(String): trying URL " + libUrl); } 523 File libFile = IoUtil.url2file(libUrl); 524 if (libFile != null) { 525 if (log.isDebugEnabled()) { 526 log.debug("findLibrary(String): URL " + libUrl + " resolved as local file " + libFile); } 529 if (libFile.isFile()) { 530 result = libFile.getAbsolutePath(); 531 break; 532 } 533 continue; 534 } 535 libFile = (File ) libraryCache.get(libUrl.toExternalForm()); 538 if (libFile != null) { 539 if (libFile.isFile()) { 540 result = libFile.getAbsolutePath(); 541 break; 542 } 543 libraryCache.remove(libUrl.toExternalForm()); 544 } 545 if (libraryCache.containsKey(libUrl.toExternalForm())) { 546 break; 548 } 549 libFile = cacheLibrary(libUrl, libname); 550 if (libFile != null) { 551 result = libFile.getAbsolutePath(); 552 break; 553 } 554 } 555 if (log.isDebugEnabled()) { 556 log.debug("findLibrary(String): name=" + name + ", libname=" + libname + ", result=" + result + ", this=" + this); } 561 return result; 562 } 563 564 protected synchronized File cacheLibrary(final URL libUrl, 565 final String libname) { 566 File cacheFolder = getLibCacheFolder(); 567 String libUrlStr = libUrl.toExternalForm(); 568 if (libraryCache.containsKey(libUrlStr)) { 569 return (File ) libraryCache.get(libUrlStr); 570 } 571 File result = null; 572 try { 573 if (cacheFolder == null) { 574 throw new IOException ( 575 "can't initialize libraries cache folder"); } 577 File libCachePluginFolder = new File (cacheFolder, 578 getPluginDescriptor().getUniqueId()); 579 if (!libCachePluginFolder.exists() 580 && !libCachePluginFolder.mkdirs()) { 581 throw new IOException ("can't create cache folder " + libCachePluginFolder); 583 } 584 result = new File (libCachePluginFolder, libname); 585 InputStream in = IoUtil.getResourceInputStream(libUrl); 586 try { 587 OutputStream out = new BufferedOutputStream ( 588 new FileOutputStream (result)); 589 try { 590 IoUtil.copyStream(in, out, 512); 591 } finally { 592 out.close(); 593 } 594 } finally { 595 in.close(); 596 } 597 libraryCache.put(libUrlStr, result); 598 if (log.isDebugEnabled()) { 599 log.debug("library " + libname + " successfully cached from URL " + libUrl + " and saved to local file " + result); } 603 } catch (IOException ioe) { 604 log.error("can't cache library " + libname + " from URL " + libUrl, ioe); libraryCache.put(libUrlStr, null); 607 result = null; 608 } 609 return result; 610 } 611 612 615 public URL findResource(final String name) { 616 URL result = findResource(name, this, null); 618 return result; 620 } 621 622 625 public Enumeration findResources(final String name) throws IOException { 626 List result = new LinkedList (); 627 findResources(result, name, this, null); 628 return Collections.enumeration(result); 629 } 630 631 protected URL findResource(final String name, 632 final StandardPluginClassLoader requestor, final Set seenPlugins) { 633 635 Set seen = seenPlugins; 636 if ((seen != null) && seen.contains(getPluginDescriptor().getId())) { 637 return null; 638 } 639 URL result = super.findResource(name); 640 if (result != null) { if (log.isDebugEnabled()) { 642 log.debug("findResource(...): resource found in classpath, name=" + name + " URL=" + result + ", this=" + this + ", requestor=" + requestor); } 646 if (isResourceVisible(name, result, requestor)) { 647 return result; 648 } 649 return null; 650 } 651 if (resourceLoader != null) { 652 result = resourceLoader.findResource(name); 653 if (result != null) { if (log.isDebugEnabled()) { 655 log.debug("findResource(...): resource found in libraries, name=" + name + ", URL=" + result + ", this=" + this + ", requestor=" + requestor); } 659 if (isResourceVisible(name, result, requestor)) { 660 return result; 661 } 662 return null; 663 } 664 } 665 if (seen == null) { 666 seen = new HashSet (); 667 } 668 if (log.isDebugEnabled()) { 669 log.debug("findResource(...): resource not found, name=" + name + ", this=" + this + ", requestor=" + requestor); } 673 seen.add(getPluginDescriptor().getId()); 674 for (int i = 0; i < publicImports.length; i++) { 675 if (seen.contains(publicImports[i].getId())) { 676 continue; 677 } 678 result = ((StandardPluginClassLoader) getPluginManager() 679 .getPluginClassLoader(publicImports[i])).findResource( 680 name, requestor, seen); 681 if (result != null) { 682 break; } 684 } 685 if ((this == requestor) && (result == null)) { 686 for (int i = 0; i < privateImports.length; i++) { 687 if (seen.contains(privateImports[i].getId())) { 688 continue; 689 } 690 result = ((StandardPluginClassLoader) getPluginManager() 691 .getPluginClassLoader(privateImports[i])).findResource( 692 name, requestor, seen); 693 if (result != null) { 694 break; } 696 } 697 } 698 if ((this == requestor) && (result == null)) { 699 for (int i = 0; i < reverseLookups.length; i++) { 700 if (seen.contains(reverseLookups[i].getId())) { 701 continue; 702 } 703 result = ((StandardPluginClassLoader) getPluginManager() 704 .getPluginClassLoader(reverseLookups[i])).findResource( 705 name, requestor, seen); 706 if (result != null) { 707 break; } 710 } 711 } 712 return result; 713 } 714 715 protected void findResources(final List result, final String name, 716 final StandardPluginClassLoader requestor, final Set seenPlugins) 717 throws IOException { 718 Set seen = seenPlugins; 719 if ((seen != null) && seen.contains(getPluginDescriptor().getId())) { 720 return; 721 } 722 for (Enumeration enm = super.findResources(name); 723 enm.hasMoreElements();) { 724 URL url = (URL ) enm.nextElement(); 725 if (isResourceVisible(name, url, requestor)) { 726 result.add(url); 727 } 728 } 729 if (resourceLoader != null) { 730 for (Enumeration enm = resourceLoader.findResources(name); 731 enm.hasMoreElements();) { 732 URL url = (URL ) enm.nextElement(); 733 if (isResourceVisible(name, url, requestor)) { 734 result.add(url); 735 } 736 } 737 } 738 if (seen == null) { 739 seen = new HashSet (); 740 } 741 seen.add(getPluginDescriptor().getId()); 742 for (int i = 0; i < publicImports.length; i++) { 743 if (seen.contains(publicImports[i].getId())) { 744 continue; 745 } 746 ((StandardPluginClassLoader) getPluginManager().getPluginClassLoader( 747 publicImports[i])).findResources(result, name, 748 requestor, seen); 749 } 750 if (this == requestor) { 751 for (int i = 0; i < privateImports.length; i++) { 752 if (seen.contains(privateImports[i].getId())) { 753 continue; 754 } 755 ((StandardPluginClassLoader) getPluginManager().getPluginClassLoader( 756 privateImports[i])).findResources(result, name, 757 requestor, seen); 758 } 759 for (int i = 0; i < reverseLookups.length; i++) { 760 if (seen.contains(reverseLookups[i].getId())) { 761 continue; 762 } 763 ((StandardPluginClassLoader) getPluginManager().getPluginClassLoader( 764 reverseLookups[i])).findResources(result, name, 765 requestor, seen); 766 } 767 } 768 } 769 770 protected boolean isResourceVisible(final String name, final URL url, 771 final StandardPluginClassLoader requestor) { 772 774 if (this == requestor) { 775 return true; 776 } 777 URL lib; 778 try { 779 String file = url.getFile(); 780 lib = new URL (url.getProtocol(), url.getHost(), 781 file.substring(0, file.length() - name.length())); 782 } catch (MalformedURLException mue) { 783 log.error("can't get resource library URL", mue); return false; 785 } 786 ResourceFilter filter = 787 (ResourceFilter) resourceFilters.get(lib.toExternalForm()); 788 if (filter == null) { 789 log.warn("no resource filter found for library " + lib + ", name=" + name + ", URL=" + url + ", this=" + this + ", requestor=" + requestor); return false; 794 } 795 if (!filter.isResourceVisible(name)) { 796 log.warn("resource not visible, name=" + name + ", URL=" + url + ", this=" + this + ", requestor=" + requestor); return false; 800 } 801 return true; 802 } 803 804 protected static final class ResourceFilter { 805 private boolean isPublic; 806 private Set entries; 807 808 protected ResourceFilter(final Library lib) { 809 entries = new HashSet (); 810 for (Iterator it = lib.getExports().iterator(); it.hasNext();) { 811 String exportPrefix = (String ) it.next(); 812 if ("*".equals(exportPrefix)) { isPublic = true; 814 entries.clear(); 815 break; 816 } 817 if (!lib.isCodeLibrary()) { 818 exportPrefix = exportPrefix.replace('\\', '.') 819 .replace('/', '.'); 820 if (exportPrefix.startsWith(".")) { exportPrefix = exportPrefix.substring(1); 822 } 823 } 824 entries.add(exportPrefix); 825 } 826 } 827 828 protected boolean isClassVisible(final String className) { 829 if (isPublic) { 830 return true; 831 } 832 if (entries.isEmpty()) { 833 return false; 834 } 835 if (entries.contains(className)) { 836 return true; 837 } 838 int p = className.lastIndexOf('.'); 839 if (p == -1) { 840 return false; 841 } 842 return entries.contains(className.substring(0, p) + ".*"); } 844 845 protected boolean isResourceVisible(final String resPath) { 846 if (isPublic) { 848 return true; 849 } 850 if (entries.isEmpty()) { 851 return false; 852 } 853 String str = resPath.replace('\\', '.').replace('/', '.'); 855 if (str.startsWith(".")) { str = str.substring(1); 857 } 858 if (str.endsWith(".")) { str = str.substring(0, str.length() - 1); 860 } 861 return isClassVisible(str); 862 } 863 } 864 865 protected static class PluginResourceLoader extends URLClassLoader { 866 private static Log logger = 867 LogFactory.getLog(PluginResourceLoader.class); 868 869 static PluginResourceLoader get(final PluginManager manager, 870 final PluginDescriptor descr) { 871 final List urls = new LinkedList (); 872 for (Iterator it = descr.getLibraries().iterator(); it.hasNext();) { 873 Library lib = (Library) it.next(); 874 if (lib.isCodeLibrary()) { 875 continue; 876 } 877 urls.add(manager.getPathResolver().resolvePath(lib, 878 lib.getPath())); 879 } 880 if (logger.isDebugEnabled()) { 881 StringBuffer buf = new StringBuffer (); 882 buf.append("Resource URL's populated for plug-in " + descr + ":\r\n"); for (Iterator it = urls.iterator(); it.hasNext();) { 885 buf.append("\t"); buf.append(it.next()); 887 buf.append("\r\n"); } 889 logger.trace(buf.toString()); 890 } 891 if (urls.isEmpty()) { 892 return null; 893 } 894 896 return (PluginResourceLoader) AccessController.doPrivileged( 897 new PrivilegedAction () { 898 public Object run() { 899 return new PluginResourceLoader( 900 (URL []) urls.toArray(new URL [urls.size()])); 901 } 902 }); 903 } 904 905 910 PluginResourceLoader(final URL [] urls) { 911 super(urls); 912 } 913 914 917 protected Class findClass(final String name) 918 throws ClassNotFoundException { 919 throw new ClassNotFoundException (name); 920 } 921 922 925 protected Class loadClass(final String name, final boolean resolve) 926 throws ClassNotFoundException { 927 throw new ClassNotFoundException (name); 928 } 929 } 930 } 931 | Popular Tags |