diff options
Diffstat (limited to 'sca-java-2.x/trunk/unreleased/samples/implementation-sample-async')
4 files changed, 117 insertions, 13 deletions
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<String, Object> asyncMessageMap; + + SampleAsyncInvoker(Map<String, Object> 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<String, Object> asyncMessageMap = new HashMap<String, Object>(); 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<String, Operation> ops; final ExtensionPointRegistry ep; final MessageFactory mf; + Map<String, Object> asyncMessageMap; - SampleWSDLProxy(EndpointReference epr, Interface wi, ExtensionPointRegistry ep) { + SampleWSDLProxy(Map<String, Object> 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); } } 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 286e619326..d3e91c7f18 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 @@ -20,6 +20,12 @@ package sampleasync.impl; import static java.lang.System.out; +import static sample.Xutil.elem; +import static sample.Xutil.text; +import static sample.Xutil.xdom; + +import org.w3c.dom.Element; + import sample.api.Java; import sample.api.WSDL; import sample.api.WSDLReference; @@ -35,11 +41,25 @@ public class UpperSampleAsyncReferenceImpl { @WSDL("http://sample/upper#Upper") WSDLReference upper; + String response; public String upper(String s) { out.println("UpperSampleAsyncReferenceImpl.upper(" + s + ")"); - upper.callAsync("upper", null); - return null; + + // TODO - I'm passing in the non-wrapped version of the parameter + // here which doesn't seem right. If I pass in the wrapped + // version then the databinding won't unwrap on the reference + // side as it thinks the target Java interface is bare? + final Element ureq = xdom("http://sample/upper", "s", text(s)); + upper.callAsync("upper", ureq); + + try { + Thread.sleep(500); + } catch (Exception ex) { + // do nothing + } + + return response; } /** @@ -47,7 +67,11 @@ public class UpperSampleAsyncReferenceImpl { * async callback arrives at an operation named * operationName + Callback */ - public void upperCallback(String s) { - out.println("UpperSampleAsyncReferenceImpl.upperCallback(" + s + ")"); + // 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 + ")"); + this.response = response; } } |