View Javadoc

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