summaryrefslogtreecommitdiff
path: root/same/src/main
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-27 18:14:59 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-27 18:14:59 +0100
commit76b2dc36a008269c4e5af03e10d0587a03c2ba3b (patch)
tree25897017859ca1d89b1b09b3d7446821b7d3908e /same/src/main
parentaea82c6c2086c587382aa85cd3b4a862a89f8b55 (diff)
Hack collection.
– MyJsonRpcHttpClient: A JsonRpcHttpClient that uses Apache HttpComponents because of a HttpURLConnection bug in Android 2.2. Receives hack status because of nasty overloading. – Log4j-android doesn't like printing stack traces – convert them to string before logging.
Diffstat (limited to 'same/src/main')
-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
3 files changed, 92 insertions, 3 deletions
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();
+ }
+}