1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.processor.chain; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleException; |
11 | |
import org.mule.api.construct.FlowConstruct; |
12 | |
import org.mule.api.construct.FlowConstructAware; |
13 | |
import org.mule.api.context.MuleContextAware; |
14 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
15 | |
import org.mule.api.lifecycle.Disposable; |
16 | |
import org.mule.api.lifecycle.Initialisable; |
17 | |
import org.mule.api.lifecycle.InitialisationException; |
18 | |
import org.mule.api.lifecycle.Lifecycle; |
19 | |
import org.mule.api.lifecycle.Startable; |
20 | |
import org.mule.api.lifecycle.Stoppable; |
21 | |
import org.mule.api.processor.MessageProcessor; |
22 | |
import org.mule.api.processor.MessageProcessorChain; |
23 | |
import org.mule.api.processor.policy.AroundPolicy; |
24 | |
import org.mule.api.processor.policy.Policies; |
25 | |
import org.mule.api.processor.policy.PolicyInvocation; |
26 | |
import org.mule.endpoint.EndpointAware; |
27 | |
import org.mule.processor.AbstractInterceptingMessageProcessor; |
28 | |
import org.mule.util.StringUtils; |
29 | |
|
30 | |
import java.util.Iterator; |
31 | |
import java.util.List; |
32 | |
|
33 | |
import org.apache.commons.logging.Log; |
34 | |
import org.apache.commons.logging.LogFactory; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public abstract class AbstractMessageProcessorChain extends AbstractInterceptingMessageProcessor |
42 | |
implements MessageProcessorChain, Lifecycle, FlowConstructAware, MuleContextAware, EndpointAware |
43 | |
{ |
44 | 0 | protected final transient Log log = LogFactory.getLog(getClass()); |
45 | |
protected String name; |
46 | |
protected List<MessageProcessor> processors; |
47 | 0 | private final Policies policies = new Policies(this); |
48 | |
|
49 | |
public AbstractMessageProcessorChain(String name, List<MessageProcessor> processors) |
50 | 0 | { |
51 | 0 | this.name = name; |
52 | 0 | this.processors = processors; |
53 | 0 | } |
54 | |
|
55 | |
public MuleEvent process(MuleEvent event) throws MuleException |
56 | |
{ |
57 | 0 | if (log.isDebugEnabled()) |
58 | |
{ |
59 | 0 | log.debug(String.format("Invoking %s with event %s", this, event)); |
60 | |
} |
61 | 0 | if (event == null) |
62 | |
{ |
63 | 0 | return null; |
64 | |
} |
65 | |
|
66 | 0 | final List<AroundPolicy> activePolicies = policies.list(); |
67 | |
MuleEvent result; |
68 | 0 | if (!activePolicies.isEmpty()) |
69 | |
{ |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 0 | PolicyInvocation invocation = new PolicyInvocation(event, activePolicies, new MessageProcessor() |
75 | 0 | { |
76 | |
public MuleEvent process(MuleEvent event) throws MuleException |
77 | |
{ |
78 | 0 | return doProcess(event); |
79 | |
} |
80 | |
}); |
81 | 0 | final AroundPolicy entryPoint = activePolicies.get(0); |
82 | 0 | result = entryPoint.invoke(invocation); |
83 | 0 | } |
84 | |
else |
85 | |
{ |
86 | |
|
87 | 0 | result = doProcess(event); |
88 | |
} |
89 | 0 | return processNext(result); |
90 | |
} |
91 | |
|
92 | |
protected abstract MuleEvent doProcess(MuleEvent event) throws MuleException; |
93 | |
|
94 | |
public void initialise() throws InitialisationException |
95 | |
{ |
96 | 0 | for (MessageProcessor processor : processors) |
97 | |
{ |
98 | |
|
99 | 0 | if (processor instanceof Initialisable ) |
100 | |
{ |
101 | 0 | ((Initialisable) processor).initialise(); |
102 | |
} |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
public void start() throws MuleException |
107 | |
{ |
108 | 0 | for (MessageProcessor processor : processors) |
109 | |
{ |
110 | 0 | if (processor instanceof Startable) |
111 | |
{ |
112 | 0 | ((Startable) processor).start(); |
113 | |
} |
114 | |
} |
115 | 0 | } |
116 | |
|
117 | |
public void stop() throws MuleException |
118 | |
{ |
119 | 0 | for (MessageProcessor processor : processors) |
120 | |
{ |
121 | 0 | if (processor instanceof Stoppable) |
122 | |
{ |
123 | 0 | ((Stoppable) processor).stop(); |
124 | |
} |
125 | |
} |
126 | 0 | } |
127 | |
|
128 | |
public void dispose() |
129 | |
{ |
130 | 0 | for (MessageProcessor processor : processors) |
131 | |
{ |
132 | 0 | if (processor instanceof Disposable) |
133 | |
{ |
134 | 0 | ((Disposable) processor).dispose(); |
135 | |
} |
136 | |
} |
137 | 0 | processors.clear(); |
138 | 0 | } |
139 | |
|
140 | |
public void setFlowConstruct(FlowConstruct flowConstruct) |
141 | |
{ |
142 | 0 | for (MessageProcessor processor : processors) |
143 | |
{ |
144 | 0 | if (processor instanceof FlowConstructAware) |
145 | |
{ |
146 | 0 | ((FlowConstructAware) processor).setFlowConstruct(flowConstruct); |
147 | |
} |
148 | |
} |
149 | 0 | } |
150 | |
|
151 | |
public String getName() |
152 | |
{ |
153 | 0 | return name; |
154 | |
} |
155 | |
|
156 | |
@Override |
157 | |
public String toString() |
158 | |
{ |
159 | 0 | StringBuilder string = new StringBuilder(); |
160 | 0 | string.append(getClass().getSimpleName()); |
161 | 0 | if (StringUtils.isNotBlank(name)) |
162 | |
{ |
163 | 0 | string.append(String.format(" '%s' ", name)); |
164 | |
} |
165 | |
|
166 | 0 | Iterator<MessageProcessor> mpIterator = processors.iterator(); |
167 | |
|
168 | 0 | final String nl = String.format("%n"); |
169 | |
|
170 | 0 | for (AroundPolicy policy : policies.list()) |
171 | |
{ |
172 | 0 | string.append(String.format("%n -- policy [%s]: %s", policy.getName(), policy)); |
173 | |
} |
174 | |
|
175 | |
|
176 | 0 | if (mpIterator.hasNext()) |
177 | |
{ |
178 | 0 | string.append(String.format("%n[ ")); |
179 | 0 | while (mpIterator.hasNext()) |
180 | |
{ |
181 | 0 | MessageProcessor mp = mpIterator.next(); |
182 | 0 | final String indented = StringUtils.replace(mp.toString(), nl, String.format("%n ")); |
183 | 0 | string.append(String.format("%n %s", indented)); |
184 | 0 | if (mpIterator.hasNext()) |
185 | |
{ |
186 | 0 | string.append(", "); |
187 | |
} |
188 | 0 | } |
189 | 0 | string.append(String.format("%n]")); |
190 | |
} |
191 | |
|
192 | 0 | return string.toString(); |
193 | |
} |
194 | |
|
195 | |
public void setEndpoint(ImmutableEndpoint endpoint) |
196 | |
{ |
197 | 0 | for (MessageProcessor processor : processors) |
198 | |
{ |
199 | 0 | if (processor instanceof EndpointAware) |
200 | |
{ |
201 | 0 | ((EndpointAware) processor).setEndpoint(endpoint); |
202 | |
} |
203 | |
} |
204 | 0 | } |
205 | |
|
206 | |
public List<MessageProcessor> getMessageProcessors() |
207 | |
{ |
208 | 0 | return processors; |
209 | |
} |
210 | |
|
211 | |
public Policies getPolicies() |
212 | |
{ |
213 | 0 | return policies; |
214 | |
} |
215 | |
} |