diff options
Diffstat (limited to 'sca-java-2.x/trunk')
8 files changed, 273 insertions, 121 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java index 82eb6ea2f4..ddd9722836 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java @@ -19,8 +19,14 @@ package org.apache.tuscany.sca.binding.jsonrpc.protocol; +import java.io.OutputStream; import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -35,21 +41,14 @@ import org.json.JSONObject; * <li>id - The request id. This can be of any type. It is used to match the response with the request that it is replying to. * </ul> */ -public class JsonRpc10Request { - private final String method; - private final Object id; - private final Object[] params; +public class JsonRpc10Request extends JsonRpcRequest { public JsonRpc10Request(Object id, String method, Object[] params) { - super(); - this.id = id; - this.method = method; - this.params = params; + super(id, method, params); } public JsonRpc10Request(JSONObject req) throws JSONException { - super(); - + super(req); method = req.getString("method"); id = req.opt("id"); Object args = req.opt("params"); @@ -65,34 +64,25 @@ public class JsonRpc10Request { } else { throw new IllegalArgumentException("Invalid request: params is not a JSON array - " + args); } - } - public JSONObject toJSONObject() throws JSONException { - JSONObject req = new JSONObject(); - req.put("id", id); - req.put("method", method); - if (params != null) { - JSONArray args = new JSONArray(Arrays.asList(params)); - req.put("params", args); - } - return req; - } + public void write(OutputStream os) throws Exception { + // Construct a map to hold JSON-RPC request + final Map<String, Object> map = new HashMap<String, Object>(); + map.put("id", id); + map.put("method", method); - public boolean isNotification() { - return id == null || id == JSONObject.NULL; - } + List<Object> parameters = null; - public String getMethod() { - return method; - } + if (params != null) { + parameters = Arrays.asList(params); + } else { + parameters = Collections.emptyList(); + } - public Object getId() { - return id; - } + map.put("params", parameters); + JacksonHelper.MAPPER.writeValue(os, map); - public Object[] getParams() { - return params; } } diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java index e73f049545..de4862bb9a 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java @@ -19,54 +19,35 @@ package org.apache.tuscany.sca.binding.jsonrpc.protocol; +import java.io.OutputStream; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; +import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -public class JsonRpc20Request { - private final String method; - private final Object id; - private final Object[] params; - private final Map<String, Object> mappedParams; +public class JsonRpc20Request extends JsonRpcRequest { + + protected Map<String, Object> mappedParams; public JsonRpc20Request(Object id, String method, Object[] params) { - super(); - this.id = id; - this.method = method; - this.params = params; + super(id, method, params); this.mappedParams = null; } public JsonRpc20Request(Object id, String method, Map<String, Object> mappedParams) { - super(); - this.id = id; - this.method = method; - this.params = null; + super(id, method, null); this.mappedParams = mappedParams; } - public JSONObject toJSONObject() throws JSONException { - JSONObject req = new JSONObject(); - req.put("jsonrpc", "2.0"); - req.put("id", id); - req.put("method", method); - if (params != null) { - JSONArray args = new JSONArray(Arrays.asList(params)); - req.put("params", args); - } else { - JSONObject args = new JSONObject(mappedParams); - req.put("params", args); - } - return req; - } - public JsonRpc20Request(JSONObject req) throws JSONException { - super(); + super(req); if (req.has("jsonrpc") && "2.0".equals(req.getString("jsonrpc"))) { method = req.getString("method"); id = req.opt("id"); @@ -100,20 +81,30 @@ public class JsonRpc20Request { } } - public boolean isNotification() { - return id == null || id == JSONObject.NULL; - } + public void write(OutputStream os) throws Exception { + // Construct a map to hold JSON-RPC request + final Map<String, Object> jsonRequest = new HashMap<String, Object>(); + jsonRequest.put("jsonrpc", "2.0"); + jsonRequest.put("id", id); + jsonRequest.put("method", method); - public String getMethod() { - return method; - } + if (mappedParams != null) { + jsonRequest.put("params", mappedParams); + } - public Object getId() { - return id; - } + else { + List<Object> parameters = null; + + if (params != null) { + parameters = Arrays.asList(params); + } else { + parameters = Collections.emptyList(); + } + + jsonRequest.put("params", parameters); + } + JacksonHelper.MAPPER.writeValue(os, jsonRequest); - public Object[] getParams() { - return params; } public Map<String, Object> getMappedParams() { diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcRequest.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcRequest.java new file mode 100644 index 0000000000..8c0a71025a --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpcRequest.java @@ -0,0 +1,88 @@ +/* + * 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.protocol; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; + +import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; +import org.json.JSONObject; + +/** + * http://json-rpc.org/wiki/specification + * A remote method is invoked by sending a request to a remote service. The request is a single object serialized using JSON. + * <br>It has three properties: + * <ul> + * <li>method - A String containing the name of the method to be invoked. + * <li>params - An Array of objects to pass as arguments to the method. + * <li>id - The request id. This can be of any type. It is used to match the response with the request that it is replying to. + * </ul> + */ +public abstract class JsonRpcRequest { + protected String method; + protected Object id; + protected Object[] params; + + protected JSONObject jsonObject; + + public JsonRpcRequest(Object id, String method, Object[] params) { + super(); + this.id = id; + this.method = method; + this.params = params; + } + + protected JsonRpcRequest(JSONObject jsonObject) { + super(); + this.jsonObject = jsonObject; + } + + public JSONObject toJSONObject() throws Exception { + if (jsonObject != null) { + return jsonObject; + } else { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + write(bos); + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + jsonObject = JacksonHelper.MAPPER.readValue(bis, JSONObject.class); + } + return jsonObject; + } + + public abstract void write(OutputStream os) throws Exception; + + public boolean isNotification() { + return id == null || id == JSONObject.NULL; + } + + public String getMethod() { + return method; + } + + public Object getId() { + return id; + } + + public Object[] getParams() { + return params; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcInvoker.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcInvoker.java index b3781d88de..efba2ac1ac 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcInvoker.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcInvoker.java @@ -40,6 +40,7 @@ import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.binding.jsonrpc.JSONRPCBinding; import org.apache.tuscany.sca.binding.jsonrpc.protocol.JsonRpc10Request; import org.apache.tuscany.sca.binding.jsonrpc.protocol.JsonRpc20Request; +import org.apache.tuscany.sca.binding.jsonrpc.protocol.JsonRpcRequest; import org.apache.tuscany.sca.databinding.json.JSONDataBinding; import org.apache.tuscany.sca.databinding.json.jackson.JacksonHelper; import org.apache.tuscany.sca.interfacedef.DataType; @@ -92,21 +93,23 @@ public class JsonRpcInvoker implements Invoker, DataExchangeSemantics { params = (Object[])args; } - JSONObject jsonReq = null; + JsonRpcRequest req = null; if (JSONRPCBinding.VERSION_20.equals(((JSONRPCBinding)endpointReference.getBinding()).getVersion())) { - JsonRpc20Request req = new JsonRpc20Request(requestId, msg.getOperation().getName(), params); - jsonReq = req.toJSONObject(); + req = new JsonRpc20Request(requestId, msg.getOperation().getName(), params); } else { - JsonRpc10Request req = new JsonRpc10Request(requestId, msg.getOperation().getName(), params); - jsonReq = req.toJSONObject(); + req = new JsonRpc10Request(requestId, msg.getOperation().getName(), params); } - final String json = jsonReq.toString(4); + final JsonRpcRequest json = req; // Create content producer so that we can stream the json result out ContentProducer cp = new ContentProducer() { public void writeTo(OutputStream outstream) throws IOException { // mapper.writeValue(outstream, req.toJSONObject().toString()); - outstream.write(json.getBytes("UTF-8")); + try { + json.write(outstream); + } catch (Exception e) { + throw new IOException(e); + } } }; entity = new EntityTemplate(cp); diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/bean/TestBean.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/bean/TestBean.java index 4b159fb563..508fc31c08 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/bean/TestBean.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/bean/TestBean.java @@ -18,11 +18,14 @@ */ package bean; +import java.util.ArrayList; +import java.util.List; + public class TestBean { private String testString; - private String[] testStringArray; + // private String[] testStringArray; // Jackson cannot deserilize String[] + private List<String> testStringArray = new ArrayList<String>(); private int testInt; - public String getTestString() { return testString; @@ -32,14 +35,14 @@ public class TestBean { this.testString = testString; } - public String[] getTestStringArray() { - return testStringArray; + public List<String> getTestStringArray() { + return testStringArray; } - public void setStringArray(String[] stringArray) { - this.testStringArray = stringArray; + public void setStringArray(List<String> stringArray) { + this.testStringArray = stringArray; } - + public int getTestInt() { return testInt; } @@ -48,4 +51,54 @@ public class TestBean { this.testInt = testInt; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + testInt; + result = prime * result + ((testString == null) ? 0 : testString.hashCode()); + result = prime * result + ((testStringArray == null) ? 0 : testStringArray.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + TestBean other = (TestBean)obj; + if (testInt != other.testInt) { + return false; + } + if (testString == null) { + if (other.testString != null) { + return false; + } + } else if (!testString.equals(other.testString)) { + return false; + } + if (testStringArray == null) { + if (other.testStringArray != null) { + return false; + } + } else if (!testStringArray.equals(other.testStringArray)) { + return false; + } + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("TestBean [testString=").append(testString).append(", testStringArray=").append(testStringArray) + .append(", testInt=").append(testInt).append("]"); + return builder.toString(); + } + } diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java index 49cb2cf0eb..09a4ab183a 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoClientImpl.java @@ -27,71 +27,76 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.oasisopen.sca.annotation.AllowsPassByReference; import org.oasisopen.sca.annotation.Reference; import bean.TestBean; +@AllowsPassByReference public class EchoClientImpl implements Echo { @Reference + @AllowsPassByReference protected Echo echoReference; public String echo(String msg) { return echoReference.echo(msg); } - + public void echoVoid() { echoReference.echoVoid(); } - public int[] echoArrayInt(int[] intArray) { - throw new UnsupportedOperationException("UNsupported !"); + public void echoRuntimeException() throws RuntimeException { + echoReference.echoRuntimeException(); } - public String[] echoArrayString(String[] stringArray) { - throw new UnsupportedOperationException("UNsupported !"); + public void echoBusinessException() throws EchoBusinessException { + echoReference.echoBusinessException(); } - public TestBean echoBean(TestBean testBean) { - throw new UnsupportedOperationException("UNsupported !"); + public int echoInt(int param) { + return echoReference.echoInt(param); } - public boolean echoBoolean(boolean param) { - throw new UnsupportedOperationException("UNsupported !"); + public double echoDouble(double param) { + return echoReference.echoDouble(param); } - public void echoBusinessException() throws EchoBusinessException { - echoReference.echoBusinessException(); + public boolean echoBoolean(boolean param) { + return echoReference.echoBoolean(param); } - public int echoInt(int param) { - throw new UnsupportedOperationException("UNsupported !"); + public Map echoMap(HashMap map) { + return echoReference.echoMap(map); } - public double echoDouble(double param) { - throw new UnsupportedOperationException("UNsupported !"); + public TestBean echoBean(TestBean testBean) { + return echoReference.echoBean(testBean); } public List echoList(ArrayList list) { - throw new UnsupportedOperationException("UNsupported !"); + return echoReference.echoList(list); } - public Map echoMap(HashMap map) { - throw new UnsupportedOperationException("UNsupported !"); + public String[] echoArrayString(String[] stringArray) { + return echoReference.echoArrayString(stringArray); } - public void echoRuntimeException() throws RuntimeException { - echoReference.echoRuntimeException(); + public int[] echoArrayInt(int[] intArray) { + return echoReference.echoArrayInt(intArray); } public Set echoSet(HashSet set) { - throw new UnsupportedOperationException("UNsupported !"); + return echoReference.echoSet(set); } public void get\u03a9\u03bb\u03c0() { - throw new UnsupportedOperationException("UNsupported !"); + echoReference.get\u03a9\u03bb\u03c0(); } public BigDecimal echoBigDecimal(BigDecimal param) { - throw new UnsupportedOperationException("UNsupported !"); + return echoReference.echoBigDecimal(param); } + + } diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoComponentImpl.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoComponentImpl.java index b8b63906b9..bdfa88c65f 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoComponentImpl.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/echo/EchoComponentImpl.java @@ -27,6 +27,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.oasisopen.sca.annotation.AllowsPassByReference; + import bean.TestBean; /** @@ -34,6 +36,7 @@ import bean.TestBean; * * @version $Rev$ $Date$ */ +@AllowsPassByReference public class EchoComponentImpl implements Echo { public String echo(String msg) { @@ -80,6 +83,7 @@ public class EchoComponentImpl implements Echo { TestBean testBean = new TestBean(); testBean.setTestString(testBean1.getTestString()); testBean.setTestInt(testBean1.getTestInt()); + testBean.setStringArray(testBean1.getTestStringArray()); return testBean; } diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java index a83adb4a1b..2c92e33f9a 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java @@ -18,6 +18,8 @@ */ package org.apache.tuscany.sca.binding.jsonrpc; +import java.util.Arrays; + import junit.framework.Assert; import org.apache.tuscany.sca.node.Contribution; @@ -28,6 +30,7 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import bean.TestBean; import echo.Echo; public class JSONRPCReferenceTestCase { @@ -38,13 +41,17 @@ public class JSONRPCReferenceTestCase { public static void setUp() throws Exception { try { String contribution = ContributionLocationHelper.getContributionLocation(JSONRPCReferenceTestCase.class); - nodeServer = NodeFactory.newInstance().createNode("JSONRPCBinding.composite", new Contribution("testServer", contribution)); + nodeServer = + NodeFactory.newInstance().createNode("JSONRPCBinding.composite", + new Contribution("testServer", contribution)); nodeServer.start(); - + contribution = ContributionLocationHelper.getContributionLocation(JSONRPCReferenceTestCase.class); - node = NodeFactory.newInstance().createNode("JSONRPCReference.composite", new Contribution("testClient", contribution)); + node = + NodeFactory.newInstance().createNode("JSONRPCReference.composite", + new Contribution("testClient", contribution)); node.start(); - + } catch (Exception e) { e.printStackTrace(); } @@ -55,20 +62,31 @@ public class JSONRPCReferenceTestCase { nodeServer.stop(); node.stop(); } - + @Test public void testInvokeReference() throws Exception { - Echo echoComponent = node.getService(Echo.class,"EchoComponentWithReference"); + Echo echoComponent = node.getService(Echo.class, "EchoComponentWithReference"); String result = echoComponent.echo("ABC"); Assert.assertEquals("echo: ABC", result); } @Test + public void testInvokeBeanReference() throws Exception { + Echo echoComponent = node.getService(Echo.class, "EchoComponentWithReference"); + TestBean bean = new TestBean(); + bean.setTestInt(1); + bean.setTestString("123"); + bean.setStringArray(Arrays.asList("A", "B")); + TestBean result = echoComponent.echoBean(bean); + Assert.assertEquals(bean, result); + } + + @Test public void testInvokeReferenceVoidOperation() throws Exception { - Echo echoComponent = node.getService(Echo.class,"EchoComponentWithReference"); + Echo echoComponent = node.getService(Echo.class, "EchoComponentWithReference"); echoComponent.echoVoid(); } - + @Test(expected = Exception.class) public void testInvokeReferenceException() throws Exception { Echo echoComponent = node.getService(Echo.class, "EchoComponentWithReference"); @@ -79,20 +97,20 @@ public class JSONRPCReferenceTestCase { throw e; } } - + @Test public void testInvokeReference20() throws Exception { - Echo echoComponent = node.getService(Echo.class,"EchoComponentWithReference20"); + Echo echoComponent = node.getService(Echo.class, "EchoComponentWithReference20"); String result = echoComponent.echo("ABC"); Assert.assertEquals("echo: ABC", result); } @Test public void testInvokeReferenceVoidOperation20() throws Exception { - Echo echoComponent = node.getService(Echo.class,"EchoComponentWithReference20"); + Echo echoComponent = node.getService(Echo.class, "EchoComponentWithReference20"); echoComponent.echoVoid(); } - + @Test(expected = Exception.class) public void testInvokeReferenceException20() throws Exception { Echo echoComponent = node.getService(Echo.class, "EchoComponentWithReference20"); |