1 11 package org.eclipse.help.internal.appserver; 12 import java.io.*; 13 import java.net.*; 14 import java.util.*; 15 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.osgi.util.*; 18 import org.osgi.framework.*; 19 25 public class PluginClassLoaderWrapper extends URLClassLoader { 26 private String plugin; 27 private Bundle bundle; 28 public PluginClassLoaderWrapper(String plugin) { 29 super(new URL[0]); 30 this.plugin = plugin; 31 this.bundle = Platform.getBundle(plugin); 32 } 33 public Class loadClass(String className) throws ClassNotFoundException { 34 return bundle.loadClass(className); 35 } 36 public URL getResource(String resName) { 37 return bundle.getResource(resName); 38 } 39 43 public URL[] getURLs() { 44 Set urls = getPluginClasspath(plugin); 45 return (URL[]) urls.toArray(new URL[urls.size()]); 46 } 47 private Set getPluginClasspath(String pluginId) { 48 Set plugins = new HashSet(); 50 addPluginWithPrereqs(pluginId, plugins); 51 Set urls = new HashSet(); 53 for (Iterator it = plugins.iterator(); it.hasNext();) { 54 String id = (String ) it.next(); 55 try { 56 Bundle b = Platform.getBundle(id); 57 if (b != null) { 58 String headers = (String ) b.getHeaders().get( 60 Constants.BUNDLE_CLASSPATH); 61 ManifestElement[] paths = ManifestElement.parseHeader( 62 Constants.BUNDLE_CLASSPATH, headers); 63 if (paths != null) { 64 for (int i = 0; i < paths.length; i++) { 65 String path = paths[i].getValue(); 66 URL url = b.getEntry(path); 67 if (url != null) 68 try { 69 urls.add(FileLocator.toFileURL(url)); 70 } catch (IOException ioe) { 71 } 72 } 73 } 74 String [] devpaths = DevClassPathHelper 76 .getDevClassPath(pluginId); 77 if (devpaths != null) { 78 for (int i = 0; i < devpaths.length; i++) { 79 URL url = b.getEntry(devpaths[i]); 80 if (url != null) 81 try { 82 urls.add(FileLocator.toFileURL(url)); 83 } catch (IOException ioe) { 84 } 85 } 86 } 87 } 88 } catch (BundleException e) { 89 } 90 } 91 return urls; 92 } 93 97 private void addPluginWithPrereqs(String pluginId, Set pluginIds) { 98 if (pluginIds.contains(pluginId)) { 99 return; 100 } 101 String [] immidiatePrereqs = getDirectPrereqs(pluginId); 102 for (int i = 0; i < immidiatePrereqs.length; i++) { 103 addPluginWithPrereqs(immidiatePrereqs[i], pluginIds); 104 } 105 pluginIds.add(pluginId); 106 } 107 113 private String [] getDirectPrereqs(String pluginId) { 114 try { 115 Bundle bundle = Platform.getBundle(pluginId); 116 if (bundle != null) { 117 String header = (String ) bundle.getHeaders().get( 118 Constants.REQUIRE_BUNDLE); 119 ManifestElement[] requires = ManifestElement.parseHeader( 120 Constants.REQUIRE_BUNDLE, header); 121 if (requires != null) { 122 String [] reqs = new String [requires.length]; 123 for (int i = 0; i < requires.length; i++) { 124 reqs[i] = requires[i].getValue(); 125 } 126 return reqs; 127 } 128 } 129 } catch (BundleException e) { 130 } 131 return new String [0]; 132 } 133 } 134 | Popular Tags |