1 12 package org.eclipse.core.internal.runtime; 13 14 import java.io.*; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.*; 18 import org.eclipse.core.internal.preferences.exchange.ILegacyPreferences; 19 import org.eclipse.core.internal.preferences.exchange.IProductPreferencesService; 20 import org.eclipse.core.internal.preferences.legacy.InitLegacyPreferences; 21 import org.eclipse.core.internal.preferences.legacy.ProductPreferencesService; 22 import org.eclipse.core.internal.runtime.auth.AuthorizationHandler; 23 import org.eclipse.core.runtime.*; 24 import org.eclipse.core.runtime.content.IContentTypeManager; 25 import org.eclipse.core.runtime.preferences.IPreferencesService; 26 import org.eclipse.equinox.app.IApplicationContext; 27 import org.eclipse.equinox.internal.app.*; 28 import org.eclipse.equinox.internal.app.Activator; 29 import org.eclipse.osgi.framework.log.FrameworkLog; 30 import org.eclipse.osgi.service.datalocation.Location; 31 import org.eclipse.osgi.service.debug.DebugOptions; 32 import org.eclipse.osgi.service.environment.EnvironmentInfo; 33 import org.eclipse.osgi.service.resolver.PlatformAdmin; 34 import org.osgi.framework.*; 35 import org.osgi.service.packageadmin.PackageAdmin; 36 import org.osgi.util.tracker.ServiceTracker; 37 38 42 public final class InternalPlatform { 43 44 private static final String [] ARCH_LIST = {Platform.ARCH_PA_RISC, Platform.ARCH_PPC, Platform.ARCH_SPARC, Platform.ARCH_X86, Platform.ARCH_AMD64, Platform.ARCH_IA64, Platform.ARCH_IA64_32}; 51 52 public static boolean DEBUG = false; 54 public static boolean DEBUG_PLUGIN_PREFERENCES = false; 55 56 static boolean splashEnded = false; 57 private static boolean initialized; 58 private static final String KEYRING = "-keyring"; private static String keyringFile; 60 61 private static Map logs = new HashMap(5); 63 64 private static final String [] OS_LIST = {Platform.OS_AIX, Platform.OS_HPUX, Platform.OS_LINUX, Platform.OS_MACOSX, Platform.OS_QNX, Platform.OS_SOLARIS, Platform.OS_WIN32}; 65 private static String password = ""; private static final String PASSWORD = "-password"; private static PlatformLogWriter platformLog = null; 68 69 private static final String PLUGIN_PATH = ".plugin-path"; 71 public static final String PROP_APPLICATION = "eclipse.application"; public static final String PROP_ARCH = "osgi.arch"; public static final String PROP_CONFIG_AREA = "osgi.configuration.area"; public static final String PROP_CONSOLE_LOG = "eclipse.consoleLog"; public static final String PROP_DEBUG = "osgi.debug"; public static final String PROP_DEV = "osgi.dev"; 78 public static final String PROP_INSTALL_AREA = "osgi.install.area"; public static final String PROP_NL = "osgi.nl"; public static final String PROP_OS = "osgi.os"; 83 public static final String PROP_PRODUCT = "eclipse.product"; public static final String PROP_WS = "osgi.ws"; public static final String PROP_ACTIVATE_PLUGINS = "eclipse.activateRuntimePlugins"; 88 private static final InternalPlatform singleton = new InternalPlatform(); 89 90 private static final String [] WS_LIST = {Platform.WS_CARBON, Platform.WS_GTK, Platform.WS_MOTIF, Platform.WS_PHOTON, Platform.WS_WIN32, Platform.WS_WPF}; 91 private Path cachedInstanceLocation; private ServiceTracker configurationLocation = null; 93 private BundleContext context; 94 95 private Map groupProviders = new HashMap(3); 96 private ServiceTracker installLocation = null; 97 private ServiceTracker instanceLocation = null; 98 private AdapterManagerListener adapterManagerListener = null; 99 100 private Plugin runtimeInstance; 102 private ServiceRegistration legacyPreferencesService = null; 103 private ServiceRegistration customPreferencesService = null; 104 105 private ServiceTracker environmentTracker = null; 106 private ServiceTracker logTracker = null; 107 private ServiceTracker bundleTracker = null; 108 private ServiceTracker debugTracker = null; 109 private ServiceTracker contentTracker = null; 110 private ServiceTracker preferencesTracker = null; 111 private ServiceTracker userLocation = null; 112 private ServiceTracker groupProviderTracker = null; 113 114 private IProduct product; 115 116 public static InternalPlatform getDefault() { 117 return singleton; 118 } 119 120 123 private InternalPlatform() { 124 super(); 125 } 126 127 130 public void addLogListener(ILogListener listener) { 131 assertInitialized(); 132 RuntimeLog.addLogListener(listener); 133 } 134 135 private void assertInitialized() { 136 if (!initialized) 138 Assert.isTrue(false, Messages.meta_appNotInit); 139 } 140 141 144 public void endSplash() { 145 synchronized (this) { 146 if (splashEnded) 147 return; splashEnded = true; 149 } 150 IApplicationContext applicationContext = getApplicationContext(); 151 if (applicationContext != null) 152 applicationContext.applicationRunning(); 153 } 154 155 158 public IAdapterManager getAdapterManager() { 159 assertInitialized(); 160 return AdapterManager.getDefault(); 161 } 162 163 public String [] getApplicationArgs() { 164 return CommandLineArgs.getApplicationArgs(); 165 } 166 167 public boolean getBooleanOption(String option, boolean defaultValue) { 168 String value = getOption(option); 169 if (value == null) 170 return defaultValue; 171 return value.equalsIgnoreCase("true"); } 173 174 public Bundle getBundle(String symbolicName) { 175 PackageAdmin packageAdmin = getBundleAdmin(); 176 if (packageAdmin == null) 177 return null; 178 Bundle[] bundles = packageAdmin.getBundles(symbolicName, null); 179 if (bundles == null) 180 return null; 181 for (int i = 0; i < bundles.length; i++) { 183 if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) { 184 return bundles[i]; 185 } 186 } 187 return null; 188 } 189 190 public BundleContext getBundleContext() { 191 return context; 192 } 193 194 198 public String getBundleId(Object object) { 199 if (object == null) 200 return null; 201 PackageAdmin packageAdmin = getBundleAdmin(); 202 if (packageAdmin == null) 203 return null; 204 Bundle source = packageAdmin.getBundle(object.getClass()); 205 if (source != null && source.getSymbolicName() != null) 206 return source.getSymbolicName(); 207 return null; 208 } 209 210 public IBundleGroupProvider[] getBundleGroupProviders() { 211 if (groupProviderTracker == null) { 212 Filter filter = null; 214 try { 215 filter = getBundleContext().createFilter("(objectClass=" + IBundleGroupProvider.class.getName() + ")"); } catch (InvalidSyntaxException e) { 217 } 219 groupProviderTracker = new ServiceTracker(getBundleContext(), filter, null); 220 groupProviderTracker.open(); 221 } 222 Object [] objectArray = groupProviderTracker.getServices(); 223 if (objectArray == null) return new IBundleGroupProvider[0]; 225 IBundleGroupProvider[] result = new IBundleGroupProvider[objectArray.length]; 226 System.arraycopy(objectArray, 0, result, 0, objectArray.length); 227 return result; 228 } 229 230 public void registerBundleGroupProvider(IBundleGroupProvider provider) { 231 ServiceRegistration registration = getBundleContext().registerService(IBundleGroupProvider.class.getName(), provider, null); 233 synchronized (groupProviders) { 235 groupProviders.put(provider, registration); 236 } 237 } 238 239 public void unregisterBundleGroupProvider(IBundleGroupProvider provider) { 240 ServiceRegistration registration; 242 synchronized (groupProviders) { 243 registration = (ServiceRegistration) groupProviders.remove(provider); 244 } 245 if (registration == null) 246 return; 247 registration.unregister(); 249 } 250 251 public Bundle[] getBundles(String symbolicName, String version) { 252 PackageAdmin packageAdmin = getBundleAdmin(); 253 if (packageAdmin == null) 254 return null; 255 Bundle[] bundles = packageAdmin.getBundles(symbolicName, version); 256 if (bundles == null) 257 return null; 258 if (bundles.length == 1 && (bundles[0].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) 260 return bundles; 261 Bundle[] selectedBundles = new Bundle[bundles.length]; 263 int added = 0; 264 for (int i = 0; i < bundles.length; i++) { 265 if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) { 266 selectedBundles[added++] = bundles[i]; 267 } 268 } 269 if (added == 0) 270 return null; 271 272 Bundle[] results = new Bundle[added]; 274 System.arraycopy(selectedBundles, 0, results, 0, added); 275 return results; 276 } 277 278 public String [] getCommandLineArgs() { 279 return CommandLineArgs.getAllArgs(); 280 } 281 282 public Location getConfigurationLocation() { 283 assertInitialized(); 284 if (configurationLocation == null) { 285 Filter filter = null; 286 try { 287 filter = context.createFilter(Location.CONFIGURATION_FILTER); 288 } catch (InvalidSyntaxException e) { 289 } 291 configurationLocation = new ServiceTracker(context, filter, null); 292 configurationLocation.open(); 293 } 294 return (Location) configurationLocation.getService(); 295 } 296 297 300 public IContentTypeManager getContentTypeManager() { 301 if (contentTracker == null) { 302 if (context == null) 303 return null; 304 contentTracker = new ServiceTracker(context, IContentTypeManager.class.getName(), null); 305 contentTracker.open(); 306 } 307 return (IContentTypeManager) contentTracker.getService(); 308 } 309 310 public EnvironmentInfo getEnvironmentInfoService() { 311 if (environmentTracker == null) { 312 if (context == null) 313 return null; 314 environmentTracker = new ServiceTracker(context, EnvironmentInfo.class.getName(), null); 315 environmentTracker.open(); 316 } 317 return (EnvironmentInfo) environmentTracker.getService(); 318 } 319 320 public Bundle[] getFragments(Bundle bundle) { 321 PackageAdmin packageAdmin = getBundleAdmin(); 322 if (packageAdmin == null) 323 return null; 324 return packageAdmin.getFragments(bundle); 325 } 326 327 public FrameworkLog getFrameworkLog() { 328 if (logTracker == null) { 329 if (context == null) 330 return null; 331 logTracker = new ServiceTracker(context, FrameworkLog.class.getName(), null); 332 logTracker.open(); 333 } 334 return (FrameworkLog) logTracker.getService(); 335 } 336 337 public Bundle[] getHosts(Bundle bundle) { 338 PackageAdmin packageAdmin = getBundleAdmin(); 339 if (packageAdmin == null) 340 return null; 341 return packageAdmin.getHosts(bundle); 342 } 343 344 public Location getInstallLocation() { 345 assertInitialized(); 346 Filter filter = null; 347 if (installLocation == null) { 348 try { 349 filter = context.createFilter(Location.INSTALL_FILTER); 350 } catch (InvalidSyntaxException e) { 351 } 353 installLocation = new ServiceTracker(context, filter, null); 354 installLocation.open(); 355 } 356 return (Location) installLocation.getService(); 357 } 358 359 public URL getInstallURL() { 360 Location location = getInstallLocation(); 361 if (location == null) 364 throw new IllegalStateException ("The installation location must not be null"); return location.getURL(); 366 } 367 368 public Location getInstanceLocation() { 369 assertInitialized(); 370 if (instanceLocation == null) { 371 Filter filter = null; 372 try { 373 filter = context.createFilter(Location.INSTANCE_FILTER); 374 } catch (InvalidSyntaxException e) { 375 } 377 instanceLocation = new ServiceTracker(context, filter, null); 378 instanceLocation.open(); 379 } 380 return (Location) instanceLocation.getService(); 381 } 382 383 386 public IPath getLocation() throws IllegalStateException { 387 if (cachedInstanceLocation == null) { 388 Location location = getInstanceLocation(); 389 if (location == null) 390 return null; 391 File file = new File(location.getURL().getFile()); 393 cachedInstanceLocation = new Path(file.toString()); 394 } 395 return cachedInstanceLocation; 396 } 397 398 401 public ILog getLog(Bundle bundle) { 402 ILog result = (ILog) logs.get(bundle); 403 if (result != null) 404 return result; 405 result = new Log(bundle); 406 logs.put(bundle, result); 407 return result; 408 } 409 410 414 public DataArea getMetaArea() { 415 return MetaDataKeeper.getMetaArea(); 417 } 418 419 public String getNL() { 420 return getBundleContext().getProperty(PROP_NL); 421 } 422 423 426 public String getOption(String option) { 427 DebugOptions options = getDebugOptions(); 428 if (options != null) 429 return options.getOption(option); 430 return null; 431 } 432 433 public String getOS() { 434 return getBundleContext().getProperty(PROP_OS); 435 } 436 437 public String getOSArch() { 438 return getBundleContext().getProperty(PROP_ARCH); 439 } 440 441 public PlatformAdmin getPlatformAdmin() { 442 if (context == null) 443 return null; 444 ServiceReference platformAdminReference = context.getServiceReference(PlatformAdmin.class.getName()); 445 if (platformAdminReference == null) 446 return null; 447 return (PlatformAdmin) context.getService(platformAdminReference); 448 } 449 450 456 public URL [] getPluginPath(URL pluginPathLocation 457 ) { 458 InputStream input = null; 459 if (pluginPathLocation == null) 461 return null; 462 try { 463 input = pluginPathLocation.openStream(); 464 } catch (IOException e) { 465 } 467 468 if (input == null) 471 try { 472 URL url = new URL ("platform:/base/" + PLUGIN_PATH); input = url.openStream(); 474 } catch (MalformedURLException e) { 475 } catch (IOException e) { 477 } 479 480 if (input == null) 482 return null; 483 URL [] result = null; 485 try { 486 try { 487 result = readPluginPath(input); 488 } finally { 489 input.close(); 490 } 491 } catch (IOException e) { 492 } 494 return result; 495 } 496 497 500 public IPreferencesService getPreferencesService() { 501 if (preferencesTracker == null) { 502 if (context == null) 503 return null; 504 preferencesTracker = new ServiceTracker(context, IPreferencesService.class.getName(), null); 505 preferencesTracker.open(); 506 } 507 return (IPreferencesService) preferencesTracker.getService(); 508 } 509 510 public IProduct getProduct() { 511 if (product != null) 512 return product; 513 EclipseAppContainer container = Activator.getContainer(); 514 IBranding branding = container == null ? null : container.getBranding(); 515 if (branding == null) 516 return null; 517 Object brandingProduct = branding.getProduct(); 518 if (!(brandingProduct instanceof IProduct)) 519 brandingProduct = new Product(branding); 520 product = (IProduct) brandingProduct; 521 return product; 522 } 523 524 public IExtensionRegistry getRegistry() { 525 return RegistryFactory.getRegistry(); 526 } 527 528 public ResourceBundle getResourceBundle(Bundle bundle) { 529 return ResourceTranslator.getResourceBundle(bundle); 530 } 531 532 public String getResourceString(Bundle bundle, String value) { 533 return ResourceTranslator.getResourceString(bundle, value); 534 } 535 536 public String getResourceString(Bundle bundle, String value, ResourceBundle resourceBundle) { 537 return ResourceTranslator.getResourceString(bundle, value, resourceBundle); 538 } 539 540 543 public Plugin getRuntimeInstance() { 544 return runtimeInstance; 545 } 546 547 private IApplicationContext getApplicationContext() { 548 ServiceReference[] ref; 549 try { 550 ref = context.getServiceReferences(IApplicationContext.class.getName(), "(eclipse.application.type=main.thread)"); } catch (InvalidSyntaxException e) { 552 return null; 553 } 554 if (ref == null || ref.length == 0) 555 return null; 556 IApplicationContext result = (IApplicationContext) context.getService(ref[0]); 558 if (result != null) { 559 context.ungetService(ref[0]); 560 return result; 561 } 562 return null; 563 } 564 565 public IPath getStateLocation(Bundle bundle) { 566 return getStateLocation(bundle, true); 567 } 568 569 public IPath getStateLocation(Bundle bundle, boolean create) throws IllegalStateException { 570 assertInitialized(); 571 IPath result = getMetaArea().getStateLocation(bundle); 572 if (create) 573 result.toFile().mkdirs(); 574 return result; 575 } 576 577 public long getStateTimeStamp() { 578 PlatformAdmin admin = getPlatformAdmin(); 579 return admin == null ? -1 : admin.getState(false).getTimeStamp(); 580 } 581 582 public Location getUserLocation() { 583 assertInitialized(); 584 if (userLocation == null) { 585 Filter filter = null; 586 try { 587 filter = context.createFilter(Location.USER_FILTER); 588 } catch (InvalidSyntaxException e) { 589 } 591 userLocation = new ServiceTracker(context, filter, null); 592 userLocation.open(); 593 } 594 return (Location) userLocation.getService(); 595 } 596 597 public String getWS() { 598 return getBundleContext().getProperty(PROP_WS); 599 } 600 601 private void initializeAuthorizationHandler() { 602 try { 603 AuthorizationHandler.setKeyringFile(keyringFile); 604 AuthorizationHandler.setPassword(password); 605 } catch (NoClassDefFoundError e) { 606 log(new Status(IStatus.WARNING, Platform.PI_RUNTIME, 0, Messages.auth_notAvailable, e)); 608 } 609 } 610 611 614 void initializeDebugFlags() { 615 DEBUG = getBooleanOption(Platform.PI_RUNTIME + "/debug", false); if (DEBUG) { 618 DEBUG_PLUGIN_PREFERENCES = getBooleanOption(Platform.PI_RUNTIME + "/preferences/plugin", false); } 620 } 621 622 public boolean isFragment(Bundle bundle) { 623 PackageAdmin packageAdmin = getBundleAdmin(); 624 if (packageAdmin == null) 625 return false; 626 return (packageAdmin.getBundleType(bundle) & PackageAdmin.BUNDLE_TYPE_FRAGMENT) > 0; 627 } 628 629 public boolean isRunning() { 630 try { 631 return initialized && context != null && context.getBundle().getState() == Bundle.ACTIVE; 632 } catch (IllegalStateException e) { 633 return false; 634 } 635 } 636 637 642 public String [] knownOSArchValues() { 643 return ARCH_LIST; 644 } 645 646 651 public String [] knownOSValues() { 652 return OS_LIST; 653 } 654 655 660 public String [] knownWSValues() { 661 return WS_LIST; 662 } 663 664 669 public void log(final IStatus status) { 670 RuntimeLog.log(status); 672 } 673 674 private void processCommandLine(String [] args) { 675 if (args == null || args.length == 0) 676 return; 677 678 for (int i = 0; i < args.length; i++) { 679 if (i == args.length - 1 || args[i + 1].startsWith("-")) continue; 682 String arg = args[++i]; 683 684 if (args[i - 1].equalsIgnoreCase(KEYRING)) 686 keyringFile = arg; 687 if (args[i - 1].equalsIgnoreCase(PASSWORD)) 689 password = arg; 690 } 691 } 692 693 private URL [] readPluginPath(InputStream input) { 694 Properties ini = new Properties(); 695 try { 696 ini.load(input); 697 } catch (IOException e) { 698 return null; 699 } 700 Vector result = new Vector(5); 701 for (Enumeration groups = ini.propertyNames(); groups.hasMoreElements();) { 702 String group = (String ) groups.nextElement(); 703 for (StringTokenizer entries = new StringTokenizer(ini.getProperty(group), ";"); entries.hasMoreElements();) { String entry = (String ) entries.nextElement(); 705 if (!entry.equals("")) try { 707 result.addElement(new URL (entry)); 708 } catch (MalformedURLException e) { 709 System.err.println("Ignoring plugin: " + entry); } 712 } 713 } 714 return (URL []) result.toArray(new URL [result.size()]); 715 } 716 717 720 public void removeLogListener(ILogListener listener) { 721 assertInitialized(); 722 RuntimeLog.removeLogListener(listener); 723 } 724 725 728 public void setRuntimeInstance(Plugin runtime) { 729 runtimeInstance = runtime; 730 } 731 732 738 public void start(BundleContext runtimeContext) { 739 this.context = runtimeContext; 740 splashEnded = false; 741 processCommandLine(getEnvironmentInfoService().getNonFrameworkArgs()); 742 initializeDebugFlags(); 743 initialized = true; 744 getMetaArea(); 745 initializeAuthorizationHandler(); 746 FrameworkLog log = getFrameworkLog(); 747 if (log != null) { 748 platformLog = new PlatformLogWriter(getFrameworkLog()); 749 addLogListener(platformLog); 750 } 751 else 752 platformLog = null; 753 adapterManagerListener = new AdapterManagerListener(); startServices(); 755 756 boolean shouldActivate = !"false".equalsIgnoreCase(context.getProperty(PROP_ACTIVATE_PLUGINS)); if (shouldActivate) { 760 new org.eclipse.core.runtime.preferences.DefaultScope(); 762 org.eclipse.core.runtime.jobs.Job.getJobManager(); 764 } 765 } 766 767 772 public void stop(BundleContext bundleContext) { 773 assertInitialized(); 774 stopServices(); if (adapterManagerListener != null) 776 adapterManagerListener.stop(); if (platformLog != null) 778 RuntimeLog.removeLogListener(platformLog); initialized = false; 780 closeOSGITrackers(); 781 context = null; 782 } 783 784 private void startServices() { 785 customPreferencesService = getBundleContext().registerService(IProductPreferencesService.class.getName(), new ProductPreferencesService(), new Hashtable()); 788 789 if (getBundle(CompatibilityHelper.PI_RUNTIME_COMPATIBILITY) != null) 792 legacyPreferencesService = getBundleContext().registerService(ILegacyPreferences.class.getName(), new InitLegacyPreferences(), new Hashtable()); 793 } 794 795 private void stopServices() { 796 if (legacyPreferencesService != null) { 797 legacyPreferencesService.unregister(); 798 legacyPreferencesService = null; 799 } 800 if (customPreferencesService != null) { 801 customPreferencesService.unregister(); 802 customPreferencesService = null; 803 } 804 } 805 806 private PackageAdmin getBundleAdmin() { 807 if (bundleTracker == null) { 808 if (context == null) 809 return null; 810 bundleTracker = new ServiceTracker(context, PackageAdmin.class.getName(), null); 811 bundleTracker.open(); 812 } 813 return (PackageAdmin) bundleTracker.getService(); 814 } 815 816 private DebugOptions getDebugOptions() { 817 if (debugTracker == null) { 818 if (context == null) 819 return null; 820 debugTracker = new ServiceTracker(context, DebugOptions.class.getName(), null); 821 debugTracker.open(); 822 } 823 return (DebugOptions) debugTracker.getService(); 824 } 825 826 private void closeOSGITrackers() { 827 if (preferencesTracker != null) { 828 preferencesTracker.close(); 829 preferencesTracker = null; 830 } 831 if (contentTracker != null) { 832 contentTracker.close(); 833 contentTracker = null; 834 } 835 if (debugTracker != null) { 836 debugTracker.close(); 837 debugTracker = null; 838 } 839 if (bundleTracker != null) { 840 bundleTracker.close(); 841 bundleTracker = null; 842 } 843 if (logTracker != null) { 844 logTracker.close(); 845 logTracker = null; 846 } 847 if (groupProviderTracker != null) { 848 groupProviderTracker.close(); 849 groupProviderTracker = null; 850 } 851 if (environmentTracker != null) { 852 environmentTracker.close(); 853 environmentTracker = null; 854 } 855 } 856 857 861 public static void message(String message) { 862 StringBuffer buffer = new StringBuffer (); 863 buffer.append(new Date(System.currentTimeMillis())); 864 buffer.append(" - ["); buffer.append(Thread.currentThread().getName()); 866 buffer.append("] "); buffer.append(message); 868 System.out.println(buffer.toString()); 869 } 870 871 public static void start(Bundle bundle) throws BundleException { 872 int originalState = bundle.getState(); 873 if ((originalState & Bundle.ACTIVE) != 0) 874 return; try { 876 bundle.start(Bundle.START_TRANSIENT); 878 } catch (BundleException e) { 879 if ((originalState & Bundle.STARTING) != 0 && (bundle.getState() & Bundle.STARTING) != 0) 880 return; 882 throw e; 883 } 884 } 885 } 886 | Popular Tags |