From 5ae609d772940b13f95cd06a989551d1ba13ef38 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 10 Jan 2011 21:50:34 +0000 Subject: Add support for both RPC and Doc-wrapped modes to the component implementation example. Test an RPC reference configured with a Doc-Wrapped binding contract. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1057387 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/main/java/sample/api/WSDLReference.java | 5 +-- .../main/java/sample/impl/SampleWSDLInvoker.java | 45 ++++++++++------------ .../src/main/java/sample/impl/SampleWSDLProxy.java | 30 ++------------- .../src/test/java/sample/ClientTest.java | 2 +- .../src/test/java/sample/WelloTest.java | 20 ++++++++-- 5 files changed, 44 insertions(+), 58 deletions(-) diff --git a/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/api/WSDLReference.java b/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/api/WSDLReference.java index a25df3e895..528ff530c2 100644 --- a/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/api/WSDLReference.java +++ b/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/api/WSDLReference.java @@ -23,8 +23,7 @@ import org.w3c.dom.Element; public interface WSDLReference { - Element call(String op, Element e); - public Element callBare(String op, Element... e); - void callAsync(String op, Element e); + Element doccall(String op, Element e); + public Object rpccall(String op, Object... args); } diff --git a/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLInvoker.java b/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLInvoker.java index 6758c1fc1f..3f68d5d219 100644 --- a/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLInvoker.java +++ b/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLInvoker.java @@ -39,12 +39,22 @@ import org.w3c.dom.Element; class SampleWSDLInvoker extends InterceptorAsyncImpl { final String name; final Object instance; - final Method method; + final Method doccall; + final Method rpccall; + + static Method method(final Class clazz, final String name, final Class... types) { + try { + return clazz.getMethod("doccall", String.class, Element.class); + } catch (Exception e) { + return null; + } + } SampleWSDLInvoker(final WSDLOperation op, final Class clazz, final Object instance) throws SecurityException, NoSuchMethodException { this.name = op.getName(); this.instance = instance; - this.method = clazz.getMethod("call", String.class, Element.class); + this.doccall = method(clazz, "doccall", String.class, Element.class); + this.rpccall = method(clazz, "rpccall", String.class, Object[].class); } public Invoker getNext() { @@ -56,31 +66,18 @@ class SampleWSDLInvoker extends InterceptorAsyncImpl { return processRequest(msg); } - public void invokeAsyncRequest(Message msg) { - // Retrieve the async callback information - AsyncResponseInvoker respInvoker = (AsyncResponseInvoker)msg.getHeaders().get("ASYNC_RESPONSE_INVOKER"); - if( respInvoker == null ) throw new ServiceRuntimeException("Async Implementation invoked with no response invoker"); - - Message responseMsg = processRequest(msg); - - // in this sample programming model we make the async - // response from the implementation provider. The - // component implementation itself doesn't get a chance to - // do async responses. - - // At this point we could serialize the AsyncResponseInvoker and pick it up again - // later to send the async response - - //((RuntimeEndpoint)msg.getTo()).invokeAsyncResponse(responseMsg); - respInvoker.invokeAsyncResponse(responseMsg); - } // end method invokeAsyncRequest - public Message processRequest(Message msg) { try { - //AsyncHeader asyncHeader = (String) message.getHeaders().get("ASYNC-HEADER"); // Invoke the generic call method - Object response = method.invoke(instance, name, ((Object[])msg.getBody())[0]); - msg.setBody(response); + if (doccall != null) { + // Document style (args wrapped in a single wrapper element) + final Object response = doccall.invoke(instance, name, ((Object[])msg.getBody())[0]); + msg.setBody(response); + } else { + // RPC style (args are passed as an array) + final Object response = rpccall.invoke(instance, name, (Object[])msg.getBody()); + msg.setBody(response); + } } catch(Exception e) { e.printStackTrace(); msg.setFaultBody(e.getCause()); diff --git a/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLProxy.java b/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLProxy.java index 5b42bce28f..707e71c187 100644 --- a/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLProxy.java +++ b/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/main/java/sample/impl/SampleWSDLProxy.java @@ -58,7 +58,7 @@ class SampleWSDLProxy implements WSDLReference { } @Override - public Element call(String op, Element e) { + public Element doccall(String op, Element e) { try { // Invoke the named operation on the endpoint reference return (Element)repr.invoke(ops.get(op), new Object[] {e}); @@ -68,37 +68,13 @@ class SampleWSDLProxy implements WSDLReference { } @Override - public Element callBare(String op, Element... e) { + public Object rpccall(String op, Object... args) { try { // Invoke the named operation on the endpoint reference - return (Element)repr.invoke(ops.get(op), e); + return repr.invoke(ops.get(op), args); } catch(InvocationTargetException ex) { throw new RuntimeException(ex); } } - @Override - public void callAsync(String op, Element e) { - // Asynchronously invoke the named operation on the endpoint reference - Message message = mf.createMessage(); - message.setBody(new Object[]{e}); - - // Generate MESSAGE_ID here. - // String messageID = "myuniqueid"; - String messageID = UUID.randomUUID().toString(); - message.getHeaders().put(Constants.MESSAGE_ID, messageID); - - // save the message id ready for when we process the response - asyncMessageMap.put(messageID, op); - - // We could add implementation specific headers here if required - //message.getHeaders().put(Constants.???, ???); - - try { - repr.invokeAsync(ops.get(op), message); - } catch (Throwable ex) { - ex.printStackTrace(); - } - - } } diff --git a/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/test/java/sample/ClientTest.java b/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/test/java/sample/ClientTest.java index 92291c98a1..779fe5d188 100644 --- a/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/test/java/sample/ClientTest.java +++ b/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/test/java/sample/ClientTest.java @@ -58,7 +58,7 @@ public class ClientTest { out.println("ClientTest.wello(" + s + ")"); final Element hreq = xdom("http://sample/hello", "hello", elem("name", text(s))); - final Element hres = wello.call("hello", hreq); + final Element hres = wello.doccall("hello", hreq); return xreduce(print, "", xfilter(select("result"), elems(hres))); } diff --git a/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/test/java/sample/WelloTest.java b/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/test/java/sample/WelloTest.java index 1bb9fc11ce..74f71162fd 100644 --- a/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/test/java/sample/WelloTest.java +++ b/sandbox/sebastien/java/wrapped/samples/extending-tuscany/implementation-sample/src/test/java/sample/WelloTest.java @@ -43,17 +43,31 @@ import sample.api.WSDLReference; @WSDL("http://sample/hello#Hello") public class WelloTest { - @WSDL("http://sample/upper#Upper") + // Uncomment and comment the next line to switch back from RPC to Doc mode + //WSDL("http://sample/upper#Upper") + @WSDL("http://sample/upperrpc#Upper") WSDLReference upper; - public Element call(String op, Element e) { + // Uncomment and comment rpccall to switch back from RPC to Doc mode + /* + public Element doccall(String op, Element e) { out.println("WelloTest." + op + "(" + xml(e) + ")"); final String name = xreduce(print, "", xfilter(select("name"), elems(e))); final Element ureq = xdom("http://sample/upper", "upper", elem("s", text("Hello " + name))); - final Element ures = upper.call("upper", ureq); + final Element ures = upper.doccall("upper", ureq); final String s = xreduce(print, "", xfilter(select("result"), elems(ures))); return xdom("http://sample/hello", "helloResponse", elem("result", text(s))); } + */ + + public Element doccall(String op, Element e) { + out.println("WelloTest." + op + "(" + xml(e) + ")"); + final String name = xreduce(print, "", xfilter(select("name"), elems(e))); + + final String s = (String)upper.rpccall("upper", "Hello " + name); + + return xdom("http://sample/hello", "helloResponse", elem("result", text(s))); + } } -- cgit v1.2.3