webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
branchglassfish
changeset 284 90be53f96e0c
parent 282 40fc213a7f43
     1.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Fri Apr 15 21:18:29 2011 +0200
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sun Jul 17 15:40:50 2011 +0200
     1.3 @@ -18,19 +18,16 @@
     1.4  
     1.5  package cz.xelfi.quoridor.webidor.resources;
     1.6  
     1.7 -import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
     1.8 -import com.sun.jersey.api.core.PackagesResourceConfig;
     1.9 -import com.sun.jersey.api.core.ResourceConfig;
    1.10  import com.sun.jersey.api.json.JSONWithPadding;
    1.11 -import com.sun.jersey.spi.resource.Singleton;
    1.12  import java.io.File;
    1.13  import java.io.FileInputStream;
    1.14  import java.io.IOException;
    1.15 -import java.net.ServerSocket;
    1.16  import java.util.HashMap;
    1.17  import java.util.Map;
    1.18  import java.util.Properties;
    1.19  import java.util.UUID;
    1.20 +import javax.annotation.ManagedBean;
    1.21 +import javax.inject.Singleton;
    1.22  import javax.ws.rs.GET;
    1.23  import javax.ws.rs.PUT;
    1.24  import javax.ws.rs.Path;
    1.25 @@ -39,7 +36,6 @@
    1.26  import javax.ws.rs.WebApplicationException;
    1.27  import javax.ws.rs.core.MediaType;
    1.28  import javax.ws.rs.core.Response.Status;
    1.29 -import org.glassfish.grizzly.http.server.HttpServer;
    1.30  
    1.31  /**
    1.32   *
    1.33 @@ -47,16 +43,17 @@
    1.34   */
    1.35  @Path("/")
    1.36  @Singleton
    1.37 -public final class Quoridor {
    1.38 +@ManagedBean
    1.39 +public class Quoridor {
    1.40      private final File path;
    1.41      private Games games;
    1.42      private Users users;
    1.43      private final Map<UUID,String> loggedIn;
    1.44  
    1.45      public Quoridor() {
    1.46 -        final String prop = System.getProperty("quoridor.dir"); // NOI18N
    1.47 +        String prop = System.getProperty("quoridor.dir"); // NOI18N
    1.48          if (prop == null) {
    1.49 -            throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
    1.50 +            prop = System.getProperty("user.dir") + File.separatorChar + ".quoridor"; // NOI18N
    1.51          }
    1.52          path = new File(prop);
    1.53          path.mkdirs();
    1.54 @@ -144,53 +141,4 @@
    1.55              return getUsers().getUserInfo(callback, id, ret);
    1.56          }
    1.57      }
    1.58 -
    1.59 -    //
    1.60 -    // start the server
    1.61 -    //
    1.62 -
    1.63 -    public static void main(String[] args) throws IOException, InterruptedException {
    1.64 -        int port = 9222;
    1.65 -        try {
    1.66 -            port = Integer.parseInt(args[0]);
    1.67 -        } catch (Exception ex) {
    1.68 -            // OK
    1.69 -        }
    1.70 -        HttpServer s = start(port);
    1.71 -        System.out.println(
    1.72 -            "Quoridor started at port " + port + "\n" + "Hit enter to stop it..."
    1.73 -        );
    1.74 -        if (args.length < 2 || !args[args.length - 1].equals("--kill")) {
    1.75 -            System.out.println("Hit enter to stop it...");
    1.76 -            System.in.read();
    1.77 -        } else {
    1.78 -            synchronized (Quoridor.class) {
    1.79 -                Quoridor.class.wait();
    1.80 -            }
    1.81 -        }
    1.82 -        System.in.read();
    1.83 -        s.stop();
    1.84 -        System.exit(0);
    1.85 -    }
    1.86 -
    1.87 -    public static HttpServer start(int port) throws IOException {
    1.88 -        if (port == -1) {
    1.89 -            ServerSocket ss = new ServerSocket(0);
    1.90 -            port =ss.getLocalPort();
    1.91 -            ss.close();
    1.92 -        }
    1.93 -        final String baseUri = "http://localhost:" + port + "/";
    1.94 -
    1.95 -        if (System.getProperty("quoridor.dir") == null) {
    1.96 -            File home = new File(System.getProperty("user.home"));
    1.97 -            File quoridor = new File(home, ".quoridor");
    1.98 -            System.setProperty("quoridor.dir", quoridor.getPath());
    1.99 -        }
   1.100 -
   1.101 -        ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.webidor");
   1.102 -        HttpServer server = GrizzlyServerFactory.createHttpServer(baseUri, rc);
   1.103 -        server.start();
   1.104 -        return server;
   1.105 -    }
   1.106 -
   1.107  }