From 968c53069d48a2a9bb54543617501cf656b77103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Tue, 28 Feb 2012 17:38:06 +0100 Subject: Add TjwsServerBuilder. Add example program. --- same/src/main/java/com/orbekk/same/TjwsApp.java | 6 +++- .../java/com/orbekk/same/http/HelloServlet.java | 15 ++++++++ .../com/orbekk/same/http/TjwsServerBuilder.java | 41 ++++++++++++++++++++++ .../com/orbekk/same/http/TjwsServerContainer.java | 9 +++-- 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 same/src/main/java/com/orbekk/same/http/HelloServlet.java create mode 100644 same/src/main/java/com/orbekk/same/http/TjwsServerBuilder.java (limited to 'same/src/main/java') diff --git a/same/src/main/java/com/orbekk/same/TjwsApp.java b/same/src/main/java/com/orbekk/same/TjwsApp.java index 0a0c30a..b06322b 100644 --- a/same/src/main/java/com/orbekk/same/TjwsApp.java +++ b/same/src/main/java/com/orbekk/same/TjwsApp.java @@ -1,10 +1,14 @@ package com.orbekk.same; +import com.orbekk.same.http.HelloServlet; +import com.orbekk.same.http.TjwsServerBuilder; import com.orbekk.same.http.TjwsServerContainer; public class TjwsApp { public static void main(String[] args) { - TjwsServerContainer server = TjwsServerContainer.create(8080); + TjwsServerContainer server = new TjwsServerBuilder(8080) + .withServlet(new HelloServlet(), "/hello") + .build(); server.start(); server.join(); } diff --git a/same/src/main/java/com/orbekk/same/http/HelloServlet.java b/same/src/main/java/com/orbekk/same/http/HelloServlet.java new file mode 100644 index 0000000..78ce6b1 --- /dev/null +++ b/same/src/main/java/com/orbekk/same/http/HelloServlet.java @@ -0,0 +1,15 @@ +package com.orbekk.same.http; + +import java.io.IOException; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class HelloServlet extends HttpServlet { + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws IOException { + response.setContentType("text/plain; charset=utf8"); + response.getWriter().println("Hello, World"); + } +} diff --git a/same/src/main/java/com/orbekk/same/http/TjwsServerBuilder.java b/same/src/main/java/com/orbekk/same/http/TjwsServerBuilder.java new file mode 100644 index 0000000..c0aed5a --- /dev/null +++ b/same/src/main/java/com/orbekk/same/http/TjwsServerBuilder.java @@ -0,0 +1,41 @@ +package com.orbekk.same.http; + +import java.util.ArrayList; + +import javax.servlet.http.HttpServlet; + +import com.googlecode.jsonrpc4j.JsonRpcServer; + +public class TjwsServerBuilder { + private int port; + private ArrayList servletPaths = new ArrayList(); + private ArrayList servlets = new ArrayList(); + + public TjwsServerBuilder(int port) { + this.port = port; + } + + /** Note: Does not preserve order. */ + public TjwsServerBuilder withServlet(HttpServlet servlet, + String pathSpec) { + servletPaths.add(pathSpec); + servlets.add(servlet); + return this; + } + + public TjwsServerBuilder withService(T service, Class clazz) { + JsonRpcServer server = new JsonRpcServer(service, clazz); + String pathSpec = "/" + clazz.getSimpleName() + ".json"; + return withServlet(new RpcServlet(server), pathSpec); + } + + public TjwsServerContainer build() { + TjwsServerContainer server = TjwsServerContainer.create(port); + for (int i = 0; i < servletPaths.size(); i++) { + String path = servletPaths.get(i); + HttpServlet servlet = servlets.get(i); + server.addServlet(path, servlet); + } + return server; + } +} diff --git a/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java b/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java index b71967c..a38cfac 100644 --- a/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java +++ b/same/src/main/java/com/orbekk/same/http/TjwsServerContainer.java @@ -1,9 +1,10 @@ package com.orbekk.same.http; -import java.util.Properties; +import javax.servlet.http.HttpServlet; -import org.slf4j.LoggerFactory; import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import Acme.Serve.Serve; public class TjwsServerContainer { @@ -45,4 +46,8 @@ public class TjwsServerContainer { public void join() { server.join(); } + + public void addServlet(String pathSpec, HttpServlet servlet) { + server.addServlet(pathSpec, servlet); + } } -- cgit v1.2.3