KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > jetty > container > JettyContainer


1 /*
2  * $Id: JettyContainer.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.jetty.container;
26
27 import java.io.IOException JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.apache.log4j.Priority;
37 import org.mortbay.http.NCSARequestLog;
38 import org.mortbay.http.SocketListener;
39 import org.mortbay.http.SunJsseListener;
40 import org.mortbay.http.ajp.AJP13Listener;
41 import org.mortbay.jetty.Server;
42 import org.mortbay.jetty.servlet.WebApplicationContext;
43 import org.mortbay.jetty.servlet.SessionManager;
44 import org.mortbay.jetty.servlet.HashSessionManager;
45 import org.mortbay.util.Frame;
46 import org.mortbay.util.Log;
47 import org.mortbay.util.LogSink;
48 import org.mortbay.util.MultiException;
49 import org.mortbay.util.ThreadedServer;
50
51 import org.ofbiz.base.component.ComponentConfig;
52 import org.ofbiz.base.util.Debug;
53 import org.ofbiz.base.util.UtilURL;
54 import org.ofbiz.base.util.SSLUtil;
55 import org.ofbiz.base.container.Container;
56 import org.ofbiz.base.container.ContainerException;
57 import org.ofbiz.base.container.ContainerConfig;
58
59 /**
60  * JettyContainer - Container implementation for Jetty
61  * This container depends on the ComponentContainer as well.
62  *
63  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
64  * @version $Rev: 5462 $
65  * @since 3.0
66  */

67 public class JettyContainer implements Container {
68
69     public static final String JavaDoc module = JettyContainer.class.getName();
70
71     protected String JavaDoc configFile = null;
72     private Map JavaDoc servers = new HashMap JavaDoc();
73
74     /**
75      * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
76      */

77     public void init(String JavaDoc[] args, String JavaDoc configFile) {
78         this.configFile = configFile;
79     }
80
81     private void initJetty() throws ContainerException {
82         // configure JSSE properties
83
SSLUtil.loadJsseProperties();
84
85         // configure jetty logging
86
Log log = Log.instance();
87         log.disableLog();
88         Log4jSink sink = new Log4jSink();
89         log.add(sink);
90         sink.setOptions(UtilURL.fromResource("debug.properties").toExternalForm());
91         try {
92             sink.start();
93         } catch (Exception JavaDoc e) {
94             Debug.logWarning(e, module);
95         }
96
97         // get the container
98
ContainerConfig.Container jc = ContainerConfig.getContainer("jetty-container", configFile);
99
100         // create the servers
101
Iterator JavaDoc sci = jc.properties.values().iterator();
102         while (sci.hasNext()) {
103             ContainerConfig.Container.Property prop = (ContainerConfig.Container.Property) sci.next();
104             servers.put(prop.name, createServer(prop));
105         }
106
107         // load the applications
108
Collection JavaDoc componentConfigs = ComponentConfig.getAllComponents();
109         if (componentConfigs != null) {
110             Iterator JavaDoc components = componentConfigs.iterator();
111             while (components.hasNext()) {
112                 ComponentConfig component = (ComponentConfig) components.next();
113                 Iterator JavaDoc appInfos = component.getWebappInfos().iterator();
114                 while (appInfos.hasNext()) {
115                     ComponentConfig.WebappInfo appInfo = (ComponentConfig.WebappInfo) appInfos.next();
116                     List JavaDoc virtualHosts = appInfo.getVirtualHosts();
117                     Map JavaDoc initParameters = appInfo.getInitParameters();
118                     Server server = (Server) servers.get(appInfo.server);
119                     if (server == null) {
120                         Debug.logWarning("Server with name [" + appInfo.server + "] not found; not mounting [" + appInfo.name + "]", module);
121                     } else {
122                         try {
123                             // set the root location (make sure we set the paths correctly)
124
String JavaDoc location = component.getRootLocation() + appInfo.location;
125                             location = location.replace('\\', '/');
126                             if (!location.endsWith("/")) {
127                                 location = location + "/";
128                             }
129
130                             // load the application
131
WebApplicationContext ctx = server.addWebApplication(appInfo.mountPoint, location);
132                             ctx.setAttribute("_serverId", appInfo.server);
133
134                             // set the session manager
135
SessionManager sm = new HashSessionManager();
136                             ctx.getWebApplicationHandler().setSessionManager(sm);
137                             
138                             // set the virtual hosts
139
Iterator JavaDoc vh = virtualHosts.iterator();
140                             while (vh.hasNext()) {
141                                 ctx.addVirtualHost((String JavaDoc)vh.next());
142                             }
143
144                             // set the init parameters
145
Iterator JavaDoc ip = initParameters.keySet().iterator();
146                             while (ip.hasNext()) {
147                                 String JavaDoc paramName = (String JavaDoc) ip.next();
148                                 ctx.setInitParameter(paramName, (String JavaDoc) initParameters.get(paramName));
149                             }
150
151                         } catch (IOException JavaDoc e) {
152                             Debug.logError(e, "Problem mounting application [" + appInfo.name + " / " + appInfo.location + "]", module);
153                         }
154                     }
155                 }
156             }
157         }
158     }
159
160     private Server createServer(ContainerConfig.Container.Property serverConfig) throws ContainerException {
161         Server server = new Server();
162
163         // configure the listeners/loggers
164
Iterator JavaDoc properties = serverConfig.properties.values().iterator();
165         while (properties.hasNext()) {
166             ContainerConfig.Container.Property props =
167                     (ContainerConfig.Container.Property) properties.next();
168
169             if ("listener".equals(props.value)) {
170                 if ("default".equals(props.getProperty("type").value)) {
171                     SocketListener listener = new SocketListener();
172                     setListenerOptions(listener, props);
173                     if (props.getProperty("identify-listener") != null) {
174                         boolean identifyListener = "true".equalsIgnoreCase(props.getProperty("identify-listener").value);
175                         listener.setIdentifyListener(identifyListener);
176                     }
177                     if (props.getProperty("buffer-size") != null) {
178                         int value = 0;
179                         try {
180                             value = Integer.parseInt(props.getProperty("buffer-size").value);
181                         } catch (NumberFormatException JavaDoc e) {
182                             value = 0;
183                         }
184                         if (value > 0) {
185                             listener.setBufferSize(value);
186                         }
187                     }
188                     if (props.getProperty("low-resource-persist-time") != null) {
189                         int value = 0;
190                         try {
191                             value = Integer.parseInt(props.getProperty("low-resource-persist-time").value);
192                         } catch (NumberFormatException JavaDoc e) {
193                             value = 0;
194                         }
195                         if (value > 0) {
196                             listener.setLowResourcePersistTimeMs(value);
197                         }
198                     }
199                     server.addListener(listener);
200                 } else if ("sun-jsse".equals(props.getProperty("type").value)) {
201                     SunJsseListener listener = new SunJsseListener();
202                     setListenerOptions(listener, props);
203                     if (props.getProperty("keystore") != null) {
204                         listener.setKeystore(props.getProperty("keystore").value);
205                     }
206                     if (props.getProperty("password") != null) {
207                         listener.setPassword(props.getProperty("password").value);
208                     }
209                     if (props.getProperty("key-password") != null) {
210                         listener.setKeyPassword(props.getProperty("key-password").value);
211                     }
212                     if (props.getProperty("need-client-auth") != null) {
213                         boolean needClientAuth = "true".equalsIgnoreCase(props.getProperty("need-client-auth").value);
214                         listener.setNeedClientAuth(needClientAuth);
215                     }
216                     if (props.getProperty("identify-listener") != null) {
217                         boolean identifyListener = "true".equalsIgnoreCase(props.getProperty("identify-listener").value);
218                         listener.setIdentifyListener(identifyListener);
219                     }
220                     if (props.getProperty("buffer-size") != null) {
221                         int value = 0;
222                         try {
223                             value = Integer.parseInt(props.getProperty("buffer-size").value);
224                         } catch (NumberFormatException JavaDoc e) {
225                             value = 0;
226                         }
227                         if (value > 0) {
228                             listener.setBufferSize(value);
229                         }
230                     }
231                     if (props.getProperty("low-resource-persist-time") != null) {
232                         int value = 0;
233                         try {
234                             value = Integer.parseInt(props.getProperty("low-resource-persist-time").value);
235                         } catch (NumberFormatException JavaDoc e) {
236                             value = 0;
237                         }
238                         if (value > 0) {
239                             listener.setLowResourcePersistTimeMs(value);
240                         }
241                     }
242                     server.addListener(listener);
243                 } else if ("ibm-jsse".equals(props.getProperty("type").value)) {
244                     throw new ContainerException("Listener not supported yet [" + props.getProperty("type").value + "]");
245                 } else if ("nio".equals(props.getProperty("type").value)) {
246                     throw new ContainerException("Listener not supported yet [" + props.getProperty("type").value + "]");
247                 } else if ("ajp13".equals(props.getProperty("type").value)) {
248                     AJP13Listener listener = new AJP13Listener();
249                     setListenerOptions(listener, props);
250                     if (props.getProperty("identify-listener") != null) {
251                         boolean identifyListener = "true".equalsIgnoreCase(props.getProperty("identify-listener").value);
252                         listener.setIdentifyListener(identifyListener);
253                     }
254                     if (props.getProperty("buffer-size") != null) {
255                         int value = 0;
256                         try {
257                             value = Integer.parseInt(props.getProperty("buffer-size").value);
258                         } catch (NumberFormatException JavaDoc e) {
259                             value = 0;
260                         }
261                         if (value > 0) {
262                             listener.setBufferSize(value);
263                         }
264                     }
265                     server.addListener(listener);
266                 }
267             } else if ("request-log".equals(props.value)) {
268                 NCSARequestLog rl = new NCSARequestLog();
269
270                 if (props.getProperty("filename") != null) {
271                     rl.setFilename(props.getProperty("filename").value);
272                 }
273
274                 if (props.getProperty("append") != null) {
275                     rl.setAppend("true".equalsIgnoreCase(props.getProperty("append").value));
276                 }
277
278                 if (props.getProperty("buffered") != null) {
279                     rl.setBuffered("true".equalsIgnoreCase(props.getProperty("buffered").value));
280                 }
281
282                 if (props.getProperty("extended") != null) {
283                     rl.setExtended("true".equalsIgnoreCase(props.getProperty("extended").value));
284                 }
285
286                 if (props.getProperty("timezone") != null) {
287                     rl.setLogTimeZone(props.getProperty("timezone").value);
288                 }
289
290                 if (props.getProperty("date-format") != null) {
291                     rl.setLogDateFormat(props.getProperty("date-format").value);
292                 }
293
294                 if (props.getProperty("retain-days") != null) {
295                     int days = 90;
296                     try {
297                         days = Integer.parseInt(props.getProperty("retain-days").value);
298                     } catch (NumberFormatException JavaDoc e) {
299                         days = 90;
300                     }
301                     rl.setRetainDays(days);
302                 }
303                 server.setRequestLog(rl);
304             }
305         }
306         return server;
307     }
308
309     private void setListenerOptions(ThreadedServer listener, ContainerConfig.Container.Property listenerProps) throws ContainerException {
310         String JavaDoc systemHost = null;
311         if ("default".equals(listenerProps.getProperty("type").value)) {
312             systemHost = System.getProperty(listenerProps.name + ".host");
313         }
314         if (listenerProps.getProperty("host") != null && systemHost == null) {
315             try {
316                 listener.setHost(listenerProps.getProperty("host").value);
317             } catch (UnknownHostException JavaDoc e) {
318                 throw new ContainerException(e);
319             }
320         } else {
321             String JavaDoc host = "0.0.0.0";
322             if (systemHost != null) {
323                 host = systemHost;
324             }
325             try {
326                 listener.setHost(host);
327             } catch (UnknownHostException JavaDoc e) {
328                 throw new ContainerException(e);
329             }
330         }
331
332         String JavaDoc systemPort = null;
333         if ("default".equals(listenerProps.getProperty("type").value)) {
334             systemPort = System.getProperty(listenerProps.name + ".port");
335         }
336         if (listenerProps.getProperty("port") != null && systemPort == null) {
337             int value = 8080;
338             try {
339                 value = Integer.parseInt(listenerProps.getProperty("port").value);
340             } catch (NumberFormatException JavaDoc e) {
341                 value = 8080;
342             }
343             if (value == 0) value = 8080;
344
345             listener.setPort(value);
346         } else {
347             int port = 8080;
348             if (systemPort != null) {
349                 try {
350                     port = Integer.parseInt(systemPort);
351                 } catch (NumberFormatException JavaDoc e) {
352                     port = 8080;
353                 }
354             }
355             listener.setPort(port);
356         }
357
358         if (listenerProps.getProperty("min-threads") != null) {
359             int value = 0;
360             try {
361                 value = Integer.parseInt(listenerProps.getProperty("min-threads").value);
362             } catch (NumberFormatException JavaDoc e) {
363                 value = 0;
364             }
365             if (value > 0) {
366                 listener.setMinThreads(value);
367             }
368         }
369
370         if (listenerProps.getProperty("max-threads") != null) {
371             int value = 0;
372             try {
373                 value = Integer.parseInt(listenerProps.getProperty("max-threads").value);
374             } catch (NumberFormatException JavaDoc e) {
375                 value = 0;
376             }
377             if (value > 0) {
378                 listener.setMaxThreads(value);
379             }
380         }
381
382         if (listenerProps.getProperty("max-idle-time") != null) {
383             int value = 0;
384             try {
385                 value = Integer.parseInt(listenerProps.getProperty("max-idle-time").value);
386             } catch (NumberFormatException JavaDoc e) {
387                 value = 0;
388             }
389             if (value > 0) {
390                 listener.setMaxIdleTimeMs(value);
391             }
392         }
393
394         if (listenerProps.getProperty("linger-time") != null) {
395             int value = 0;
396             try {
397                 value = Integer.parseInt(listenerProps.getProperty("linger-time").value);
398             } catch (NumberFormatException JavaDoc e) {
399                 value = 0;
400             }
401             if (value > 0) {
402                 listener.setLingerTimeSecs(value);
403             }
404         }
405     }
406
407     /**
408      * @see org.ofbiz.base.container.Container#start()
409      */

410     public boolean start() throws ContainerException {
411         // start the server(s)
412
this.initJetty();
413         if (servers != null) {
414             Iterator JavaDoc i = servers.values().iterator();
415             while (i.hasNext()) {
416                 Server server = (Server) i.next();
417                 try {
418                     server.start();
419                 } catch (MultiException e) {
420                     Debug.logError(e, "Jetty Server Multi-Exception", module);
421                     throw new ContainerException(e);
422                 }
423             }
424         }
425         return true;
426     }
427
428     /**
429      * @see org.ofbiz.base.container.Container#stop()
430      */

431     public void stop() throws ContainerException {
432         if (servers != null) {
433             Iterator JavaDoc i = servers.values().iterator();
434             while(i.hasNext()) {
435                 Server server = (Server) i.next();
436                 try {
437                     server.stop();
438                 } catch (InterruptedException JavaDoc e) {
439                     Debug.logWarning(e, module);
440                 }
441             }
442         }
443     }
444 }
445
446 // taken from JettyPlus
447
class Log4jSink implements LogSink {
448
449     private String JavaDoc _options;
450     private transient boolean _started;
451
452     public void setOptions(String JavaDoc filename) {
453         _options=filename;
454     }
455
456     public String JavaDoc getOptions() {
457         return _options;
458     }
459
460     public void start() throws Exception JavaDoc {
461         _started=true;
462     }
463
464     public void stop() {
465         _started=false;
466     }
467
468     public boolean isStarted() {
469         return _started;
470     }
471
472     public void log(String JavaDoc tag, Object JavaDoc msg, Frame frame, long time) {
473         String JavaDoc method = frame.getMethod();
474         int lb = method.indexOf('(');
475         int ld = (lb > 0) ? method.lastIndexOf('.', lb) : method.lastIndexOf('.');
476         if (ld < 0) ld = lb;
477         String JavaDoc class_name = (ld > 0) ? method.substring(0,ld) : method;
478
479         Logger log = Logger.getLogger(class_name);
480
481         Priority priority = Priority.INFO;
482
483         if (Log.DEBUG.equals(tag)) {
484             priority = Priority.DEBUG;
485         } else if (Log.WARN.equals(tag) || Log.ASSERT.equals(tag)) {
486             priority = Priority.ERROR;
487         } else if (Log.FAIL.equals(tag)) {
488             priority = Priority.FATAL;
489         }
490
491         if (!log.isEnabledFor(priority)) {
492             return;
493         }
494
495         log.log(Log4jSink.class.getName(), priority, "" + msg, null);
496     }
497
498     public synchronized void log(String JavaDoc s) {
499         Logger.getRootLogger().log("jetty.log4jSink", Priority.INFO, s, null);
500     }
501 }
502
Popular Tags