summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-23 21:13:30 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-23 21:13:30 +0100
commit92e46bea722a2dcf038353101c7f705d38c08097 (patch)
treea398e80ab20fd78063daa6c5fc8c4d799fc13b35
parent675d39342eeee15dda340557c88321edb431de98 (diff)
Support more than one rpc Service on a Jetty server.
Implent a URL based lookup that determines which service to handle a request.
-rw-r--r--same/src/main/java/com/orbekk/same/ClientApp.java3
-rw-r--r--same/src/main/java/com/orbekk/same/MasterApp.java3
-rw-r--r--same/src/main/java/com/orbekk/same/RpcHandler.java29
3 files changed, 27 insertions, 8 deletions
diff --git a/same/src/main/java/com/orbekk/same/ClientApp.java b/same/src/main/java/com/orbekk/same/ClientApp.java
index 64fad20..a98b719 100644
--- a/same/src/main/java/com/orbekk/same/ClientApp.java
+++ b/same/src/main/java/com/orbekk/same/ClientApp.java
@@ -27,7 +27,8 @@ public class ClientApp {
JsonRpcServer jsonServer = new JsonRpcServer(client, ClientService.class);
server = new Server(port);
- RpcHandler rpcHandler = new RpcHandler(jsonServer, client);
+ RpcHandler rpcHandler = new RpcHandler(client);
+ rpcHandler.addRpcServer("/ClientService.json", jsonServer);
server.setHandler(rpcHandler);
try {
diff --git a/same/src/main/java/com/orbekk/same/MasterApp.java b/same/src/main/java/com/orbekk/same/MasterApp.java
index ee5f50b..428336c 100644
--- a/same/src/main/java/com/orbekk/same/MasterApp.java
+++ b/same/src/main/java/com/orbekk/same/MasterApp.java
@@ -21,7 +21,8 @@ public class MasterApp {
broadcaster);
JsonRpcServer jsonServer = new JsonRpcServer(master, MasterService.class);
server = new Server(port);
- RpcHandler rpcHandler = new RpcHandler(jsonServer, master);
+ RpcHandler rpcHandler = new RpcHandler(master);
+ rpcHandler.addRpcServer("/MasterService.json", jsonServer);
server.setHandler(rpcHandler);
Thread masterThread = new Thread(master);
diff --git a/same/src/main/java/com/orbekk/same/RpcHandler.java b/same/src/main/java/com/orbekk/same/RpcHandler.java
index 7b2adb3..cddb307 100644
--- a/same/src/main/java/com/orbekk/same/RpcHandler.java
+++ b/same/src/main/java/com/orbekk/same/RpcHandler.java
@@ -3,6 +3,8 @@ package com.orbekk.same;
import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.orbekk.net.HttpUtil;
import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -13,14 +15,24 @@ import org.slf4j.LoggerFactory;
public class RpcHandler extends AbstractHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
- private JsonRpcServer rpcServer;
+ private JsonRpcServer rpcServer = null;
private UrlReceiver urlReceiver;
+ private Map<String, JsonRpcServer> rpcServers =
+ new HashMap<String, JsonRpcServer>();
- public RpcHandler(JsonRpcServer rpcServer,
- UrlReceiver urlReceiver) {
- this.rpcServer = rpcServer;
+ public RpcHandler(UrlReceiver urlReceiver) {
this.urlReceiver = urlReceiver;
}
+
+ /**
+ * Add an RpcServer to this Handler.
+ *
+ * @param url the base url of the service, e.g.
+ * /MyService.json
+ */
+ public void addRpcServer(String url, JsonRpcServer rpcServer) {
+ rpcServers.put(url, rpcServer);
+ }
@Override
public synchronized void handle(String target, Request baseRequest,
@@ -41,11 +53,16 @@ public class RpcHandler extends AbstractHandler {
remotePort + "/pong";
logger.info("Got ping. Sending pong to {}", pongUrl);
HttpUtil.sendHttpRequest(pongUrl);
+ baseRequest.setHandled(true);
} else if (target.equals("/pong")) {
logger.info("Received pong from {}", request.getRemoteAddr());
+ baseRequest.setHandled(true);
} else {
- rpcServer.handle(request, response);
+ JsonRpcServer server = rpcServers.get(target);
+ if (server != null) {
+ server.handle(request, response);
+ baseRequest.setHandled(true);
+ }
}
- baseRequest.setHandled(true);
}
}