tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lrese...@apache.org
Subject svn commit: r821095 - in /tuscany/java/sca/modules/binding-jsonrpc-runtime: ./ META-INF/ src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/ src/test/java/echo/ src/test/java/org/apache/tuscany/sca/binding/jsonrpc/ src/test/resources/
Date Fri, 02 Oct 2009 16:50:24 GMT
Author: lresende
Date: Fri Oct  2 16:50:23 2009
New Revision: 821095

URL: http://svn.apache.org/viewvc?rev=821095&view=rev
Log:
Initial support for JSNO-RPC Reference support

Added:
    tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java
  (with props)
    tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java
  (with props)
    tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite
      - copied, changed from r821094, tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
Modified:
    tuscany/java/sca/modules/binding-jsonrpc-runtime/META-INF/MANIFEST.MF
    tuscany/java/sca/modules/binding-jsonrpc-runtime/pom.xml
    tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
    tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java

Modified: tuscany/java/sca/modules/binding-jsonrpc-runtime/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/META-INF/MANIFEST.MF?rev=821095&r1=821094&r2=821095&view=diff
==============================================================================
--- tuscany/java/sca/modules/binding-jsonrpc-runtime/META-INF/MANIFEST.MF (original)
+++ tuscany/java/sca/modules/binding-jsonrpc-runtime/META-INF/MANIFEST.MF Fri Oct  2 16:50:23
2009
@@ -13,6 +13,10 @@
  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",

Modified: tuscany/java/sca/modules/binding-jsonrpc-runtime/pom.xml
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/pom.xml?rev=821095&r1=821094&r2=821095&view=diff
==============================================================================
--- tuscany/java/sca/modules/binding-jsonrpc-runtime/pom.xml (original)
+++ tuscany/java/sca/modules/binding-jsonrpc-runtime/pom.xml Fri Oct  2 16:50:23 2009
@@ -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>

Modified: tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java?rev=821095&r1=821094&r2=821095&view=diff
==============================================================================
--- tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
(original)
+++ tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
Fri Oct  2 16:50:23 2009
@@ -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 @@
  * @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;
+    }
 }

Modified: tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java?rev=821095&r1=821094&r2=821095&view=diff
==============================================================================
--- tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java
(original)
+++ tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCReferenceBindingProvider.java
Fri Oct  2 16:50:23 2009
@@ -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 @@
  */
 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() {

Added: tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java?rev=821095&view=auto
==============================================================================
--- tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java
(added)
+++ tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java
Fri Oct  2 16:50:23 2009
@@ -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 !");
+    }
+
+}

Propchange: tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java?rev=821095&view=auto
==============================================================================
--- tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java
(added)
+++ tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java
Fri Oct  2 16:50:23 2009
@@ -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();
+        }
+    }
+}

Propchange: tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite
(from r821094, tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java)
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite?p2=tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite&p1=tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java&r1=821094&r2=821095&rev=821095&view=diff
==============================================================================
--- tuscany/java/sca/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JSONRPCBindingInvoker.java
(original)
+++ tuscany/java/sca/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite
Fri Oct  2 16:50:23 2009
@@ -1,4 +1,5 @@
-/*
+<?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
@@ -15,31 +16,18 @@
  * 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">
 
-package org.apache.tuscany.sca.binding.jsonrpc.provider;
-
-import org.apache.tuscany.sca.interfacedef.Operation;
-import org.apache.tuscany.sca.invocation.Invoker;
-import org.apache.tuscany.sca.invocation.Message;
-
-/**
- * Invoker for the JSONRPC Binding
- * 
- * @version $Rev$ $Date$
- */
-public class JSONRPCBindingInvoker implements Invoker {
-    Operation operation;
-    String uri;
-
-    public JSONRPCBindingInvoker(Operation operation, String uri) {
-        this.operation = operation;
-        this.uri = uri;        
-    }
+    <component name="EchoComponentWithReference">
+        <implementation.java class="echo.EchoClientImpl"/>
+        <reference name="echoReference">
+            <tuscany:binding.jsonrpc uri="http://localhost:8085/SCADomain/EchoService"/>
+        </reference>
+    </component>
     
-    public Message invoke(Message msg) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-}
+</composite>



Mime
View raw message