View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.io.File;
22  import java.lang.management.ManagementFactory;
23  
24  import org.eclipse.jetty.jmx.MBeanContainer;
25  import org.eclipse.jetty.security.HashLoginService;
26  import org.eclipse.jetty.server.Server;
27  import org.eclipse.jetty.webapp.WebAppContext;
28  
29  public class OneWebApp
30  {
31      public static void main( String[] args ) throws Exception
32      {
33          // Create a basic jetty server object that will listen on port 8080.
34          // Note that if you set this to port 0 then a randomly available port
35          // will be assigned that you can either look in the logs for the port,
36          // or programmatically obtain it for use in test cases.
37          Server server = new Server(8080);
38  
39          // Setup JMX
40          MBeanContainer mbContainer = new MBeanContainer(
41                  ManagementFactory.getPlatformMBeanServer());
42          server.addBean(mbContainer);
43  
44          // The WebAppContext is the entity that controls the environment in
45          // which a web application lives and breathes. In this example the
46          // context path is being set to "/" so it is suitable for serving root
47          // context requests and then we see it setting the location of the war.
48          // A whole host of other configurations are available, ranging from
49          // configuring to support annotation scanning in the webapp (through
50          // PlusConfiguration) to choosing where the webapp will unpack itself.
51          WebAppContext webapp = new WebAppContext();
52          webapp.setContextPath("/");
53          File warFile = new File(
54                  "../../jetty-distribution/target/distribution/demo-base/webapps/test.war");
55          webapp.setWar(warFile.getAbsolutePath());
56  
57          // A WebAppContext is a ContextHandler as well so it needs to be set to
58          // the server so it is aware of where to send the appropriate requests.
59          server.setHandler(webapp);
60  
61          // Configure a LoginService
62          // Since this example is for our test webapp, we need to setup a
63          // LoginService so this shows how to create a very simple hashmap based
64          // one. The name of the LoginService needs to correspond to what is
65          // configured in the webapp's web.xml and since it has a lifecycle of
66          // its own we register it as a bean with the Jetty server object so it
67          // can be started and stopped according to the lifecycle of the server
68          // itself.
69          HashLoginService loginService = new HashLoginService();
70          loginService.setName("Test Realm");
71          loginService.setConfig("src/test/resources/realm.properties");
72          server.addBean(loginService);
73  
74          // Start things up! 
75          server.start();
76  
77          // The use of server.join() the will make the current thread join and
78          // wait until the server is done executing.
79          // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
80          server.join();
81      }
82  }