Skip to main content
summaryrefslogtreecommitdiffstats
blob: dc7e221499a2d0bead0bffdf3832c0e47a5c279f (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
 * Copyright (c) 2000, 2004 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.help.internal.appserver;
import org.eclipse.core.runtime.*;
import org.osgi.framework.*;
/**
 */
public class AppserverPlugin extends Plugin {
	public final static String PLUGIN_ID = "org.eclipse.help.appserver"; //$NON-NLS-1$
	public final static String HOST_KEY = "host"; //$NON-NLS-1$
	public final static String PORT_KEY = "port"; //$NON-NLS-1$
	private final static String APP_SERVER_EXTENSION_ID = PLUGIN_ID + ".server"; //$NON-NLS-1$
	private static final String APP_SERVER_CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
	private static final String APP_SERVER_DEFAULT_ATTRIBUTE = "default"; //$NON-NLS-1$
	// singleton object
	private static AppserverPlugin plugin;
//	private static BundleContext bundleContext;
	private IWebappServer appServer;
	private String contributingServerPlugin;
	private String hostAddress;
	private int port;
	/**
	 */
	public static AppserverPlugin getDefault() {
		return plugin;
	}
	/**
	 * Returns the instance of WebappServer.
	 */
	public synchronized IWebappServer getAppServer() throws CoreException {
		if (appServer == null) {
			createWebappServer();
			startWebappServer();
		}
		return appServer;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		if (appServer != null) {
			appServer.stop();
		}
		plugin = null;
//		bundleContext = null;
		super.stop(context);
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
//		bundleContext = context;
	}
	/**
	 * Returns the plugin ID that contributes the server implementation
	 * 
	 * @return String
	 */
	public String getContributingServerPlugin() {
		return contributingServerPlugin;
	}
	private void createWebappServer() throws CoreException {
		// Initializes the app server by getting an instance via
		// app-server the extension point
		// get the app server extension from the system plugin registry
		IExtensionPoint point = Platform.getExtensionRegistry()
				.getExtensionPoint(APP_SERVER_EXTENSION_ID);
		if (point != null) {
			IExtension[] extensions = point.getExtensions();
			if (extensions.length != 0) {
				// We need to pick up the non-default configuration
				IConfigurationElement[] elements = extensions[0]
						.getConfigurationElements();
				if (elements.length == 0)
					return;
				IConfigurationElement serverElement = null;
				for (int i = 0; i < elements.length; i++) {
					String defaultValue = elements[i]
							.getAttribute(APP_SERVER_DEFAULT_ATTRIBUTE);
					if (defaultValue == null || defaultValue.equals("false")) { //$NON-NLS-1$
						serverElement = elements[i];
						break;
					}
				}
				// if all the servers are default, then pick the first one
				if (serverElement == null)
					serverElement = elements[0];
				// Instantiate the app server
				try {
					appServer = (IWebappServer) serverElement
							.createExecutableExtension(APP_SERVER_CLASS_ATTRIBUTE);
					contributingServerPlugin = serverElement
							.getNamespace();
				} catch (CoreException e) {
					getLog().log(e.getStatus());
					throw e;
				}
			}
		}
	}
	private void startWebappServer() throws CoreException {
		// Initialize host and port from preferences
		hostAddress = getPluginPreferences().getString(HOST_KEY);
		if ("".equals(hostAddress)) { //$NON-NLS-1$
			hostAddress = null;
		}
		port = getPluginPreferences().getInt(PORT_KEY);
		// apply host and port overrides passed as command line arguments
		try {
			String hostCommandLineOverride = System.getProperty("server_host"); //$NON-NLS-1$
			if (hostCommandLineOverride != null
					&& hostCommandLineOverride.trim().length() > 0) {
				hostAddress = hostCommandLineOverride;
			}
		} catch (Exception e) {
		}
		try {
			String portCommandLineOverride = System.getProperty("server_port"); //$NON-NLS-1$
			if (portCommandLineOverride != null
					&& portCommandLineOverride.trim().length() > 0) {
				port = Integer.parseInt(portCommandLineOverride);
			}
		} catch (Exception e) {
		}
		if (appServer == null)
			throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID,
					IStatus.OK,
					AppserverResources.Appserver_start, null)); 
		appServer.start(port, hostAddress);
	}
}

Back to the top