1 11 package org.eclipse.core.internal.registry; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.*; 17 import javax.xml.parsers.ParserConfigurationException ; 18 import javax.xml.parsers.SAXParserFactory ; 19 import org.eclipse.core.internal.runtime.*; 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 import org.osgi.util.tracker.ServiceTracker; 25 import org.xml.sax.InputSource ; 26 import org.xml.sax.SAXException ; 27 28 37 public class EclipseBundleListener implements SynchronousBundleListener { 38 private static final String PLUGIN_MANIFEST = "plugin.xml"; private static final String FRAGMENT_MANIFEST = "fragment.xml"; 41 private ExtensionRegistry registry; 42 private ServiceTracker xmlTracker; 43 44 public EclipseBundleListener(ExtensionRegistry registry) { 45 this.registry = registry; 46 xmlTracker = new ServiceTracker(InternalPlatform.getDefault().getBundleContext(), SAXParserFactory .class.getName(), null); 47 xmlTracker.open(); 48 } 49 50 public void bundleChanged(BundleEvent event) { 51 68 Bundle bundle = event.getBundle(); 69 switch (event.getType()) { 70 case BundleEvent.RESOLVED : 71 addBundle(bundle); 72 break; 73 case BundleEvent.UNRESOLVED : 74 removeBundle(bundle); 75 break; 76 } 77 } 78 79 public void processBundles(Bundle[] bundles) { 80 for (int i = 0; i < bundles.length; i++) { 81 if (isBundleResolved(bundles[i])) 82 addBundle(bundles[i]); 83 else 84 removeBundle(bundles[i]); 85 } 86 } 87 88 private boolean isBundleResolved(Bundle bundle) { 89 return (bundle.getState() & (Bundle.RESOLVED | Bundle.ACTIVE | Bundle.STARTING | Bundle.STOPPING)) != 0; 90 } 91 92 private void removeBundle(Bundle bundle) { 93 registry.remove(bundle.getBundleId()); 94 } 95 96 private void addBundle(Bundle bundle) { 97 if (registry.hasNamespace(bundle.getBundleId())) 100 return; 101 Contribution bundleModel = getBundleModel(bundle); 102 if (bundleModel == null) 103 return; 104 if (Platform.PI_RUNTIME.equals(bundleModel.getNamespace())) 107 Messages.reloadMessages(); 108 registry.add(bundleModel); 111 } 112 113 private boolean isSingleton(Bundle bundle) { 114 Dictionary allHeaders = bundle.getHeaders(""); String symbolicNameHeader = (String ) allHeaders.get(Constants.BUNDLE_SYMBOLICNAME); try { 117 if (symbolicNameHeader != null) { 118 ManifestElement[] symbolicNameElements = ManifestElement.parseHeader(Constants.BUNDLE_SYMBOLICNAME, symbolicNameHeader); 119 if (symbolicNameElements.length > 0) { 120 String singleton = symbolicNameElements[0].getDirective(Constants.SINGLETON_DIRECTIVE); 121 if (singleton == null) 122 singleton = symbolicNameElements[0].getAttribute(Constants.SINGLETON_DIRECTIVE); 123 124 if (!"true".equalsIgnoreCase(singleton)) { int status = IStatus.INFO; 126 String manifestVersion = (String ) allHeaders.get(org.osgi.framework.Constants.BUNDLE_MANIFESTVERSION); 127 if (manifestVersion == null) { if (InternalPlatform.getDefault().getBundle(symbolicNameElements[0].getValue()) == bundle) { 130 return true; 131 } 132 status = IStatus.ERROR; 133 } 134 if (InternalPlatform.DEBUG_REGISTRY || status == IStatus.ERROR) { 135 String message = NLS.bind(Messages.parse_nonSingleton, bundle.getLocation()); 136 InternalPlatform.getDefault().log(new Status(status, Platform.PI_RUNTIME, 0, message, null)); 137 } 138 return false; 139 } 140 } 141 } 142 } catch (BundleException e1) { 143 } 145 return true; 146 } 147 148 151 private Contribution getBundleModel(Bundle bundle) { 152 if (bundle.getBundleId() == 0) 154 return null; 155 if (bundle.getSymbolicName() == null) 157 return null; 158 159 if (!isSingleton(bundle)) 161 return null; 162 163 boolean isFragment = InternalPlatform.getDefault().isFragment(bundle); 164 165 if (isFragment) { 167 Bundle[] hosts = InternalPlatform.getDefault().getHosts(bundle); 168 if (hosts != null && isSingleton(hosts[0]) == false) 169 return null; 170 } 171 172 InputStream is = null; 173 String manifestType = null; 174 String manifestName = isFragment ? FRAGMENT_MANIFEST : PLUGIN_MANIFEST; 175 try { 176 URL url = bundle.getEntry(manifestName); 177 if (url != null) { 178 is = url.openStream(); 179 manifestType = isFragment ? ExtensionsParser.FRAGMENT : ExtensionsParser.PLUGIN; 180 } 181 } catch (IOException ex) { 182 is = null; 183 } 184 if (is == null) 185 return null; 186 try { 187 String message = NLS.bind(Messages.parse_problems, bundle.getLocation()); 188 MultiStatus problems = new MultiStatus(Platform.PI_RUNTIME, ExtensionsParser.PARSE_PROBLEM, message, null); ResourceBundle b = null; 190 try { 191 b = ResourceTranslator.getResourceBundle(bundle); 192 } catch (MissingResourceException e) { 193 } 195 ExtensionsParser parser = new ExtensionsParser(problems); 196 Contribution bundleModel = new Contribution(bundle); 197 parser.parseManifest(xmlTracker, new InputSource (is), manifestType, manifestName, registry.getObjectManager(), bundleModel, b); 198 if (problems.getSeverity() != IStatus.OK) 199 InternalPlatform.getDefault().log(problems); 200 return bundleModel; 201 } catch (ParserConfigurationException e) { 202 logParsingError(bundle, e); 203 return null; 204 } catch (SAXException e) { 205 logParsingError(bundle, e); 206 return null; 207 } catch (IOException e) { 208 logParsingError(bundle, e); 209 return null; 210 } finally { 211 try { 212 is.close(); 213 } catch (IOException ioe) { 214 } 216 } 217 } 218 219 private void logParsingError(Bundle bundle, Exception e) { 220 String message = NLS.bind(Messages.parse_failedParsingManifest, bundle.getLocation()); 221 InternalPlatform.getDefault().log(new Status(IStatus.ERROR, Platform.PI_RUNTIME, 0, message, e)); 222 } 223 } 224 | Popular Tags |