1 11 12 package org.eclipse.core.internal.plugins; 13 14 import java.io.IOException ; 15 import java.lang.reflect.Constructor ; 16 import java.net.URL ; 17 import java.util.*; 18 import org.eclipse.core.internal.boot.PlatformURLHandler; 19 import org.eclipse.core.internal.runtime.*; 20 import org.eclipse.core.internal.runtime.InternalPlatform; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.osgi.service.resolver.BundleDescription; 23 import org.eclipse.osgi.service.resolver.BundleSpecification; 24 import org.eclipse.osgi.util.ManifestElement; 25 import org.eclipse.osgi.util.NLS; 26 import org.osgi.framework.*; 27 28 29 32 public class PluginDescriptor implements IPluginDescriptor { 33 34 private static final String PLUGIN_CLASS = "Plugin-Class"; protected Plugin pluginObject = null; private Bundle bundleOsgi; 37 38 private boolean active = false; private volatile boolean activePending = false; private boolean deactivated = false; private ResourceBundle resources = null; 43 private PluginClassLoader classLoader; 44 45 static final String PLUGIN_URL = PlatformURLHandler.PROTOCOL + PlatformURLHandler.PROTOCOL_SEPARATOR + "/" + PlatformURLPluginConnection.PLUGIN + "/"; static final String VERSION_SEPARATOR = "_"; 49 synchronized public void doPluginDeactivation() { 50 pluginObject = null; 51 active = false; 52 activePending = false; 53 deactivated = false; 54 } 55 56 59 public IExtension getExtension(String id) { 60 IExtension[] exts = getExtensions(); 61 for (int i = 0; i < exts.length; i++) { 62 if (exts[i].getSimpleIdentifier().equals(id)) 63 return exts[i]; 64 } 65 return null; 66 } 67 68 71 public IExtensionPoint getExtensionPoint(String extensionPointId) { 72 return InternalPlatform.getDefault().getRegistry().getExtensionPoint(getId(), extensionPointId); 73 } 74 75 78 public IExtensionPoint[] getExtensionPoints() { 79 return InternalPlatform.getDefault().getRegistry().getExtensionPoints(getId()); 80 } 81 82 85 public IExtension[] getExtensions() { 86 return org.eclipse.core.internal.runtime.InternalPlatform.getDefault().getRegistry().getExtensions(getId()); 87 } 88 89 92 public URL getInstallURL() { 93 try { 94 return new URL (PLUGIN_URL + toString() + '/'); 95 } catch (IOException e) { 96 throw new IllegalStateException (); } 98 } 99 100 103 public String getLabel() { 104 return (String ) bundleOsgi.getHeaders().get(Constants.BUNDLE_NAME); 105 } 106 107 110 public ClassLoader getPluginClassLoader() { 111 synchronized (this) { 112 if (classLoader == null) 113 classLoader = new PluginClassLoader(this); 114 } 115 return classLoader; 116 } 117 118 public PluginRegistry getPluginRegistry() { 119 return (PluginRegistry) org.eclipse.core.internal.plugins.InternalPlatform.getPluginRegistry(); 120 } 121 122 125 public String getProviderName() { 126 return (String ) bundleOsgi.getHeaders().get(Constants.BUNDLE_VENDOR); 127 } 128 129 132 public ResourceBundle getResourceBundle() throws MissingResourceException { 133 if (resources==null) 134 resources = ResourceTranslator.getResourceBundle(bundleOsgi); 135 return resources; 136 } 137 138 141 public String getResourceString(String value) { 142 return ResourceTranslator.getResourceString(bundleOsgi, value); 143 } 144 145 148 public String getResourceString(String value, ResourceBundle b) { 149 return ResourceTranslator.getResourceString(bundleOsgi, value, b); 150 } 151 152 155 public ILibrary[] getRuntimeLibraries() { 156 Bundle[] allBundles; 157 Bundle[] fragments = InternalPlatform.getDefault().getFragments(bundleOsgi); 158 if (fragments != null) { 159 allBundles = new Bundle[fragments.length + 1]; 160 allBundles[0] = bundleOsgi; 161 System.arraycopy(fragments, 0, allBundles, 1, fragments.length); 162 } else 163 allBundles = new Bundle[] {bundleOsgi}; 164 ArrayList allLibraries = new ArrayList(); 165 boolean addedDot = false; 167 for (int i = 0; i < allBundles.length; i++) 168 try { 169 ManifestElement[] classpathElements = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, (String ) allBundles[i].getHeaders("").get(Constants.BUNDLE_CLASSPATH)); if (classpathElements == null) { 173 if (addedDot) 174 continue; 175 allLibraries.add(new Library(".")); addedDot = true; 177 } else 178 for (int j = 0; j < classpathElements.length; j++) 179 allLibraries.add(new Library(classpathElements[j].getValue())); 180 } catch (BundleException e) { 181 } 183 return (ILibrary[]) allLibraries.toArray(new ILibrary[allLibraries.size()]); 184 } 185 186 189 public String getUniqueIdentifier() { 190 return getId(); 191 } 192 193 196 public static String getUniqueIdentifierFromString(String pluginString) { 197 int ix = pluginString.indexOf(VERSION_SEPARATOR); 198 return ix == -1 ? pluginString : pluginString.substring(0, ix); 199 } 200 201 204 public PluginVersionIdentifier getVersionIdentifier() { 205 String version = (String ) bundleOsgi.getHeaders("").get(Constants.BUNDLE_VERSION); try { 207 return new PluginVersionIdentifier(version); 208 } catch (Exception e) { 209 return new PluginVersionIdentifier("1.0.0"); } 211 } 212 213 216 public static PluginVersionIdentifier getVersionIdentifierFromString(String pluginString) { 217 return new PluginVersionIdentifier(pluginString); 218 } 219 220 public IPluginPrerequisite[] getPluginPrerequisites() { 221 BundleDescription description = Platform.getPlatformAdmin().getState(false).getBundle(bundleOsgi.getBundleId()); 222 BundleSpecification[] specs = description.getRequiredBundles(); 223 224 IPluginPrerequisite[] resolvedPrerequisites = new IPluginPrerequisite[specs.length]; 225 for (int j = 0; j < specs.length; j++) 226 resolvedPrerequisites[j] = new PluginPrerequisite(specs[j]); 227 return resolvedPrerequisites; 228 } 229 230 236 boolean hasActivationStarted() { 237 return activePending || active; 238 } 239 240 243 public synchronized boolean isPluginActivated() { 244 return bundleOsgi.getState() == Bundle.ACTIVE; 251 } 252 253 257 public boolean isPluginDeactivated() { 258 return deactivated; 259 } 260 261 private void logError(IStatus status) { 262 InternalPlatform.getDefault().getLog(org.eclipse.core.internal.runtime.InternalPlatform.getDefault().getBundleContext().getBundle()).log(status); 263 } 264 265 268 private boolean pluginActivationEnter() throws CoreException { 269 if (deactivated) { 270 String errorMsg = NLS.bind(Messages.plugin_pluginDisabled, getId()); 272 throwException(errorMsg, null); 273 } 274 if (active || activePending) { 275 return false; 277 } 278 activePending = true; 279 return true; 281 } 282 283 private void pluginActivationExit(boolean errorExit) { 284 if (errorExit) { 285 active = false; 286 deactivated = true; 287 } else 288 active = true; 289 activePending = false; 291 } 292 293 private void throwException(String message, Throwable exception) throws CoreException { 294 IStatus status = new Status(IStatus.ERROR, Platform.PI_RUNTIME, Platform.PLUGIN_ERROR, message, exception); 295 logError(status); 296 throw new CoreException(status); 297 } 298 299 303 public String toString() { 304 return getUniqueIdentifier() + VERSION_SEPARATOR + getVersionIdentifier().toString(); 305 } 306 307 310 public final URL find(IPath path) { 311 URL result = FindSupport.find(bundleOsgi, path); 312 if (result != null) 313 try { 314 result = Platform.resolve(result); 315 } catch (IOException e) { 316 } 318 return result; 319 } 320 321 324 public final URL find(IPath path, Map override) { 325 URL result = FindSupport.find(bundleOsgi, path, override); 326 if (result != null) 327 try { 328 result = Platform.resolve(result); 329 } catch (IOException e) { 330 } 332 return result; 333 } 334 335 338 public Plugin getPlugin() throws CoreException { 339 if (pluginObject == null) 340 doPluginActivation(); 341 return pluginObject; 342 } 343 344 synchronized void doPluginActivation() throws CoreException { 345 350 355 if ((bundleOsgi.getState() & (Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE)) == 0) 357 throw new IllegalArgumentException (); 358 try { 359 InternalPlatform.start(bundleOsgi); 361 } catch (BundleException e) { 362 throwException(NLS.bind(Messages.plugin_startupProblems, e), e); 363 } 364 if (pluginObject != null) 365 return; 366 boolean errorExit = true; 367 if (pluginActivationEnter()) { 369 try { 370 internalDoPluginActivation(); 371 errorExit = false; 372 } finally { 373 pluginActivationExit(errorExit); 374 } 375 } else { 376 if (active && pluginObject == null) { 378 active = false; 379 pluginObject = new DefaultPlugin(this); 380 active = true; 381 } 382 } 383 384 } 385 386 private String getPluginClass() { 387 return (String ) bundleOsgi.getHeaders("").get(PLUGIN_CLASS); } 389 390 private String getId() { 391 return bundleOsgi.getSymbolicName(); 392 } 393 394 private void internalDoPluginActivation() throws CoreException { 395 String errorMsg; 396 String pluginClassName = getPluginClass(); 398 Class runtimeClass = null; 399 try { 400 if (pluginClassName == null || pluginClassName.equals("")) { runtimeClass = DefaultPlugin.class; 402 pluginClassName = DefaultPlugin.class.getName(); 403 } 404 else 405 runtimeClass = bundleOsgi.loadClass(pluginClassName); 406 } catch (ClassNotFoundException e) { 407 errorMsg = NLS.bind(Messages.plugin_loadClassError, getId(), pluginClassName); 408 throwException(errorMsg, e); 409 } 410 411 Constructor construct = null; 413 try { 414 construct = runtimeClass.getConstructor(new Class [] {IPluginDescriptor.class}); 415 } catch (NoSuchMethodException eNoConstructor) { 416 errorMsg = NLS.bind(Messages.plugin_instantiateClassError, getId(), pluginClassName); 417 throwException(errorMsg, eNoConstructor); 418 } 419 420 try { 422 pluginObject = (Plugin) construct.newInstance(new Object [] {this}); 423 } catch (ClassCastException e) { 424 errorMsg = NLS.bind(Messages.plugin_notPluginClass, pluginClassName); 425 throwException(errorMsg, e); 426 } catch (Exception e) { 427 errorMsg = NLS.bind(Messages.plugin_instantiateClassError, getId(), pluginClassName); 428 throwException(errorMsg, e); 429 } 430 } 431 432 public PluginDescriptor(org.osgi.framework.Bundle b) { 433 bundleOsgi = b; 434 if ((b.getState() & Bundle.ACTIVE) != 0) 435 active = true; 436 } 437 438 public Bundle getBundle() { 439 return bundleOsgi; 440 } 441 442 public void setPlugin(Plugin object) { 443 pluginObject = object; 444 } 445 446 public synchronized void setActive() { 447 this.active = true; 448 } 449 450 public boolean hasPluginObject() { 451 return pluginObject!=null; 452 } 453 454 public void markAsDeactivated() { 455 deactivated = true; 456 } 457 } 458 | Popular Tags |