Skip to main content
summaryrefslogtreecommitdiffstats
blob: 36c8ebdc21aa8b0f63255d7a2ce7f889e4eb3930 (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
224
225
226
227
228
229
230
231
232
233
234
/*******************************************************************************
 * Copyright (c) 2007, 2013 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
 *     Florian Pirchner - added to preview bundle
 *******************************************************************************/

package org.eclipse.osbp.ecview.vaadin.ide.preview.jetty;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.eclipse.equinox.http.servlet.HttpServiceServlet;
import org.eclipse.jetty.server.AbstractNetworkConnector;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.osgi.framework.Constants;

public class PreviewJettyManager {

	/**
	 * name="http.port" type="Integer" (default: 0 -- first available port)
	 */
	public static final String PROP_HTTP_PORT = "http.port"; //$NON-NLS-1$

	/**
	 * name="other.info" type="String"
	 */
	public static final String PROP_OTHER_INFO = "other.info"; //$NON-NLS-1$

	public static final String PROP_IDEPREVIEW = "idepreview";
	public static final String PROP_MOBILEPREVIEW = "mobilepreview";

	// private static final int HTTP_PORT = 8099;
	private static final String CONTEXT_TEMPDIR = "javax.servlet.context.tempdir"; //$NON-NLS-1$
	private static final String DIR_PREFIX = "preview"; //$NON-NLS-1$
	private static final String INTERNAL_CONTEXT_CLASSLOADER = "org.eclipse.equinox.http.jetty.internal.ContextClassLoader"; //$NON-NLS-1$

	private Server server;
	private File workDir;

	private int port;

	public PreviewJettyManager(File workDir) {
		this.workDir = workDir;
	}

	public synchronized void stop() {
		if (server != null) {
			try {
				server.stop();
			} catch (Exception e) {
				// cleaning up
				e.printStackTrace();
			}
			File contextWorkDir = new File(workDir, DIR_PREFIX);
			deleteDirectory(contextWorkDir);
		}
	}

	public String getName() {
		return this.getClass().getName();
	}

	public synchronized void start() {
		stop();
		Server server = new Server(0);

		Connector httpConnector = createHttpConnector(server);
		server.addConnector(httpConnector);

		ContextHandlerCollection handlers = new ContextHandlerCollection();
		ServletContextHandler ideContext = createServletContext(PROP_IDEPREVIEW);
		ServletContextHandler mobileContext = createServletContext(PROP_MOBILEPREVIEW);
		handlers.addHandler(ideContext);
		handlers.addHandler(mobileContext);

		server.setHandler(handlers);
		try {
			server.start();
			AbstractNetworkConnector connector = (AbstractNetworkConnector) server.getConnectors()[0];
			port = connector.getLocalPort();
			System.getProperties().setProperty("preview.port", Integer.toString(port));
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		this.server = server;
	}

	protected ServletContextHandler createServletContext(String contextPath) {
		ServletHolder holder = new ServletHolder(new InternalHttpServiceServlet());
		holder.setInitOrder(0);
		holder.setInitParameter(Constants.SERVICE_VENDOR, "OSBP.org"); //$NON-NLS-1$
		holder.setInitParameter(Constants.SERVICE_DESCRIPTION, "ECView" + contextPath); //$NON-NLS-1$
		// holder.setInitParameter(PROP_HTTP_PORT,
		// Integer.toString(HTTP_PORT));
		holder.setInitParameter(PROP_OTHER_INFO, contextPath);
		ServletContextHandler httpContext = createHttpContext("/" + contextPath);

		httpContext.addServlet(holder, "/*"); //$NON-NLS-1$
		return httpContext;
	}

	public synchronized void shutdown() throws Exception {
		server.stop();
		server = null;
	}

	private Connector createHttpConnector(Server server) {
		Connector connector = new ServerConnector(server);
		return connector;
	}

	private Boolean getDefaultNIOEnablement() {
		Properties systemProperties = System.getProperties();
		String javaVendor = systemProperties.getProperty("java.vendor", ""); //$NON-NLS-1$ //$NON-NLS-2$
		if (javaVendor.equals("IBM Corporation")) { //$NON-NLS-1$
			String javaVersion = systemProperties.getProperty("java.version", ""); //$NON-NLS-1$ //$NON-NLS-2$
			if (javaVersion.startsWith("1.4")) //$NON-NLS-1$
				return Boolean.FALSE;
			// Note: no problems currently logged with 1.5
			if (javaVersion.equals("1.6.0")) { //$NON-NLS-1$
				String jclVersion = systemProperties.getProperty("java.jcl.version", ""); //$NON-NLS-1$ //$NON-NLS-2$
				if (jclVersion.startsWith("2007")) //$NON-NLS-1$
					return Boolean.FALSE;
				if (jclVersion.startsWith("2008") && !jclVersion.startsWith("200811") //$NON-NLS-1$ //$NON-NLS-2$
						&& !jclVersion.startsWith("200812")) //$NON-NLS-1$
					return Boolean.FALSE;
			}
		}
		return Boolean.TRUE;
	}

	private ServletContextHandler createHttpContext(String contextPath) {
		ServletContextHandler httpContext = new ServletContextHandler();
		// hack in the mime type for xsd until jetty fixes it (bug 393218)
		httpContext.getMimeTypes().addMimeMapping("xsd", "application/xml"); //$NON-NLS-1$ //$NON-NLS-2$
		httpContext.setAttribute(INTERNAL_CONTEXT_CLASSLOADER, Thread.currentThread().getContextClassLoader());
		httpContext.setClassLoader(this.getClass().getClassLoader());
		httpContext.setContextPath(contextPath);

		File contextWorkDir = new File(workDir, DIR_PREFIX + contextPath);
		contextWorkDir.mkdir();
		httpContext.setAttribute(CONTEXT_TEMPDIR, contextWorkDir);

		httpContext.setSessionHandler(new SessionHandler());

		return httpContext;
	}

	public static class InternalHttpServiceServlet implements Servlet {
		// private static final long serialVersionUID = 7477982882399972088L;
		private Servlet httpServiceServlet = new HttpServiceServlet();
		private ClassLoader contextLoader;

		public void init(ServletConfig config) throws ServletException {
			ServletContext context = config.getServletContext();
			contextLoader = (ClassLoader) context.getAttribute(INTERNAL_CONTEXT_CLASSLOADER);

			Thread thread = Thread.currentThread();
			ClassLoader current = thread.getContextClassLoader();
			thread.setContextClassLoader(contextLoader);
			try {
				httpServiceServlet.init(config);
			} finally {
				thread.setContextClassLoader(current);
			}
		}

		public void destroy() {
			Thread thread = Thread.currentThread();
			ClassLoader current = thread.getContextClassLoader();
			thread.setContextClassLoader(contextLoader);
			try {
				httpServiceServlet.destroy();
			} finally {
				thread.setContextClassLoader(current);
			}
			contextLoader = null;
		}

		public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
			Thread thread = Thread.currentThread();
			ClassLoader current = thread.getContextClassLoader();
			thread.setContextClassLoader(contextLoader);
			try {
				httpServiceServlet.service(req, res);
			} finally {
				thread.setContextClassLoader(current);
			}
		}

		public ServletConfig getServletConfig() {
			return httpServiceServlet.getServletConfig();
		}

		public String getServletInfo() {
			return httpServiceServlet.getServletInfo();
		}
	}

	// deleteDirectory is a convenience method to recursively delete a directory
	private static boolean deleteDirectory(File directory) {
		if (directory.exists() && directory.isDirectory()) {
			File[] files = directory.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					deleteDirectory(files[i]);
				} else {
					files[i].delete();
				}
			}
		}
		return directory.delete();
	}
}

Back to the top