1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.component; |
12 | |
|
13 | |
import org.mule.DefaultMuleEvent; |
14 | |
import org.mule.DefaultMuleMessage; |
15 | |
import org.mule.OptimizedRequestContext; |
16 | |
import org.mule.VoidResult; |
17 | |
import org.mule.api.MuleContext; |
18 | |
import org.mule.api.MuleEvent; |
19 | |
import org.mule.api.MuleException; |
20 | |
import org.mule.api.MuleMessage; |
21 | |
import org.mule.api.component.Component; |
22 | |
import org.mule.api.construct.FlowConstruct; |
23 | |
import org.mule.api.context.MuleContextAware; |
24 | |
import org.mule.api.context.notification.ServerNotificationHandler; |
25 | |
import org.mule.api.interceptor.Interceptor; |
26 | |
import org.mule.api.lifecycle.Initialisable; |
27 | |
import org.mule.api.lifecycle.InitialisationException; |
28 | |
import org.mule.api.lifecycle.Lifecycle; |
29 | |
import org.mule.api.lifecycle.LifecycleException; |
30 | |
import org.mule.api.processor.MessageProcessor; |
31 | |
import org.mule.api.transformer.Transformer; |
32 | |
import org.mule.config.i18n.CoreMessages; |
33 | |
import org.mule.config.i18n.MessageFactory; |
34 | |
import org.mule.context.notification.ComponentMessageNotification; |
35 | |
import org.mule.context.notification.OptimisedNotificationHandler; |
36 | |
import org.mule.management.stats.ComponentStatistics; |
37 | |
import org.mule.processor.builder.InterceptingChainMessageProcessorBuilder; |
38 | |
import org.mule.transformer.TransformerTemplate; |
39 | |
import org.mule.transport.NullPayload; |
40 | |
|
41 | |
import java.util.ArrayList; |
42 | |
import java.util.Collections; |
43 | |
import java.util.List; |
44 | |
|
45 | |
import org.apache.commons.logging.Log; |
46 | |
import org.apache.commons.logging.LogFactory; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public abstract class AbstractComponent implements Component, MuleContextAware, Lifecycle |
52 | |
{ |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 0 | protected final Log logger = LogFactory.getLog(this.getClass()); |
58 | |
|
59 | |
protected FlowConstruct flowConstruct; |
60 | 0 | protected ComponentStatistics statistics = null; |
61 | |
protected ServerNotificationHandler notificationHandler; |
62 | 0 | protected List<Interceptor> interceptors = new ArrayList<Interceptor>(); |
63 | |
protected MessageProcessor interceptorChain; |
64 | |
protected MuleContext muleContext; |
65 | |
|
66 | |
public void setMuleContext(MuleContext context) |
67 | |
{ |
68 | 0 | this.muleContext = context; |
69 | 0 | } |
70 | |
|
71 | |
public List<Interceptor> getInterceptors() |
72 | |
{ |
73 | 0 | return interceptors; |
74 | |
} |
75 | |
|
76 | |
public void setInterceptors(List<Interceptor> interceptors) |
77 | |
{ |
78 | 0 | this.interceptors = interceptors; |
79 | 0 | } |
80 | |
|
81 | |
public AbstractComponent() |
82 | 0 | { |
83 | 0 | statistics = new ComponentStatistics(); |
84 | 0 | } |
85 | |
|
86 | |
private MuleEvent invokeInternal(MuleEvent event) throws MuleException |
87 | |
{ |
88 | |
|
89 | 0 | OptimizedRequestContext.unsafeSetEvent(event); |
90 | |
|
91 | 0 | if (logger.isTraceEnabled()) |
92 | |
{ |
93 | 0 | logger.trace("Invoking " + this.getClass().getName() + "component for service " |
94 | |
+ flowConstruct.getName()); |
95 | |
} |
96 | |
|
97 | 0 | if (!flowConstruct.getLifecycleState().isStarted() || flowConstruct.getLifecycleState().isStopping()) |
98 | |
{ |
99 | 0 | throw new LifecycleException(CoreMessages.isStopped(flowConstruct.getName()), this); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
try |
104 | |
{ |
105 | 0 | fireComponentNotification(event.getMessage(), ComponentMessageNotification.COMPONENT_PRE_INVOKE); |
106 | |
|
107 | 0 | long startTime = 0; |
108 | 0 | if (statistics.isEnabled()) |
109 | |
{ |
110 | 0 | startTime = System.currentTimeMillis(); |
111 | |
} |
112 | |
|
113 | 0 | Object result = doInvoke(event); |
114 | |
|
115 | 0 | if (statistics.isEnabled()) |
116 | |
{ |
117 | 0 | statistics.addExecutionTime(System.currentTimeMillis() - startTime); |
118 | |
} |
119 | |
|
120 | 0 | MuleEvent resultEvent = createResultEvent(event, result); |
121 | |
|
122 | 0 | resultEvent.setStopFurtherProcessing(event.isStopFurtherProcessing()); |
123 | 0 | fireComponentNotification(resultEvent.getMessage(), ComponentMessageNotification.COMPONENT_POST_INVOKE); |
124 | |
|
125 | 0 | return resultEvent; |
126 | |
} |
127 | 0 | catch (MuleException me) |
128 | |
{ |
129 | 0 | throw me; |
130 | |
} |
131 | 0 | catch (Exception e) |
132 | |
{ |
133 | 0 | throw new ComponentException(CoreMessages.failedToInvoke(this.toString()), event, |
134 | |
this, e); |
135 | |
} |
136 | |
} |
137 | |
|
138 | |
public MuleEvent process(MuleEvent event) throws MuleException |
139 | |
{ |
140 | 0 | if (interceptorChain == null) |
141 | |
{ |
142 | 0 | return invokeInternal(event); |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | 0 | return interceptorChain.process(event); |
147 | |
} |
148 | |
} |
149 | |
|
150 | |
protected MuleEvent createResultEvent(MuleEvent event, Object result) throws MuleException |
151 | |
{ |
152 | 0 | if (result instanceof MuleMessage) |
153 | |
{ |
154 | 0 | return new DefaultMuleEvent((MuleMessage) result, event); |
155 | |
} |
156 | 0 | else if (result instanceof VoidResult) |
157 | |
{ |
158 | 0 | return event; |
159 | |
} |
160 | 0 | else if (result != null) |
161 | |
{ |
162 | 0 | event.getMessage().applyTransformers( |
163 | |
event, Collections.<Transformer>singletonList(new TransformerTemplate( |
164 | |
new TransformerTemplate.OverwitePayloadCallback(result)))); |
165 | 0 | return event; |
166 | |
} |
167 | |
else |
168 | |
{ |
169 | 0 | return new DefaultMuleEvent(new DefaultMuleMessage(NullPayload.getInstance(), muleContext), event); |
170 | |
} |
171 | |
} |
172 | |
|
173 | |
protected abstract Object doInvoke(MuleEvent event) throws Exception; |
174 | |
|
175 | |
@Override |
176 | |
public String toString() |
177 | |
{ |
178 | 0 | StringBuilder buf = new StringBuilder(this.getClass().getName()); |
179 | |
|
180 | 0 | if (flowConstruct != null) |
181 | |
{ |
182 | 0 | buf.append(" component for: ").append(flowConstruct.toString()); |
183 | |
} |
184 | |
else |
185 | |
{ |
186 | 0 | buf.append(" no component"); |
187 | |
} |
188 | |
|
189 | 0 | return buf.toString(); |
190 | |
} |
191 | |
|
192 | |
public void release() |
193 | |
{ |
194 | |
|
195 | 0 | } |
196 | |
|
197 | |
public ComponentStatistics getStatistics() |
198 | |
{ |
199 | 0 | return statistics; |
200 | |
} |
201 | |
|
202 | |
public void setFlowConstruct(FlowConstruct flowConstruct) |
203 | |
{ |
204 | 0 | this.flowConstruct = flowConstruct; |
205 | |
|
206 | |
|
207 | 0 | if (this.muleContext == null && flowConstruct.getMuleContext() != null) |
208 | |
{ |
209 | 0 | this.muleContext = flowConstruct.getMuleContext(); |
210 | |
} |
211 | |
|
212 | 0 | } |
213 | |
|
214 | |
public FlowConstruct getFlowConstruct() |
215 | |
{ |
216 | 0 | return flowConstruct; |
217 | |
} |
218 | |
|
219 | |
public final void initialise() throws InitialisationException |
220 | |
{ |
221 | 0 | if (logger.isInfoEnabled()) |
222 | |
{ |
223 | 0 | logger.info("Initialising: " + this); |
224 | |
} |
225 | 0 | if (flowConstruct == null) |
226 | |
{ |
227 | 0 | throw new InitialisationException( |
228 | |
MessageFactory.createStaticMessage("Component has not been initialized properly, no service."), |
229 | |
this); |
230 | |
} |
231 | |
|
232 | 0 | InterceptingChainMessageProcessorBuilder chainBuilder = new InterceptingChainMessageProcessorBuilder(flowConstruct); |
233 | 0 | for (Interceptor interceptor : interceptors) |
234 | |
{ |
235 | 0 | chainBuilder.chain(interceptor); |
236 | |
} |
237 | 0 | chainBuilder.chain(new MessageProcessor() |
238 | 0 | { |
239 | |
public MuleEvent process(MuleEvent event) throws MuleException |
240 | |
{ |
241 | 0 | return invokeInternal(event); |
242 | |
} |
243 | |
}); |
244 | |
|
245 | |
try |
246 | |
{ |
247 | 0 | interceptorChain = chainBuilder.build(); |
248 | 0 | if (interceptorChain instanceof Initialisable) |
249 | |
{ |
250 | 0 | ((Initialisable) interceptorChain).initialise(); |
251 | |
} |
252 | |
} |
253 | 0 | catch (MuleException e) |
254 | |
{ |
255 | 0 | throw new InitialisationException(e, this); |
256 | 0 | } |
257 | |
|
258 | 0 | doInitialise(); |
259 | 0 | } |
260 | |
|
261 | |
protected void doInitialise() throws InitialisationException |
262 | |
{ |
263 | |
|
264 | 0 | } |
265 | |
|
266 | |
public void dispose() |
267 | |
{ |
268 | 0 | if(flowConstruct.getLifecycleState().isDisposed()) |
269 | |
{ |
270 | 0 | return; |
271 | |
} |
272 | |
|
273 | |
try |
274 | |
{ |
275 | |
|
276 | 0 | if (flowConstruct.getLifecycleState().isStarted()) |
277 | |
{ |
278 | 0 | stop(); |
279 | |
} |
280 | |
} |
281 | 0 | catch (MuleException e) |
282 | |
{ |
283 | 0 | logger.error(CoreMessages.failedToStop(toString())); |
284 | 0 | } |
285 | |
try |
286 | |
{ |
287 | 0 | doDispose(); |
288 | |
|
289 | |
} |
290 | 0 | catch (Exception e) |
291 | |
{ |
292 | 0 | logger.warn(CoreMessages.failedToDispose(toString()), e); |
293 | 0 | } |
294 | 0 | } |
295 | |
|
296 | |
protected void doDispose() |
297 | |
{ |
298 | |
|
299 | 0 | } |
300 | |
|
301 | |
public void stop() throws MuleException |
302 | |
{ |
303 | 0 | if(flowConstruct.getLifecycleState().isStopped()) |
304 | |
{ |
305 | 0 | return; |
306 | |
} |
307 | 0 | if (logger.isInfoEnabled()) |
308 | |
{ |
309 | 0 | logger.info("Stopping: " + this); |
310 | |
} |
311 | 0 | doStop(); |
312 | 0 | } |
313 | |
|
314 | |
protected void doStart() throws MuleException |
315 | |
{ |
316 | |
|
317 | 0 | } |
318 | |
|
319 | |
public void start() throws MuleException |
320 | |
{ |
321 | 0 | if(flowConstruct.getLifecycleState().isStarted()) |
322 | |
{ |
323 | 0 | return; |
324 | |
} |
325 | |
|
326 | 0 | if (logger.isInfoEnabled()) |
327 | |
{ |
328 | 0 | logger.info("Starting: " + this); |
329 | |
} |
330 | 0 | notificationHandler = new OptimisedNotificationHandler(flowConstruct.getMuleContext() |
331 | |
.getNotificationManager(), ComponentMessageNotification.class); |
332 | 0 | doStart(); |
333 | 0 | } |
334 | |
|
335 | |
protected void doStop() throws MuleException |
336 | |
{ |
337 | |
|
338 | 0 | } |
339 | |
|
340 | |
protected void fireComponentNotification(MuleMessage message, int action) |
341 | |
{ |
342 | 0 | if (notificationHandler != null && notificationHandler.isNotificationEnabled(ComponentMessageNotification.class)) |
343 | |
{ |
344 | 0 | notificationHandler.fireNotification(new ComponentMessageNotification(message, this, flowConstruct, action)); |
345 | |
} |
346 | 0 | } |
347 | |
|
348 | |
} |