diff options
4 files changed, 75 insertions, 38 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(); + } +} diff --git a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java index b09b76e..5042168 100644 --- a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java +++ b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java @@ -4,37 +4,46 @@ import static org.junit.Assert.*; import com.googlecode.jsonrpc4j.JsonRpcServer; import com.orbekk.same.ConnectionManagerImpl; -import com.orbekk.same.http.RpcHandler; +import com.orbekk.same.http.RpcServlet; +import com.orbekk.same.http.ServerBuilder; +import com.orbekk.same.http.ServerContainer; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; +import org.junit.After; import org.junit.Before; import org.junit.Test; public class PaxosServiceFunctionalTest { ConnectionManagerImpl connections = new ConnectionManagerImpl(500, 500); List<String> paxosUrls = new ArrayList<String>(); - RpcHandler handler = new RpcHandler(null); - TestServer server; + // RpcHandler handler = new RpcHandler(null); + ServerContainer server; String myUrl; int successfulProposals = 0; @Before public void setUp() throws Exception { - server = TestServer.create(handler); - myUrl = "http://localhost:" + server.port; - setupPaxos(10); + ServerBuilder builder = new ServerBuilder(0); + setupPaxos(builder, 10); + server = builder.build(); + myUrl = "http://localhost:" + server.getPort(); } - public void setupPaxos(int instances) { + @After + public void tearDown() throws Exception { + server.stop(); + } + + public void setupPaxos(ServerBuilder builder, int instances) { for (int i = 1; i <= instances; i++) { JsonRpcServer jsonServer = new JsonRpcServer( new PaxosServiceImpl("P" + i + ": "), PaxosService.class); String serviceId = "/PaxosService" + i + ".json"; - handler.addRpcServer(serviceId, jsonServer); + builder.withServlet(new RpcServlet(jsonServer), serviceId); paxosUrls.add(myUrl + serviceId); } } @@ -79,22 +88,4 @@ public class PaxosServiceFunctionalTest { public synchronized void incrementSuccessfulProposals() { successfulProposals += 1; } - - public static class TestServer { - public Server server; - public int port; - - public static TestServer create(Handler handler) throws Exception { - Server server = new Server(0); - server.setHandler(handler); - server.start(); - int port = server.getConnectors()[0].getLocalPort(); - return new TestServer(server, port); - } - - private TestServer(Server server, int port) { - this.server = server; - this.port = port; - } - } } |