1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
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 | |
{ |
24 | |
private volatile Object refreshableBean; |
25 | |
private String methodName; |
26 | |
private static final String ON_CALL = "onCall"; |
27 | 0 | private static final Class[] MULE_EVENT_CONTEXT = new Class[]{MuleEventContext.class}; |
28 | |
|
29 | |
public GroovyRefreshableBeanBuilder() |
30 | |
{ |
31 | 0 | super(); |
32 | 0 | } |
33 | |
|
34 | |
public Object onCall(MuleEventContext eventContext) throws Exception |
35 | |
{ |
36 | 0 | if (refreshableBean instanceof GroovyObject) |
37 | |
{ |
38 | 0 | GroovyObject script = (GroovyObject)refreshableBean; |
39 | 0 | MetaMethod onCall = script.getMetaClass().pickMethod("onCall", MULE_EVENT_CONTEXT); |
40 | |
|
41 | 0 | if (onCall != null) |
42 | |
{ |
43 | 0 | return script.invokeMethod(ON_CALL, eventContext); |
44 | |
} |
45 | |
else |
46 | |
{ |
47 | 0 | if (StringUtils.isEmpty(methodName)) |
48 | |
{ |
49 | 0 | throw new DefaultMuleException(CoreMessages.propertiesNotSet("methodName")); |
50 | |
} |
51 | |
|
52 | 0 | return script.invokeMethod(methodName, eventContext.getMessage().getPayload()); |
53 | |
} |
54 | |
|
55 | |
} |
56 | |
|
57 | 0 | throw new Exception(new DefaultMuleException("script engine not supported")); |
58 | |
} |
59 | |
|
60 | |
public Object getRefreshableBean() |
61 | |
{ |
62 | 0 | return refreshableBean; |
63 | |
} |
64 | |
|
65 | |
public void setRefreshableBean(Object refreshableBean) |
66 | |
{ |
67 | 0 | this.refreshableBean = refreshableBean; |
68 | 0 | } |
69 | |
|
70 | |
public String getMethodName() |
71 | |
{ |
72 | 0 | return methodName; |
73 | |
} |
74 | |
|
75 | |
public void setMethodName(String methodName) |
76 | |
{ |
77 | 0 | this.methodName = methodName; |
78 | 0 | } |
79 | |
} |
80 | |
|
81 | |
|