Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1752b18a79e7808b0dc5e0a569320aed5d8bfb18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.app;

import org.eclipse.equinox.app.IAppContext;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.osgi.service.runnable.ApplicationRunnable;
import org.eclipse.osgi.util.NLS;

public class MainThreadApplication implements ApplicationRunnable {
	private static final String PROP_ECLIPSE_EXITCODE = "eclipse.exitcode"; //$NON-NLS-1$
	private Object application;
	private EclipseAppHandle appContext;
	private Exception launchException;

	public MainThreadApplication(EclipseAppHandle appContext) {
		this.appContext = appContext;
		try {
			application = appContext.getConfiguration().createExecutableExtension("run"); //$NON-NLS-1$
		} catch (Exception e) {
			// had an error creating the executable extension
			// save the exception to throw on the main thread (keeping legacy behavior)
			this.launchException = e;
		}
	}

	public Object run(Object context) throws Exception {
		// if the given arg is null then pass in the left over command line args.
		if (context == null)
			context = AppManager.getApplicationArgs();
		Object result;
		try {
			if (launchException != null)
				// this is a dummy handle used to throw an exception on the main thread.
				throw launchException;
			if (application instanceof IApplication) {
				result = ((IApplication) application).run(context);
			} else
				result = ContainerManager.execMethod(application, "run", Object.class, context); //$NON-NLS-1$
		} finally {
			application = null;
			// The application exited itself; notify the app context
			appContext.setAppStatus(IAppContext.STOPPED);
		}
		int exitCode = result instanceof Integer ? ((Integer) result).intValue() : 0;
		// use the long way to set the property to compile against eeminimum
		System.getProperties().setProperty(PROP_ECLIPSE_EXITCODE, Integer.toString(exitCode));
		if (Activator.DEBUG)
			System.out.println(NLS.bind(Messages.application_returned, (new String[] {appContext.getApplicationDescriptor().getApplicationId(), result == null ? "null" : result.toString()}))); //$NON-NLS-1$
		return result;
	}

	public void stop() {
		// we can only handle forced stops if this application is an IApplication
		if (application instanceof IApplication)
			((IApplication) application).stop();
	}

	IApplication getApplication() {
		return (IApplication) ((application instanceof IApplication) ? application : null);
	}
}

Back to the top