summaryrefslogtreecommitdiff
path: root/same
diff options
context:
space:
mode:
Diffstat (limited to 'same')
-rw-r--r--same/pom.xml9
-rw-r--r--same/src/main/java/com/orbekk/net/MyJsonRpcHttpClient.java75
-rw-r--r--same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java6
-rw-r--r--same/src/main/java/com/orbekk/same/StackTraceUtil.java14
4 files changed, 100 insertions, 4 deletions
diff --git a/same/pom.xml b/same/pom.xml
index 93f611f..60402b3 100644
--- a/same/pom.xml
+++ b/same/pom.xml
@@ -83,8 +83,15 @@
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
- <version>8.0.0.M3</version>
+ <version>8.0.0.M3</version>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.1.2</version>
+ </dependency>
+
</dependencies>
<build>
diff --git a/same/src/main/java/com/orbekk/net/MyJsonRpcHttpClient.java b/same/src/main/java/com/orbekk/net/MyJsonRpcHttpClient.java
new file mode 100644
index 0000000..98de054
--- /dev/null
+++ b/same/src/main/java/com/orbekk/net/MyJsonRpcHttpClient.java
@@ -0,0 +1,75 @@
+package com.orbekk.net;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.reflect.Type;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentProducer;
+import org.apache.http.entity.EntityTemplate;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.codehaus.jackson.map.ObjectMapper;
+
+import com.googlecode.jsonrpc4j.JsonRpcClient;
+import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
+
+/**
+ * This class is horrible. :S
+ *
+ * We extend JsonRpcHttpClient but try to override everything it does.
+ */
+public class MyJsonRpcHttpClient extends JsonRpcHttpClient {
+ private URL serviceUrl;
+ private JsonRpcClient rpcClient;
+ private HttpClient httpClient;
+
+ public MyJsonRpcHttpClient(URL serviceUrl, int connectionTimeout,
+ int readTimeout) {
+ super(null);
+ httpClient = new DefaultHttpClient();
+ HttpParams params = httpClient.getParams();
+ HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
+ HttpConnectionParams.setSoTimeout(params, readTimeout);
+ rpcClient = new JsonRpcClient();
+ this.serviceUrl = serviceUrl;
+ }
+
+ @Override
+ public synchronized Object invoke(
+ final String methodName, final Object[] arguments, Type returnType,
+ Map<String, String> extraHeaders)
+ throws Exception {
+ EntityTemplate entity = new EntityTemplate(new ContentProducer() {
+ @Override
+ public void writeTo(OutputStream out) throws IOException {
+ try {
+ rpcClient.invoke(methodName, arguments, out);
+ } catch (Exception e) {
+ throw new IOException(e);
+ }
+ }
+ });
+ entity.setContentType("application/json-rpc");
+
+ HttpPost post = new HttpPost(serviceUrl.toString());
+
+ for (Map.Entry<String, String> entry : extraHeaders.entrySet()) {
+ post.addHeader(entry.getKey(), entry.getValue());
+ }
+
+ post.setEntity(entity);
+
+ HttpResponse response = httpClient.execute(post);
+ HttpEntity responseEntity = response.getEntity();
+
+ return super.readResponse(returnType, responseEntity.getContent());
+ }
+}
diff --git a/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java b/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java
index d477834..92b7588 100644
--- a/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java
+++ b/same/src/main/java/com/orbekk/same/ConnectionManagerImpl.java
@@ -2,6 +2,7 @@ package com.orbekk.same;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import com.googlecode.jsonrpc4j.ProxyUtil;
+import com.orbekk.net.MyJsonRpcHttpClient;
import com.orbekk.paxos.PaxosService;
import java.net.MalformedURLException;
@@ -32,9 +33,8 @@ public class ConnectionManagerImpl implements ConnectionManager {
private <T>T getClassProxy(String url, Class<T> clazz) {
T service = null;
try {
- JsonRpcHttpClient client = new JsonRpcHttpClient(new URL(url));
- client.setConnectionTimeoutMillis(connectionTimeout);
- client.setReadTimeoutMillis(readTimeout);
+ MyJsonRpcHttpClient client = new MyJsonRpcHttpClient(new URL(url),
+ connectionTimeout, readTimeout);
service = ProxyUtil.createProxy(
this.getClass().getClassLoader(),
clazz,
diff --git a/same/src/main/java/com/orbekk/same/StackTraceUtil.java b/same/src/main/java/com/orbekk/same/StackTraceUtil.java
new file mode 100644
index 0000000..0c0c3d0
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/StackTraceUtil.java
@@ -0,0 +1,14 @@
+package com.orbekk.same;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+
+public class StackTraceUtil {
+ public static String throwableToString(Throwable t) {
+ Writer writer = new StringWriter();
+ PrintWriter printWriter = new PrintWriter(writer);
+ t.printStackTrace(printWriter);
+ return writer.toString();
+ }
+}