KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > webcontainer > PicoJettyServer


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  *****************************************************************************/

9
10 package org.nanocontainer.webcontainer;
11
12 import org.mortbay.jetty.Connector;
13 import org.mortbay.jetty.Server;
14 import org.mortbay.jetty.RequestLog;
15 import org.mortbay.jetty.servlet.ErrorPageErrorHandler;
16 import org.mortbay.jetty.handler.ContextHandler;
17 import org.mortbay.jetty.handler.HandlerList;
18 import org.mortbay.jetty.handler.RequestLogHandler;
19 import org.mortbay.jetty.handler.ErrorHandler;
20 import org.mortbay.jetty.nio.BlockingChannelConnector;
21 import org.picocontainer.PicoContainer;
22 import org.picocontainer.alternatives.EmptyPicoContainer;
23
24 public class PicoJettyServer extends EmptyPicoContainer implements PicoContainer {
25
26     private final Server server;
27     private final PicoContainer parentContainer;
28     private ErrorHandler errorHandler;
29
30     public PicoJettyServer(PicoContainer parentContainer) {
31         this.parentContainer = parentContainer;
32         server = new Server();
33         server.setHandler(new HandlerList());
34     }
35
36     public PicoJettyServer(String JavaDoc host, int port, PicoContainer parentContainer) {
37         this(parentContainer);
38         createBlockingChannelConnector(host, port);
39     }
40
41     public Connector createBlockingChannelConnector(String JavaDoc host, int port) {
42         BlockingChannelConnector connector = new BlockingChannelConnector();
43         connector.setHost(host);
44         connector.setPort(port);
45         server.addConnector(connector);
46         return connector;
47     }
48
49     public PicoContextHandler createContext(String JavaDoc contextPath, boolean withSessionHandler) {
50         ContextHandler context = new ContextHandler();
51         context.setContextPath(contextPath);
52         server.addHandler(context);
53         return new PicoContextHandler(context, server, parentContainer, withSessionHandler);
54     }
55
56
57     public PicoWebAppContext addWebApplication(String JavaDoc contextPath, String JavaDoc warFile) {
58         PicoWebAppContext wah = new PicoWebAppContext(parentContainer);
59         wah.setContextPath(contextPath);
60         wah.setExtractWAR(true);
61         wah.setWar(warFile);
62         wah.setParentLoaderPriority(true);
63         server.addHandler(wah);
64         return wah;
65     }
66
67
68     public void start() {
69         try {
70             server.start();
71         } catch (Exception JavaDoc e) {
72             e.printStackTrace();
73             throw new JettyServerLifecycleException("Jetty couldn't start", e);
74         }
75     }
76
77     public void stop() {
78         try {
79             server.stop();
80         } catch (Exception JavaDoc e) {
81             throw new JettyServerLifecycleException("Jetty couldn't stop", e);
82         }
83     }
84
85     public void addRequestLog(RequestLog requestLog) {
86         RequestLogHandler requestLogHandler = new RequestLogHandler();
87         requestLogHandler.setRequestLog(requestLog);
88         server.addHandler(requestLogHandler);
89
90     }
91
92 }
93
Popular Tags