Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8178edb7d1162e74a0df2ff01311fdc80e9e3a83 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*******************************************************************************
 * Copyright (c) 2008, 2011 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.server;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.http.jetty.JettyConfigurator;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.base.HelpBasePlugin;
import org.eclipse.help.server.HelpServer;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;


public class JettyHelpServer extends HelpServer {
	
	private abstract class WorkerThread extends Thread {	
		private Throwable exception;

		public WorkerThread(String name) {
			super(name);
		}
		
		public synchronized void setException(Throwable status) {
			this.exception = status;
		}
		
		public synchronized Throwable getException() {
			return exception;
		}
	}
	
	private final class StartServerThread extends WorkerThread {

		private final String webappName;

		public StartServerThread(String webappName) {
			super("Start Help Server"); //$NON-NLS-1$
			this.webappName = webappName;
		}

		public void run() {
			try {
				final Dictionary d = new Hashtable();
				configurePort();
				d.put("http.port", new Integer(getPortParameter())); //$NON-NLS-1$

				// set the base URL
				d.put("context.path", getContextPath()); //$NON-NLS-1$ 
				d.put("other.info", getOtherInfo()); //$NON-NLS-1$ 

				// suppress Jetty INFO/DEBUG messages to stderr
				Logger.getLogger("org.mortbay").setLevel(Level.WARNING); //$NON-NLS-1$	

				if (bindServerToHostname()) { 
					d.put("http.host", getHost()); //$NON-NLS-1$
				}		   
				
				JettyConfigurator.startServer(webappName, d);
			} catch (Throwable t) {
				setException(t);
			}
		}
	}

	private final class StopServerThread extends WorkerThread {

		private final String webappName;

		public StopServerThread(String webappName) {
			super("Stop Help Server"); //$NON-NLS-1$
			this.webappName = webappName;
		}

		public void run() {
			try {
				JettyConfigurator.stopServer(webappName);
				port = -1;
			} catch (Throwable t) {
				setException(t); 
			}
		}
	}


	private String host;
	protected int port = -1;
	protected static final int AUTO_SELECT_JETTY_PORT = 0;
	
	public void start(final String webappName) throws Exception {		
		WorkerThread startRunnable = new StartServerThread(webappName); 
		execute(startRunnable);		
		checkBundle();	
	}

	/*
	 * Ensures that the bundle with the specified name and the highest available
	 * version is started and reads the port number
	 */
	protected void checkBundle() throws InvalidSyntaxException, BundleException {
		Bundle bundle = Platform.getBundle("org.eclipse.equinox.http.registry"); //$NON-NLS-1$if (bundle != null) {
		if (bundle.getState() == Bundle.RESOLVED) {
			bundle.start(Bundle.START_TRANSIENT);
		}
		if (port == -1) {
			// Jetty selected a port number for us
			ServiceReference[] reference = bundle.getBundleContext().getServiceReferences("org.osgi.service.http.HttpService", "(other.info=" + getOtherInfo() + ')'); //$NON-NLS-1$ //$NON-NLS-2$
			Object assignedPort = reference[0].getProperty("http.port"); //$NON-NLS-1$
			port = Integer.parseInt((String)assignedPort);
		}
	}

	public void stop(final String webappName) throws CoreException {
		try {
			WorkerThread stopRunnable = new StopServerThread(webappName);
			execute(stopRunnable);
		}
		catch (Exception e) {
			HelpBasePlugin.logError("An error occured while stopping the help server", e); //$NON-NLS-1$
		}
	}
	
	private void execute(WorkerThread runnable) throws Exception {
		boolean interrupted = false;
		Thread thread = runnable;
		thread.setDaemon(true);
		thread.start();
		while(true) {
			try {
				thread.join();
				break;
			} catch (InterruptedException e) {
				interrupted = true;
			}
		}
		if (interrupted)
			Thread.currentThread().interrupt();
		
		Throwable t = runnable.getException();

		if (t != null) {
			if (t instanceof Exception) {
				throw (Exception)t;
			}
			throw (Error) t;
		}
	}

	public int getPort() {
		return port;
	}

	private void configurePort() {
		if (port == -1) {
			String portCommandLineOverride = HelpBasePlugin.getBundleContext().getProperty("server_port"); //$NON-NLS-1$
			if (portCommandLineOverride != null && portCommandLineOverride.trim().length() > 0) {
				try {
					port = Integer.parseInt(portCommandLineOverride);
				}
				catch (NumberFormatException e) {
					String msg = "Help server port specified in VM arguments is invalid (" + portCommandLineOverride + ")"; //$NON-NLS-1$ //$NON-NLS-2$
					HelpBasePlugin.logError(msg, e);
				}
			}
		}
	}
	
	/*
	 * Get the port number which will be passed to Jetty
	 */
	protected int getPortParameter() {
		if (port == -1) { 
			return AUTO_SELECT_JETTY_PORT;
		}
		return port;
	}

	public String getHost() {
		if (host == null) {
			String hostCommandLineOverride = HelpBasePlugin.getBundleContext().getProperty("server_host"); //$NON-NLS-1$
			if (hostCommandLineOverride != null && hostCommandLineOverride.trim().length() > 0) {
				host = hostCommandLineOverride;
			}
			else {
				host = "127.0.0.1"; //$NON-NLS-1$
			}
		}
		return host;
	}

	protected String getOtherInfo() {
		return "org.eclipse.help"; //$NON-NLS-1$
	}	
	
	protected String getContextPath() {
		return "/help"; //$NON-NLS-1$
	}
	
	public boolean bindServerToHostname() {
		if (BaseHelpSystem.getMode() == BaseHelpSystem.MODE_WORKBENCH) {
			return true;
		}
		String host = HelpBasePlugin.getBundleContext().getProperty("server_host"); //$NON-NLS-1$
        return host != null && host.trim().length() > 0;
	}

}

Back to the top