001 /*****************************************************************************
002 * Copyright (C) NanoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file. *
007 * *
008 *****************************************************************************/
009
010 package org.picocontainer.jetty;
011
012 import org.mortbay.jetty.Connector;
013 import org.mortbay.jetty.RequestLog;
014 import org.mortbay.jetty.Server;
015 import org.mortbay.jetty.handler.ErrorHandler;
016 import org.mortbay.jetty.handler.HandlerList;
017 import org.mortbay.jetty.handler.RequestLogHandler;
018 import org.mortbay.jetty.nio.BlockingChannelConnector;
019 import org.mortbay.jetty.servlet.Context;
020 import org.picocontainer.PicoContainer;
021 import org.picocontainer.Startable;
022 import org.picocontainer.containers.EmptyPicoContainer;
023
024 public class PicoJettyServer extends EmptyPicoContainer implements PicoContainer, Startable {
025
026 private final Server server;
027 private final PicoContainer parentContainer;
028 private ErrorHandler errorHandler;
029
030 public PicoJettyServer(PicoContainer parentContainer) {
031 this.parentContainer = parentContainer;
032 server = new Server();
033 server.setHandler(new HandlerList());
034 }
035
036 public PicoJettyServer(String host, int port, PicoContainer parentContainer) {
037 this(parentContainer);
038 createBlockingChannelConnector(host, port);
039 }
040 public PicoJettyServer(String host, int port, PicoContainer parentContainer, int timeout) {
041 this(parentContainer);
042 createBlockingChannelConnector(host, port, timeout);
043 }
044
045 public Connector createBlockingChannelConnector(String host, int port) {
046 return createBlockingChannelConnector(host, port, 10*1000);
047 }
048
049 public Connector createBlockingChannelConnector(String host, int port, int timeout) {
050 BlockingChannelConnector connector = new BlockingChannelConnector();
051 connector.setHost(host);
052 connector.setPort(port);
053 connector.setLowResourceMaxIdleTime(timeout);
054 server.addConnector(connector);
055 return connector;
056 }
057
058 public PicoContext createContext(String contextPath, boolean withSessionHandler) {
059 Context context = new Context(server, contextPath, Context.SESSIONS);
060 return new PicoContext(context, parentContainer, withSessionHandler);
061 }
062
063
064 public PicoWebAppContext addWebApplication(String contextPath, String warFile) {
065 PicoWebAppContext wah = new PicoWebAppContext(parentContainer);
066 wah.setContextPath(contextPath);
067 wah.setExtractWAR(true);
068 wah.setWar(warFile);
069 wah.setParentLoaderPriority(true);
070 server.addHandler(wah);
071 return wah;
072 }
073
074
075 public void start() {
076 try {
077 server.start();
078 } catch (Exception e) {
079 e.printStackTrace();
080 throw new JettyServerLifecycleException("Jetty couldn't start", e);
081 }
082 }
083
084 public void stop() {
085 try {
086 server.stop();
087 } catch (Exception e) {
088 throw new JettyServerLifecycleException("Jetty couldn't stop", e);
089 }
090 }
091
092 public void addRequestLog(RequestLog requestLog) {
093 RequestLogHandler requestLogHandler = new RequestLogHandler();
094 requestLogHandler.setRequestLog(requestLog);
095 server.addHandler(requestLogHandler);
096
097 }
098
099 }