1 11 package org.eclipse.core.runtime; 12 13 import java.io.*; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.lang.reflect.Method ; 16 import java.net.URL ; 17 import java.util.Map ; 18 import org.eclipse.core.internal.runtime.*; 19 import org.eclipse.osgi.util.NLS; 20 import org.osgi.framework.*; 21 22 107 public abstract class Plugin implements BundleActivator { 108 109 118 public static final String PLUGIN_PREFERENCE_SCOPE = "instance"; 120 123 private Bundle bundle; 124 125 130 private boolean debug = false; 131 132 135 private IPluginDescriptor descriptor; 136 137 144 public static final String PREFERENCES_DEFAULT_OVERRIDE_BASE_NAME = "preferences"; 146 157 public static final String PREFERENCES_DEFAULT_OVERRIDE_FILE_NAME = PREFERENCES_DEFAULT_OVERRIDE_BASE_NAME + ".ini"; 159 165 private Preferences preferences = null; 166 167 186 public Plugin() { 187 super(); 188 } 189 190 212 public Plugin(IPluginDescriptor descriptor) { 213 Assert.isNotNull(descriptor); 214 Assert.isTrue(!CompatibilityHelper.hasPluginObject(descriptor), NLS.bind(Messages.plugin_deactivatedLoad, this.getClass().getName(), descriptor.getUniqueIdentifier() + " is not activated")); this.descriptor = descriptor; 216 217 bundle = InternalPlatform.getDefault().getBundle(descriptor.getUniqueIdentifier()); 219 try { 220 InternalPlatform.start(bundle); 221 } catch (BundleException e) { 222 String message = NLS.bind(Messages.plugin_startupProblems, descriptor.getUniqueIdentifier()); 223 IStatus status = new Status(IStatus.ERROR, Platform.PI_RUNTIME, IStatus.ERROR, message, e); 224 InternalPlatform.getDefault().log(status); 225 } 226 } 227 228 236 public final URL find(IPath path) { 237 return FileLocator.find(bundle, path, null); 238 } 239 240 254 public final URL find(IPath path, Map override) { 255 return FileLocator.find(bundle, path, override); 256 } 257 258 268 public final IPluginDescriptor getDescriptor() { 269 if (descriptor != null) 270 return descriptor; 271 272 return initializeDescriptor(bundle.getSymbolicName()); 273 } 274 275 281 public final ILog getLog() { 282 return InternalPlatform.getDefault().getLog(bundle); 283 } 284 285 303 public final IPath getStateLocation() throws IllegalStateException { 304 return InternalPlatform.getDefault().getStateLocation(bundle, true); 305 } 306 307 336 public final Preferences getPluginPreferences() { 337 if (preferences != null) { 338 if (InternalPlatform.DEBUG_PLUGIN_PREFERENCES) 339 InternalPlatform.message("Plugin preferences already loaded for: " + bundle.getSymbolicName()); return preferences; 341 } 342 343 if (InternalPlatform.DEBUG_PLUGIN_PREFERENCES) 344 InternalPlatform.message("Loading preferences for plugin: " + bundle.getSymbolicName()); 346 final Bundle bundleCopy = bundle; 350 final Preferences[] preferencesCopy = new Preferences[1]; 351 Runnable innerCall = new Runnable () { 352 public void run() { 353 preferencesCopy[0] = new org.eclipse.core.internal.preferences.legacy.PreferenceForwarder(this, bundleCopy.getSymbolicName()); 354 } 355 }; 356 innerCall.run(); 357 preferences = preferencesCopy[0]; 358 return preferences; 359 } 360 361 373 public final void savePluginPreferences() { 374 getPluginPreferences(); 378 379 final Preferences preferencesCopy = preferences; 383 Runnable innerCall = new Runnable () { 384 public void run() { 385 try { 386 ((org.eclipse.core.internal.preferences.legacy.PreferenceForwarder) preferencesCopy).flush(); 387 } catch (org.osgi.service.prefs.BackingStoreException e) { 388 IStatus status = new Status(IStatus.ERROR, Platform.PI_RUNTIME, IStatus.ERROR, Messages.preferences_saveProblems, e); 389 InternalPlatform.getDefault().log(status); 390 } 391 } 392 }; 393 innerCall.run(); 394 } 395 396 437 protected void initializeDefaultPluginPreferences() { 438 } 440 441 448 public final void internalInitializeDefaultPluginPreferences() { 449 initializeDefaultPluginPreferences(); 450 } 451 452 465 public boolean isDebugging() { 466 return debug; 467 } 468 469 480 public final InputStream openStream(IPath file) throws IOException { 481 return FileLocator.openStream(bundle, file, false); 482 } 483 484 502 public final InputStream openStream(IPath file, boolean substituteArgs) throws IOException { 503 return FileLocator.openStream(bundle, file, substituteArgs); 504 } 505 506 519 public void setDebugging(boolean value) { 520 debug = value; 521 } 522 523 558 public void shutdown() throws CoreException { 559 if (CompatibilityHelper.initializeCompatibility() == null) 560 return; 561 Throwable exception = null; 562 Method m; 563 try { 564 m = descriptor.getClass().getMethod("doPluginDeactivation", new Class [0]); m.invoke(descriptor, null); 566 } catch (SecurityException e) { 567 exception = e; 568 } catch (NoSuchMethodException e) { 569 exception = e; 570 } catch (IllegalArgumentException e) { 571 exception = e; 572 } catch (IllegalAccessException e) { 573 exception = e; 574 } catch (InvocationTargetException e) { 575 exception = e; 576 } 577 if (exception == null) 578 return; 579 String message = NLS.bind(Messages.plugin_shutdownProblems, descriptor.getUniqueIdentifier()); 580 IStatus status = new Status(IStatus.ERROR, Platform.PI_RUNTIME, IStatus.ERROR, message, exception); 581 InternalPlatform.getDefault().log(status); 582 } 583 584 628 public void startup() throws CoreException { 629 } 630 631 635 public String toString() { 636 String name = bundle.getSymbolicName(); 637 return name == null ? new Long (bundle.getBundleId()).toString() : name; 638 } 639 640 686 public void start(BundleContext context) throws Exception { 687 bundle = context.getBundle(); 688 689 String symbolicName = bundle.getSymbolicName(); 690 if (symbolicName != null) { 691 String key = symbolicName + "/debug"; String value = InternalPlatform.getDefault().getOption(key); 693 this.debug = value == null ? false : value.equalsIgnoreCase("true"); } 695 696 initializeDescriptor(symbolicName); 697 } 698 699 702 private IPluginDescriptor initializeDescriptor(String symbolicName) { 703 if (CompatibilityHelper.initializeCompatibility() == null) 704 return null; 705 706 if (symbolicName == null) 708 return null; 709 710 IPluginDescriptor tmp = CompatibilityHelper.getPluginDescriptor(symbolicName); 711 712 if (!symbolicName.equals(Platform.PI_RUNTIME)) 714 descriptor = tmp; 715 716 CompatibilityHelper.setPlugin(tmp, this); 717 CompatibilityHelper.setActive(tmp); 718 return tmp; 719 } 720 721 757 public void stop(BundleContext context) throws Exception { 758 } 760 761 767 public final Bundle getBundle() { 768 return bundle; 769 } 770 } 771 | Popular Tags |