webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Aug 2009 14:37:47 +0200
changeset 48 69e897fe8140
parent 41 c94f68ddef59
child 52 45fb5f885591
permissions -rw-r--r--
Spliting Game into GameId and Game with full info about the state of the game
     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 javax.ws.rs.Path;
    37 
    38 /**
    39  *
    40  * @author Jaroslav Tulach <jtulach@netbeans.org>
    41  */
    42 @Path("/api")
    43 @Singleton
    44 public final class Quoridor {
    45     private final File path;
    46     private Games games;
    47 
    48     public Quoridor() {
    49         final String prop = System.getProperty("quoridor.dir"); // NOI18N
    50         if (prop == null) {
    51             throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
    52         }
    53         path = new File(prop);
    54         path.mkdirs();
    55     }
    56 
    57     @Path("games")
    58     public Games getGames() {
    59         if (games == null) {
    60             games = new Games(new File(path, "games"));
    61         }
    62         return games;
    63     }
    64 
    65     //
    66     // start the server
    67     //
    68 
    69     public static void main(String[] args) throws IOException {
    70         HttpServer s = start(9998);
    71         System.out.println(
    72             "Quoridor started at port 9998\n" + "Hit enter to stop it..."
    73         );
    74         System.in.read();
    75         s.stop(0);
    76         System.exit(0);
    77     }
    78 
    79     public static HttpServer start(int port) throws IOException {
    80         final String baseUri = "http://localhost:" + port + "/";
    81 
    82         File home = new File(System.getProperty("user.home"));
    83         File quoridor = new File(home, ".quoridor");
    84 
    85         System.setProperty("quoridor.dir", quoridor.getPath());
    86 
    87         ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
    88         HttpServer server = HttpServerFactory.create(baseUri, rc);
    89         server.start();
    90         return server;
    91     }
    92 
    93 }