1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.components.simple; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.impl.NoSatisfiableMethodsException; |
15 | |
import org.mule.impl.VoidResult; |
16 | |
import org.mule.umo.UMOEventContext; |
17 | |
import org.mule.umo.lifecycle.Callable; |
18 | |
import org.mule.umo.lifecycle.Initialisable; |
19 | |
import org.mule.umo.lifecycle.InitialisationException; |
20 | |
import org.mule.util.ClassUtils; |
21 | |
import org.mule.util.StringUtils; |
22 | |
|
23 | |
import java.lang.reflect.Method; |
24 | |
|
25 | 16 | public class NoArgsCallWrapper implements Callable, Initialisable |
26 | |
{ |
27 | |
|
28 | |
|
29 | |
|
30 | |
private Object delegateInstance; |
31 | |
private String delegateMethod; |
32 | |
private String delegateClass; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public void initialise() throws InitialisationException |
46 | |
{ |
47 | 16 | if (delegateInstance == null) |
48 | |
{ |
49 | |
|
50 | 12 | if (StringUtils.isBlank(delegateClass) || StringUtils.isBlank(delegateMethod)) |
51 | |
{ |
52 | 6 | throw new InitialisationException(CoreMessages.noDelegateClassAndMethodProvidedForNoArgsWrapper(), this); |
53 | |
} |
54 | |
} |
55 | |
else |
56 | |
{ |
57 | |
|
58 | 4 | if (StringUtils.isNotBlank(delegateClass)) |
59 | |
{ |
60 | 2 | throw new InitialisationException(CoreMessages.noDelegateClassIfDelegateInstanceSpecified(), this); |
61 | |
} |
62 | |
} |
63 | |
|
64 | 8 | if (StringUtils.isBlank(delegateMethod)) |
65 | |
{ |
66 | 2 | throw new InitialisationException(CoreMessages.objectIsNull("delegateMethod"), this); |
67 | |
} |
68 | 6 | } |
69 | |
|
70 | |
public Object onCall(UMOEventContext context) throws Exception |
71 | |
{ |
72 | 6 | Class clazz = delegateInstance == null ? ClassUtils.loadClass(delegateClass, getClass()) |
73 | |
: delegateInstance.getClass(); |
74 | |
|
75 | 6 | Method method = ClassUtils.getMethod(clazz, delegateMethod, null); |
76 | 6 | if (method == null) |
77 | |
{ |
78 | 0 | throw new NoSatisfiableMethodsException(clazz, delegateMethod); |
79 | |
} |
80 | 6 | if (delegateInstance == null) |
81 | |
{ |
82 | 6 | delegateInstance = clazz.newInstance(); |
83 | |
} |
84 | 6 | Object result = method.invoke(delegateInstance, null); |
85 | 6 | if (Void.TYPE.equals(method.getReturnType())) |
86 | |
{ |
87 | 2 | result = VoidResult.getInstance(); |
88 | |
} |
89 | 6 | return result; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public Object getDelegateInstance() |
98 | |
{ |
99 | 0 | return delegateInstance; |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void setDelegateInstance(final Object delegateInstance) |
108 | |
{ |
109 | 8 | this.delegateInstance = delegateInstance; |
110 | 8 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public String getDelegateMethod() |
118 | |
{ |
119 | 0 | return delegateMethod; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public void setDelegateMethod(final String delegateMethod) |
128 | |
{ |
129 | 16 | this.delegateMethod = delegateMethod; |
130 | 16 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public String getDelegateClass() |
138 | |
{ |
139 | 0 | return delegateClass; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void setDelegateClass(final String delegateClass) |
148 | |
{ |
149 | 20 | this.delegateClass = delegateClass; |
150 | 20 | } |
151 | |
} |