1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // All rights reserved. This program and the accompanying materials 6 // are made available under the terms of the Eclipse Public License v1.0 7 // and Apache License v2.0 which accompanies this distribution. 8 // 9 // The Eclipse Public License is available at 10 // http://www.eclipse.org/legal/epl-v10.html 11 // 12 // The Apache License v2.0 is available at 13 // http://www.opensource.org/licenses/apache2.0.php 14 // 15 // You may elect to redistribute this code under either of these licenses. 16 // ======================================================================== 17 // 18 19 package org.eclipse.jetty.embedded; 20 21 import java.lang.management.ManagementFactory; 22 23 import org.eclipse.jetty.jmx.MBeanContainer; 24 import org.eclipse.jetty.security.HashLoginService; 25 import org.eclipse.jetty.server.Server; 26 import org.eclipse.jetty.webapp.WebAppContext; 27 28 public class OneWebApp 29 { 30 public static void main(String[] args) throws Exception 31 { 32 // Create a basic jetty server object that will listen on port 8080. Note that if you set this to port 0 then 33 // a randomly available port will be assigned that you can either look in the logs for the port, 34 // or programmatically obtain it for use in test cases. 35 Server server = new Server(8080); 36 37 // Setup JMX 38 MBeanContainer mbContainer=new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); 39 server.addBean(mbContainer); 40 41 // The WebAppContext is the entity that controls the environment in which a web application lives and 42 // breathes. In this example the context path is being set to "/" so it is suitable for serving root context 43 // requests and then we see it setting the location of the war. A whole host of other configurations are 44 // available, ranging from configuring to support annotation scanning in the webapp (through 45 // PlusConfiguration) to choosing where the webapp will unpack itself. 46 WebAppContext webapp = new WebAppContext(); 47 webapp.setContextPath("/"); 48 webapp.setWar("../../jetty-distribution/target/distribution/demo-base/webapps/test.war"); 49 50 // A WebAppContext is a ContextHandler as well so it needs to be set to the server so it is aware of where to 51 // send the appropriate requests. 52 server.setHandler(webapp); 53 54 // Configure a LoginService 55 // Since this example is for our test webapp, we need to setup a LoginService so this shows how to create a 56 // very simple hashmap based one. The name of the LoginService needs to correspond to what is configured in 57 // the webapp's web.xml and since it has a lifecycle of its own we register it as a bean with the Jetty 58 // server object so it can be started and stopped according to the lifecycle of the server itself. 59 HashLoginService loginService = new HashLoginService(); 60 loginService.setName("Test Realm"); 61 loginService.setConfig("src/test/resources/realm.properties"); 62 server.addBean(loginService); 63 64 // Start things up! By using the server.join() the server thread will join with the current thread. 65 // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details. 66 server.start(); 67 server.join(); 68 } 69 }