001// Copyright 2011, 2013 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.test;
016
017import org.apache.catalina.connector.Connector;
018import org.apache.catalina.core.StandardContext;
019import org.apache.catalina.startup.Tomcat;
020import org.apache.tapestry5.test.constants.TapestryRunnerConstants;
021
022import java.io.File;
023
024/**
025 * Launches an instance of Tomcat.
026 *
027 * @since 5.3
028 */
029public class TomcatRunner implements ServletContainerRunner
030{
031    private final String description;
032    private final int port;
033    private final int sslPort;
034    private Tomcat tomcatServer;
035
036    public TomcatRunner(String webappFolder, String contextPath, int port, int sslPort) throws Exception
037    {
038        this.port = port;
039
040        this.sslPort = sslPort;
041
042        final String expandedPath = expand(webappFolder);
043
044        description = String.format("<TomcatRunner:%s:%s/%s (%s)", contextPath, port, sslPort, expandedPath);
045
046        tomcatServer = new Tomcat();
047
048        // Tomcat creates a folder, try to put it in an OS agnostic tmp dir
049        String tmpDir = System.getProperty("java.io.tmpdir");
050        String fileSeparator = System.getProperty("file.separator");
051        if (!tmpDir.endsWith(fileSeparator))
052            tmpDir = tmpDir + fileSeparator;
053        tomcatServer.setBaseDir(tmpDir + "tomcat");
054
055        tomcatServer.addWebapp("/", expandedPath);
056
057        tomcatServer.getConnector().setAllowTrace(true);
058
059        // SSL support
060        final File keystoreFile = new File(TapestryRunnerConstants.MODULE_BASE_DIR, "src/test/conf/keystore");
061
062        if (keystoreFile.exists())
063        {
064            final Connector https = new Connector();
065            https.setPort(sslPort);
066            https.setProperty("keystore", keystoreFile.getPath());
067            https.setProperty("keypass", "tapestry");
068            tomcatServer.getService().addConnector(https);
069        }
070
071        tomcatServer.start();
072    }
073
074    /**
075     * Immediately shuts down the server instance.
076     */
077    @Override
078    public void stop()
079    {
080        System.out.printf("Stopping Tomcat instance on port %d/%d\n", port, sslPort);
081
082        try
083        {
084            // Stop immediately and not gracefully.
085            tomcatServer.stop();
086        }
087        catch (Exception ex)
088        {
089            throw new RuntimeException("Error stopping Tomcat instance: " + ex.toString(), ex);
090        }
091
092        System.out.println("Tomcat instance has stopped.");
093    }
094
095    @Override
096    public String toString()
097    {
098        return description;
099    }
100
101    /**
102     * Needed inside Maven multi-projects to expand a path relative to the module to a complete
103     * path. If the path already is absolute and points to an existing directory, it will be used
104     * unchanged.
105     *
106     * @param moduleLocalPath
107     * @return expanded path
108     * @see TapestryRunnerConstants#MODULE_BASE_DIR
109     */
110    protected String expand(String moduleLocalPath)
111    {
112        File path = new File(moduleLocalPath);
113
114        // Don't expand if the path provided already exists.
115        if (path.isAbsolute() && path.isDirectory())
116            return moduleLocalPath;
117
118        return new File(TapestryRunnerConstants.MODULE_BASE_DIR, moduleLocalPath).getPath();
119    }
120
121}