CVS: plexus-components/jetty/src/java/org/apache/plexus/jetty JettyAdvancedContainer.java,NONE,1.1

Dan Diephouse <[email protected]> Thu, 17 Jul 2003 16:28:09 -0500
Newsgroups gmane.comp.java.plexus.devel
Message-ID <[email protected]>
Update of /cvsroot/plexus/plexus-components/jetty/src/java/org/apache/plexus/jetty
In directory eng.werken.com:/tmp/cvs-serv6444/jetty/src/java/org/apache/plexus/jetty

Added Files:
	JettyAdvancedContainer.java 
Log Message:
Add AJP and multiple host support via a new ServletContainer
implementation. Let me know thoughts/opinions.  An example config
is below:

      <configuration>
        <listeners>
          <ajp13>
            <host>127.0.0.1</host>
            <port>8009</port>
          </ajp13>
          <socket>
            <host>*</host>
            <port>8080</port>
          </socket>
        </listeners>
        <webappsDirectory>${plexus.home}/web-apps</webappsDirectory>
        <extractWars>true</extractWars>
      </configuration>

--- NEW FILE: JettyAdvancedContainer.java ---
package org.apache.plexus.jetty;

/* ----------------------------------------------------------------------------
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Plexus", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [email protected].
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 * ----------------------------------------------------------------------------
 */

import java.io.File;

import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.activity.Startable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.plexus.logging.AbstractLogEnabled;
import org.mortbay.http.SocketListener;
import org.mortbay.http.ajp.AJP13Listener;
import org.mortbay.util.Log;

/** Avalon component that provides an environment for Web Applications.
 *
 *  @author <a href="mailto:[email protected]">Jason van Zyl</a>
 *  @author <a href="mailto:[email protected]">bob mcwhirter</a>
 *
 *  @version $Id: JettyAdvancedContainer.java,v 1.1 2003/07/17 21:28:00 dandiep Exp $
 */
public class JettyAdvancedContainer
    extends AbstractLogEnabled
    implements Contextualizable, Serviceable, Configurable, Initializable, Startable, ServletContainer
{
    // ----------------------------------------------------------------------
    //     Instance members
    // ----------------------------------------------------------------------

    /** Webapps Directory */
    private File webappsDirectory;

    /** Jetty Server */
    private JettyServer server;

    /** State to indicate that WAR files should be unpacked. */
    private boolean extractWars;

    /** State to indicate that the server should be stopped gracefully. */
    private boolean stopGracefully;

    /** Service Manager. */
    private ServiceManager serviceManager;

    /** ClassLoader. */
    private ClassLoader classLoader;
    
    /** Listeners for the server */
    private Configuration[] ajp13;
    
    private Configuration[] socket;
    
    // ----------------------------------------------------------------------
    //     Constructors
    // ----------------------------------------------------------------------

    /** Construct.
     */
    public JettyAdvancedContainer()
    {
        // intentionally left blank
    }

    // ----------------------------------------------------------------------
    //     Instance methods
    // ----------------------------------------------------------------------

    /** Set the webappsDirectory attribute of the JettyServletContainer object.
     *
     *  @param webappsDirectory The directory of web-apps.
     */
    public void setWebappsDirectory( File webappsDirectory )
    {
        this.webappsDirectory = webappsDirectory;
    }

    /** Get the webappsDirectory attribute of the JettyServletContainer object.
     *
     *  @return The directory of web-apps.
     */
    public File getWebappsDirectory()
    {
        return webappsDirectory;
    }

    /** Set the extractWars attribute of the JettyServletContainer object.
     *
     *  @param extractWars The extraction flag.
     */
    public void setExtractWars( boolean extractWars )
    {
        this.extractWars = extractWars;
    }

    /** Get the extractWars attribute of the JettyServletContainer object
     *
     *  @return The extraction flag.
     */
    public boolean getExtractWars()
    {
        return extractWars;
    }

    /** Set the stopGracefully attribute of the JettyServletContainer object
     *
     *  @param stopGracefully The stop-gracefully flag.
     */
    public void setStopGracefully( boolean stopGracefully )
    {
        this.stopGracefully = stopGracefully;
    }

    /** Get the stopGracefully attribute of the JettyServletContainer object
     *
     *  @return THe stop-gracefully flag.
     */
    public boolean getStopGracefully()
    {
        return stopGracefully;
    }

    // ----------------------------------------------------------------------
    // Lifecylce Management
    // ----------------------------------------------------------------------

    /** @see Contextualizable#contextualize */
    public void contextualize( Context context )
        throws ContextException
    {
        classLoader = (ClassLoader) context.get( "common.classloader" );
    }

    /** @see */
    public void service( ServiceManager serviceBroker )
        throws ServiceException
    {
        this.serviceManager = serviceBroker;
    }

    /** @see */
    public void configure( Configuration configuration )
        throws ConfigurationException
    {
        setWebappsDirectory( new File( configuration.getChild( "webappsDirectory" ).getValue( "web-apps" ) ) );
        setExtractWars( configuration.getChild( "extractWars" ).getValueAsBoolean( true ) );
        setStopGracefully( configuration.getChild( "stopGracefully" ).getValueAsBoolean( true ) );
        
        Configuration listenersConfig = configuration.getChild("listeners");
        ajp13  = listenersConfig.getChildren("ajp13");
        socket = listenersConfig.getChildren("socket");
    }

    public void initialize()
        throws Exception
    {
        // Create a Jetty logsink that is backed by the Avalon
        // logging mechanism.
        AvalonLogSink logSink = new AvalonLogSink();
        logSink.enableLogging( getLogger() );
        Log.instance().add( logSink );

        // Create the Jetty server which understands servlet requests.
        server = new JettyServer();
 
        for( int i = 0; i < ajp13.length; i++ )
        {
            AJP13Listener list = new AJP13Listener();
            
            String host = socket[i].getChild("host").getValue("*");
            if ( !host.equals("*") )
                list.setHost( host );

            list.setPort( ajp13[i].getChild("port").getValueAsInteger(8009) );
            server.addListener( list );
        }
 
        for( int i = 0; i < socket.length; i++ )
        {
            SocketListener list = new SocketListener();
            
            String host = socket[i].getChild("host").getValue("*");
            if ( !host.equals("*") )
                list.setHost( host );

            list.setPort( socket[i].getChild("port").getValueAsInteger(8009) );
            server.addListener( list );
        }
        
        server.setServiceManager( serviceManager );
        
        // This is wrong, but will suffice at the moment. jvz.
        server.setClassLoader( classLoader );

        // Make sure the webapps directory exists.
        if ( getWebappsDirectory().exists() == false )
        {
            getWebappsDirectory().mkdirs();
        }

        server.addWebApplications( "*",
                                   getWebappsDirectory().getCanonicalPath(),
                                   getExtractWars() );
    }

    public void start()
        throws Exception
    {
        server.start();
    }

    public void stop()
        throws Exception
    {
        server.stop( getStopGracefully() );
    }
}