summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-28 17:24:27 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-28 17:24:27 +0100
commitb0569609d29217887d55803368abeb9a1f71a70c (patch)
treeecfbb6531a0e44703046d6ea20661c75cdb7f40f
parentbf8e87bec12e6ec53ec2150d7a6f6d0e493c79ce (diff)
Begin support for TJWS.
Add TjwsServerContainer. Rename Jetty specific containers.
-rw-r--r--same/pom.xml5
-rw-r--r--same/src/main/java/com/orbekk/same/SameController.java10
-rw-r--r--same/src/main/java/com/orbekk/same/TjwsApp.java11
-rw-r--r--same/src/main/java/com/orbekk/same/http/JettyServerBuilder.java (renamed from same/src/main/java/com/orbekk/same/http/ServerBuilder.java)12
-rw-r--r--same/src/main/java/com/orbekk/same/http/JettyServerContainer.java (renamed from same/src/main/java/com/orbekk/same/http/ServerContainer.java)8
-rw-r--r--same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java48
-rw-r--r--same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java10
7 files changed, 84 insertions, 20 deletions
diff --git a/same/pom.xml b/same/pom.xml
index d7d5ca4..c1f7946 100644
--- a/same/pom.xml
+++ b/same/pom.xml
@@ -72,6 +72,11 @@
<version>8.0.0.M3</version>
</dependency>
<dependency>
+ <groupId>org.jboss.resteasy</groupId>
+ <artifactId>tjws</artifactId>
+ <version>2.3.1.GA</version>
+ </dependency>
+ <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
diff --git a/same/src/main/java/com/orbekk/same/SameController.java b/same/src/main/java/com/orbekk/same/SameController.java
index f1b94e7..ac99197 100644
--- a/same/src/main/java/com/orbekk/same/SameController.java
+++ b/same/src/main/java/com/orbekk/same/SameController.java
@@ -10,13 +10,13 @@ import com.orbekk.net.DefaultBroadcasterFactory;
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.JettyServerBuilder;
+import com.orbekk.same.http.JettyServerContainer;
import com.orbekk.same.http.StateServlet;
public class SameController {
private Logger logger = LoggerFactory.getLogger(getClass());
- private ServerContainer server;
+ private JettyServerContainer server;
private Master master;
private Client client;
private PaxosServiceImpl paxos;
@@ -60,7 +60,7 @@ public class SameController {
StateServlet stateServlet = new StateServlet(client.getInterface(),
new VariableFactory(client.getInterface()));
- ServerContainer server = new ServerBuilder(port)
+ JettyServerContainer server = new JettyServerBuilder(port)
.withServlet(stateServlet, "/_/state")
.withService(client.getService(), ClientService.class)
.withService(master.getService(), MasterService.class)
@@ -79,7 +79,7 @@ public class SameController {
public SameController(
Configuration configuration,
- ServerContainer server,
+ JettyServerContainer server,
Master master,
Client client,
PaxosServiceImpl paxos,
diff --git a/same/src/main/java/com/orbekk/same/TjwsApp.java b/same/src/main/java/com/orbekk/same/TjwsApp.java
new file mode 100644
index 0000000..0a0c30a
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/TjwsApp.java
@@ -0,0 +1,11 @@
+package com.orbekk.same;
+
+import com.orbekk.same.http.TjwsServerContainer;
+
+public class TjwsApp {
+ public static void main(String[] args) {
+ TjwsServerContainer server = TjwsServerContainer.create(8080);
+ server.start();
+ server.join();
+ }
+}
diff --git a/same/src/main/java/com/orbekk/same/http/ServerBuilder.java b/same/src/main/java/com/orbekk/same/http/JettyServerBuilder.java
index 543366e..91d2725 100644
--- a/same/src/main/java/com/orbekk/same/http/ServerBuilder.java
+++ b/same/src/main/java/com/orbekk/same/http/JettyServerBuilder.java
@@ -9,30 +9,30 @@ import org.slf4j.LoggerFactory;
import com.googlecode.jsonrpc4j.JsonRpcServer;
-public class ServerBuilder {
+public class JettyServerBuilder {
Logger logger = LoggerFactory.getLogger(getClass());
int port;
ServletContextHandler context = null;
- public ServerBuilder(int port) {
+ public JettyServerBuilder(int port) {
this.port = port;
}
- public ServerBuilder withServlet(HttpServlet servlet, String pathSpec) {
+ public JettyServerBuilder 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) {
+ public <T> JettyServerBuilder withService(T service, Class<T> clazz) {
JsonRpcServer server = new JsonRpcServer(service, clazz);
String pathSpec = "/" + clazz.getSimpleName() + ".json";
return withServlet(new RpcServlet(server), pathSpec);
}
- public ServerContainer build() {
- ServerContainer server = ServerContainer.create(port);
+ public JettyServerContainer build() {
+ JettyServerContainer server = JettyServerContainer.create(port);
server.setReuseAddress(true);
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/JettyServerContainer.java
index af577a0..34db226 100644
--- a/same/src/main/java/com/orbekk/same/http/ServerContainer.java
+++ b/same/src/main/java/com/orbekk/same/http/JettyServerContainer.java
@@ -7,21 +7,21 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class ServerContainer {
+public class JettyServerContainer {
Logger logger = LoggerFactory.getLogger(getClass());
Server server;
int port;
ServletContextHandler context = null;
- public ServerContainer(Server server, int port, ServletContextHandler context) {
+ public JettyServerContainer(Server server, int port, ServletContextHandler context) {
this.server = server;
this.port = port;
this.context = context;
}
- public static ServerContainer create(int port) {
+ public static JettyServerContainer create(int port) {
Server server = new Server(port);
- return new ServerContainer(server, port, null);
+ return new JettyServerContainer(server, port, null);
}
public void setContext(ServletContextHandler context) {
diff --git a/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java b/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java
new file mode 100644
index 0000000..b71967c
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java
@@ -0,0 +1,48 @@
+package com.orbekk.same.http;
+
+import java.util.Properties;
+
+import org.slf4j.LoggerFactory;
+import org.slf4j.Logger;
+import Acme.Serve.Serve;
+
+public class TjwsServerContainer {
+ private static class MyServer extends Serve {
+ public void join() {
+ try {
+ backgroundThread.join();
+ } catch (InterruptedException e) {
+ return;
+ };
+ }
+ }
+
+ private Logger logger = LoggerFactory.getLogger(getClass());
+ private MyServer server;
+
+ public static TjwsServerContainer create(int port) {
+ MyServer server = new MyServer();
+ server.setAttribute(Serve.ARG_PORT, port);
+ return new TjwsServerContainer(server);
+ }
+
+ public TjwsServerContainer(MyServer server) {
+ this.server = server;
+ }
+
+ public int getPort() {
+ return (Integer)this.server.getAttribute(Serve.ARG_PORT);
+ }
+
+ public void start() {
+ server.runInBackground();
+ }
+
+ public void stop() {
+ server.stopBackground();
+ }
+
+ public void join() {
+ 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 278f775..32c7dff 100644
--- a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java
+++ b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java
@@ -5,8 +5,8 @@ import static org.junit.Assert.*;
import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.orbekk.same.ConnectionManagerImpl;
import com.orbekk.same.http.RpcServlet;
-import com.orbekk.same.http.ServerBuilder;
-import com.orbekk.same.http.ServerContainer;
+import com.orbekk.same.http.JettyServerBuilder;
+import com.orbekk.same.http.JettyServerContainer;
import java.util.ArrayList;
import java.util.List;
@@ -20,13 +20,13 @@ import org.junit.Test;
public class PaxosServiceFunctionalTest {
ConnectionManagerImpl connections = new ConnectionManagerImpl(500, 500);
List<String> paxosUrls = new ArrayList<String>();
- ServerContainer server;
+ JettyServerContainer server;
String myUrl;
int successfulProposals = 0;
@Before
public void setUp() throws Exception {
- ServerBuilder builder = new ServerBuilder(0);
+ JettyServerBuilder builder = new JettyServerBuilder(0);
List<String> tempUrls = setupPaxos(builder, 10);
server = builder.build();
server.start();
@@ -40,7 +40,7 @@ public class PaxosServiceFunctionalTest {
server.stop();
}
- public List<String> setupPaxos(ServerBuilder builder, int instances) {
+ public List<String> setupPaxos(JettyServerBuilder builder, int instances) {
List<String> tempUrls = new ArrayList<String>();
for (int i = 1; i <= instances; i++) {
JsonRpcServer jsonServer = new JsonRpcServer(