From 93777d8895392ec7b484ae0426a8bdef1149b4d6 Mon Sep 17 00:00:00 2001 From: slaws Date: Tue, 30 Nov 2010 16:02:31 +0000 Subject: Add code to process the async response git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1040598 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/sampleasync/impl/SampleAsyncInvoker.java | 68 ++++++++++++++++++++++ .../java/sampleasync/impl/SampleAsyncProvider.java | 13 ++++- .../java/sampleasync/impl/SampleWSDLProxy.java | 17 ++++-- 3 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncInvoker.java (limited to 'sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl') diff --git a/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncInvoker.java b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncInvoker.java new file mode 100644 index 0000000000..74ac0467cf --- /dev/null +++ b/sca-java-2.x/trunk/unreleased/samples/implementation-sample-async/src/main/java/sampleasync/impl/SampleAsyncInvoker.java @@ -0,0 +1,68 @@ +/* + * 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 sampleasync.impl; + +import java.lang.reflect.Method; +import java.util.Map; + +import org.apache.tuscany.sca.core.invocation.Constants; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLOperation; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.w3c.dom.Element; + +/** + * Invoker for Sample components that implement a WSDL interface using a generic + * call method. + * + * @version $Rev$ $Date$ + */ +class SampleAsyncInvoker implements Invoker { + final String name; + final Object instance; + final Operation op; + Map asyncMessageMap; + + SampleAsyncInvoker(Map asyncMessageMap, final Operation op, final Class clazz, final Object instance) { + this.asyncMessageMap = asyncMessageMap; + this.name = op.getName(); + this.instance = instance; + this.op = op; + } + + public Message invoke(final Message msg) { + try { + String messageID = (String) msg.getHeaders().get(Constants.MESSAGE_ID); + String forwardOpName = (String)asyncMessageMap.get(messageID); + + // process the async response + Object reponse = ((Object[])msg.getBody())[0]; + + //Method method = instance.getClass().getMethod(forwardOpName + "Callback", Element.class); + Method method = instance.getClass().getMethod(forwardOpName + "Callback", String.class); + method.invoke(instance, reponse); + } catch(Exception e) { + e.printStackTrace(); + msg.setFaultBody(e.getCause()); + } + return msg; + } +} 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 9538a88a67..bc56877469 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 @@ -20,6 +20,8 @@ package sampleasync.impl; import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; import org.apache.tuscany.sca.assembly.ComponentReference; import org.apache.tuscany.sca.core.ExtensionPointRegistry; @@ -30,7 +32,7 @@ import org.apache.tuscany.sca.interfacedef.java.JavaInterface; import org.apache.tuscany.sca.interfacedef.java.JavaOperation; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLOperation; import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.provider.ImplementationAsyncProvider; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentService; @@ -39,12 +41,13 @@ import org.apache.tuscany.sca.runtime.RuntimeComponentService; * * @version $Rev$ $Date$ */ -class SampleAsyncProvider implements ImplementationProvider { +class SampleAsyncProvider implements ImplementationAsyncProvider { final RuntimeComponent comp; final SampleAsyncImplementation impl; final ProxyFactory pxf; final ExtensionPointRegistry ep; Object instance; + Map asyncMessageMap = new HashMap(); SampleAsyncProvider(final RuntimeComponent comp, final SampleAsyncImplementation impl, ProxyFactory pf, ExtensionPointRegistry ep) { this.comp = comp; @@ -66,7 +69,7 @@ class SampleAsyncProvider implements ImplementationProvider { if(i instanceof JavaInterface) f.set(instance, pxf.createProxy(comp.getComponentContext().getServiceReference(f.getType(), r.getName()))); else - f.set(instance, new SampleWSDLProxy(r.getEndpointReferences().get(0), i, ep)); + f.set(instance, new SampleWSDLProxy(asyncMessageMap, r.getEndpointReferences().get(0), i, ep)); } } catch(Exception e) { throw new RuntimeException(e); @@ -91,4 +94,8 @@ class SampleAsyncProvider implements ImplementationProvider { throw new RuntimeException(e); } } + + public Invoker createAsyncInvoker(RuntimeComponentService service, Operation operation) { + return new SampleAsyncInvoker(asyncMessageMap, operation, impl.clazz, instance); + } } 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 19d518e44c..55a4d6b488 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 @@ -43,8 +43,10 @@ class SampleWSDLProxy implements WSDLReference { final Map ops; final ExtensionPointRegistry ep; final MessageFactory mf; + Map asyncMessageMap; - SampleWSDLProxy(EndpointReference epr, Interface wi, ExtensionPointRegistry ep) { + SampleWSDLProxy(Map asyncMessageMap, EndpointReference epr, Interface wi, ExtensionPointRegistry ep) { + this.asyncMessageMap = asyncMessageMap; this.ep = ep; mf = ep.getExtensionPoint(MessageFactory.class); @@ -68,15 +70,18 @@ class SampleWSDLProxy implements WSDLReference { public void callAsync(String op, Element e) { // Asynchronously invoke the named operation on the endpoint reference Message message = mf.createMessage(); - message.setBody(message); + message.setBody(new Object[]{e}); // We could add implementation specific headers here if required - - repr.invokeAsync(ops.get(op), message); - - String messageID = (String) message.getHeaders().get(Constants.MESSAGE_ID); + 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); } } -- cgit v1.2.3