001package ca.bc.webarts.tools;
002
003import java.io.File;
004import java.lang.management.ManagementFactory;
005
006import org.eclipse.jetty.jmx.MBeanContainer;
007import org.eclipse.jetty.security.HashLoginService;
008import org.eclipse.jetty.server.Server;
009import org.eclipse.jetty.webapp.WebAppContext;
010
011public class WebAppServer
012{
013    public static void main( String[] args ) throws Exception
014    {
015        // Create a basic jetty server object that will listen on port 8080.
016        // Note that if you set this to port 0 then a randomly available port
017        // will be assigned that you can either look in the logs for the port,
018        // or programmatically obtain it for use in test cases.
019        Server server = new Server(8088);
020
021        // Setup JMX
022        MBeanContainer mbContainer = new MBeanContainer(
023                ManagementFactory.getPlatformMBeanServer());
024        server.addBean(mbContainer);
025
026        // The WebAppContext is the entity that controls the environment in
027        // which a web application lives and breathes. In this example the
028        // context path is being set to "/" so it is suitable for serving root
029        // context requests and then we see it setting the location of the war.
030        // A whole host of other configurations are available, ranging from
031        // configuring to support annotation scanning in the webapp (through
032        // PlusConfiguration) to choosing where the webapp will unpack itself.
033
034        /*WebAppContext webapp = new WebAppContext();
035        webapp.setContextPath("/");
036        File warFile = new File(
037                "../../jetty-distribution/target/distribution/demo-base/webapps/test.war");
038        webapp.setWar(warFile.getAbsolutePath());*/
039
040        //OR
041        WebAppContext webapp = new WebAppContext("/opt/webapps/static",  // this can be a path to a dir or a war
042                                   "/");
043
044        // A WebAppContext is a ContextHandler as well so it needs to be set to
045        // the server so it is aware of where to send the appropriate requests.
046        server.setHandler(webapp);
047
048        // Configure a LoginService
049        // Since this example is for our test webapp, we need to setup a
050        // LoginService so this shows how to create a very simple hashmap based
051        // one. The name of the LoginService needs to correspond to what is
052        // configured in the webapp's web.xml and since it has a lifecycle of
053        // its own we register it as a bean with the Jetty server object so it
054        // can be started and stopped according to the lifecycle of the server
055        // itself.
056
057        /*HashLoginService loginService = new HashLoginService();
058        loginService.setName("Test Realm");
059        loginService.setConfig("src/test/resources/realm.properties");
060        server.addBean(loginService);*/
061
062        // Start things up!
063        server.start();
064
065        // The use of server.join() the will make the current thread join and
066        // wait until the server is done executing.
067        // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
068        server.join();
069    }
070}