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  
23  import org.eclipse.jetty.server.Connector;
24  import org.eclipse.jetty.server.Handler;
25  import org.eclipse.jetty.server.Server;
26  import org.eclipse.jetty.server.ServerConnector;
27  import org.eclipse.jetty.server.handler.ContextHandler;
28  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
29  import org.eclipse.jetty.server.handler.ResourceHandler;
30  import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
31  import org.eclipse.jetty.util.resource.Resource;
32  
33  /**
34   * A {@link ContextHandlerCollection} handler may be used to direct a request to
35   * a specific Context. The URI path prefix and optional virtual host is used to
36   * select the context.
37   */
38  public class SplitFileServer
39  {
40      public static void main( String[] args ) throws Exception
41      {
42          // Create the Server object and a corresponding ServerConnector and then
43          // set the port for the connector. In this example the server will
44          // listen on port 8090. If you set this to port 0 then when the server
45          // has been started you can called connector.getLocalPort() to
46          // programmatically get the port the server started on.
47          Server server = new Server();
48          ServerConnector connector = new ServerConnector(server);
49          connector.setPort(8090);
50          server.setConnectors(new Connector[] { connector });
51  
52          // Create a Context Handler and ResourceHandler. The ContextHandler is
53          // getting set to "/" path but this could be anything you like for
54          // builing out your url. Note how we are setting the ResourceBase using
55          // our jetty maven testing utilities to get the proper resource
56          // directory, you needn't use these, you simply need to supply the paths
57          // you are looking to serve content from.
58          ContextHandler context0 = new ContextHandler();
59          context0.setContextPath("/");
60          ResourceHandler rh0 = new ResourceHandler();
61          File dir0 = MavenTestingUtils.getTestResourceDir("dir0");
62          rh0.setBaseResource(Resource.newResource(dir0));
63          context0.setHandler(rh0);
64  
65          // Rinse and repeat the previous item, only specifying a different
66          // resource base.
67          ContextHandler context1 = new ContextHandler();
68          context1.setContextPath("/");
69          ResourceHandler rh1 = new ResourceHandler();
70          File dir1 = MavenTestingUtils.getTestResourceDir("dir1");
71          rh1.setBaseResource(Resource.newResource(dir1));
72          context1.setHandler(rh1);
73  
74          // Create a ContextHandlerCollection and set the context handlers to it.
75          // This will let jetty process urls against the declared contexts in
76          // order to match up content.
77          ContextHandlerCollection contexts = new ContextHandlerCollection();
78          contexts.setHandlers(new Handler[] { context0, context1 });
79  
80          server.setHandler(contexts);
81  
82          // Start things up! 
83          server.start();
84          
85          // Dump the server state
86          System.out.println(server.dump());
87  
88          // The use of server.join() the will make the current thread join and
89          // wait until the server is done executing.
90          // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
91          server.join();
92      }
93  }