1 11 package org.eclipse.core.internal.registry.osgi; 12 13 import java.io.*; 14 import java.net.URL ; 15 import java.util.*; 16 import org.eclipse.core.internal.registry.ExtensionRegistry; 17 import org.eclipse.core.internal.registry.RegistryMessages; 18 import org.eclipse.core.internal.runtime.ResourceTranslator; 19 import org.eclipse.core.internal.runtime.RuntimeLog; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.osgi.util.ManifestElement; 22 import org.eclipse.osgi.util.NLS; 23 import org.osgi.framework.*; 24 25 34 public class EclipseBundleListener implements SynchronousBundleListener { 35 private static final String PLUGIN_MANIFEST = "plugin.xml"; private static final String FRAGMENT_MANIFEST = "fragment.xml"; 38 private ExtensionRegistry registry; 39 private RegistryStrategyOSGI strategy; 40 private Object token; 41 42 public EclipseBundleListener(ExtensionRegistry registry, Object key, RegistryStrategyOSGI strategy) { 43 this.registry = registry; 44 this.token = key; 45 this.strategy = strategy; 46 } 47 48 public void bundleChanged(BundleEvent event) { 49 66 Bundle bundle = event.getBundle(); 67 switch (event.getType()) { 68 case BundleEvent.RESOLVED : 69 addBundle(bundle); 70 break; 71 case BundleEvent.UNRESOLVED : 72 removeBundle(bundle); 73 break; 74 } 75 } 76 77 public void processBundles(Bundle[] bundles) { 78 for (int i = 0; i < bundles.length; i++) { 79 if (isBundleResolved(bundles[i])) 80 addBundle(bundles[i]); 81 else 82 removeBundle(bundles[i]); 83 } 84 } 85 86 private boolean isBundleResolved(Bundle bundle) { 87 return (bundle.getState() & (Bundle.RESOLVED | Bundle.ACTIVE | Bundle.STARTING | Bundle.STOPPING)) != 0; 88 } 89 90 private void removeBundle(Bundle bundle) { 91 long timestamp = 0; 92 if (strategy.checkContributionsTimestamp()) { 93 URL pluginManifest = getExtensionURL(bundle, false); 94 if (pluginManifest != null) 95 timestamp = strategy.getExtendedTimestamp(bundle, pluginManifest); 96 } 97 registry.remove(Long.toString(bundle.getBundleId()), timestamp); 98 } 99 100 static public URL getExtensionURL(Bundle bundle, boolean report) { 101 if (bundle.getBundleId() == 0) 103 return null; 104 if (bundle.getSymbolicName() == null) 106 return null; 107 108 boolean isFragment = OSGIUtils.getDefault().isFragment(bundle); 109 String manifestName = isFragment ? FRAGMENT_MANIFEST : PLUGIN_MANIFEST; 110 URL extensionURL = bundle.getEntry(manifestName); 111 if (extensionURL == null) 112 return null; 113 114 if (!isSingleton(bundle)) { 116 if (report) { 117 String message = NLS.bind(RegistryMessages.parse_nonSingleton, bundle.getLocation()); 118 RuntimeLog.log(new Status(IStatus.INFO, RegistryMessages.OWNER_NAME, 0, message, null)); 119 } 120 return null; 121 } 122 if (!isFragment) 123 return extensionURL; 124 125 Bundle[] hosts = OSGIUtils.getDefault().getHosts(bundle); 127 if (hosts == null) 128 return null; 130 if (isSingleton(hosts[0])) 131 return extensionURL; 132 133 if (report) { 134 String message = NLS.bind(RegistryMessages.parse_nonSingleton, hosts[0].getLocation()); 135 RuntimeLog.log(new Status(IStatus.INFO, RegistryMessages.OWNER_NAME, 0, message, null)); 136 } 137 return null; 138 } 139 140 private void addBundle(Bundle bundle) { 141 String contributorId = Long.toString(bundle.getBundleId()); 142 if (registry.hasContribution(contributorId)) 145 return; 146 URL pluginManifest = getExtensionURL(bundle, registry.debug()); 147 if (pluginManifest == null) 148 return; 149 InputStream is; 150 try { 151 is = new BufferedInputStream(pluginManifest.openStream()); 152 } catch (IOException ex) { 153 is = null; 154 } 155 if (is == null) 156 return; 157 158 ResourceBundle translationBundle = null; 159 try { 160 translationBundle = ResourceTranslator.getResourceBundle(bundle); 161 } catch (MissingResourceException e) { 162 } 164 long timestamp = 0; 165 if (strategy.checkContributionsTimestamp()) 166 timestamp = strategy.getExtendedTimestamp(bundle, pluginManifest); 167 IContributor contributor = ContributorFactoryOSGi.createContributor(bundle); 168 registry.addContribution(is, contributor, true, pluginManifest.getPath(), translationBundle, token, timestamp); 169 170 } 177 178 private static boolean isSingleton(Bundle bundle) { 179 Dictionary allHeaders = bundle.getHeaders(""); String symbolicNameHeader = (String ) allHeaders.get(Constants.BUNDLE_SYMBOLICNAME); 181 try { 182 if (symbolicNameHeader != null) { 183 ManifestElement[] symbolicNameElements = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, symbolicNameHeader); 184 if (symbolicNameElements.length > 0) { 185 String singleton = symbolicNameElements[0].getDirective(Constants.SINGLETON_DIRECTIVE); 186 if (singleton == null) 187 singleton = symbolicNameElements[0].getAttribute(Constants.SINGLETON_DIRECTIVE); 188 189 if (!"true".equalsIgnoreCase(singleton)) { String manifestVersion = (String ) allHeaders.get(org.osgi.framework.Constants.BUNDLE_MANIFESTVERSION); 191 if (manifestVersion == null) { if (OSGIUtils.getDefault().getBundle(symbolicNameElements[0].getValue()) == bundle) 194 return true; 195 } 196 return false; 197 } 198 } 199 } 200 } catch (BundleException e1) { 201 } 203 return true; 204 } 205 } 206 | Popular Tags |