| 1 | /* |
| 2 | * $Id$ |
| 3 | * |
| 4 | * Copyright 2001,2006 Daniel F. Savarese |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.savarese.org/software/ApacheLicense-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | package org.savarese.barehttp; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.File; |
| 23 | |
| 24 | /** |
| 25 | * The BareHTTP server, a sample program that starts an {@link HTTPServer} |
| 26 | * instance. The program expects two arguments: a port |
| 27 | * number and a fully-qualified directory pathname specifying the |
| 28 | * document root. It could easily be expanded to allow the |
| 29 | * specification of a binding address and maximum number of |
| 30 | * connections. |
| 31 | * |
| 32 | * @author <a href="http://www.savarese.org/">Daniel F. Savarese</a> |
| 33 | */ |
| 34 | public final class Main { |
| 35 | |
| 36 | private Main() {} |
| 37 | |
| 38 | // Static only so we can stop the server in a unit test. |
| 39 | static HTTPServer server = null; |
| 40 | |
| 41 | /** |
| 42 | * Starts an {@link HTTPServer} instance on a specified port |
| 43 | * allowing {@link HTTPServer#DEFAULT_MAX_CONNECTIONS}. No |
| 44 | * provisions are made for terminating the program. The program |
| 45 | * must be terminated by SIGKILL, Ctrl-C, or whatever OS-specific |
| 46 | * mechanism exists to kill a running process. |
| 47 | * |
| 48 | * @param args The program arguments: a port number and document |
| 49 | * root directory. The root directory is converted to a canonical |
| 50 | * path prior to starting the server. |
| 51 | * @throws Exception if the {@link HTTPServer} cannot be instantiated |
| 52 | * or started. |
| 53 | */ |
| 54 | public static void main(String[] args) throws IOException { |
| 55 | if(args.length != 2) { |
| 56 | System.err.println("usage: HTTPServer port documentRoot"); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | server = |
| 61 | new HTTPServer(new File(args[1]).getCanonicalPath(), |
| 62 | Integer.parseInt(args[0]), |
| 63 | HTTPServer.DEFAULT_MAX_CONNECTIONS); |
| 64 | |
| 65 | server.start(); |
| 66 | } |
| 67 | |
| 68 | } |