webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Aug 2009 16:04:27 +0200
changeset 52 45fb5f885591
parent 48 69e897fe8140
child 82 9ac7acee7d9f
permissions -rw-r--r--
Verifying that empty games can be read
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor.resources;
    28 
    29 import com.sun.jersey.api.container.httpserver.HttpServerFactory;
    30 import com.sun.jersey.api.core.PackagesResourceConfig;
    31 import com.sun.jersey.api.core.ResourceConfig;
    32 import com.sun.jersey.spi.resource.Singleton;
    33 import com.sun.net.httpserver.HttpServer;
    34 import java.io.File;
    35 import java.io.IOException;
    36 import java.net.ServerSocket;
    37 import javax.ws.rs.Path;
    38 
    39 /**
    40  *
    41  * @author Jaroslav Tulach <jtulach@netbeans.org>
    42  */
    43 @Path("/api")
    44 @Singleton
    45 public final class Quoridor {
    46     private final File path;
    47     private Games games;
    48 
    49     public Quoridor() {
    50         final String prop = System.getProperty("quoridor.dir"); // NOI18N
    51         if (prop == null) {
    52             throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
    53         }
    54         path = new File(prop);
    55         path.mkdirs();
    56     }
    57 
    58     @Path("games")
    59     public Games getGames() {
    60         if (games == null) {
    61             games = new Games(new File(path, "games"));
    62         }
    63         return games;
    64     }
    65 
    66     //
    67     // start the server
    68     //
    69 
    70     public static void main(String[] args) throws IOException {
    71         HttpServer s = start(9998);
    72         System.out.println(
    73             "Quoridor started at port 9998\n" + "Hit enter to stop it..."
    74         );
    75         System.in.read();
    76         s.stop(0);
    77         System.exit(0);
    78     }
    79 
    80     public static HttpServer start(int port) throws IOException {
    81         if (port == -1) {
    82             ServerSocket ss = new ServerSocket(0);
    83             port =ss.getLocalPort();
    84             ss.close();
    85         }
    86         final String baseUri = "http://localhost:" + port + "/";
    87 
    88         File home = new File(System.getProperty("user.home"));
    89         File quoridor = new File(home, ".quoridor");
    90 
    91         System.setProperty("quoridor.dir", quoridor.getPath());
    92 
    93         ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
    94         HttpServer server = HttpServerFactory.create(baseUri, rc);
    95         server.start();
    96         return server;
    97     }
    98 
    99 }