diff options
Diffstat (limited to 'sca-java-1.x/tags/java-stable-20060304/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoTargetInvoker.java')
-rw-r--r-- | sca-java-1.x/tags/java-stable-20060304/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoTargetInvoker.java | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoTargetInvoker.java b/sca-java-1.x/tags/java-stable-20060304/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoTargetInvoker.java new file mode 100644 index 0000000000..62cf40f333 --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoTargetInvoker.java @@ -0,0 +1,81 @@ +package org.apache.tuscany.container.js.rhino; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.core.context.QualifiedName; +import org.apache.tuscany.core.context.ScopeContext; +import org.apache.tuscany.core.invocation.Interceptor; +import org.apache.tuscany.core.invocation.TargetInvoker; +import org.apache.tuscany.core.message.Message; + +public class RhinoTargetInvoker implements TargetInvoker { + + private ScopeContext container; + + private QualifiedName name; + + private String operation; + + private RhinoScript target; + + public RhinoTargetInvoker(String serviceName, String operation, ScopeContext container) { + assert (serviceName != null) : "No service name specified"; + assert (container != null) : "No scope container specified"; + assert (operation != null) : "No operation specified"; + this.name = new QualifiedName(serviceName); + this.container = container; + this.operation = operation; + } + + public Object invokeTarget(Object payload) throws InvocationTargetException { + if (cacheable) { + if (target == null) { + target = (RhinoScript) container.getContext(name.getPartName()).getImplementationInstance(); + } + return target.invoke(operation, payload); + } else { + return ((RhinoScript) container.getContext(name.getPartName()).getImplementationInstance()) + .invoke(operation, payload); + } + } + + private boolean cacheable; + + public boolean isCacheable() { + return cacheable; + } + + public void setCacheable(boolean val) { + cacheable = val; + } + + public Message invoke(Message msg) { + try { + Object resp = invokeTarget(msg.getBody()); + msg.setBody(resp); + } catch (InvocationTargetException e) { + msg.setBody(e.getCause()); + } catch (Throwable e) { + msg.setBody(e); + } + return msg; + } + + public void setNext(Interceptor next) { + throw new IllegalStateException("This interceptor must be the last interceptor in an interceptor chain"); + } + + public Object clone() { + try { + RhinoTargetInvoker invoker = (RhinoTargetInvoker) super.clone(); + invoker.container = this.container; + invoker.cacheable = this.cacheable; + invoker.name = this.name; + invoker.operation = this.operation; + invoker.target = null; + return invoker; + } catch (CloneNotSupportedException e) { + return null; // will not happen + } + } +} |