1
2
3
4
5
6
7
8
9
10 package org.mule.providers;
11
12 import org.mule.impl.MuleMessage;
13 import org.mule.transaction.TransactionCallback;
14 import org.mule.transaction.TransactionCoordination;
15 import org.mule.transaction.TransactionTemplate;
16 import org.mule.umo.TransactionException;
17 import org.mule.umo.UMOMessage;
18 import org.mule.umo.UMOTransaction;
19 import org.mule.umo.endpoint.UMOImmutableEndpoint;
20 import org.mule.umo.provider.UMOMessageAdapter;
21
22 import java.io.OutputStream;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import javax.resource.spi.work.Work;
28
29
30
31
32 public abstract class AbstractReceiverWorker implements Work
33 {
34 protected List messages;
35 protected UMOImmutableEndpoint endpoint;
36 protected AbstractMessageReceiver receiver;
37 protected OutputStream out;
38
39 public AbstractReceiverWorker(List messages, AbstractMessageReceiver receiver)
40 {
41 this(messages, receiver, null);
42 }
43
44
45 public AbstractReceiverWorker(List messages, AbstractMessageReceiver receiver, OutputStream out)
46 {
47 this.messages = messages;
48 this.receiver = receiver;
49 this.endpoint = receiver.getEndpoint();
50 this.out = out;
51
52 }
53
54
55
56
57
58 public final void run()
59 {
60 doRun();
61 release();
62 }
63
64
65
66
67
68
69 protected void doRun()
70 {
71 TransactionTemplate tt = new TransactionTemplate(endpoint.getTransactionConfig(),
72 endpoint.getConnector().getExceptionListener());
73
74
75
76
77 TransactionCallback cb = new TransactionCallback()
78 {
79 public Object doInTransaction() throws Exception
80 {
81 UMOTransaction tx = TransactionCoordination.getInstance().getTransaction();
82 if(tx!=null)
83 {
84 bindTransaction(tx);
85 }
86 List results = new ArrayList(messages.size());
87
88 for (Iterator iterator = messages.iterator(); iterator.hasNext();)
89 {
90 Object o = iterator.next();
91
92 o = preProcessMessage(o);
93 if(o!=null)
94 {
95 UMOMessageAdapter adapter;
96 if(o instanceof UMOMessageAdapter)
97 {
98 adapter = (UMOMessageAdapter)o;
99 }
100 else
101 {
102 adapter = endpoint.getConnector().getMessageAdapter(o);
103 }
104
105 MuleMessage muleMessage = new MuleMessage(adapter);
106 preRouteMuleMessage(muleMessage);
107 UMOMessage result = receiver.routeMessage(muleMessage, tx, tx != null || endpoint.isSynchronous(), out);
108 if(result!=null)
109 {
110 o = postProcessMessage(result);
111 if(o!=null)
112 {
113 results.add(o);
114 }
115 }
116 }
117 }
118 return results;
119 }
120 };
121
122 try
123 {
124 List results = (List)tt.execute(cb);
125 handleResults(results);
126 }
127 catch (InterruptedException iex)
128 {
129
130 Thread.currentThread().interrupt();
131 }
132 catch (Exception e)
133 {
134 handleException(e);
135 }
136 finally
137 {
138 messages.clear();
139 }
140 }
141
142
143
144
145
146
147
148
149 protected void preRouteMuleMessage(MuleMessage message) throws Exception
150 {
151
152 }
153
154
155
156
157
158
159
160 protected abstract void bindTransaction(UMOTransaction tx) throws TransactionException;
161
162
163
164
165
166 protected void handleException(Exception e)
167 {
168 endpoint.getConnector().handleException(e);
169 }
170
171
172
173
174
175
176
177 protected void handleResults(List messages) throws Exception
178 {
179
180 }
181
182
183
184
185
186
187
188
189 protected Object preProcessMessage(Object message) throws Exception
190 {
191
192 return message;
193 }
194
195
196
197
198
199
200
201
202
203 protected UMOMessage postProcessMessage(UMOMessage message) throws Exception
204 {
205
206 return message;
207 }
208
209
210
211
212
213
214 public void release()
215 {
216
217 }
218 }