1 package org.objectweb.celtix.bus.transports.http; 2 3 import java.io.IOException ; 4 import java.net.MalformedURLException ; 5 import java.net.URL ; 6 import java.util.HashMap ; 7 import java.util.Map ; 8 9 import org.mortbay.http.HttpContext; 10 import org.mortbay.http.HttpHandler; 11 import org.mortbay.http.HttpServer; 12 import org.mortbay.http.SocketListener; 13 import org.mortbay.http.SslListener; 14 import org.mortbay.http.handler.AbstractHttpHandler; 15 import org.mortbay.util.InetAddrPort; 16 import org.objectweb.celtix.Bus; 17 import org.objectweb.celtix.bus.configuration.security.SSLServerPolicy; 18 import org.objectweb.celtix.bus.transports.https.JettySslListenerConfigurer; 19 import org.objectweb.celtix.configuration.Configuration; 20 import org.objectweb.celtix.configuration.ConfigurationBuilder; 21 import org.objectweb.celtix.configuration.ConfigurationBuilderFactory; 22 import org.objectweb.celtix.transports.http.configuration.HTTPListenerPolicy; 23 24 25 public final class JettyHTTPServerEngine { 26 private static final long serialVersionUID = 1L; 27 private static final String HTTP_LISTENER_CONFIGURATION_URI = 28 "http://celtix.objectweb.org/bus/transports/http/http-listener-config"; 29 30 private static Map <Integer , JettyHTTPServerEngine> portMap = 31 new HashMap <Integer , JettyHTTPServerEngine>(); 32 33 int servantCount; 34 HttpServer server; 35 SocketListener listener; 36 Configuration config; 37 HTTPListenerPolicy policy; 38 SSLServerPolicy sslPolicy; 39 int port; 40 41 private JettyHTTPServerEngine(Bus bus, String protocol, int p) { 42 port = p; 43 config = createConfiguration(bus, port); 44 policy = config.getObject(HTTPListenerPolicy.class, "httpListener"); 45 sslPolicy = config.getObject(SSLServerPolicy.class, "sslServer"); 46 if (sslPolicy == null && "https".equals(protocol)) { 47 sslPolicy = new SSLServerPolicy(); 48 } 49 } 50 51 private Configuration createConfiguration(Bus bus, int p) { 52 Configuration busCfg = bus.getConfiguration(); 54 String id = "http-listener." + p; 55 ConfigurationBuilder cb = ConfigurationBuilderFactory.getBuilder(null); 56 Configuration cfg = cb.getConfiguration(HTTP_LISTENER_CONFIGURATION_URI, id, busCfg); 57 if (null == cfg) { 58 cfg = cb.buildConfiguration(HTTP_LISTENER_CONFIGURATION_URI, id, busCfg); 59 } 60 return cfg; 61 } 62 63 static synchronized JettyHTTPServerEngine getForPort(Bus bus, String protocol, int p) { 64 JettyHTTPServerEngine ref = portMap.get(p); 65 if (ref == null) { 66 ref = new JettyHTTPServerEngine(bus, protocol, p); 67 portMap.put(p, ref); 68 } 69 return ref; 70 } 71 public static synchronized void destroyForPort(int p) { 72 JettyHTTPServerEngine ref = portMap.remove(p); 73 if (ref != null && ref.server != null) { 74 try { 75 ref.listener.getServerSocket().close(); 76 ref.server.stop(true); 77 ref.server.destroy(); 78 ref.server = null; 79 ref.listener = null; 80 } catch (InterruptedException ex) { 81 ex.printStackTrace(); 82 } catch (IOException ex) { 83 ex.printStackTrace(); 84 } 85 86 } 87 } 88 89 synchronized void addServant(String url, AbstractHttpHandler handler) { 90 91 URL nurl = null; 92 try { 93 nurl = new URL (url); 94 } catch (MalformedURLException e1) { 95 e1.printStackTrace(); 97 } 98 String lpath = nurl.getPath(); 99 100 if (server == null) { 101 server = new HttpServer(); 102 103 if (sslPolicy != null) { 104 listener = new SslListener(new InetAddrPort(port)); 105 SslListener secureListener = (SslListener)listener; 106 107 JettySslListenerConfigurer secureListenerConfigurer = 108 new JettySslListenerConfigurer(config, sslPolicy, secureListener); 109 secureListenerConfigurer.configure(); 110 111 } else { 112 listener = new SocketListener(new InetAddrPort(port)); 113 } 114 115 if (policy.isSetMinThreads()) { 116 listener.setMinThreads(policy.getMinThreads()); 117 } 118 if (policy.isSetMaxThreads()) { 119 listener.setMaxThreads(policy.getMaxThreads()); 120 } 121 if (policy.isSetMaxIdleTimeMs()) { 122 listener.setMaxIdleTimeMs(policy.getMaxIdleTimeMs().intValue()); 123 } 124 if (policy.isSetLowResourcePersistTimeMs()) { 125 listener.setLowResourcePersistTimeMs(policy.getLowResourcePersistTimeMs().intValue()); 126 } 127 128 server.addListener(listener); 129 try { 130 server.start(); 131 } catch (Exception e) { 132 e.printStackTrace(); 134 } 135 } 136 137 String contextName = ""; 138 String servletMap = lpath; 139 int idx = lpath.lastIndexOf('/'); 140 if (idx > 0) { 141 contextName = lpath.substring(0, idx); 142 servletMap = lpath.substring(idx); 143 } 144 final String smap = servletMap; 145 146 147 HttpContext context = server.getContext(contextName); 148 try { 149 context.start(); 150 } catch (Exception e1) { 151 e1.printStackTrace(); 153 } 154 155 if ("".equals(smap) && "".equals(contextName)) { 156 handler.setName("/"); 157 } else { 158 handler.setName(smap); 159 } 160 context.addHandler(handler); 161 try { 162 handler.start(); 163 } catch (Exception e) { 164 e.printStackTrace(); 166 } 167 ++servantCount; 168 } 169 170 synchronized void removeServant(String url) throws IOException { 171 URL nurl = new URL (url); 172 String lpath = nurl.getPath(); 173 174 String contextName = ""; 175 String servletMap = lpath; 176 int idx = lpath.lastIndexOf('/'); 177 if (idx > 0) { 178 contextName = lpath.substring(0, idx); 179 servletMap = lpath.substring(idx); 180 } 181 if ("".equals(servletMap) && "".equals(contextName)) { 182 servletMap = "/"; 183 } 184 185 boolean found = false; 186 if (server != null) { 188 HttpContext context = server.getContext(contextName); 189 for (HttpHandler handler : context.getHandlers()) { 190 if (servletMap.equals(handler.getName())) { 191 try { 192 handler.stop(); 193 } catch (InterruptedException e) { 194 e.printStackTrace(); 196 } 197 context.removeHandler(handler); 198 found = true; 199 } 200 } 201 } 202 if (!found) { 203 System.err.println("Not able to remove the handler"); 204 } 205 206 --servantCount; 207 213 } 214 215 synchronized HttpHandler getServant(String url) throws IOException { 216 URL nurl = new URL (url); 217 String lpath = nurl.getPath(); 218 219 String contextName = ""; 220 String servletMap = lpath; 221 int idx = lpath.lastIndexOf('/'); 222 if (idx > 0) { 223 contextName = lpath.substring(0, idx); 224 servletMap = lpath.substring(idx); 225 } 226 227 HttpHandler ret = null; 228 if (server != null) { 230 HttpContext context = server.getContext(contextName); 231 for (HttpHandler handler : context.getHandlers()) { 232 if (servletMap.equals(handler.getName())) { 233 ret = handler; 234 break; 235 } 236 } 237 } 238 return ret; 239 } 240 } 241 | Popular Tags |