summaryrefslogtreecommitdiff
path: root/same/src/main/java/com
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-03 16:09:18 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-03 16:09:18 +0100
commit1c7bbed107d0767536c93a092dbf192a6a45a620 (patch)
tree8c0fd5aa8daf4a443dfe018d7f275fbb0156c14e /same/src/main/java/com
parentaf22c94b833643bc8545cfcc82b25b8f515ae513 (diff)
Refactor Handler code.
Use servlets for everything. Ping service not yet implemented (becuase it's ugly and will be replaced with something else).
Diffstat (limited to 'same/src/main/java/com')
-rw-r--r--same/src/main/java/com/orbekk/same/ClientApp.java1
-rw-r--r--same/src/main/java/com/orbekk/same/SameController.java27
-rw-r--r--same/src/main/java/com/orbekk/same/http/HandlerFactory.java50
-rw-r--r--same/src/main/java/com/orbekk/same/http/RpcServlet.java24
-rw-r--r--same/src/main/java/com/orbekk/same/http/ServerBuilder.java48
-rw-r--r--same/src/main/java/com/orbekk/same/http/StateServlet.java23
6 files changed, 111 insertions, 62 deletions
diff --git a/same/src/main/java/com/orbekk/same/ClientApp.java b/same/src/main/java/com/orbekk/same/ClientApp.java
index 3c67ae2..499e8ef 100644
--- a/same/src/main/java/com/orbekk/same/ClientApp.java
+++ b/same/src/main/java/com/orbekk/same/ClientApp.java
@@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory;
import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.orbekk.net.HttpUtil;
+import com.orbekk.same.http.RpcHandler;
public class ClientApp {
private Logger logger = LoggerFactory.getLogger(getClass());
diff --git a/same/src/main/java/com/orbekk/same/SameController.java b/same/src/main/java/com/orbekk/same/SameController.java
index b75de96..bc912e6 100644
--- a/same/src/main/java/com/orbekk/same/SameController.java
+++ b/same/src/main/java/com/orbekk/same/SameController.java
@@ -1,6 +1,10 @@
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;
@@ -8,6 +12,10 @@ import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.orbekk.net.HttpUtil;
import com.orbekk.paxos.PaxosService;
import com.orbekk.paxos.PaxosServiceImpl;
+import com.orbekk.same.http.HandlerFactory;
+import com.orbekk.same.http.RpcHandler;
+import com.orbekk.same.http.ServerBuilder;
+import com.orbekk.same.http.StateServlet;
public class SameController implements UrlReceiver {
private Logger logger = LoggerFactory.getLogger(getClass());
@@ -30,23 +38,18 @@ public class SameController implements UrlReceiver {
MasterServiceImpl master = new MasterServiceImpl(state, connections,
broadcaster);
- JsonRpcServer jsonMaster = new JsonRpcServer(master, MasterService.class);
ClientServiceImpl client = new ClientServiceImpl(state, connections);
- JsonRpcServer jsonClient = new JsonRpcServer(client.getService(),
- ClientService.class);
PaxosServiceImpl paxos = new PaxosServiceImpl("");
- JsonRpcServer jsonPaxos = new JsonRpcServer(paxos, PaxosService.class);
- Server server = new Server(port);
+ Server server = new ServerBuilder(port)
+ .withServlet(new StateServlet(), "/_/state")
+ .withService(client.getService(), ClientService.class)
+ .withService(master, MasterService.class)
+ .withService(paxos, PaxosService.class)
+ .build();
+
SameController controller = new SameController(port, server, master, client,
paxos);
- RpcHandler rpcHandler = new RpcHandler(null);
-
- rpcHandler.addRpcServer("/MasterService.json", jsonMaster);
- rpcHandler.addRpcServer("/ClientService.json", jsonClient);
- rpcHandler.addRpcServer("/PaxosService.json", jsonPaxos);
-
- server.setHandler(rpcHandler);
return controller;
}
diff --git a/same/src/main/java/com/orbekk/same/http/HandlerFactory.java b/same/src/main/java/com/orbekk/same/http/HandlerFactory.java
deleted file mode 100644
index 5d5d34c..0000000
--- a/same/src/main/java/com/orbekk/same/http/HandlerFactory.java
+++ /dev/null
@@ -1,50 +0,0 @@
-package com.orbekk.same.http;
-
-import org.eclipse.jetty.server.Handler;
-import org.eclipse.jetty.server.handler.ContextHandler.Context;
-import org.eclipse.jetty.server.handler.HandlerList;
-import org.eclipse.jetty.servlet.ServletContextHandler;
-import org.eclipse.jetty.servlet.ServletHandler;
-import org.eclipse.jetty.servlet.ServletHolder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.googlecode.jsonrpc4j.JsonRpcServer;
-import com.orbekk.paxos.PaxosService;
-import com.orbekk.same.ClientService;
-import com.orbekk.same.MasterService;
-
-public class HandlerFactory {
- Logger logger = LoggerFactory.getLogger(getClass());
-
- public Handler createServletHandler() {
- logger.info("Creating servlet handler.");
- ServletContextHandler context = new ServletContextHandler(
- ServletContextHandler.SESSIONS);
- context.setContextPath("/*");
- context.addServlet(new ServletHolder(new StateServlet()), "/*");
- return context;
- }
-
- public RpcHandler createRpcHandler(MasterService master,
- ClientService client, PaxosService paxos) {
- RpcHandler rpcHandler = new RpcHandler(null);
- rpcHandler.addRpcServer("/MasterService.json",
- new JsonRpcServer(master, MasterService.class));
- rpcHandler.addRpcServer("/ClientService.json",
- new JsonRpcServer(client, ClientService.class));
- rpcHandler.addRpcServer("/PaxosService.json",
- new JsonRpcServer(paxos, PaxosService.class));
- return rpcHandler;
- }
-
- public Handler createMainHandler(RpcHandler rpcHandler,
- Handler servletHandler) {
- HandlerList handler = new HandlerList();
- handler.addHandler(rpcHandler);
- handler.addHandler(servletHandler);
-
- return handler;
- }
-
-}
diff --git a/same/src/main/java/com/orbekk/same/http/RpcServlet.java b/same/src/main/java/com/orbekk/same/http/RpcServlet.java
new file mode 100644
index 0000000..6d73759
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/http/RpcServlet.java
@@ -0,0 +1,24 @@
+package com.orbekk.same.http;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import com.googlecode.jsonrpc4j.JsonRpcServer;
+
+public class RpcServlet extends HttpServlet {
+ JsonRpcServer rpcServer;
+
+ public RpcServlet(JsonRpcServer rpcServer) {
+ super();
+ this.rpcServer = rpcServer;
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest request,
+ HttpServletResponse response) throws IOException {
+ rpcServer.handle(request, response);
+ }
+}
diff --git a/same/src/main/java/com/orbekk/same/http/ServerBuilder.java b/same/src/main/java/com/orbekk/same/http/ServerBuilder.java
new file mode 100644
index 0000000..a582283
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/http/ServerBuilder.java
@@ -0,0 +1,48 @@
+package com.orbekk.same.http;
+
+import javax.servlet.http.HttpServlet;
+
+import org.eclipse.jetty.server.Server;
+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;
+
+public class ServerBuilder {
+ Logger logger = LoggerFactory.getLogger(getClass());
+ int port;
+ ServletContextHandler context = null;
+
+ public ServerBuilder(int port) {
+ this.port = port;
+ }
+
+ public ServerBuilder withServlet(HttpServlet servlet, String pathSpec) {
+ logger.info("Servlet binding: {} → {}", pathSpec, servlet);
+ getServletContextHandler().addServlet(new ServletHolder(servlet),
+ pathSpec);
+ return this;
+ }
+
+ public <T> ServerBuilder withService(T service, Class<T> clazz) {
+ JsonRpcServer server = new JsonRpcServer(service, clazz);
+ String pathSpec = "/" + clazz.getSimpleName() + ".json";
+ return withServlet(new RpcServlet(server), pathSpec);
+ }
+
+ public Server build() {
+ Server server = new Server(port);
+ server.setHandler(getServletContextHandler());
+ return server;
+ }
+
+ private ServletContextHandler getServletContextHandler() {
+ if (context == null) {
+ context = new ServletContextHandler();
+ context.setContextPath("/");
+ }
+ return context;
+ }
+}
diff --git a/same/src/main/java/com/orbekk/same/http/StateServlet.java b/same/src/main/java/com/orbekk/same/http/StateServlet.java
new file mode 100644
index 0000000..62b304c
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/http/StateServlet.java
@@ -0,0 +1,23 @@
+package com.orbekk.same.http;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class StateServlet extends HttpServlet {
+ private Logger logger = LoggerFactory.getLogger(getClass());
+
+ @Override
+ protected void doGet(HttpServletRequest request,
+ HttpServletResponse response) throws IOException {
+ logger.error("GOT HERE");
+ response.setContentType("text/plain; charset=utf8");
+ response.getWriter().println("HI");
+ response.setStatus(HttpServletResponse.SC_OK);
+ }
+}