summaryrefslogtreecommitdiffstats
path: root/sandbox/container.groovy/src/main/java/org/apache/tuscany/container/groovy/invoker/GroovyInvoker.java
blob: 0a478269bb29cdd6a80662c2aafc4bd48f9c466b (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed 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 org.apache.tuscany.container.groovy.invoker;

import java.lang.reflect.InvocationTargetException;

import org.apache.tuscany.core.context.AtomicContext;
import org.apache.tuscany.core.context.ScopeContext;
import org.apache.tuscany.core.message.Message;
import org.apache.tuscany.core.wire.Interceptor;
import org.apache.tuscany.core.wire.TargetInvoker;

/**
 * Groovy target invoker.
 *
 */
public class GroovyInvoker implements TargetInvoker, Cloneable {
	
	// SCope context
	private ScopeContext scopeContext;	
	
	// Srevice name
	private String serviceName;
	
	// Method name
	private String methodName;
	
	/**
	 * Initializes the invoker.
	 * 
	 * @param scopeContext SCope context.
	 * @param serviceName Service name.
	 * @param methodName Method name.
	 */
	public GroovyInvoker(ScopeContext scopeContext, String serviceName, String methodName) {
		this.scopeContext = scopeContext;
		this.serviceName = serviceName;
		this.methodName = methodName;
	}

	/**
	 * Invokes the target.
	 */
	public Object invokeTarget(Object payload) throws InvocationTargetException {
		
		AtomicContext atomicContext = (AtomicContext)scopeContext.getContext(serviceName);
        GroovyScript groovyScript = (GroovyScript)atomicContext.getTargetInstance();
        
		Object[] args = (Object[])payload;
		
		try {
			return groovyScript.runScript(methodName, args);
		} catch(Exception ex) {
			throw new InvocationTargetException(ex);
		}
		
	}

	/**
	 * Whether the invoker is cacheable.
	 */
	public boolean isCacheable() {
		return false;
	}

	/**
	 * Invokes the servce.
	 */
	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;
	}

	/**
	 * Sets interceptor.
	 */
	public void setNext(Interceptor next) {
	}

    /**
     * Implementations must support deep cloning
     */
    public Object clone() {
    	return new GroovyInvoker(scopeContext, serviceName, methodName);
    }

}