Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Excerpt
hiddentrue

How to test using an embedded instance of Jetty6

How to test using an embedded instance of Jetty6

This is a note to describe how to embed an instance of Jetty6 in order to test in the same way as the QuickStart project currently does using Jetty4.

...

Code Block
package com.smarttrust.cd.mysp.tpm;

import org.grlea.log.SimpleLogger;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class Start {
{panel}
    private static final SimpleLogger log = new SimpleLogger(Start.class);
{panel}

{panel}
    /**
     * Main function, starts the Jetty 6 server.
     *
     * @param args
     */
    public static void main(String[] args) {
        Server jettyServer = null;
        try {
            jettyServer = new Server();
{panel}

{panel}
            SocketConnector conn = new SocketConnector();
            conn.setPort(8080);
            jettyServer.setConnectors(new Connector[]{conn});
{panel}

{panel}
            WebAppContext wah = new WebAppContext();
            wah.setContextPath("/myapp");
            wah.setWar("src/main/webapp");
            jettyServer.setHandler(wah);
{panel}

{panel}
            jettyServer.start();
        }
        catch (Exception e) {
            log.fatal("Could not start the Jetty server: " + e);
            if (jettyServer != null) {
                try {
                    jettyServer.stop();
                }
                catch (Exception e1) {
                    log.fatal("Unable to stop the jetty server: " + e1);
                }
            }
        }
    }
}

SSL version of the above

A keystore is needed (can be generated with $JAVA_HOME/bin/keytool -genkey -keystore keystore).

Code Block

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.security.SslSocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class StartSsl {

	public static void main(String[] args) {
		String password = System.getProperty("keystorepassword", "jetty6");
		String keystore = System.getProperty("keystore", "src/main/resources/keystore");
		String war = System.getProperty("warpath", "src/main/webapp");
		String contextPath = System.getProperty("contextpath", "/");
		int sslPort = Integer.parseInt(System.getProperty("port", "8443"));

		Server jettyServer = null;
		try {			
			jettyServer = new Server();

			SslSocketConnector sslConn = new SslSocketConnector();
			sslConn.setPort(sslPort);
			sslConn.setPassword(password);
			sslConn.setKeystore(keystore);

			jettyServer.setConnectors(new Connector[] { sslConn });

			WebAppContext wah = new WebAppContext();
			wah.setContextPath(contextPath);
			wah.setWar(war);
			jettyServer.setHandler(wah);

			jettyServer.start();
		} catch (Exception e) {
			System.out.println("Could not start the Jetty server: " + e);
			if (jettyServer != null) {
				try {
					jettyServer.stop();
{panel				} catch (Exception e1) {
					System.out.println("Unable to stop the jetty server: " + e1);
				}
			}
		}
	}
}