1 11 12 package org.eclipse.equinox.http.jetty.internal; 13 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.net.UnknownHostException ; 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 DIR_PREFIX = "pid_"; private static final String INTERNAL_CONTEXT_CLASSLOADER = "org.eclipse.equinox.http.jetty.internal.ContextClassLoader"; 32 static final String HTTP_ENABLED = "http.enabled"; static final String HTTP_PORT = "http.port"; static final String HTTP_HOST = "http.host"; static final String HTTPS_ENABLED = "https.enabled"; static final String HTTPS_HOST = "https.host"; static final String HTTPS_PORT = "https.port"; static final String SSL_KEYSTORE = "ssl.keystore"; static final String SSL_PASSWORD = "ssl.password"; static final String SSL_KEYPASSWORD = "ssl.keypassword"; static final String SSL_NEEDCLIENTAUTH = "ssl.needclientauth"; static final String SSL_WANTCLIENTAUTH = "ssl.wantclientauth"; static final String SSL_PROTOCOL = "ssl.protocol"; static final String SSL_ALGORITHM = "ssl.algorithm"; static final String SSL_KEYSTORETYPE = "ssl.keystoretype"; static final String CONTEXT_PATH = "context.path"; static final String CONTEXT_SESSIONINACTIVEINTERVAL = "context.sessioninactiveinterval"; static final String OTHER_INFO = "other.info"; 51 private Map servers = new HashMap(); 52 private File workDir; 53 54 public HttpServerManager(File workDir) { 55 this.workDir = workDir; 56 } 57 58 public synchronized void deleted(String pid) { 59 HttpServer server = (HttpServer) servers.remove(pid); 60 if (server != null) { 61 try { 62 server.stop(); 63 } catch (Exception e) { 64 e.printStackTrace(); 66 } 67 File contextWorkDir = new File (workDir, DIR_PREFIX + pid.hashCode()); 68 deleteDirectory(contextWorkDir); 69 } 70 } 71 72 public String getName() { 73 return this.getClass().getName(); 74 } 75 76 public synchronized void updated(String 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()); holder.setInitOrder(0); 92 holder.setInitParameter(Constants.SERVICE_VENDOR, "Eclipse.org"); holder.setInitParameter(Constants.SERVICE_DESCRIPTION, "Equinox Jetty-based Http Service"); if (httpListener != null) 95 holder.setInitParameter(HTTP_PORT, new Integer (httpListener.getPort()).toString()); 96 if (httpsListener != null) 97 holder.setInitParameter(HTTPS_PORT, new Integer (httpsListener.getPort()).toString()); 98 99 String otherInfo = (String ) 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 sessionInactiveInterval = (Integer ) 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 e) { 114 throw new ConfigurationException(pid, e.getMessage(), e); 115 } 116 servers.put(pid, server); 117 } 118 119 public synchronized void shutdown() throws Exception { 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 httpEnabled = (Boolean ) dictionary.get(HTTP_ENABLED); 129 if (httpEnabled != null && !httpEnabled.booleanValue()) 130 return null; 131 132 Integer httpPort = (Integer ) 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 httpHost = (String ) dictionary.get(HTTP_HOST); 140 if (httpHost != null) { 141 try { 142 listener.setHost(httpHost); 143 } catch (UnknownHostException e) { 144 e.printStackTrace(); 146 return null; 147 } 148 } 149 150 if (listener.getPort() == 0) { 151 try { 152 listener.open(); 153 } catch (IOException e) { 154 e.printStackTrace(); 156 } 157 } 158 return listener; 159 } 160 161 private SocketListener createHttpsListener(Dictionary dictionary) { 162 Boolean httpsEnabled = (Boolean ) dictionary.get(HTTPS_ENABLED); 163 if (httpsEnabled == null || !httpsEnabled.booleanValue()) 164 return null; 165 166 Integer httpsPort = (Integer ) 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 httpsHost = (String ) dictionary.get(HTTPS_HOST); 174 if (httpsHost != null) { 175 try { 176 listener.setHost(httpsHost); 177 } catch (UnknownHostException e) { 178 e.printStackTrace(); 180 return null; 181 } 182 } 183 184 String keyStore = (String ) dictionary.get(SSL_KEYSTORE); 185 if (keyStore != null) 186 listener.setKeystore(keyStore); 187 188 String password = (String ) dictionary.get(SSL_PASSWORD); 189 if (password != null) 190 listener.setPassword(password); 191 192 String keyPassword = (String ) dictionary.get(SSL_KEYPASSWORD); 193 if (keyPassword != null) 194 listener.setKeyPassword(keyPassword); 195 196 Object needClientAuth = dictionary.get(SSL_NEEDCLIENTAUTH); 197 if (needClientAuth != null) { 198 if (needClientAuth instanceof String ) 199 needClientAuth = Boolean.valueOf((String )needClientAuth); 200 201 listener.setNeedClientAuth(((Boolean ) needClientAuth).booleanValue()); 202 } 203 204 Object wantClientAuth = (Boolean ) dictionary.get(SSL_WANTCLIENTAUTH); 205 if (wantClientAuth != null) { 206 if (wantClientAuth instanceof String ) 207 wantClientAuth = Boolean.valueOf((String )wantClientAuth); 208 209 listener.setWantClientAuth(((Boolean ) wantClientAuth).booleanValue()); 210 } 211 212 String protocol = (String ) dictionary.get(SSL_PROTOCOL); 213 if (protocol != null) 214 listener.setProtocol(protocol); 215 216 String algorithm = (String ) dictionary.get(SSL_ALGORITHM); 217 if (algorithm != null) 218 listener.setAlgorithm(algorithm); 219 220 String keystoreType = (String ) 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 e) { 228 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 contextPathProperty = (String ) dictionary.get(CONTEXT_PATH); 241 if (contextPathProperty == null) 242 contextPathProperty = "/"; httpContext.setContextPath(contextPathProperty); 244 245 File contextWorkDir = new File (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 contextLoader; 256 257 public void init(ServletConfig config) throws ServletException { 258 ServletContext context = config.getServletContext(); 259 contextLoader = (ClassLoader ) context.getAttribute(INTERNAL_CONTEXT_CLASSLOADER); 260 261 Thread thread = Thread.currentThread(); 262 ClassLoader 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 thread = Thread.currentThread(); 273 ClassLoader 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 { 284 Thread thread = Thread.currentThread(); 285 ClassLoader 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 getServletInfo() { 299 return httpServiceServlet.getServletInfo(); 300 } 301 } 302 303 304 private static boolean deleteDirectory(File directory) { 306 if (directory.exists() && directory.isDirectory()) { 307 File [] 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 |