KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > http > jetty > internal > HttpServerManager


1 /*******************************************************************************
2  * Copyright (c) 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  *******************************************************************************/

11
12 package org.eclipse.equinox.http.jetty.internal;
13
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.net.UnknownHostException JavaDoc;
17 import java.util.*;
18 import javax.servlet.*;
19 import org.eclipse.equinox.http.servlet.HttpServiceServlet;
20 import org.mortbay.http.*;
21 import org.mortbay.jetty.servlet.ServletHandler;
22 import org.mortbay.jetty.servlet.ServletHolder;
23 import org.osgi.framework.Constants;
24 import org.osgi.service.cm.ConfigurationException;
25 import org.osgi.service.cm.ManagedServiceFactory;
26
27 public class HttpServerManager implements ManagedServiceFactory {
28
29     private static final String JavaDoc DIR_PREFIX = "pid_"; //$NON-NLS-1$
30
private static final String JavaDoc INTERNAL_CONTEXT_CLASSLOADER = "org.eclipse.equinox.http.jetty.internal.ContextClassLoader"; //$NON-NLS-1$
31

32     // Http Service properties - the autostarted service will look these up in the BundleContext by prefixing with "org.eclipse.equinox.http.jetty."
33
static final String JavaDoc HTTP_ENABLED = "http.enabled"; //$NON-NLS-1$
34
static final String JavaDoc HTTP_PORT = "http.port"; //$NON-NLS-1$
35
static final String JavaDoc HTTP_HOST = "http.host"; //$NON-NLS-1$
36
static final String JavaDoc HTTPS_ENABLED = "https.enabled"; //$NON-NLS-1$
37
static final String JavaDoc HTTPS_HOST = "https.host"; //$NON-NLS-1$
38
static final String JavaDoc HTTPS_PORT = "https.port"; //$NON-NLS-1$
39
static final String JavaDoc SSL_KEYSTORE = "ssl.keystore"; //$NON-NLS-1$
40
static final String JavaDoc SSL_PASSWORD = "ssl.password"; //$NON-NLS-1$
41
static final String JavaDoc SSL_KEYPASSWORD = "ssl.keypassword"; //$NON-NLS-1$
42
static final String JavaDoc SSL_NEEDCLIENTAUTH = "ssl.needclientauth"; //$NON-NLS-1$
43
static final String JavaDoc SSL_WANTCLIENTAUTH = "ssl.wantclientauth"; //$NON-NLS-1$
44
static final String JavaDoc SSL_PROTOCOL = "ssl.protocol"; //$NON-NLS-1$
45
static final String JavaDoc SSL_ALGORITHM = "ssl.algorithm"; //$NON-NLS-1$
46
static final String JavaDoc SSL_KEYSTORETYPE = "ssl.keystoretype"; //$NON-NLS-1$
47
static final String JavaDoc CONTEXT_PATH = "context.path"; //$NON-NLS-1$
48
static final String JavaDoc CONTEXT_SESSIONINACTIVEINTERVAL = "context.sessioninactiveinterval"; //$NON-NLS-1$
49
static final String JavaDoc OTHER_INFO = "other.info"; //$NON-NLS-1$
50

51     private Map servers = new HashMap();
52     private File JavaDoc workDir;
53
54     public HttpServerManager(File JavaDoc workDir) {
55         this.workDir = workDir;
56     }
57
58     public synchronized void deleted(String JavaDoc pid) {
59         HttpServer server = (HttpServer) servers.remove(pid);
60         if (server != null) {
61             try {
62                 server.stop();
63             } catch (Exception JavaDoc e) {
64                 // TODO: consider logging this, but we should still continue cleaning up
65
e.printStackTrace();
66             }
67             File JavaDoc contextWorkDir = new File JavaDoc(workDir, DIR_PREFIX + pid.hashCode());
68             deleteDirectory(contextWorkDir);
69         }
70     }
71
72     public String JavaDoc getName() {
73         return this.getClass().getName();
74     }
75
76     public synchronized void updated(String JavaDoc pid, Dictionary dictionary) throws ConfigurationException {
77         deleted(pid);
78         HttpServer server = new HttpServer();
79         SocketListener httpListener = createHttpListener(dictionary);
80         if (httpListener != null)
81             server.addListener(httpListener);
82
83         SocketListener httpsListener = createHttpsListener(dictionary);
84         if (httpsListener != null)
85             server.addListener(httpsListener);
86
87         ServletHandler servlets = new ServletHandler();
88         servlets.setAutoInitializeServlets(true);
89
90         ServletHolder holder = servlets.addServlet("/*", InternalHttpServiceServlet.class.getName()); //$NON-NLS-1$
91
holder.setInitOrder(0);
92         holder.setInitParameter(Constants.SERVICE_VENDOR, "Eclipse.org"); //$NON-NLS-1$
93
holder.setInitParameter(Constants.SERVICE_DESCRIPTION, "Equinox Jetty-based Http Service"); //$NON-NLS-1$
94
if (httpListener != null)
95             holder.setInitParameter(HTTP_PORT, new Integer JavaDoc(httpListener.getPort()).toString());
96         if (httpsListener != null)
97             holder.setInitParameter(HTTPS_PORT, new Integer JavaDoc(httpsListener.getPort()).toString());
98
99         String JavaDoc otherInfo = (String JavaDoc) dictionary.get(OTHER_INFO);
100         if (otherInfo != null)
101             holder.setInitParameter(OTHER_INFO, otherInfo);
102
103         HttpContext httpContext = createHttpContext(dictionary);
104         httpContext.addHandler(servlets);
105
106         Integer JavaDoc sessionInactiveInterval = (Integer JavaDoc) dictionary.get(CONTEXT_SESSIONINACTIVEINTERVAL);
107         if (sessionInactiveInterval != null)
108             servlets.setSessionInactiveInterval(sessionInactiveInterval.intValue());
109
110         server.addContext(httpContext);
111         try {
112             server.start();
113         } catch (Exception JavaDoc e) {
114             throw new ConfigurationException(pid, e.getMessage(), e);
115         }
116         servers.put(pid, server);
117     }
118
119     public synchronized void shutdown() throws Exception JavaDoc {
120         for (Iterator it = servers.values().iterator(); it.hasNext();) {
121             HttpServer server = (HttpServer) it.next();
122             server.stop();
123         }
124         servers.clear();
125     }
126
127     private SocketListener createHttpListener(Dictionary dictionary) {
128         Boolean JavaDoc httpEnabled = (Boolean JavaDoc) dictionary.get(HTTP_ENABLED);
129         if (httpEnabled != null && !httpEnabled.booleanValue())
130             return null;
131
132         Integer JavaDoc httpPort = (Integer JavaDoc) dictionary.get(HTTP_PORT);
133         if (httpPort == null)
134             return null;
135
136         SocketListener listener = new SocketListener();
137         listener.setPort(httpPort.intValue());
138
139         String JavaDoc httpHost = (String JavaDoc) dictionary.get(HTTP_HOST);
140         if (httpHost != null) {
141             try {
142                 listener.setHost(httpHost);
143             } catch (UnknownHostException JavaDoc e) {
144                 // if the host name is invalid we do not want to create this listener
145
e.printStackTrace();
146                 return null;
147             }
148         }
149
150         if (listener.getPort() == 0) {
151             try {
152                 listener.open();
153             } catch (IOException JavaDoc e) {
154                 // this would be unexpected since we're opening the next available port
155
e.printStackTrace();
156             }
157         }
158         return listener;
159     }
160
161     private SocketListener createHttpsListener(Dictionary dictionary) {
162         Boolean JavaDoc httpsEnabled = (Boolean JavaDoc) dictionary.get(HTTPS_ENABLED);
163         if (httpsEnabled == null || !httpsEnabled.booleanValue())
164             return null;
165
166         Integer JavaDoc httpsPort = (Integer JavaDoc) dictionary.get(HTTPS_PORT);
167         if (httpsPort == null)
168             return null;
169
170         SslListener listener = new SslListener();
171         listener.setPort(httpsPort.intValue());
172
173         String JavaDoc httpsHost = (String JavaDoc) dictionary.get(HTTPS_HOST);
174         if (httpsHost != null) {
175             try {
176                 listener.setHost(httpsHost);
177             } catch (UnknownHostException JavaDoc e) {
178                 // if the host name is invalid we do not want to use this listener
179
e.printStackTrace();
180                 return null;
181             }
182         }
183
184         String JavaDoc keyStore = (String JavaDoc) dictionary.get(SSL_KEYSTORE);
185         if (keyStore != null)
186             listener.setKeystore(keyStore);
187
188         String JavaDoc password = (String JavaDoc) dictionary.get(SSL_PASSWORD);
189         if (password != null)
190             listener.setPassword(password);
191
192         String JavaDoc keyPassword = (String JavaDoc) dictionary.get(SSL_KEYPASSWORD);
193         if (keyPassword != null)
194             listener.setKeyPassword(keyPassword);
195
196         Object JavaDoc needClientAuth = dictionary.get(SSL_NEEDCLIENTAUTH);
197         if (needClientAuth != null) {
198             if (needClientAuth instanceof String JavaDoc)
199                 needClientAuth = Boolean.valueOf((String JavaDoc)needClientAuth);
200             
201             listener.setNeedClientAuth(((Boolean JavaDoc) needClientAuth).booleanValue());
202         }
203
204         Object JavaDoc wantClientAuth = (Boolean JavaDoc) dictionary.get(SSL_WANTCLIENTAUTH);
205         if (wantClientAuth != null) {
206             if (wantClientAuth instanceof String JavaDoc)
207                 wantClientAuth = Boolean.valueOf((String JavaDoc)wantClientAuth);
208             
209             listener.setWantClientAuth(((Boolean JavaDoc) wantClientAuth).booleanValue());
210         }
211
212         String JavaDoc protocol = (String JavaDoc) dictionary.get(SSL_PROTOCOL);
213         if (protocol != null)
214             listener.setProtocol(protocol);
215
216         String JavaDoc algorithm = (String JavaDoc) dictionary.get(SSL_ALGORITHM);
217         if (algorithm != null)
218             listener.setAlgorithm(algorithm);
219
220         String JavaDoc keystoreType = (String JavaDoc) dictionary.get(SSL_KEYSTORETYPE);
221         if (keystoreType != null)
222             listener.setKeystoreType(keystoreType);
223
224         if (listener.getPort() == 0) {
225             try {
226                 listener.open();
227             } catch (IOException JavaDoc e) {
228                 // this would be unexpected since we're opening the next available port
229
e.printStackTrace();
230             }
231         }
232         return listener;
233     }
234
235     private HttpContext createHttpContext(Dictionary dictionary) {
236         HttpContext httpContext = new HttpContext();
237         httpContext.setAttribute(INTERNAL_CONTEXT_CLASSLOADER, Thread.currentThread().getContextClassLoader());
238         httpContext.setClassLoader(this.getClass().getClassLoader());
239
240         String JavaDoc contextPathProperty = (String JavaDoc) dictionary.get(CONTEXT_PATH);
241         if (contextPathProperty == null)
242             contextPathProperty = "/"; //$NON-NLS-1$
243
httpContext.setContextPath(contextPathProperty);
244
245         File JavaDoc contextWorkDir = new File JavaDoc(workDir, DIR_PREFIX + dictionary.get(Constants.SERVICE_PID).hashCode());
246         contextWorkDir.mkdir();
247         httpContext.setTempDirectory(contextWorkDir);
248
249         return httpContext;
250     }
251
252     public static class InternalHttpServiceServlet implements Servlet {
253         private static final long serialVersionUID = 7477982882399972088L;
254         private Servlet httpServiceServlet = new HttpServiceServlet();
255         private ClassLoader JavaDoc contextLoader;
256
257         public void init(ServletConfig config) throws ServletException {
258             ServletContext context = config.getServletContext();
259             contextLoader = (ClassLoader JavaDoc) context.getAttribute(INTERNAL_CONTEXT_CLASSLOADER);
260
261             Thread JavaDoc thread = Thread.currentThread();
262             ClassLoader JavaDoc current = thread.getContextClassLoader();
263             thread.setContextClassLoader(contextLoader);
264             try {
265                 httpServiceServlet.init(config);
266             } finally {
267                 thread.setContextClassLoader(current);
268             }
269         }
270
271         public void destroy() {
272             Thread JavaDoc thread = Thread.currentThread();
273             ClassLoader JavaDoc current = thread.getContextClassLoader();
274             thread.setContextClassLoader(contextLoader);
275             try {
276                 httpServiceServlet.destroy();
277             } finally {
278                 thread.setContextClassLoader(current);
279             }
280             contextLoader = null;
281         }
282
283         public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException JavaDoc {
284             Thread JavaDoc thread = Thread.currentThread();
285             ClassLoader JavaDoc current = thread.getContextClassLoader();
286             thread.setContextClassLoader(contextLoader);
287             try {
288                 httpServiceServlet.service(req, res);
289             } finally {
290                 thread.setContextClassLoader(current);
291             }
292         }
293
294         public ServletConfig getServletConfig() {
295             return httpServiceServlet.getServletConfig();
296         }
297
298         public String JavaDoc getServletInfo() {
299             return httpServiceServlet.getServletInfo();
300         }
301     }
302
303
304     // deleteDirectory is a convenience method to recursively delete a directory
305
private static boolean deleteDirectory(File JavaDoc directory) {
306         if (directory.exists() && directory.isDirectory()) {
307             File JavaDoc[] files = directory.listFiles();
308             for (int i = 0; i < files.length; i++) {
309                 if (files[i].isDirectory()) {
310                     deleteDirectory(files[i]);
311                 } else {
312                     files[i].delete();
313                 }
314             }
315         }
316         return directory.delete();
317     }
318 }
319
Popular Tags