KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > InternalPlatform


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Julian Chen - fix for bug #92572, jclRM
11  *******************************************************************************/

12 package org.eclipse.core.internal.runtime;
13
14 import java.io.*;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
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 /**
39  * Bootstrap class for the platform. It is responsible for setting up the
40  * platform class loader and passing control to the actual application class
41  */

42 public final class InternalPlatform {
43
44     private static final String JavaDoc[] ARCH_LIST = {Platform.ARCH_PA_RISC, //
45
Platform.ARCH_PPC, //
46
Platform.ARCH_SPARC, //
47
Platform.ARCH_X86, //
48
Platform.ARCH_AMD64, //
49
Platform.ARCH_IA64, //
50
Platform.ARCH_IA64_32};
51
52     // debug support: set in loadOptions()
53
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 JavaDoc KEYRING = "-keyring"; //$NON-NLS-1$
59
private static String JavaDoc keyringFile;
60
61     //XXX This is not synchronized
62
private static Map logs = new HashMap(5);
63
64     private static final String JavaDoc[] 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 JavaDoc password = ""; //$NON-NLS-1$
66
private static final String JavaDoc PASSWORD = "-password"; //$NON-NLS-1$
67
private static PlatformLogWriter platformLog = null;
68
69     private static final String JavaDoc PLUGIN_PATH = ".plugin-path"; //$NON-NLS-1$
70

71     public static final String JavaDoc PROP_APPLICATION = "eclipse.application"; //$NON-NLS-1$
72
public static final String JavaDoc PROP_ARCH = "osgi.arch"; //$NON-NLS-1$
73
public static final String JavaDoc PROP_CONFIG_AREA = "osgi.configuration.area"; //$NON-NLS-1$
74
public static final String JavaDoc PROP_CONSOLE_LOG = "eclipse.consoleLog"; //$NON-NLS-1$
75
public static final String JavaDoc PROP_DEBUG = "osgi.debug"; //$NON-NLS-1$
76
public static final String JavaDoc PROP_DEV = "osgi.dev"; //$NON-NLS-1$
77

78     // OSGI system properties. Copied from EclipseStarter
79
public static final String JavaDoc PROP_INSTALL_AREA = "osgi.install.area"; //$NON-NLS-1$
80
public static final String JavaDoc PROP_NL = "osgi.nl"; //$NON-NLS-1$
81
public static final String JavaDoc PROP_OS = "osgi.os"; //$NON-NLS-1$
82

83     // Eclipse System Properties
84
public static final String JavaDoc PROP_PRODUCT = "eclipse.product"; //$NON-NLS-1$
85
public static final String JavaDoc PROP_WS = "osgi.ws"; //$NON-NLS-1$
86
public static final String JavaDoc PROP_ACTIVATE_PLUGINS = "eclipse.activateRuntimePlugins"; //$NON-NLS-1$
87

88     private static final InternalPlatform singleton = new InternalPlatform();
89
90     private static final String JavaDoc[] WS_LIST = {Platform.WS_CARBON, Platform.WS_GTK, Platform.WS_MOTIF, Platform.WS_PHOTON, Platform.WS_WIN32, Platform.WS_WPF};
91     private Path cachedInstanceLocation; // Cache the path of the instance location
92
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; // Keep track of the plugin object for runtime in case the backward compatibility is run.
101

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     /**
121      * Private constructor to block instance creation.
122      */

123     private InternalPlatform() {
124         super();
125     }
126
127     /**
128      * @see Platform#addLogListener(ILogListener)
129      */

130     public void addLogListener(ILogListener listener) {
131         assertInitialized();
132         RuntimeLog.addLogListener(listener);
133     }
134
135     private void assertInitialized() {
136         //avoid the Policy.bind if assertion is true
137
if (!initialized)
138             Assert.isTrue(false, Messages.meta_appNotInit);
139     }
140
141     /**
142      * @see Platform#endSplash()
143      */

144     public void endSplash() {
145         synchronized (this) {
146             if (splashEnded)
147                 return; // do not do this more than once
148
splashEnded = true;
149         }
150         IApplicationContext applicationContext = getApplicationContext();
151         if (applicationContext != null)
152             applicationContext.applicationRunning();
153     }
154
155     /**
156      * @see Platform#getAdapterManager()
157      */

158     public IAdapterManager getAdapterManager() {
159         assertInitialized();
160         return AdapterManager.getDefault();
161     }
162
163     public String JavaDoc[] getApplicationArgs() {
164         return CommandLineArgs.getApplicationArgs();
165     }
166
167     public boolean getBooleanOption(String JavaDoc option, boolean defaultValue) {
168         String JavaDoc value = getOption(option);
169         if (value == null)
170             return defaultValue;
171         return value.equalsIgnoreCase("true"); //$NON-NLS-1$
172
}
173
174     public Bundle getBundle(String JavaDoc 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         //Return the first bundle that is not installed or uninstalled
182
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     /**
195      * Returns the bundle id of the bundle that contains the provided object, or
196      * <code>null</code> if the bundle could not be determined.
197      */

198     public String JavaDoc getBundleId(Object JavaDoc 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             // acquire the service and get the list of services
213
Filter filter = null;
214             try {
215                 filter = getBundleContext().createFilter("(objectClass=" + IBundleGroupProvider.class.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
216
} catch (InvalidSyntaxException e) {
217                 // ignore this, it should never happen
218
}
219             groupProviderTracker = new ServiceTracker(getBundleContext(), filter, null);
220             groupProviderTracker.open();
221         }
222         Object JavaDoc[] objectArray = groupProviderTracker.getServices();
223         if (objectArray == null) // getServices may return null; but we can not.
224
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         // get the bundle context and register the provider as a service
232
ServiceRegistration registration = getBundleContext().registerService(IBundleGroupProvider.class.getName(), provider, null);
233         // store the service registration (map provider -> registration)
234
synchronized (groupProviders) {
235             groupProviders.put(provider, registration);
236         }
237     }
238
239     public void unregisterBundleGroupProvider(IBundleGroupProvider provider) {
240         // get the service reference (map provider -> reference)
241
ServiceRegistration registration;
242         synchronized (groupProviders) {
243             registration = (ServiceRegistration) groupProviders.remove(provider);
244         }
245         if (registration == null)
246             return;
247         // unregister the provider
248
registration.unregister();
249     }
250
251     public Bundle[] getBundles(String JavaDoc symbolicName, String JavaDoc 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         // optimize for common case; length==1
259
if (bundles.length == 1 && (bundles[0].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0)
260             return bundles;
261         //Remove all the bundles that are installed or uninstalled
262
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         //return an array of the correct size
273
Bundle[] results = new Bundle[added];
274         System.arraycopy(selectedBundles, 0, results, 0, added);
275         return results;
276     }
277
278     public String JavaDoc[] 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                 // ignore this. It should never happen as we have tested the above format.
290
}
291             configurationLocation = new ServiceTracker(context, filter, null);
292             configurationLocation.open();
293         }
294         return (Location) configurationLocation.getService();
295     }
296
297     /**
298      * Lazy initialize ContentTypeManager - it can only be used after the registry is up and running
299      */

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                 // ignore this. It should never happen as we have tested the above format.
352
}
353             installLocation = new ServiceTracker(context, filter, null);
354             installLocation.open();
355         }
356         return (Location) installLocation.getService();
357     }
358
359     public URL JavaDoc getInstallURL() {
360         Location location = getInstallLocation();
361         // it is pretty much impossible for the install location to be null. If it is, the
362
// system is in a bad way so throw and exception and get the heck outta here.
363
if (location == null)
364             throw new IllegalStateException JavaDoc("The installation location must not be null"); //$NON-NLS-1$
365
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                 // ignore this. It should never happen as we have tested the above format.
376
}
377             instanceLocation = new ServiceTracker(context, filter, null);
378             instanceLocation.open();
379         }
380         return (Location) instanceLocation.getService();
381     }
382
383     /**
384      * @see Platform#getLocation()
385      */

386     public IPath getLocation() throws IllegalStateException JavaDoc {
387         if (cachedInstanceLocation == null) {
388             Location location = getInstanceLocation();
389             if (location == null)
390                 return null;
391             // This makes the assumption that the instance location is a file: URL
392
File file = new File(location.getURL().getFile());
393             cachedInstanceLocation = new Path(file.toString());
394         }
395         return cachedInstanceLocation;
396     }
397
398     /**
399      * Returns a log for the given plugin. Creates a new one if needed.
400      */

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     /**
411      * Returns the object which defines the location and organization
412      * of the platform's meta area.
413      */

414     public DataArea getMetaArea() {
415         // TODO: deprecate?
416
return MetaDataKeeper.getMetaArea();
417     }
418
419     public String JavaDoc getNL() {
420         return getBundleContext().getProperty(PROP_NL);
421     }
422
423     /**
424      * @see Platform
425      */

426     public String JavaDoc getOption(String JavaDoc option) {
427         DebugOptions options = getDebugOptions();
428         if (options != null)
429             return options.getOption(option);
430         return null;
431     }
432
433     public String JavaDoc getOS() {
434         return getBundleContext().getProperty(PROP_OS);
435     }
436
437     public String JavaDoc 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     //TODO I guess it is now time to get rid of that
451
/*
452      * This method is retained for R1.0 compatibility because it is defined as API.
453      * Its function matches the API description (returns <code>null</code> when
454      * argument URL is <code>null</code> or cannot be read).
455      */

456     public URL JavaDoc[] getPluginPath(URL JavaDoc pluginPathLocation /*R1.0 compatibility*/
457     ) {
458         InputStream input = null;
459         // first try and see if the given plugin path location exists.
460
if (pluginPathLocation == null)
461             return null;
462         try {
463             input = pluginPathLocation.openStream();
464         } catch (IOException e) {
465             //fall through
466
}
467
468         // if the given path was null or did not exist, look for a plugin path
469
// definition in the install location.
470
if (input == null)
471             try {
472                 URL JavaDoc url = new URL JavaDoc("platform:/base/" + PLUGIN_PATH); //$NON-NLS-1$
473
input = url.openStream();
474             } catch (MalformedURLException JavaDoc e) {
475                 //fall through
476
} catch (IOException e) {
477                 //fall through
478
}
479
480         // nothing was found at the supplied location or in the install location
481
if (input == null)
482             return null;
483         // if we found a plugin path definition somewhere so read it and close the location.
484
URL JavaDoc[] result = null;
485         try {
486             try {
487                 result = readPluginPath(input);
488             } finally {
489                 input.close();
490             }
491         } catch (IOException e) {
492             //let it return null on failure to read
493
}
494         return result;
495     }
496
497     /**
498      *
499      */

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 JavaDoc 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 JavaDoc getResourceString(Bundle bundle, String JavaDoc value) {
533         return ResourceTranslator.getResourceString(bundle, value);
534     }
535
536     public String JavaDoc getResourceString(Bundle bundle, String JavaDoc value, ResourceBundle resourceBundle) {
537         return ResourceTranslator.getResourceString(bundle, value, resourceBundle);
538     }
539
540     /**
541      * This method is only used to register runtime once compatibility has been started.
542      */

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)"); //$NON-NLS-1$
551
} catch (InvalidSyntaxException e) {
552             return null;
553         }
554         if (ref == null || ref.length == 0)
555             return null;
556         // assumes the application context is available as a service
557
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 JavaDoc {
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                 // ignore this. It should never happen as we have tested the above format.
590
}
591             userLocation = new ServiceTracker(context, filter, null);
592             userLocation.open();
593         }
594         return (Location) userLocation.getService();
595     }
596
597     public String JavaDoc 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 JavaDoc e) {
606             // The authorization code is not available so just log and continue
607
log(new Status(IStatus.WARNING, Platform.PI_RUNTIME, 0, Messages.auth_notAvailable, e));
608         }
609     }
610
611     /*
612      * Finds and loads the options file
613      */

614     void initializeDebugFlags() {
615         // load runtime options
616
DEBUG = getBooleanOption(Platform.PI_RUNTIME + "/debug", false); //$NON-NLS-1$
617
if (DEBUG) {
618             DEBUG_PLUGIN_PREFERENCES = getBooleanOption(Platform.PI_RUNTIME + "/preferences/plugin", false); //$NON-NLS-1$
619
}
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 JavaDoc e) {
633             return false;
634         }
635     }
636
637     /**
638      * Returns a list of known system architectures.
639      *
640      * @return the list of system architectures known to the system
641      */

642     public String JavaDoc[] knownOSArchValues() {
643         return ARCH_LIST;
644     }
645
646     /**
647      * Returns a list of known operating system names.
648      *
649      * @return the list of operating systems known to the system
650      */

651     public String JavaDoc[] knownOSValues() {
652         return OS_LIST;
653     }
654
655     /**
656      * Returns a list of known windowing system names.
657      *
658      * @return the list of window systems known to the system
659      */

660     public String JavaDoc[] knownWSValues() {
661         return WS_LIST;
662     }
663
664     /**
665      * Notifies all listeners of the platform log. This includes the console log, if
666      * used, and the platform log file. All Plugin log messages get funnelled
667      * through here as well.
668      */

669     public void log(final IStatus status) {
670         // TODO: deprecate?
671
RuntimeLog.log(status);
672     }
673
674     private void processCommandLine(String JavaDoc[] args) {
675         if (args == null || args.length == 0)
676             return;
677
678         for (int i = 0; i < args.length; i++) {
679             // check for args with parameters
680
if (i == args.length - 1 || args[i + 1].startsWith("-")) //$NON-NLS-1$
681
continue;
682             String JavaDoc arg = args[++i];
683
684             // look for the keyring file
685
if (args[i - 1].equalsIgnoreCase(KEYRING))
686                 keyringFile = arg;
687             // look for the user password.
688
if (args[i - 1].equalsIgnoreCase(PASSWORD))
689                 password = arg;
690         }
691     }
692
693     private URL JavaDoc[] 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 JavaDoc group = (String JavaDoc) groups.nextElement();
703             for (StringTokenizer entries = new StringTokenizer(ini.getProperty(group), ";"); entries.hasMoreElements();) { //$NON-NLS-1$
704
String JavaDoc entry = (String JavaDoc) entries.nextElement();
705                 if (!entry.equals("")) //$NON-NLS-1$
706
try {
707                         result.addElement(new URL JavaDoc(entry));
708                     } catch (MalformedURLException JavaDoc e) {
709                         //intentionally ignore bad URLs
710
System.err.println("Ignoring plugin: " + entry); //$NON-NLS-1$
711
}
712             }
713         }
714         return (URL JavaDoc[]) result.toArray(new URL JavaDoc[result.size()]);
715     }
716
717     /**
718      * @see Platform#removeLogListener(ILogListener)
719      */

720     public void removeLogListener(ILogListener listener) {
721         assertInitialized();
722         RuntimeLog.removeLogListener(listener);
723     }
724
725     /**
726      * This method is only used to register runtime once compatibility has been started.
727      */

728     public void setRuntimeInstance(Plugin runtime) {
729         runtimeInstance = runtime;
730     }
731
732     /**
733      * Internal method for starting up the platform. The platform is not started with any location
734      * and should not try to access the instance data area.
735      *
736      * Note: the content type manager must be initialized only after the registry has been created
737      */

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(); // after extension registry
754
startServices();
755
756         // See if need to activate rest of the runtime plugins. Plugins are "gently" activated by touching
757
// a class from the corresponding plugin(s).
758
boolean shouldActivate = !"false".equalsIgnoreCase(context.getProperty(PROP_ACTIVATE_PLUGINS)); //$NON-NLS-1$
759
if (shouldActivate) {
760             // activate Preferences plugin by creating a class from it:
761
new org.eclipse.core.runtime.preferences.DefaultScope();
762             // activate Jobs plugin by creating a class from it:
763
org.eclipse.core.runtime.jobs.Job.getJobManager();
764         }
765     }
766
767     /**
768      * Shutdown runtime pieces in this order:
769      * Content[auto shutdown] -> Preferences[auto shutdown] -> Registry -> Jobs
770      * The "auto" shutdown takes place before this code is executed
771      */

772     public void stop(BundleContext bundleContext) {
773         assertInitialized();
774         stopServices(); // should be done after preferences shutdown
775
if (adapterManagerListener != null)
776             adapterManagerListener.stop(); // before extension registry
777
if (platformLog != null)
778             RuntimeLog.removeLogListener(platformLog); // effectively turns the platform logging off
779
initialized = false;
780         closeOSGITrackers();
781         context = null;
782     }
783
784     private void startServices() {
785         // The check for getProduct() is relatively expensive (about 3% of the headless startup),
786
// so we don't want to enforce it here.
787
customPreferencesService = getBundleContext().registerService(IProductPreferencesService.class.getName(), new ProductPreferencesService(), new Hashtable());
788
789         // Only register this interface if compatibility is installed - the check for a bundle presence
790
// is a quick test that doesn't consume much.
791
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     /**
858      * Print a debug message to the console.
859      * Pre-pend the message with the current date and the name of the current thread.
860      */

861     public static void message(String JavaDoc message) {
862         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
863         buffer.append(new Date(System.currentTimeMillis()));
864         buffer.append(" - ["); //$NON-NLS-1$
865
buffer.append(Thread.currentThread().getName());
866         buffer.append("] "); //$NON-NLS-1$
867
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; // bundle is already active
875
try {
876             // attempt to activate the bundle
877
bundle.start(Bundle.START_TRANSIENT);
878         } catch (BundleException e) {
879             if ((originalState & Bundle.STARTING) != 0 && (bundle.getState() & Bundle.STARTING) != 0)
880                 // This can happen if the bundle was in the process of being activated on this thread, just return
881
return;
882             throw e;
883         }
884     }
885 }
886
Popular Tags