View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.embedded;
15  
16  import org.eclipse.jetty.server.Handler;
17  import org.eclipse.jetty.server.Server;
18  import org.eclipse.jetty.server.handler.DefaultHandler;
19  import org.eclipse.jetty.server.handler.HandlerList;
20  import org.eclipse.jetty.server.handler.ResourceHandler;
21  import org.eclipse.jetty.util.log.Log;
22  
23  /* ------------------------------------------------------------ */
24  /** Simple Jetty FileServer.
25   * This is a simple example of Jetty configured as a FileServer.
26   * 
27   * File server Usage - java org.eclipse.jetty.server.example.FileServer [ port [
28   * docroot ]]
29   * 
30   * @see FileServerXml for the equivalent example done in XML configuration.
31   * @author gregw
32   * 
33   */
34  public class FileServer
35  {
36      public static void main(String[] args) throws Exception
37      {
38          Server server = new Server(args.length == 0?8080:Integer.parseInt(args[0]));
39  
40          ResourceHandler resource_handler = new ResourceHandler();
41          resource_handler.setDirectoriesListed(true);
42          resource_handler.setWelcomeFiles(new String[]{ "index.html" });
43  
44          resource_handler.setResourceBase(args.length == 2?args[1]:".");
45          Log.info("serving " + resource_handler.getBaseResource());
46          
47          HandlerList handlers = new HandlerList();
48          handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
49          server.setHandler(handlers);
50  
51          server.start();
52          server.join();
53      }
54  
55  }