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
This commit is contained in:
jsdelfino 2011-01-10 21:50:34 +00:00
parent 669ca7bbf2
commit 5ae609d772
5 changed files with 44 additions and 58 deletions

View file

@ -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);
}

View file

@ -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());

View file

@ -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();
}
}
}

View file

@ -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)));
}

View file

@ -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)));
}
}