01: /*****************************************************************************
02: * Copyright (C) NanoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: *****************************************************************************/package org.nanocontainer.webcontainer;
09:
10: import org.mortbay.jetty.Connector;
11: import org.mortbay.jetty.RequestLog;
12: import org.mortbay.jetty.Server;
13: import org.mortbay.jetty.handler.HandlerList;
14: import org.mortbay.jetty.handler.RequestLogHandler;
15: import org.mortbay.jetty.nio.BlockingChannelConnector;
16: import org.mortbay.jetty.servlet.Context;
17: import org.picocontainer.PicoContainer;
18: import org.picocontainer.Startable;
19: import org.picocontainer.alternatives.EmptyPicoContainer;
20:
21: public class PicoJettyServer extends EmptyPicoContainer implements
22: PicoContainer, Startable {
23:
24: private final Server server;
25: private final PicoContainer parentContainer;
26:
27: public PicoJettyServer(PicoContainer parentContainer) {
28: this .parentContainer = parentContainer;
29: server = new Server();
30: server.setHandler(new HandlerList());
31: }
32:
33: public PicoJettyServer(String host, int port,
34: PicoContainer parentContainer) {
35: this (parentContainer);
36: createBlockingChannelConnector(host, port);
37: }
38:
39: public Connector createBlockingChannelConnector(String host,
40: int port) {
41: BlockingChannelConnector connector = new BlockingChannelConnector();
42: connector.setHost(host);
43: connector.setPort(port);
44: server.addConnector(connector);
45: return connector;
46: }
47:
48: public PicoContext createContext(String contextPath,
49: boolean withSessionHandler) {
50: Context context = new Context(server, contextPath,
51: Context.SESSIONS);
52: return new PicoContext(context, parentContainer,
53: withSessionHandler);
54: }
55:
56: public PicoWebAppContext addWebApplication(String contextPath,
57: String warFile) {
58: PicoWebAppContext context = new PicoWebAppContext(
59: parentContainer);
60: context.setContextPath(contextPath);
61: context.setExtractWAR(true);
62: context.setWar(warFile);
63: context.setParentLoaderPriority(true);
64: server.addHandler(context);
65: return context;
66: }
67:
68: public void start() {
69: try {
70: server.start();
71: } catch (Exception e) {
72: throw new JettyServerLifecycleException(
73: "Jetty couldn't start", e);
74: }
75: }
76:
77: public void stop() {
78: try {
79: server.stop();
80: } catch (Exception e) {
81: throw new JettyServerLifecycleException(
82: "Jetty couldn't stop", e);
83: }
84: }
85:
86: public void addRequestLog(RequestLog requestLog) {
87: RequestLogHandler handler = new RequestLogHandler();
88: handler.setRequestLog(requestLog);
89: server.addHandler(handler);
90: }
91:
92: }
|