webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 29 Jul 2009 17:24:20 +0200
changeset 38 373f537e0153
parent 37 782d925cb5a1
child 41 c94f68ddef59
permissions -rw-r--r--
It is possible to play the game from command line with curl
     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.util.HashMap;
    37 import java.util.Map;
    38 import javax.ws.rs.Path;
    39 
    40 /**
    41  *
    42  * @author Jaroslav Tulach <jtulach@netbeans.org>
    43  */
    44 @Path("/")
    45 @Singleton
    46 public final class Quoridor {
    47     private final File path;
    48     private Games games;
    49 
    50     public Quoridor() {
    51         final String prop = System.getProperty("quoridor.dir"); // NOI18N
    52         if (prop == null) {
    53             throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
    54         }
    55         path = new File(prop);
    56         path.mkdirs();
    57     }
    58 
    59     @Path("games")
    60     public Games getGames() {
    61         if (games == null) {
    62             games = new Games(new File(path, "games"));
    63         }
    64         return games;
    65     }
    66 
    67     //
    68     // start the server
    69     //
    70 
    71     public static void main(String[] args) throws IOException {
    72 
    73         final String baseUri = "http://localhost:9998/";
    74         final Map<String, String> initParams = new HashMap<String, String>();
    75 
    76         File home = new File(System.getProperty("user.home"));
    77         File quoridor = new File(home, ".quoridor");
    78 
    79         System.setProperty("quoridor.dir", quoridor.getPath());
    80 
    81         initParams.put("com.sun.jersey.config.property.packages",
    82                 "cz.xelfi.quoridor.webidor.resources");
    83 
    84         System.out.println("Starting Quoridor...");
    85         ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
    86         HttpServer threadSelector = HttpServerFactory.create(baseUri, rc);
    87         threadSelector.start();
    88         System.out.println(String.format(
    89                     "Quoridor started with WADL available at %sapplication.wadl\n" +
    90             "Hit enter to stop it...", baseUri, baseUri));
    91             System.in.read();
    92             threadSelector.stop(0);
    93             System.exit(0);
    94     }
    95 
    96 }