summaryrefslogtreecommitdiffstats
path: root/tags/java-stable-20060304/sca/container.js/src/main/java/org/apache/tuscany/container/js/rhino/RhinoTargetInvoker.java
blob: 62cf40f3330542f38650a849421a3490ce0a089e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
        }
    }
}