1 17 package org.apache.servicemix.sca.handler; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 22 import org.apache.tuscany.core.context.ExternalServiceContext; 23 import org.apache.tuscany.core.context.InstanceContext; 24 import org.apache.tuscany.core.context.QualifiedName; 25 import org.apache.tuscany.core.context.ScopeContext; 26 import org.apache.tuscany.core.context.TargetException; 27 import org.apache.tuscany.core.invocation.Interceptor; 28 import org.apache.tuscany.core.invocation.TargetInvoker; 29 import org.apache.tuscany.core.message.Message; 30 31 public class ExternalJbiServiceTargetInvoker implements TargetInvoker { 32 33 private QualifiedName serviceName; 34 private String esName; 35 private Method method; 36 private ScopeContext container; 37 38 private ExternalServiceContext context; 39 40 45 public ExternalJbiServiceTargetInvoker(QualifiedName serviceName, Method method, ScopeContext container) { 46 assert (serviceName != null) : "No service name specified"; 47 assert (method != null) : "No method specified"; 48 assert (container != null) : "No scope container specified"; 49 this.serviceName = serviceName; 50 this.esName = serviceName.getPartName(); 51 this.method = method; 52 this.container = container; 53 } 54 55 public Object invokeTarget(Object payload) throws InvocationTargetException { 56 if (context == null) { 57 InstanceContext iContext = container.getContext(esName); 58 if (!(iContext instanceof ExternalServiceContext)) { 59 TargetException te = new TargetException ("Unexpected target context type"); 60 te.setIdentifier(iContext.getClass().getName()); 61 te.addContextName(iContext.getName()); 62 throw te; 63 } 64 context = (ExternalServiceContext) iContext; 65 } 66 ExternalJbiServiceClient client = (ExternalJbiServiceClient) context.getImplementationInstance(true); 67 if (payload != null) { 68 return client.invoke(method, (Object [])payload); 69 } else { 70 return client.invoke(method, null); 71 } 72 } 73 74 public boolean isCacheable() { 75 return false; 76 } 77 78 public Message invoke(Message msg) { 79 try { 80 Object resp = invokeTarget(msg.getBody()); 81 msg.setBody(resp); 82 } catch (InvocationTargetException e) { 83 msg.setBody(e.getCause()); 84 } catch (Throwable e) { 85 msg.setBody(e); 86 } 87 return msg; 88 } 89 90 public void setNext(Interceptor next) { 91 throw new UnsupportedOperationException (); 92 } 93 94 public Object clone() { 95 try { 96 ExternalJbiServiceTargetInvoker invoker = (ExternalJbiServiceTargetInvoker) super.clone(); 97 invoker.container = container; 98 invoker.context = this.context; 99 invoker.esName = this.esName; 100 invoker.method = this.method; 101 invoker.serviceName = this.serviceName; 102 return invoker; 103 } catch (CloneNotSupportedException e) { 104 return null; } 106 } 107 108 } 109 | Popular Tags |