View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.server.handler.AllowSymLinkAliasChecker;
28  import org.eclipse.jetty.webapp.WebAppContext;
29  
30  public class OneWebApp
31  {
32      public static void main( String[] args ) throws Exception
33      {
34          // Create a basic jetty server object that will listen on port 8080.
35          // Note that if you set this to port 0 then a randomly available port
36          // will be assigned that you can either look in the logs for the port,
37          // or programmatically obtain it for use in test cases.
38          Server server = new Server(8080);
39  
40          // Setup JMX
41          MBeanContainer mbContainer = new MBeanContainer(
42                  ManagementFactory.getPlatformMBeanServer());
43          server.addBean(mbContainer);
44  
45          // The WebAppContext is the entity that controls the environment in
46          // which a web application lives and breathes. In this example the
47          // context path is being set to "/" so it is suitable for serving root
48          // context requests and then we see it setting the location of the war.
49          // A whole host of other configurations are available, ranging from
50          // configuring to support annotation scanning in the webapp (through
51          // PlusConfiguration) to choosing where the webapp will unpack itself.
52          WebAppContext webapp = new WebAppContext();
53          webapp.setContextPath("/");
54          File warFile = new File(
55                  "../../jetty-distribution/target/distribution/test/webapps/test/");
56          webapp.setWar(warFile.getAbsolutePath());
57          webapp.addAliasCheck(new AllowSymLinkAliasChecker());
58  
59          // A WebAppContext is a ContextHandler as well so it needs to be set to
60          // the server so it is aware of where to send the appropriate requests.
61          server.setHandler(webapp);
62  
63          // Start things up! 
64          server.start();
65  
66          // The use of server.join() the will make the current thread join and
67          // wait until the server is done executing.
68          // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
69          server.join();
70      }
71  }