diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-06 13:44:04 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-06 13:53:15 +0100 |
commit | bac2fa8e7e6286d64b50cdf6b7fd32f958080ceb (patch) | |
tree | 95e43cf95350e4a9508c3cf419ade35716fe5c9d /same/src/main/java/com | |
parent | 922ce7491ade85b084f365b8bdcf9f8ef251cf82 (diff) |
Add a class that holds server information.
– Provides access to the context and port, used in testing.
Diffstat (limited to 'same/src/main/java/com')
3 files changed, 58 insertions, 12 deletions
diff --git a/same/src/main/java/com/orbekk/same/SameController.java b/same/src/main/java/com/orbekk/same/SameController.java index 4c923d9..2faf669 100644 --- a/same/src/main/java/com/orbekk/same/SameController.java +++ b/same/src/main/java/com/orbekk/same/SameController.java @@ -1,25 +1,20 @@ package com.orbekk.same; -import org.eclipse.jetty.server.Handler; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.handler.HandlerList; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.googlecode.jsonrpc4j.JsonRpcServer; import com.orbekk.net.HttpUtil; import com.orbekk.paxos.PaxosService; import com.orbekk.paxos.PaxosServiceImpl; import com.orbekk.same.config.Configuration; import com.orbekk.same.http.ServerBuilder; +import com.orbekk.same.http.ServerContainer; import com.orbekk.same.http.StateServlet; public class SameController { private Logger logger = LoggerFactory.getLogger(getClass()); private int port; - private Server server; + private ServerContainer server; private MasterServiceImpl master; private ClientServiceImpl client; private PaxosServiceImpl paxos; @@ -41,7 +36,7 @@ public class SameController { ClientServiceImpl client = new ClientServiceImpl(state, connections); PaxosServiceImpl paxos = new PaxosServiceImpl(""); - Server server = new ServerBuilder(port) + ServerContainer server = new ServerBuilder(port) .withServlet(new StateServlet(), "/_/state") .withService(client.getService(), ClientService.class) .withService(master, MasterService.class) @@ -55,7 +50,7 @@ public class SameController { public SameController( int port, - Server server, + ServerContainer server, MasterServiceImpl master, ClientServiceImpl client, PaxosServiceImpl paxos) { diff --git a/same/src/main/java/com/orbekk/same/http/ServerBuilder.java b/same/src/main/java/com/orbekk/same/http/ServerBuilder.java index a582283..ff29e43 100644 --- a/same/src/main/java/com/orbekk/same/http/ServerBuilder.java +++ b/same/src/main/java/com/orbekk/same/http/ServerBuilder.java @@ -32,9 +32,9 @@ public class ServerBuilder { return withServlet(new RpcServlet(server), pathSpec); } - public Server build() { - Server server = new Server(port); - server.setHandler(getServletContextHandler()); + public ServerContainer build() { + ServerContainer server = ServerContainer.create(port); + server.setContext(getServletContextHandler()); return server; } diff --git a/same/src/main/java/com/orbekk/same/http/ServerContainer.java b/same/src/main/java/com/orbekk/same/http/ServerContainer.java new file mode 100644 index 0000000..cd4dc4d --- /dev/null +++ b/same/src/main/java/com/orbekk/same/http/ServerContainer.java @@ -0,0 +1,51 @@ +package com.orbekk.same.http; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ServerContainer { + Logger logger = LoggerFactory.getLogger(getClass()); + Server server; + int port; + ServletContextHandler context = null; + + public ServerContainer(Server server, int port, ServletContextHandler context) { + this.server = server; + this.port = port; + this.context = context; + } + + public static ServerContainer create(int port) { + Server server = new Server(port); + return new ServerContainer(server, port, null); + } + + public void setContext(ServletContextHandler context) { + server.setHandler(context); + this.context = context; + } + + public int getPort() { + if (port == 0) { + return server.getConnectors()[0].getLocalPort(); + } else { + return port; + } + } + + public void start() throws Exception { + server.start(); + logger.info("Started server on port {}", getPort()); + } + + public void stop() throws Exception { + server.stop(); + logger.info("Server stopped."); + } + + public void join() throws InterruptedException { + server.join(); + } +} |