1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.transformer.simple; |
11 | |
|
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.api.transformer.TransformerException; |
15 | |
import org.mule.config.i18n.CoreMessages; |
16 | |
import org.mule.transformer.AbstractMessageAwareTransformer; |
17 | |
import org.mule.util.expression.ExpressionEvaluatorManager; |
18 | |
import org.mule.util.expression.ExpressionRuntimeException; |
19 | |
|
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.List; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class ExpressionTransformer extends AbstractMessageAwareTransformer |
44 | |
{ |
45 | |
private List arguments; |
46 | 0 | private boolean returnSourceIfNull = false; |
47 | |
|
48 | |
public ExpressionTransformer() |
49 | 0 | { |
50 | |
|
51 | 0 | registerSourceType(Object.class); |
52 | 0 | setReturnClass(Object.class); |
53 | 0 | arguments = new ArrayList(4); |
54 | 0 | } |
55 | |
|
56 | |
public void addArgument(Argument argument) |
57 | |
{ |
58 | 0 | arguments.add(argument); |
59 | 0 | } |
60 | |
|
61 | |
public boolean removeArgument(Argument argument) |
62 | |
{ |
63 | 0 | return arguments.remove(argument); |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public void initialise() throws InitialisationException |
75 | |
{ |
76 | 0 | if (arguments == null || arguments.size() == 0) |
77 | |
{ |
78 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("arguments[]"), this); |
79 | |
} |
80 | |
|
81 | 0 | for (Iterator iterator = arguments.iterator(); iterator.hasNext();) |
82 | |
{ |
83 | 0 | Argument argument = (Argument) iterator.next(); |
84 | |
try |
85 | |
{ |
86 | 0 | argument.validate(); |
87 | |
} |
88 | 0 | catch (Exception e) |
89 | |
{ |
90 | 0 | throw new InitialisationException(e, this); |
91 | 0 | } |
92 | 0 | } |
93 | 0 | } |
94 | |
|
95 | |
public Object transform(MuleMessage message, String outputEncoding) throws TransformerException |
96 | |
{ |
97 | 0 | Object results[] = new Object[arguments.size()]; |
98 | 0 | int i = 0; |
99 | 0 | for (Iterator iterator = arguments.iterator(); iterator.hasNext(); i++) |
100 | |
{ |
101 | 0 | Argument argument = (Argument) iterator.next(); |
102 | |
try |
103 | |
{ |
104 | 0 | results[i] = ExpressionEvaluatorManager.evaluate(argument.getFullExpression(), message); |
105 | |
} |
106 | 0 | catch (ExpressionRuntimeException e) |
107 | |
{ |
108 | 0 | throw new TransformerException(this, e); |
109 | 0 | } |
110 | |
|
111 | 0 | if (!argument.isOptional() && results[i] == null) |
112 | |
{ |
113 | 0 | throw new TransformerException(CoreMessages.expressionEvaluatorReturnedNull( |
114 | |
argument.getEvaluator(), argument.getExpression()), this); |
115 | |
|
116 | |
} |
117 | |
|
118 | |
} |
119 | 0 | if (isReturnSourceIfNull() && checkIfAllAreNull(results)) |
120 | |
{ |
121 | 0 | return message; |
122 | |
} |
123 | |
|
124 | 0 | if (results.length == 1) |
125 | |
{ |
126 | 0 | return results[0]; |
127 | |
} |
128 | |
else |
129 | |
{ |
130 | 0 | return results; |
131 | |
} |
132 | |
} |
133 | |
|
134 | |
private boolean checkIfAllAreNull(Object[] objects) |
135 | |
{ |
136 | 0 | for (int i = 0; i < objects.length; i++) |
137 | |
{ |
138 | 0 | if (objects[i] != null) |
139 | |
{ |
140 | 0 | return false; |
141 | |
} |
142 | |
} |
143 | 0 | return true; |
144 | |
} |
145 | |
|
146 | |
public List getArguments() |
147 | |
{ |
148 | 0 | return arguments; |
149 | |
} |
150 | |
|
151 | |
public void setArguments(List arguments) |
152 | |
{ |
153 | 0 | this.arguments = arguments; |
154 | 0 | } |
155 | |
|
156 | |
public boolean isReturnSourceIfNull() |
157 | |
{ |
158 | 0 | return returnSourceIfNull; |
159 | |
} |
160 | |
|
161 | |
public void setReturnSourceIfNull(boolean returnSourceIfNull) |
162 | |
{ |
163 | 0 | this.returnSourceIfNull = returnSourceIfNull; |
164 | 0 | } |
165 | |
|
166 | |
public static class Argument |
167 | |
{ |
168 | |
public static final String EVAL_TOKEN = ":"; |
169 | |
private String expression; |
170 | |
private String evaluator; |
171 | |
private String customEvaluator; |
172 | |
private boolean optional; |
173 | |
|
174 | |
public Argument() |
175 | 0 | { |
176 | 0 | } |
177 | |
|
178 | |
public String getCustomEvaluator() |
179 | |
{ |
180 | 0 | return customEvaluator; |
181 | |
} |
182 | |
|
183 | |
public void setCustomEvaluator(String customEvaluator) |
184 | |
{ |
185 | 0 | this.customEvaluator = customEvaluator; |
186 | 0 | } |
187 | |
|
188 | |
public String getEvaluator() |
189 | |
{ |
190 | 0 | return evaluator; |
191 | |
} |
192 | |
|
193 | |
public void setEvaluator(String evaluator) |
194 | |
{ |
195 | 0 | this.evaluator = evaluator; |
196 | 0 | } |
197 | |
|
198 | |
public String getExpression() |
199 | |
{ |
200 | 0 | return expression; |
201 | |
} |
202 | |
|
203 | |
public void setExpression(String expression) |
204 | |
{ |
205 | 0 | this.expression = expression; |
206 | 0 | } |
207 | |
|
208 | |
public boolean isOptional() |
209 | |
{ |
210 | 0 | return optional; |
211 | |
} |
212 | |
|
213 | |
public void setOptional(boolean optional) |
214 | |
{ |
215 | 0 | this.optional = optional; |
216 | 0 | } |
217 | |
|
218 | |
protected String getFullExpression() |
219 | |
{ |
220 | |
|
221 | 0 | if (!optional && (evaluator.equals("headers") || evaluator.equals("headers-list") || |
222 | |
(evaluator.equals("attachments") || evaluator.equals("attachments-list")))) |
223 | |
{ |
224 | 0 | return evaluator + EVAL_TOKEN + expression + "required"; |
225 | |
} |
226 | 0 | return evaluator + EVAL_TOKEN + expression; |
227 | |
} |
228 | |
|
229 | |
protected void validate() |
230 | |
{ |
231 | 0 | if (expression == null) |
232 | |
{ |
233 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("expression").getMessage()); |
234 | |
} |
235 | |
|
236 | 0 | if (evaluator == null) |
237 | |
{ |
238 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("evaluator").getMessage()); |
239 | |
} |
240 | |
|
241 | 0 | if (evaluator.equals("custom")) |
242 | |
{ |
243 | 0 | if (customEvaluator == null) |
244 | |
{ |
245 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("customEvaluator").getMessage()); |
246 | |
} |
247 | |
else |
248 | |
{ |
249 | 0 | evaluator = customEvaluator; |
250 | |
} |
251 | |
} |
252 | |
|
253 | 0 | if (!ExpressionEvaluatorManager.isEvaluatorRegistered(evaluator)) |
254 | |
{ |
255 | 0 | throw new IllegalArgumentException(CoreMessages.expressionEvaluatorNotRegistered(evaluator).getMessage()); |
256 | |
} |
257 | 0 | } |
258 | |
} |
259 | |
} |