All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jlot.test.TomcatContainer Maven / Gradle / Ivy

There is a newer version: 1.2
Show newest version
package org.jlot.test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.loader.VirtualWebappLoader;
import org.apache.catalina.startup.Tomcat;

public class TomcatContainer
{
	private static final Map serverMap = new HashMap();

	private TomcatContainer ()
	{}

	public static synchronized void start ( TomcatConfig tomcatConfig ) throws Exception
	{
		if (serverMap.get(tomcatConfig) == null)
		{
			Object server = startServer(tomcatConfig);
			serverMap.put(tomcatConfig, server);
		}
	}

	public static synchronized void stop ( TomcatConfig tomcatConfig ) throws Exception
	{
		if (serverMap.get(tomcatConfig) == null)
		{
			throw new IllegalStateException("Server has not been started");
		}
		stopServer(serverMap.get(tomcatConfig));
		serverMap.remove(tomcatConfig);
	}

	public static void stopServer ( Object object ) throws Exception
	{
		Tomcat tomcat = (Tomcat) object;
		tomcat.stop();
	}

	public static Tomcat startServer ( final TomcatConfig tomcatConfig ) throws Exception
	{
		final Tomcat tomcat = new Tomcat();

		Map map = tomcatConfig.getProperties();
		for (String key : map.keySet())
		{
			System.setProperty(key, map.get(key));
		}

		tomcat.setBaseDir("./target/tomcat");
		tomcat.setSilent(true);
		// um zu verhindern dass sich die jmx namen mit www/info überschneiden
		tomcat.getEngine().setName(tomcatConfig.getName());
		configureHttpConnector(tomcat, tomcatConfig);
		configureHttpsConnector(tomcat, tomcatConfig);
		// jndi wird gestartet, sonst bekommen wir eine hässliche Warnung, weil
		// wir in der HibernateConfig das Property
		// hibernate.session_factory_name setzen
		tomcat.enableNaming();
		String absolutePath = new File(tomcatConfig.getPath()).getAbsolutePath();
		Context context = tomcat.addWebapp("", absolutePath);
		VirtualWebappLoader virtualWebappLoader = new VirtualWebappLoader();
		virtualWebappLoader.setVirtualClasspath("../jlot-web/target/classes");
		context.setLoader(virtualWebappLoader);
		context.setUseHttpOnly(false);
		tomcat.getServer().addLifecycleListener(new TomcatStartupTimeLogger(tomcatConfig.getName()));
		tomcat.start();
		new TomcatRunThread(tomcat).start();
		return tomcat;
	}

	private static void configureHttpConnector ( Tomcat tomcat, TomcatConfig tomcatConfig )
	{
		Connector connector = tomcat.getConnector();
		connector.setProperty("server", "kicktipp");
		connector.setPort(tomcatConfig.getPort());
		connector.setRedirectPort(tomcatConfig.getSslPort());
		addCompression(connector);
	}

	private static void addCompression ( Connector c )
	{
		c.setProperty("compression", "on");
		c.setProperty("noCompressionUserAgents", "gozilla, traviata");
		c.setProperty("compressableMimeType", "text/html,text/xml,text/css,text/javascript,application/javascript");
	}

	private static void configureHttpsConnector ( Tomcat tomcat, TomcatConfig tomcatConfig )
	{
		Connector connector = new Connector();
		connector.setProperty("server", "jlot");
		connector.setPort(tomcatConfig.getSslPort());
		connector.setSecure(true);
		connector.setEnableLookups(false);
		connector.setScheme("https");
		connector.setAttribute("SSLEnabled", true);
		File file = new File("../kicktipp.org.keystore");
		String keystoreFile = file.getAbsolutePath();
		connector.setAttribute("keystoreFile", keystoreFile);
		connector.setAttribute("keyAlias", "tomcat");
		connector.setAttribute("keystorePass", "tomcat");
		connector.setAttribute("clientAuth", "false");
		connector.setAttribute("sslProtocol", "TLS");
		addCompression(connector);
		tomcat.getService().addConnector(connector);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy