KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > adaptor > EclipseStarter


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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  * Alex Blewitt (bug 172969)
11  *******************************************************************************/

12 package org.eclipse.core.runtime.adaptor;
13
14 import java.io.*;
15 import java.lang.reflect.Constructor JavaDoc;
16 import java.lang.reflect.Method JavaDoc;
17 import java.net.*;
18 import java.security.CodeSource JavaDoc;
19 import java.security.ProtectionDomain JavaDoc;
20 import java.util.*;
21 import org.eclipse.core.runtime.internal.adaptor.*;
22 import org.eclipse.osgi.framework.adaptor.FilePath;
23 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
24 import org.eclipse.osgi.framework.internal.core.*;
25 import org.eclipse.osgi.framework.internal.core.Constants;
26 import org.eclipse.osgi.framework.log.FrameworkLog;
27 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
28 import org.eclipse.osgi.internal.profile.Profile;
29 import org.eclipse.osgi.service.datalocation.Location;
30 import org.eclipse.osgi.service.resolver.*;
31 import org.eclipse.osgi.service.runnable.ApplicationLauncher;
32 import org.eclipse.osgi.service.runnable.StartupMonitor;
33 import org.eclipse.osgi.util.ManifestElement;
34 import org.eclipse.osgi.util.NLS;
35 import org.osgi.framework.*;
36 import org.osgi.service.packageadmin.PackageAdmin;
37 import org.osgi.service.startlevel.StartLevel;
38 import org.osgi.util.tracker.ServiceTracker;
39
40 /**
41  * Special startup class for the Eclipse Platform. This class cannot be
42  * instantiated; all functionality is provided by static methods.
43  * <p>
44  * The Eclipse Platform makes heavy use of Java class loaders for loading
45  * plug-ins. Even the Eclipse Runtime itself and the OSGi framework need
46  * to be loaded by special class loaders. The upshot is that a
47  * client program (such as a Java main program, a servlet) cannot
48  * reference any part of Eclipse directly. Instead, a client must use this
49  * loader class to start the platform, invoking functionality defined
50  * in plug-ins, and shutting down the platform when done.
51  * </p>
52  * <p>Note that the fields on this class are not API. </p>
53  * @since 3.0
54  */

55 public class EclipseStarter {
56     private static FrameworkAdaptor adaptor;
57     private static BundleContext context;
58     private static boolean initialize = false;
59     public static boolean debug = false;
60     private static boolean running = false;
61     private static OSGi osgi = null;
62     private static ServiceRegistration defaultMonitorRegistration = null;
63     private static ServiceRegistration appLauncherRegistration = null;
64     private static ServiceRegistration splashStreamRegistration = null;
65
66     // command line arguments
67
private static final String JavaDoc CLEAN = "-clean"; //$NON-NLS-1$
68
private static final String JavaDoc CONSOLE = "-console"; //$NON-NLS-1$
69
private static final String JavaDoc CONSOLE_LOG = "-consoleLog"; //$NON-NLS-1$
70
private static final String JavaDoc DEBUG = "-debug"; //$NON-NLS-1$
71
private static final String JavaDoc INITIALIZE = "-initialize"; //$NON-NLS-1$
72
private static final String JavaDoc DEV = "-dev"; //$NON-NLS-1$
73
private static final String JavaDoc WS = "-ws"; //$NON-NLS-1$
74
private static final String JavaDoc OS = "-os"; //$NON-NLS-1$
75
private static final String JavaDoc ARCH = "-arch"; //$NON-NLS-1$
76
private static final String JavaDoc NL = "-nl"; //$NON-NLS-1$
77
private static final String JavaDoc CONFIGURATION = "-configuration"; //$NON-NLS-1$
78
private static final String JavaDoc USER = "-user"; //$NON-NLS-1$
79
private static final String JavaDoc NOEXIT = "-noExit"; //$NON-NLS-1$
80

81     // this is more of an Eclipse argument but this OSGi implementation stores its
82
// metadata alongside Eclipse's.
83
private static final String JavaDoc DATA = "-data"; //$NON-NLS-1$
84

85     // System properties
86
public static final String JavaDoc PROP_BUNDLES = "osgi.bundles"; //$NON-NLS-1$
87
public static final String JavaDoc PROP_BUNDLES_STARTLEVEL = "osgi.bundles.defaultStartLevel"; //$NON-NLS-1$ //The start level used to install the bundles
88
public static final String JavaDoc PROP_EXTENSIONS = "osgi.framework.extensions"; //$NON-NLS-1$
89
public static final String JavaDoc PROP_INITIAL_STARTLEVEL = "osgi.startLevel"; //$NON-NLS-1$ //The start level when the fwl start
90
public static final String JavaDoc PROP_DEBUG = "osgi.debug"; //$NON-NLS-1$
91
public static final String JavaDoc PROP_DEV = "osgi.dev"; //$NON-NLS-1$
92
public static final String JavaDoc PROP_CLEAN = "osgi.clean"; //$NON-NLS-1$
93
public static final String JavaDoc PROP_CONSOLE = "osgi.console"; //$NON-NLS-1$
94
public static final String JavaDoc PROP_CONSOLE_CLASS = "osgi.consoleClass"; //$NON-NLS-1$
95
public static final String JavaDoc PROP_CHECK_CONFIG = "osgi.checkConfiguration"; //$NON-NLS-1$
96
public static final String JavaDoc PROP_OS = "osgi.os"; //$NON-NLS-1$
97
public static final String JavaDoc PROP_WS = "osgi.ws"; //$NON-NLS-1$
98
public static final String JavaDoc PROP_NL = "osgi.nl"; //$NON-NLS-1$
99
public static final String JavaDoc PROP_ARCH = "osgi.arch"; //$NON-NLS-1$
100
public static final String JavaDoc PROP_ADAPTOR = "osgi.adaptor"; //$NON-NLS-1$
101
public static final String JavaDoc PROP_SYSPATH = "osgi.syspath"; //$NON-NLS-1$
102
public static final String JavaDoc PROP_LOGFILE = "osgi.logfile"; //$NON-NLS-1$
103
public static final String JavaDoc PROP_FRAMEWORK = "osgi.framework"; //$NON-NLS-1$
104
public static final String JavaDoc PROP_INSTALL_AREA = "osgi.install.area"; //$NON-NLS-1$
105
public static final String JavaDoc PROP_FRAMEWORK_SHAPE = "osgi.framework.shape"; //$NON-NLS-1$ //the shape of the fwk (jar, or folder)
106
public static final String JavaDoc PROP_NOSHUTDOWN = "osgi.noShutdown"; //$NON-NLS-1$
107
private static final String JavaDoc PROP_FORCED_RESTART = "osgi.forcedRestart"; //$NON-NLS-1$
108

109     public static final String JavaDoc PROP_EXITCODE = "eclipse.exitcode"; //$NON-NLS-1$
110
public static final String JavaDoc PROP_EXITDATA = "eclipse.exitdata"; //$NON-NLS-1$
111
public static final String JavaDoc PROP_CONSOLE_LOG = "eclipse.consoleLog"; //$NON-NLS-1$
112
public static final String JavaDoc PROP_IGNOREAPP = "eclipse.ignoreApp"; //$NON-NLS-1$
113
public static final String JavaDoc PROP_REFRESH_BUNDLES = "eclipse.refreshBundles"; //$NON-NLS-1$
114
private static final String JavaDoc PROP_ALLOW_APPRELAUNCH = "eclipse.allowAppRelaunch"; //$NON-NLS-1$
115
private static final String JavaDoc PROP_APPLICATION_LAUNCHDEFAULT = "eclipse.application.launchDefault"; //$NON-NLS-1$
116

117     private static final String JavaDoc FILE_SCHEME = "file:"; //$NON-NLS-1$
118
private static final String JavaDoc REFERENCE_SCHEME = "reference:"; //$NON-NLS-1$
119
private static final String JavaDoc REFERENCE_PROTOCOL = "reference"; //$NON-NLS-1$
120
private static final String JavaDoc INITIAL_LOCATION = "initial@"; //$NON-NLS-1$
121
/** string containing the classname of the adaptor to be used in this framework instance */
122     protected static final String JavaDoc DEFAULT_ADAPTOR_CLASS = "org.eclipse.osgi.baseadaptor.BaseAdaptor"; //$NON-NLS-1$
123

124     private static final int DEFAULT_INITIAL_STARTLEVEL = 6; // default value for legacy purposes
125
private static final String JavaDoc DEFAULT_BUNDLES_STARTLEVEL = "4"; //$NON-NLS-1$
126
// Console information
127
protected static final String JavaDoc DEFAULT_CONSOLE_CLASS = "org.eclipse.osgi.framework.internal.core.FrameworkConsole"; //$NON-NLS-1$
128
private static final String JavaDoc CONSOLE_NAME = "OSGi Console"; //$NON-NLS-1$
129

130     private static Runnable JavaDoc console;
131     private static FrameworkLog log;
132     // directory of serch candidates keyed by directory abs path -> directory listing (bug 122024)
133
private static HashMap searchCandidates = new HashMap(4);
134     private static EclipseAppLauncher appLauncher;
135     private static List shutdownHandlers;
136
137     /**
138      * This is the main to start osgi.
139      * It only works when the framework is being jared as a single jar
140      */

141     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
142         if (FrameworkProperties.getProperty("eclipse.startTime") == null) //$NON-NLS-1$
143
FrameworkProperties.setProperty("eclipse.startTime", Long.toString(System.currentTimeMillis())); //$NON-NLS-1$
144
if (FrameworkProperties.getProperty(PROP_NOSHUTDOWN) == null)
145             FrameworkProperties.setProperty(PROP_NOSHUTDOWN, "true"); //$NON-NLS-1$
146
// set the compatibility boot delegation flag to false to get "standard" OSGi behavior WRT boot delegation (bug 178477)
147
if (FrameworkProperties.getProperty(Constants.OSGI_COMPATIBILITY_BOOTDELEGATION) == null)
148             FrameworkProperties.setProperty(Constants.OSGI_COMPATIBILITY_BOOTDELEGATION, "false"); //$NON-NLS-1$
149
run(args, null);
150     }
151
152     /**
153      * Launches the platform and runs a single application. The application is either identified
154      * in the given arguments (e.g., -application &ltapp id&gt) or in the <code>eclipse.application</code>
155      * System property. This convenience method starts
156      * up the platform, runs the indicated application, and then shuts down the
157      * platform. The platform must not be running already.
158      *
159      * @param args the command line-style arguments used to configure the platform
160      * @param endSplashHandler the block of code to run to tear down the splash
161      * screen or <code>null</code> if no tear down is required
162      * @return the result of running the application
163      * @throws Exception if anything goes wrong
164      */

165     public static Object JavaDoc run(String JavaDoc[] args, Runnable JavaDoc endSplashHandler) throws Exception JavaDoc {
166         if (Profile.PROFILE && Profile.STARTUP)
167             Profile.logEnter("EclipseStarter.run()", null); //$NON-NLS-1$
168
if (running)
169             throw new IllegalStateException JavaDoc(EclipseAdaptorMsg.ECLIPSE_STARTUP_ALREADY_RUNNING);
170         boolean startupFailed = true;
171         try {
172             startup(args, endSplashHandler);
173             startupFailed = false;
174             if (Boolean.valueOf(FrameworkProperties.getProperty(PROP_IGNOREAPP)).booleanValue() || isForcedRestart())
175                 return null;
176             return run(null);
177         } catch (Throwable JavaDoc e) {
178             // ensure the splash screen is down
179
if (endSplashHandler != null)
180                 endSplashHandler.run();
181             // may use startupFailed to understand where the error happened
182
FrameworkLogEntry logEntry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, startupFailed ? EclipseAdaptorMsg.ECLIPSE_STARTUP_STARTUP_ERROR : EclipseAdaptorMsg.ECLIPSE_STARTUP_APP_ERROR, 1, e, null);
183             if (log != null) {
184                 log.log(logEntry);
185                 if (context != null) // this can be null if OSGi failed to launch (bug 151413)
186
logUnresolvedBundles(context.getBundles());
187             } else
188                 // TODO desperate measure - ideally, we should write this to disk (a la Main.log)
189
e.printStackTrace();
190         } finally {
191             try {
192                 // The application typically sets the exit code however the framework can request that
193
// it be re-started. We need to check for this and potentially override the exit code.
194
if (isForcedRestart())
195                     FrameworkProperties.setProperty(PROP_EXITCODE, "23"); //$NON-NLS-1$
196
if (!Boolean.valueOf(FrameworkProperties.getProperty(PROP_NOSHUTDOWN)).booleanValue())
197                     shutdown();
198             } catch (Throwable JavaDoc e) {
199                 FrameworkLogEntry logEntry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, EclipseAdaptorMsg.ECLIPSE_STARTUP_SHUTDOWN_ERROR, 1, e, null);
200                 if (log != null)
201                     log.log(logEntry);
202                 else
203                     // TODO desperate measure - ideally, we should write this to disk (a la Main.log)
204
e.printStackTrace();
205             }
206             if (Profile.PROFILE && Profile.STARTUP)
207                 Profile.logExit("EclipseStarter.run()"); //$NON-NLS-1$
208
if (Profile.PROFILE) {
209                 String JavaDoc report = Profile.getProfileLog();
210                 // avoiding writing to the console if there is nothing to print
211
if (report != null && report.length() > 0)
212                     System.out.println(report);
213             }
214         }
215         // we only get here if an error happened
216
FrameworkProperties.setProperty(PROP_EXITCODE, "13"); //$NON-NLS-1$
217
FrameworkProperties.setProperty(PROP_EXITDATA, NLS.bind(EclipseAdaptorMsg.ECLIPSE_STARTUP_ERROR_CHECK_LOG, log == null ? null : log.getFile().getPath()));
218         return null;
219     }
220
221     /**
222      * Returns true if the platform is already running, false otherwise.
223      * @return whether or not the platform is already running
224      */

225     public static boolean isRunning() {
226         return running;
227     }
228
229     protected static FrameworkLog createFrameworkLog() {
230         FrameworkLog frameworkLog;
231         String JavaDoc logFileProp = FrameworkProperties.getProperty(EclipseStarter.PROP_LOGFILE);
232         if (logFileProp != null) {
233             frameworkLog = new EclipseLog(new File(logFileProp));
234         } else {
235             Location location = LocationManager.getConfigurationLocation();
236             File configAreaDirectory = null;
237             if (location != null)
238                 // TODO assumes the URL is a file: url
239
configAreaDirectory = new File(location.getURL().getFile());
240
241             if (configAreaDirectory != null) {
242                 String JavaDoc logFileName = Long.toString(System.currentTimeMillis()) + ".log"; //$NON-NLS-1$
243
File logFile = new File(configAreaDirectory, logFileName);
244                 FrameworkProperties.setProperty(EclipseStarter.PROP_LOGFILE, logFile.getAbsolutePath());
245                 frameworkLog = new EclipseLog(logFile);
246             } else
247                 frameworkLog = new EclipseLog();
248         }
249         if ("true".equals(FrameworkProperties.getProperty(EclipseStarter.PROP_CONSOLE_LOG))) //$NON-NLS-1$
250
frameworkLog.setConsoleLog(true);
251         return frameworkLog;
252     }
253
254     /**
255      * Starts the platform and sets it up to run a single application. The application is either identified
256      * in the given arguments (e.g., -application &ltapp id&gt) or in the <code>eclipse.application</code>
257      * System property. The platform must not be running already.
258      * <p>
259      * The given runnable (if not <code>null</code>) is used to tear down the splash screen if required.
260      * </p>
261      * @param args the arguments passed to the application
262      * @return BundleContext the context of the system bundle
263      * @throws Exception if anything goes wrong
264      */

265     public static BundleContext startup(String JavaDoc[] args, Runnable JavaDoc endSplashHandler) throws Exception JavaDoc {
266         if (Profile.PROFILE && Profile.STARTUP)
267             Profile.logEnter("EclipseStarter.startup()", null); //$NON-NLS-1$
268
if (running)
269             throw new IllegalStateException JavaDoc(EclipseAdaptorMsg.ECLIPSE_STARTUP_ALREADY_RUNNING);
270         initializeProperties();
271         processCommandLine(args);
272         LocationManager.initializeLocations();
273         loadConfigurationInfo();
274         finalizeProperties();
275         if (Profile.PROFILE)
276             Profile.initProps(); // catch any Profile properties set in eclipse.properties...
277
if (Profile.PROFILE && Profile.STARTUP)
278             Profile.logTime("EclipseStarter.startup()", "props inited"); //$NON-NLS-1$ //$NON-NLS-2$
279
adaptor = createAdaptor();
280         log = adaptor.getFrameworkLog();
281         if (Profile.PROFILE && Profile.STARTUP)
282             Profile.logTime("EclipseStarter.startup()", "adapter created"); //$NON-NLS-1$ //$NON-NLS-2$
283
osgi = new OSGi(adaptor);
284         if (Profile.PROFILE && Profile.STARTUP)
285             Profile.logTime("EclipseStarter.startup()", "OSGi created"); //$NON-NLS-1$ //$NON-NLS-2$
286
context = osgi.getBundleContext();
287         registerFrameworkShutdownHandlers();
288         publishSplashScreen(endSplashHandler);
289         osgi.launch();
290         if (Profile.PROFILE && Profile.STARTUP)
291             Profile.logTime("EclipseStarter.startup()", "osgi launched"); //$NON-NLS-1$ //$NON-NLS-2$
292
String JavaDoc consolePort = FrameworkProperties.getProperty(PROP_CONSOLE);
293         if (consolePort != null) {
294             startConsole(osgi, new String JavaDoc[0], consolePort);
295             if (Profile.PROFILE && Profile.STARTUP)
296                 Profile.logTime("EclipseStarter.startup()", "console started"); //$NON-NLS-1$ //$NON-NLS-2$
297
}
298         if ("true".equals(FrameworkProperties.getProperty(PROP_REFRESH_BUNDLES)) && refreshPackages(getCurrentBundles(false))) //$NON-NLS-1$
299
return context; // cannot continue; refreshPackages shutdown the framework
300
if (Profile.PROFILE && Profile.STARTUP)
301             Profile.logTime("EclipseStarter.startup()", "loading basic bundles"); //$NON-NLS-1$ //$NON-NLS-2$
302
long stateStamp = adaptor.getState().getTimeStamp();
303         Bundle[] startBundles = loadBasicBundles();
304         if (startBundles == null)
305             return context; // cannot continue; loadBasicBundles caused refreshPackages to shutdown the framework
306
// set the framework start level to the ultimate value. This will actually start things
307
// running if they are persistently active.
308
setStartLevel(getStartLevel());
309         if (Profile.PROFILE && Profile.STARTUP)
310             Profile.logTime("EclipseStarter.startup()", "StartLevel set"); //$NON-NLS-1$ //$NON-NLS-2$
311
// they should all be active by this time
312
ensureBundlesActive(startBundles);
313         if (debug || FrameworkProperties.getProperty(PROP_DEV) != null)
314             // only spend time showing unresolved bundles in dev/debug mode and the state has changed
315
if (stateStamp != adaptor.getState().getTimeStamp())
316                 logUnresolvedBundles(context.getBundles());
317         running = true;
318         if (Profile.PROFILE && Profile.STARTUP)
319             Profile.logExit("EclipseStarter.startup()"); //$NON-NLS-1$
320
return context;
321     }
322
323     private static int getStartLevel() {
324         String JavaDoc level = FrameworkProperties.getProperty(PROP_INITIAL_STARTLEVEL);
325         if (level != null)
326             try {
327                 return Integer.parseInt(level);
328             } catch (NumberFormatException JavaDoc e) {
329                 if (debug)
330                     System.out.println("Start level = " + level + " parsed. Using hardcoded default: 6"); //$NON-NLS-1$ //$NON-NLS-2$
331
}
332         return DEFAULT_INITIAL_STARTLEVEL;
333     }
334
335     /**
336      * Runs the application for which the platform was started. The platform
337      * must be running.
338      * <p>
339      * The given argument is passed to the application being run. If it is <code>null</code>
340      * then the command line arguments used in starting the platform, and not consumed
341      * by the platform code, are passed to the application as a <code>String[]</code>.
342      * </p>
343      * @param argument the argument passed to the application. May be <code>null</code>
344      * @return the result of running the application
345      * @throws Exception if anything goes wrong
346      */

347     public static Object JavaDoc run(Object JavaDoc argument) throws Exception JavaDoc {
348         if (Profile.PROFILE && Profile.STARTUP)
349             Profile.logEnter("EclipseStarter.run(Object)()", null); //$NON-NLS-1$
350
if (!running)
351             throw new IllegalStateException JavaDoc(EclipseAdaptorMsg.ECLIPSE_STARTUP_NOT_RUNNING);
352         // if we are just initializing, do not run the application just return.
353
if (initialize)
354             return new Integer JavaDoc(0);
355         if (appLauncher == null) {
356             boolean launchDefault = Boolean.valueOf(FrameworkProperties.getProperty(PROP_APPLICATION_LAUNCHDEFAULT, "true")).booleanValue(); //$NON-NLS-1$
357
// create the ApplicationLauncher and register it as a service
358
appLauncher = new EclipseAppLauncher(context, Boolean.valueOf(FrameworkProperties.getProperty(PROP_ALLOW_APPRELAUNCH)).booleanValue(), launchDefault, log);
359             appLauncherRegistration = context.registerService(ApplicationLauncher.class.getName(), appLauncher, null);
360             // must start the launcher AFTER service restration because this method
361
// blocks and runs the application on the current thread. This method
362
// will return only after the application has stopped.
363
return appLauncher.start(argument);
364         }
365         return appLauncher.reStart(argument);
366     }
367
368     /**
369      * Shuts down the Platform. The state of the Platform is not automatically
370      * saved before shutting down.
371      * <p>
372      * On return, the Platform will no longer be running (but could be re-launched
373      * with another call to startup). If relaunching, care must be taken to reinitialize
374      * any System properties which the platform uses (e.g., osgi.instance.area) as
375      * some policies in the platform do not allow resetting of such properties on
376      * subsequent runs.
377      * </p><p>
378      * Any objects handed out by running Platform,
379      * including Platform runnables obtained via getRunnable, will be
380      * permanently invalid. The effects of attempting to invoke methods
381      * on invalid objects is undefined.
382      * </p>
383      * @throws Exception if anything goes wrong
384      */

385     public static void shutdown() throws Exception JavaDoc {
386         if (!running || osgi == null)
387             return;
388         if (appLauncherRegistration != null)
389             appLauncherRegistration.unregister();
390         if (splashStreamRegistration != null)
391             splashStreamRegistration.unregister();
392         if (defaultMonitorRegistration != null)
393             defaultMonitorRegistration.unregister();
394         appLauncherRegistration = null;
395         appLauncher = null;
396         splashStreamRegistration = null;
397         defaultMonitorRegistration = null;
398         stopConsole();
399         osgi.close();
400         osgi = null;
401         context = null;
402         running = false;
403     }
404
405     private static void ensureBundlesActive(Bundle[] bundles) {
406         ServiceTracker tracker = null;
407         try {
408             for (int i = 0; i < bundles.length; i++) {
409                 if (bundles[i].getState() != Bundle.ACTIVE) {
410                     if (bundles[i].getState() == Bundle.INSTALLED) {
411                         // Log that the bundle is not resolved
412
log.log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, NLS.bind(EclipseAdaptorMsg.ECLIPSE_STARTUP_ERROR_BUNDLE_NOT_RESOLVED, bundles[i].getLocation()), 0, null, null));
413                         continue;
414                     }
415                     // check that the startlevel allows the bundle to be active (111550)
416
if (tracker == null) {
417                         tracker = new ServiceTracker(context, StartLevel.class.getName(), null);
418                         tracker.open();
419                     }
420                     StartLevel sl = (StartLevel) tracker.getService();
421                     if (sl != null && (sl.getBundleStartLevel(bundles[i]) <= sl.getStartLevel())) {
422                         log.log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, NLS.bind(EclipseAdaptorMsg.ECLIPSE_STARTUP_ERROR_BUNDLE_NOT_ACTIVE, bundles[i]), 0, null, null));
423                     }
424                 }
425             }
426         } finally {
427             if (tracker != null)
428                 tracker.close();
429         }
430     }
431
432     private static void logUnresolvedBundles(Bundle[] bundles) {
433         State state = adaptor.getState();
434         FrameworkLog logService = adaptor.getFrameworkLog();
435         StateHelper stateHelper = adaptor.getPlatformAdmin().getStateHelper();
436
437         // first lets look for missing leaf constraints (bug 114120)
438
VersionConstraint[] leafConstraints = stateHelper.getUnsatisfiedLeaves(state.getBundles());
439         // hash the missing leaf constraints by the declaring bundles
440
Map missing = new HashMap();
441         for (int i = 0; i < leafConstraints.length; i++) {
442             // only include non-optional and non-dynamic constraint leafs
443
if (leafConstraints[i] instanceof BundleSpecification && ((BundleSpecification) leafConstraints[i]).isOptional())
444                 continue;
445             if (leafConstraints[i] instanceof ImportPackageSpecification) {
446                 if (ImportPackageSpecification.RESOLUTION_OPTIONAL.equals(((ImportPackageSpecification) leafConstraints[i]).getDirective(Constants.RESOLUTION_DIRECTIVE)))
447                     continue;
448                 if (ImportPackageSpecification.RESOLUTION_DYNAMIC.equals(((ImportPackageSpecification) leafConstraints[i]).getDirective(Constants.RESOLUTION_DIRECTIVE)))
449                     continue;
450             }
451             BundleDescription bundle = leafConstraints[i].getBundle();
452             ArrayList constraints = (ArrayList) missing.get(bundle);
453             if (constraints == null) {
454                 constraints = new ArrayList();
455                 missing.put(bundle, constraints);
456             }
457             constraints.add(leafConstraints[i]);
458         }
459
460         // found some bundles with missing leaf constraints; log them first
461
if (missing.size() > 0) {
462             FrameworkLogEntry[] rootChildren = new FrameworkLogEntry[missing.size()];
463             int rootIndex = 0;
464             for (Iterator iter = missing.keySet().iterator(); iter.hasNext(); rootIndex++) {
465                 BundleDescription description = (BundleDescription) iter.next();
466                 String JavaDoc symbolicName = description.getSymbolicName() == null ? FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME : description.getSymbolicName();
467                 String JavaDoc generalMessage = NLS.bind(EclipseAdaptorMsg.ECLIPSE_STARTUP_ERROR_BUNDLE_NOT_RESOLVED, description.getLocation());
468                 ArrayList constraints = (ArrayList) missing.get(description);
469                 FrameworkLogEntry[] logChildren = new FrameworkLogEntry[constraints.size()];
470                 for (int i = 0; i < logChildren.length; i++)
471                     logChildren[i] = new FrameworkLogEntry(symbolicName, FrameworkLogEntry.WARNING, 0, MessageHelper.getResolutionFailureMessage((VersionConstraint) constraints.get(i)), 0, null, null);
472                 rootChildren[rootIndex] = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.WARNING, 0, generalMessage, 0, null, logChildren);
473             }
474             logService.log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.WARNING, 0, EclipseAdaptorMsg.ECLIPSE_STARTUP_ROOTS_NOT_RESOLVED, 0, null, rootChildren));
475         }
476
477         // There may be some bundles unresolved for other reasons, causing the system to be unresolved
478
// log all unresolved constraints now
479
ArrayList allChildren = new ArrayList();
480         for (int i = 0; i < bundles.length; i++)
481             if (bundles[i].getState() == Bundle.INSTALLED) {
482                 String JavaDoc symbolicName = bundles[i].getSymbolicName() == null ? FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME : bundles[i].getSymbolicName();
483                 String JavaDoc generalMessage = NLS.bind(EclipseAdaptorMsg.ECLIPSE_STARTUP_ERROR_BUNDLE_NOT_RESOLVED, bundles[i]);
484                 BundleDescription description = state.getBundle(bundles[i].getBundleId());
485                 // for some reason, the state does not know about that bundle
486
if (description == null)
487                     continue;
488                 FrameworkLogEntry[] logChildren = null;
489                 VersionConstraint[] unsatisfied = stateHelper.getUnsatisfiedConstraints(description);
490                 if (unsatisfied.length > 0) {
491                     // the bundle wasn't resolved due to some of its constraints were unsatisfiable
492
logChildren = new FrameworkLogEntry[unsatisfied.length];
493                     for (int j = 0; j < unsatisfied.length; j++)
494                         logChildren[j] = new FrameworkLogEntry(symbolicName, FrameworkLogEntry.WARNING, 0, MessageHelper.getResolutionFailureMessage(unsatisfied[j]), 0, null, null);
495                 } else {
496                     ResolverError[] resolverErrors = state.getResolverErrors(description);
497                     if (resolverErrors.length > 0) {
498                         logChildren = new FrameworkLogEntry[resolverErrors.length];
499                         for (int j = 0; j < resolverErrors.length; j++)
500                             logChildren[j] = new FrameworkLogEntry(symbolicName, FrameworkLogEntry.WARNING, 0, resolverErrors[j].toString(), 0, null, null);
501                     }
502                 }
503
504                 allChildren.add(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.WARNING, 0, generalMessage, 0, null, logChildren));
505             }
506         if (allChildren.size() > 0)
507             logService.log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.WARNING, 0, EclipseAdaptorMsg.ECLIPSE_STARTUP_ALL_NOT_RESOLVED, 0, null, (FrameworkLogEntry[]) allChildren.toArray(new FrameworkLogEntry[allChildren.size()])));
508     }
509
510     private static void publishSplashScreen(final Runnable JavaDoc endSplashHandler) {
511         if (endSplashHandler == null)
512             return;
513         // register the output stream to the launcher if it exists
514
try {
515             Method JavaDoc method = endSplashHandler.getClass().getMethod("getOutputStream", new Class JavaDoc[0]); //$NON-NLS-1$
516
Object JavaDoc outputStream = method.invoke(endSplashHandler, new Object JavaDoc[0]);
517             if (outputStream instanceof OutputStream) {
518                 Dictionary osProperties = new Hashtable();
519                 osProperties.put("name", "splashstream"); //$NON-NLS-1$//$NON-NLS-2$
520
splashStreamRegistration = context.registerService(OutputStream.class.getName(), outputStream, osProperties);
521             }
522         } catch (Exception JavaDoc ex) {
523             // ignore
524
}
525         // keep this splash handler as the default startup monitor
526
try {
527             Dictionary monitorProps = new Hashtable();
528             monitorProps.put(Constants.SERVICE_RANKING, new Integer JavaDoc(Integer.MIN_VALUE));
529             defaultMonitorRegistration = context.registerService(StartupMonitor.class.getName(), new DefaultStartupMonitor(endSplashHandler), monitorProps);
530         } catch (IllegalStateException JavaDoc e) {
531             //splash handler did not provide the necessary methods, ignore it
532
}
533     }
534
535     private static URL searchForBundle(String JavaDoc name, String JavaDoc parent) throws MalformedURLException {
536         URL url = null;
537         File fileLocation = null;
538         boolean reference = false;
539         try {
540             new URL(name); // quick check to see if the name is a valid URL
541
url = new URL(new File(parent).toURL(), name);
542         } catch (MalformedURLException e) {
543             // TODO this is legacy support for non-URL names. It should be removed eventually.
544
// if name was not a URL then construct one.
545
// Assume it should be a reference and htat it is relative. This support need not
546
// be robust as it is temporary..
547
File child = new File(name);
548             fileLocation = child.isAbsolute() ? child : new File(parent, name);
549             url = new URL(REFERENCE_PROTOCOL, null, fileLocation.toURL().toExternalForm());
550             reference = true;
551         }
552         // if the name was a URL then see if it is relative. If so, insert syspath.
553
if (!reference) {
554             URL baseURL = url;
555             // if it is a reference URL then strip off the reference: and set base to the file:...
556
if (url.getProtocol().equals(REFERENCE_PROTOCOL)) {
557                 reference = true;
558                 String JavaDoc baseSpec = url.getFile();
559                 if (baseSpec.startsWith(FILE_SCHEME)) {
560                     File child = new File(baseSpec.substring(5));
561                     baseURL = child.isAbsolute() ? child.toURL() : new File(parent, child.getPath()).toURL();
562                 } else
563                     baseURL = new URL(baseSpec);
564             }
565
566             fileLocation = new File(baseURL.getFile());
567             // if the location is relative, prefix it with the parent
568
if (!fileLocation.isAbsolute())
569                 fileLocation = new File(parent, fileLocation.toString());
570         }
571         // If the result is a reference then search for the real result and
572
// reconstruct the answer.
573
if (reference) {
574             String JavaDoc result = searchFor(fileLocation.getName(), new File(fileLocation.getParent()).getAbsolutePath());
575             if (result != null)
576                 url = new URL(REFERENCE_PROTOCOL, null, FILE_SCHEME + result);
577             else
578                 return null;
579         }
580
581         // finally we have something worth trying
582
try {
583             URLConnection result = url.openConnection();
584             result.connect();
585             return url;
586         } catch (IOException e) {
587             // int i = location.lastIndexOf('_');
588
// return i == -1? location : location.substring(0, i);
589
return null;
590         }
591     }
592
593     /*
594      * Ensure all basic bundles are installed, resolved and scheduled to start. Returns an array containing
595      * all basic bundles that are marked to start.
596      * Returns null if the framework has been shutdown as a result of refreshPackages
597      */

598     private static Bundle[] loadBasicBundles() throws IOException {
599         long startTime = System.currentTimeMillis();
600         String JavaDoc osgiBundles = FrameworkProperties.getProperty(PROP_BUNDLES);
601         String JavaDoc osgiExtensions = FrameworkProperties.getProperty(PROP_EXTENSIONS);
602         if (osgiExtensions != null && osgiExtensions.length() > 0) {
603             osgiBundles = osgiExtensions + ',' + osgiBundles;
604             FrameworkProperties.setProperty(PROP_BUNDLES, osgiBundles);
605         }
606         String JavaDoc[] installEntries = getArrayFromList(osgiBundles, ","); //$NON-NLS-1$
607
// get the initial bundle list from the installEntries
608
InitialBundle[] initialBundles = getInitialBundles(installEntries);
609         // get the list of currently installed initial bundles from the framework
610
Bundle[] curInitBundles = getCurrentBundles(true);
611
612         // list of bundles to be refreshed
613
List toRefresh = new ArrayList(curInitBundles.length);
614         // uninstall any of the currently installed bundles that do not exist in the
615
// initial bundle list from installEntries.
616
uninstallBundles(curInitBundles, initialBundles, toRefresh);
617
618      &nbs