KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > transports > http > JettyHTTPServerEngine


1 package org.objectweb.celtix.bus.transports.http;
2
3 import java.io.IOException JavaDoc;
4 import java.net.MalformedURLException JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
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 JavaDoc HTTP_LISTENER_CONFIGURATION_URI =
28         "http://celtix.objectweb.org/bus/transports/http/http-listener-config";
29     
30     private static Map JavaDoc<Integer JavaDoc, JettyHTTPServerEngine> portMap =
31         new HashMap JavaDoc<Integer JavaDoc, 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 JavaDoc 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         // REVISIT: listener config should not be child of bus configuration
53
Configuration busCfg = bus.getConfiguration();
54         String JavaDoc 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 JavaDoc 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 JavaDoc ex) {
81                 ex.printStackTrace();
82             } catch (IOException JavaDoc ex) {
83                 ex.printStackTrace();
84             }
85             
86         }
87     }
88     
89     synchronized void addServant(String JavaDoc url, AbstractHttpHandler handler) {
90
91         URL JavaDoc nurl = null;
92         try {
93             nurl = new URL JavaDoc(url);
94         } catch (MalformedURLException JavaDoc e1) {
95             // TODO Auto-generated catch block
96
e1.printStackTrace();
97         }
98         String JavaDoc 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 JavaDoc e) {
132                 // TODO Auto-generated catch block
133
e.printStackTrace();
134             }
135         }
136
137         String JavaDoc contextName = "";
138         String JavaDoc 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 JavaDoc smap = servletMap;
145         
146         
147         HttpContext context = server.getContext(contextName);
148         try {
149             context.start();
150         } catch (Exception JavaDoc e1) {
151             // TODO Auto-generated catch block
152
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 JavaDoc e) {
164             // TODO Auto-generated catch block
165
e.printStackTrace();
166         }
167         ++servantCount;
168     }
169     
170     synchronized void removeServant(String JavaDoc url) throws IOException JavaDoc {
171         URL JavaDoc nurl = new URL JavaDoc(url);
172         String JavaDoc lpath = nurl.getPath();
173         
174         String JavaDoc contextName = "";
175         String JavaDoc 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         // REVISIT: how come server can be null?
187
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 JavaDoc e) {
194                         // TODO Auto-generated catch block
195
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         /* Bug in Jetty, we cannot do this. If we restart later, data goes off
208          * someplace unknown
209         if (servantCount == 0) {
210             server.removeListener(listener);
211         }
212         */

213     }
214     
215     synchronized HttpHandler getServant(String JavaDoc url) throws IOException JavaDoc {
216         URL JavaDoc nurl = new URL JavaDoc(url);
217         String JavaDoc lpath = nurl.getPath();
218         
219         String JavaDoc contextName = "";
220         String JavaDoc 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         // REVISIT: how come server can be null?
229
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