01: /*
02: * $Id: GroovyRefreshableBeanBuilder.java 11195 2008-03-06 04:13:01Z tcarlson $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.module.scripting.component;
12:
13: import org.mule.api.DefaultMuleException;
14: import org.mule.api.MuleEventContext;
15: import org.mule.api.lifecycle.Callable;
16: import org.mule.config.i18n.CoreMessages;
17: import org.mule.util.StringUtils;
18:
19: import groovy.lang.GroovyObject;
20: import groovy.lang.MetaMethod;
21:
22: public class GroovyRefreshableBeanBuilder implements Callable {
23: private volatile Object refreshableBean;
24: private String methodName;
25: private static final String ON_CALL = "onCall";
26: private static final Class[] UMOEVENTCONTEXT = new Class[] { MuleEventContext.class };
27:
28: public GroovyRefreshableBeanBuilder() {
29: super ();
30: }
31:
32: public Object onCall(MuleEventContext eventContext)
33: throws Exception {
34: if (refreshableBean instanceof GroovyObject) {
35: GroovyObject script = (GroovyObject) refreshableBean;
36: MetaMethod onCall = script.getMetaClass().pickMethod(
37: "onCall", UMOEVENTCONTEXT);
38:
39: if (onCall != null) {
40: return script.invokeMethod(ON_CALL, eventContext);
41: } else {
42: if (StringUtils.isEmpty(methodName)) {
43: throw new DefaultMuleException(CoreMessages
44: .propertiesNotSet("methodName"));
45: }
46:
47: return script.invokeMethod(methodName, eventContext
48: .transformMessage());
49: }
50:
51: }
52:
53: throw new Exception(new DefaultMuleException(
54: "script engine not supported"));
55: }
56:
57: public Object getRefreshableBean() {
58: return refreshableBean;
59: }
60:
61: public void setRefreshableBean(Object refreshableBean) {
62: this .refreshableBean = refreshableBean;
63: }
64:
65: public String getMethodName() {
66: return methodName;
67: }
68:
69: public void setMethodName(String methodName) {
70: this.methodName = methodName;
71: }
72: }
|