Package org.apache.tapestry.integration

Source Code of org.apache.tapestry.integration.JettyRunner

// Copyright 2006 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry.integration;

import static java.lang.String.format;

import java.io.File;

import org.mortbay.http.NCSARequestLog;
import org.mortbay.http.SocketListener;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.WebApplicationContext;

/**
*
*/
public class JettyRunner
{
    public static final String DEFAULT_CONTEXT_PATH = "/";

    public static final int DEFAULT_PORT = 80;

    private static final String DEFAULTS_DESCRIPTOR = "src/test-data/conf/webdefault.xml";
   
    private final String _contextPath;

    private final int _port;

    private String _warPath;

    private final Server _jetty;
   
    public static void main(String[] args)
    {
        new JettyRunner("src/test-data/app1");
    }

    /** Defaults the context path to "/" and the port to 80. */
    public JettyRunner(String warPath)
    {
        this(DEFAULT_CONTEXT_PATH, DEFAULT_PORT, warPath);
    }

    /**
     * Creates and starts a new instance of Jetty. This should be done from a test case setup
     * method.
     *
     * @param contextPath
     *            the context path for the deployed application
     * @param port
     *            the port number used to access the application
     * @param warPath
     *            the path to the exploded web application (typically, "src/main/webapp")
     */
    public JettyRunner(String contextPath, int port, String warPath)
    {
        _contextPath = contextPath;
        _port = port;
        _warPath = warPath;
       
        _jetty = createAndStart();
    }

    /** Stops the Jetty instance. This should be called from a test case tear down method. */
    public void stop()
    {
        try
        {
            _jetty.stop();
        }
        catch (Exception ex)
        {
            throw new RuntimeException("Error stopping Jetty instance: " + ex.toString(), ex);
        }
    }

    @Override
    public String toString()
    {
        return format("<JettyRunner %s:%d (%s)>", _contextPath, _port, _warPath);
    }

    private Server createAndStart()
    {
        try
        {
            Server server = new Server();

            SocketListener socketListener = new SocketListener();
            socketListener.setPort(_port);
            server.addListener(socketListener);
           
            NCSARequestLog log = new NCSARequestLog();
            server.setRequestLog(log);
           
            File warPath = new File(_warPath);
            if (!warPath.exists()) {
                _warPath = new File("tapestry-framework", _warPath).getPath();
            }
           
            WebApplicationContext context = server.addWebApplication(_contextPath, _warPath);
           
            File descPath = new File(DEFAULTS_DESCRIPTOR);
            String descriptorPath = descPath.getPath();
           
            if (!descPath.exists()) {
                descriptorPath = new File("tapestry-framework", DEFAULTS_DESCRIPTOR).getPath();
            }
           
            context.setDefaultsDescriptor(descriptorPath);

            server.start();

            return server;
        }
        catch (Exception ex)
        {
            throw new RuntimeException("Failure starting Jetty instance: " + ex.toString(), ex);
        }
    }
}
TOP

Related Classes of org.apache.tapestry.integration.JettyRunner

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.