/** * * 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); } }