summaryrefslogtreecommitdiffstats
path: root/java/sca/modules
diff options
context:
space:
mode:
authorlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2009-10-02 16:50:23 +0000
committerlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2009-10-02 16:50:23 +0000
commit8b3dbaddb7d0727edd0521514e0c0bace1f909f4 (patch)
tree6a6b52911d0e82f98402652c02fe4a8723c75048 /java/sca/modules
parent8eaf5a0a37fdf4209e968d6a5f1686d844fd5668 (diff)
Initial support for JSNO-RPC Reference support
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@821095 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules')
-rw-r--r--java/sca/modules/binding-jsonrpc-runtime/META-INF/MANIFEST.MF4
-rw-r--r--java/sca/modules/binding-jsonrpc-runtime/pom.xml6
-rw-r--r--java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java112
-rw-r--r--java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java25
-rw-r--r--java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java85
-rw-r--r--java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java72
-rw-r--r--java/sca/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite33
7 files changed, 321 insertions, 16 deletions
diff --git a/java/sca/modules/binding-jsonrpc-runtime/META-INF/MANIFEST.MF b/java/sca/modules/binding-jsonrpc-runtime/META-INF/MANIFEST.MF
index 856417a3ca..ccf4d374a5 100644
--- a/java/sca/modules/binding-jsonrpc-runtime/META-INF/MANIFEST.MF
+++ b/java/sca/modules/binding-jsonrpc-runtime/META-INF/MANIFEST.MF
@@ -13,6 +13,10 @@ Import-Package: com.metaparadigm.jsonrpc;version="1.0.0",
javax.servlet.http,
javax.xml.namespace,
javax.xml.stream;version="1.0",
+ org.apache.commons.codec;version="1.3.0",
+ org.apache.commons.httpclient;version="3.1.0",
+ org.apache.commons.httpclient.methods,
+ org.apache.commons.httpclient.params;version="3.1.0",
org.apache.tuscany.sca.assembly;version="2.0",
org.apache.tuscany.sca.assembly.xml;version="2.0.0",
org.apache.tuscany.sca.binding.jsonrpc;version="2.0.0",
diff --git a/java/sca/modules/binding-jsonrpc-runtime/pom.xml b/java/sca/modules/binding-jsonrpc-runtime/pom.xml
index 3bde2efcb0..41dc87b91e 100644
--- a/java/sca/modules/binding-jsonrpc-runtime/pom.xml
+++ b/java/sca/modules/binding-jsonrpc-runtime/pom.xml
@@ -67,6 +67,12 @@
</dependency>
<dependency>
+ <groupId>commons-httpclient</groupId>
+ <artifactId>commons-httpclient</artifactId>
+ <version>3.1</version>
+ </dependency>
+
+ <dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-host-jetty</artifactId>
<version>2.0-SNAPSHOT</version>
diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java b/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
index a9fff462fa..abad796ef0 100644
--- a/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
+++ b/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
@@ -19,9 +19,17 @@
package org.apache.tuscany.sca.binding.jsonrpc.provider;
+import java.io.ByteArrayInputStream;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.tuscany.sca.assembly.EndpointReference;
+import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.invocation.Invoker;
import org.apache.tuscany.sca.invocation.Message;
+import org.json.JSONArray;
+import org.json.JSONObject;
/**
* Invoker for the JSONRPC Binding
@@ -29,17 +37,107 @@ import org.apache.tuscany.sca.invocation.Message;
* @version $Rev$ $Date$
*/
public class JSONRPCBindingInvoker implements Invoker {
- Operation operation;
- String uri;
+ private EndpointReference endpointReference;
+ private Operation operation;
+ private String uri;
+
+ private HttpClient httpClient;
- public JSONRPCBindingInvoker(Operation operation, String uri) {
+ public JSONRPCBindingInvoker(EndpointReference endpointReference, Operation operation, HttpClient httpClient) {
+ this.endpointReference = endpointReference;
this.operation = operation;
- this.uri = uri;
+ this.uri = ((JSONRPCBinding) endpointReference.getBinding()).getURI();
+
+ this.httpClient = httpClient;
}
-
+
public Message invoke(Message msg) {
- // TODO Auto-generated method stub
- return null;
+ Object[] jsonArgs = (Object[])msg.getBody();
+
+ PostMethod post = null;
+ try {
+
+ JSONObject jsonRequest = null;;
+ String requestId = "1";
+ Object[] args = null;
+ try {
+ // Extract the method
+ jsonRequest = new JSONObject();
+ jsonRequest.putOpt("method", "Service" + "." + msg.getOperation().getName());
+
+ // Extract the arguments
+ args = msg.getBody();
+ JSONArray array = new JSONArray();
+ for (int i = 0; i < args.length; i++) {
+ array.put(args[i]);
+ }
+ jsonRequest.putOpt("params", array);
+ jsonRequest.put("id", requestId);
+
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to parse JSON parameter", e);
+ }
+
+ post = new PostMethod(uri);
+ post.setRequestBody(new ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")));
+
+ httpClient.executeMethod(post);
+ int status = post.getStatusCode();
+
+ if (status == 200) {
+ //success
+ JSONObject jsonResponse = null;
+ try {
+ jsonResponse = new JSONObject(post.getResponseBodyAsString());
+
+ //check requestId
+ if (! jsonResponse.getString("id").equalsIgnoreCase(requestId)) {
+ throw new RuntimeException("Invalid response id:" + requestId );
+ }
+
+ msg.setBody(jsonResponse.get("result"));
+ } catch (Exception e) {
+ //FIXME Exceptions are not handled correctly here
+ // They should be reported to the client JavaScript as proper
+ // JavaScript exceptions.
+ throw new RuntimeException("Unable to parse response", e);
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ msg.setFaultBody(e);
+ } finally {
+ if (post != null) {
+ post.releaseConnection();
+ }
+ }
+
+ return msg;
}
+
+ private static JSONObject getJSONRequest(Message msg) {
+
+ JSONObject jsonRequest = null;;
+ Object[] args = null;
+ Object id = null;
+ try {
+ // Extract the method
+ jsonRequest = new JSONObject();
+ jsonRequest.putOpt("method", "Service" + "." + msg.getOperation().getName());
+
+ // Extract the arguments
+ args = msg.getBody();
+ JSONArray array = new JSONArray();
+ for (int i = 0; i < args.length; i++) {
+ array.put(args[i]);
+ }
+ jsonRequest.putOpt("params", array);
+ id = jsonRequest.put("id", "1");
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to parse JSON parameter", e);
+ }
+
+ return jsonRequest;
+ }
}
diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java b/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java
index e7a830e5d0..3ed27df3be 100644
--- a/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java
+++ b/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java
@@ -19,8 +19,10 @@
package org.apache.tuscany.sca.binding.jsonrpc.provider;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpConnectionManager;
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.tuscany.sca.assembly.EndpointReference;
-import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding;
import org.apache.tuscany.sca.interfacedef.InterfaceContract;
import org.apache.tuscany.sca.interfacedef.Operation;
import org.apache.tuscany.sca.invocation.Invoker;
@@ -34,23 +36,28 @@ import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
*/
public class JSONRPCReferenceBindingProvider implements ReferenceBindingProvider {
- private EndpointReference endpointReference;
+ private EndpointReference endpointReference;
private RuntimeComponentReference reference;
- private JSONRPCBinding binding;
-
+ private HttpClient httpClient;
+
public JSONRPCReferenceBindingProvider(EndpointReference endpointReference) {
+
+ this.endpointReference = endpointReference;
+ this.reference = (RuntimeComponentReference) endpointReference.getReference();
- this.endpointReference = endpointReference;
- this.reference = (RuntimeComponentReference) endpointReference.getReference();
- this.binding = (JSONRPCBinding) endpointReference.getBinding();
+ // Create an HTTP client
+ HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
+ connectionManager.getParams().setDefaultMaxConnectionsPerHost(10);
+ connectionManager.getParams().setConnectionTimeout(60000);
+ httpClient = new HttpClient(connectionManager);
}
public InterfaceContract getBindingInterfaceContract() {
return reference.getInterfaceContract();
}
-
+
public Invoker createInvoker(Operation operation) {
- return new JSONRPCBindingInvoker(operation, binding.getURI());
+ return new JSONRPCBindingInvoker(endpointReference, operation, httpClient);
}
public void start() {
diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java
new file mode 100644
index 0000000000..2dcdaa83a7
--- /dev/null
+++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package echo;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.oasisopen.sca.annotation.Reference;
+
+import bean.TestBean;
+
+public class EchoClientImpl implements Echo {
+ @Reference
+ protected Echo echoReference;
+
+ public String echo(String msg) {
+ return echoReference.echo(msg);
+ }
+
+ public int[] echoArrayInt(int[] intArray) {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+ public String[] echoArrayString(String[] stringArray) {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+ public TestBean echoBean(TestBean testBean) {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+ public boolean echoBoolean(boolean param) {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+ public void echoBusinessException() throws EchoBusinessException {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+ public int echoInt(int param) {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+ public List echoList(ArrayList list) {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+ public Map echoMap(HashMap map) {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+ public void echoRuntimeException() throws RuntimeException {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+ public Set echoSet(HashSet set) {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+ public void getΩλπ() {
+ throw new UnsupportedOperationException("UNsupported !");
+ }
+
+}
diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java
new file mode 100644
index 0000000000..0fb748b0d9
--- /dev/null
+++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.sca.binding.jsonrpc;
+
+import junit.framework.Assert;
+
+import org.apache.tuscany.sca.node.Contribution;
+import org.apache.tuscany.sca.node.ContributionLocationHelper;
+import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.NodeFactory;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import echo.Echo;
+
+public class JSONRPCReferenceTestCase {
+ private static final String SERVICE_PATH = "/EchoService";
+ private static final String SERVICE_URL = "http://localhost:8085/SCADomain" + SERVICE_PATH;
+
+ private static Node nodeServer;
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ try {
+ String contribution = ContributionLocationHelper.getContributionLocation(JSONRPCReferenceTestCase.class);
+ nodeServer = NodeFactory.newInstance().createNode("JSONRPCBinding.composite", new Contribution("testServer", contribution));
+ nodeServer.start();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ @AfterClass
+ public static void tearDown() throws Exception {
+ nodeServer.stop();
+ nodeServer.destroy();
+ }
+
+ @Test
+ public void testInvokeReference() throws Exception {
+ Node node = null;
+
+ String contribution = ContributionLocationHelper.getContributionLocation(JSONRPCReferenceTestCase.class);
+ node = NodeFactory.newInstance().createNode("JSONRPCReference.composite", new Contribution("testClient", contribution));
+ node.start();
+
+ Echo echoComponent = node.getService(Echo.class,"EchoComponentWithReference");
+ String result = echoComponent.echo("ABC");
+ Assert.assertEquals("echo: ABC", result);
+ if (node != null) {
+ node.stop();
+ node.destroy();
+ }
+ }
+}
diff --git a/java/sca/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite b/java/sca/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite
new file mode 100644
index 0000000000..0cda58286e
--- /dev/null
+++ b/java/sca/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+-->
+<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903"
+ xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
+ targetNamespace="http://jsonrpc"
+ xmlns:jsonrpc="http://jsonrpc"
+ name="JSONRPCReference">
+
+ <component name="EchoComponentWithReference">
+ <implementation.java class="echo.EchoClientImpl"/>
+ <reference name="echoReference">
+ <tuscany:binding.jsonrpc uri="http://localhost:8085/SCADomain/EchoService"/>
+ </reference>
+ </component>
+
+</composite>