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