webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 11 Oct 2009 19:32:48 +0200
changeset 123 4529cb22ff7d
parent 121 95dfb04fcee1
child 124 90371f3eb106
permissions -rw-r--r--
Support for --kill mode in the webidor.jar
     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.FileInputStream;
    36 import java.io.IOException;
    37 import java.net.ServerSocket;
    38 import java.util.HashMap;
    39 import java.util.Map;
    40 import java.util.Properties;
    41 import java.util.UUID;
    42 import javax.ws.rs.GET;
    43 import javax.ws.rs.PUT;
    44 import javax.ws.rs.Path;
    45 import javax.ws.rs.Produces;
    46 import javax.ws.rs.QueryParam;
    47 import javax.ws.rs.core.MediaType;
    48 
    49 /**
    50  *
    51  * @author Jaroslav Tulach <jtulach@netbeans.org>
    52  */
    53 @Path("/api")
    54 @Singleton
    55 public final class Quoridor {
    56     private final File path;
    57     private Games games;
    58     private final Map<UUID,String> loggedIn;
    59 
    60     public Quoridor() {
    61         final String prop = System.getProperty("quoridor.dir"); // NOI18N
    62         if (prop == null) {
    63             throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
    64         }
    65         path = new File(prop);
    66         path.mkdirs();
    67         loggedIn = new HashMap<UUID, String>();
    68     }
    69 
    70     @Path("games")
    71     public Games getGames() {
    72         if (games == null) {
    73             games = new Games(new File(path, "games"), this); // NOI18N
    74         }
    75         return games;
    76     }
    77 
    78     @Path("login")
    79     @PUT
    80     @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    81     public String login(
    82         @QueryParam("name") String name,
    83         @QueryParam("password") String password
    84     ) {
    85         File f = new File(path, "passwd"); // NOI18Nt
    86         Properties p = new Properties();
    87         try {
    88             p.load(new FileInputStream(f));
    89         } catch (IOException ex) {
    90             ex.printStackTrace();
    91         }
    92         if (name != null && password.equals(p.getProperty(name))) {
    93             UUID uuid = UUID.randomUUID();
    94             loggedIn.put(uuid, name);
    95             return uuid.toString();
    96         } else {
    97             return null;
    98         }
    99         
   100     }
   101 
   102     @Path("login")
   103     @GET
   104     @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
   105     public String isLoggedIn(
   106         @QueryParam("id") String id
   107     ) {
   108         String ret = null;
   109         try {
   110             if (id != null) {
   111                 ret = loggedIn.get(UUID.fromString(id));
   112             }
   113         } catch (IllegalArgumentException ex) {
   114             // OK, happens for invalid ids
   115         }
   116         return ret == null ? "" : ret;
   117     }
   118 
   119     //
   120     // start the server
   121     //
   122 
   123     public static void main(String[] args) throws IOException, InterruptedException {
   124         int port = 9222;
   125         try {
   126             port = Integer.parseInt(args[0]);
   127         } catch (Exception ex) {
   128             // OK
   129         }
   130         HttpServer s = start(port);
   131         System.out.println(
   132             "Quoridor started at port " + port + "\n" + "Hit enter to stop it..."
   133         );
   134         if (args.length < 2 || !args[args.length - 1].equals("--kill")) {
   135             System.out.println("Hit enter to stop it...");
   136             System.in.read();
   137         } else {
   138             synchronized (Quoridor.class) {
   139                 Quoridor.class.wait();
   140             }
   141         }
   142         System.in.read();
   143         s.stop(0);
   144         System.exit(0);
   145     }
   146 
   147     public static HttpServer start(int port) throws IOException {
   148         if (port == -1) {
   149             ServerSocket ss = new ServerSocket(0);
   150             port =ss.getLocalPort();
   151             ss.close();
   152         }
   153         final String baseUri = "http://localhost:" + port + "/";
   154 
   155         if (System.getProperty("quoridor.dir") == null) {
   156             File home = new File(System.getProperty("user.home"));
   157             File quoridor = new File(home, ".quoridor");
   158             System.setProperty("quoridor.dir", quoridor.getPath());
   159         }
   160 
   161         ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
   162         HttpServer server = HttpServerFactory.create(baseUri, rc);
   163         server.start();
   164         return server;
   165     }
   166 
   167 }