From 1a206d215b186fc87acd7c7662dbc2d29b974557 Mon Sep 17 00:00:00 2001 From: slaws Date: Fri, 3 Dec 2010 12:23:19 +0000 Subject: TUSCANY-3801 - update to match infrastructure changes to support native async bindings (not committed yet) git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1041784 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/sampleasync/impl/SampleAsyncProvider.java | 11 +++++-- .../impl/SampleAsyncResponseInvoker.java | 6 ++-- .../java/sampleasync/impl/SampleWSDLInvoker.java | 38 ++++++++++++++++++++-- .../java/sampleasync/impl/SampleWSDLProxy.java | 19 ++++++++--- .../impl/UpperSampleAsyncReferenceImpl.java | 11 +++---- 5 files changed, 66 insertions(+), 19 deletions(-) (limited to 'sca-java-2.x/trunk/unreleased/samples') diff --git a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncProvider.java b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncProvider.java index 80193277da..146d027df8 100644 --- a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncProvider.java +++ b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncProvider.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.Map; import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.invocation.ProxyFactory; import org.apache.tuscany.sca.interfacedef.Interface; @@ -85,17 +86,23 @@ class SampleAsyncProvider implements ImplementationAsyncProvider { } public Invoker createInvoker(final RuntimeComponentService s, final Operation op) { + // TODO - we're passing EP into the WSDL invoker so this isn't going to work + // properly for sync calls + return createAsyncInvoker(null, s, op); + } + + public Invoker createAsyncInvoker(Endpoint endpoint, final RuntimeComponentService s, final Operation op) { try { // Creating an invoker for a Java or WSDL-typed implementation if(op instanceof JavaOperation) return new SampleJavaInvoker((JavaOperation)op, impl.clazz, instance); - return new SampleWSDLInvoker((WSDLOperation)op, impl.clazz, instance); + return new SampleWSDLInvoker(endpoint, (WSDLOperation)op, impl.clazz, instance); } catch(Exception e) { throw new RuntimeException(e); } } - public Invoker createAsyncResponseInvoker(RuntimeComponentService service, Operation operation) { + public Invoker createAsyncResponseInvoker(Operation operation) { return new SampleAsyncResponseInvoker(asyncMessageMap, operation, impl.clazz, instance); } } diff --git a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncResponseInvoker.java b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncResponseInvoker.java index 3e7e0f6a51..c73fdbc7e3 100644 --- a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncResponseInvoker.java +++ b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncResponseInvoker.java @@ -54,10 +54,10 @@ class SampleAsyncResponseInvoker implements Invoker { String forwardOpName = (String)asyncMessageMap.get(messageID); // process the async response - Object reponse = ((Object[])msg.getBody())[0]; + //Object reponse = ((Object[])msg.getBody())[0]; + Object reponse = msg.getBody(); - //Method method = instance.getClass().getMethod(forwardOpName + "Callback", Element.class); - Method method = instance.getClass().getMethod(forwardOpName + "Callback", String.class); + Method method = instance.getClass().getMethod(forwardOpName + "Callback", Element.class); method.invoke(instance, reponse); } catch(Exception e) { e.printStackTrace(); diff --git a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleWSDLInvoker.java b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleWSDLInvoker.java index 0c1051b58c..3140da16da 100644 --- a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleWSDLInvoker.java +++ b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleWSDLInvoker.java @@ -21,9 +21,14 @@ package sampleasync.impl; import java.lang.reflect.Method; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.core.invocation.impl.InterceptorAsyncImpl; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLOperation; +import org.apache.tuscany.sca.invocation.InterceptorAsync; import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.InvokerAsync; import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; import org.w3c.dom.Element; /** @@ -32,26 +37,53 @@ import org.w3c.dom.Element; * * @version $Rev$ $Date$ */ -class SampleWSDLInvoker implements Invoker { +class SampleWSDLInvoker extends InterceptorAsyncImpl { + final Endpoint endpoint; final String name; final Object instance; final Method method; - SampleWSDLInvoker(final WSDLOperation op, final Class clazz, final Object instance) throws SecurityException, NoSuchMethodException { + SampleWSDLInvoker(Endpoint endpoint, final WSDLOperation op, final Class clazz, final Object instance) throws SecurityException, NoSuchMethodException { + this.endpoint = endpoint; this.name = op.getName(); this.instance = instance; this.method = clazz.getMethod("call", String.class, Element.class); } + + public Invoker getNext() { + // Can't get next for an implementation invoker + return null; + } public Message invoke(final Message msg) { + return processRequest(msg); + } + + public void invokeAsyncRequest(Message msg) { + 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. + + ((RuntimeEndpoint)endpoint).invokeAsyncResponse(this, responseMsg); + } + + public Message processRequest(Message msg) { try { //AsyncHeader asyncHeader = (String) message.getHeaders().get("ASYNC-HEADER"); // Invoke the generic call method - msg.setBody(method.invoke(instance, name, ((Object[])msg.getBody())[0])); + Object response = method.invoke(instance, name, ((Object[])msg.getBody())[0]); + msg.setBody(response); } catch(Exception e) { e.printStackTrace(); msg.setFaultBody(e.getCause()); } return msg; } + + public Message processResponse(Message msg) { + return msg; + } } diff --git a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleWSDLProxy.java b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleWSDLProxy.java index 55a4d6b488..a02132ac9e 100644 --- a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleWSDLProxy.java +++ b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleWSDLProxy.java @@ -72,16 +72,27 @@ class SampleWSDLProxy implements WSDLReference { Message message = mf.createMessage(); message.setBody(new Object[]{e}); + // We could MESSAGE_ID here if required. If not the infrastructure + // will generate a UUID + String messageID = "myuniqueid"; + 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(); } - // save the message id ready for when we process the response - String messageID = (String) message.getHeaders().get(Constants.MESSAGE_ID); - - asyncMessageMap.put(messageID, op); + // if we don't provide a message id we can get the one the + // infrastructure generates + //String messageID = (String) message.getHeaders().get(Constants.MESSAGE_ID); + //asyncMessageMap.put(messageID, op); + } } diff --git a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/test/java/sampleasync/impl/UpperSampleAsyncReferenceImpl.java b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/test/java/sampleasync/impl/UpperSampleAsyncReferenceImpl.java index d3e91c7f18..101c6a4399 100644 --- a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/test/java/sampleasync/impl/UpperSampleAsyncReferenceImpl.java +++ b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/test/java/sampleasync/impl/UpperSampleAsyncReferenceImpl.java @@ -41,7 +41,7 @@ public class UpperSampleAsyncReferenceImpl { @WSDL("http://sample/upper#Upper") WSDLReference upper; - String response; + Element response; public String upper(String s) { out.println("UpperSampleAsyncReferenceImpl.upper(" + s + ")"); @@ -59,7 +59,7 @@ public class UpperSampleAsyncReferenceImpl { // do nothing } - return response; + return response.getTextContent(); } /** @@ -67,11 +67,8 @@ public class UpperSampleAsyncReferenceImpl { * async callback arrives at an operation named * operationName + Callback */ - // TODO - I think this should take and Element parameter but the response - // handler interface is a Java interface at the moment and so no - // transformation takes place automatically. - public void upperCallback(String response) { - out.println("UpperSampleAsyncReferenceImpl.upperCallback(" + response + ")"); + public void upperCallback(Element response) { + out.println("UpperSampleAsyncReferenceImpl.upperCallback(" + response.getTextContent() + ")"); this.response = response; } } -- cgit v1.2.3