1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.vm; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.i18n.CoreMessages; |
15 | |
import org.mule.impl.MuleMessage; |
16 | |
import org.mule.providers.AbstractMessageDispatcher; |
17 | |
import org.mule.providers.vm.i18n.VMMessages; |
18 | |
import org.mule.transformers.simple.ObjectToByteArray; |
19 | |
import org.mule.umo.UMOEvent; |
20 | |
import org.mule.umo.UMOMessage; |
21 | |
import org.mule.umo.endpoint.UMOEndpointURI; |
22 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
23 | |
import org.mule.umo.provider.DispatchException; |
24 | |
import org.mule.umo.provider.NoReceiverForEndpointException; |
25 | |
import org.mule.umo.provider.UMOStreamMessageAdapter; |
26 | |
import org.mule.util.queue.Queue; |
27 | |
import org.mule.util.queue.QueueSession; |
28 | |
|
29 | |
import java.io.ByteArrayInputStream; |
30 | |
import java.io.InputStream; |
31 | |
import java.io.PipedInputStream; |
32 | |
import java.io.PipedOutputStream; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class VMMessageDispatcher extends AbstractMessageDispatcher |
39 | |
{ |
40 | |
private final VMConnector connector; |
41 | |
private final ObjectToByteArray objectToByteArray; |
42 | |
|
43 | |
public VMMessageDispatcher(UMOImmutableEndpoint endpoint) |
44 | |
{ |
45 | 0 | super(endpoint); |
46 | 0 | this.connector = (VMConnector)endpoint.getConnector(); |
47 | 0 | objectToByteArray = new ObjectToByteArray(); |
48 | 0 | } |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
protected UMOMessage doReceive(long timeout) throws Exception |
62 | |
{ |
63 | 0 | if (!connector.isQueueEvents()) |
64 | |
{ |
65 | 0 | throw new UnsupportedOperationException("Receive requested on VM Connector, but queueEvents is false"); |
66 | |
} |
67 | |
|
68 | |
try |
69 | |
{ |
70 | 0 | QueueSession queueSession = connector.getQueueSession(); |
71 | 0 | Queue queue = queueSession.getQueue(endpoint.getEndpointURI().getAddress()); |
72 | |
|
73 | 0 | if (queue == null) |
74 | |
{ |
75 | 0 | if (logger.isDebugEnabled()) |
76 | |
{ |
77 | 0 | logger.debug("No queue with name " + endpoint.getEndpointURI().getAddress()); |
78 | |
} |
79 | 0 | return null; |
80 | |
} |
81 | |
else |
82 | |
{ |
83 | 0 | UMOEvent event = null; |
84 | 0 | if (logger.isDebugEnabled()) |
85 | |
{ |
86 | 0 | logger.debug("Waiting for a message on " + endpoint.getEndpointURI().getAddress()); |
87 | |
} |
88 | |
try |
89 | |
{ |
90 | 0 | event = (UMOEvent)queue.poll(timeout); |
91 | |
} |
92 | 0 | catch (InterruptedException e) |
93 | |
{ |
94 | 0 | logger.error("Failed to receive event from queue: " + endpoint.getEndpointURI()); |
95 | 0 | } |
96 | 0 | if (event != null) |
97 | |
{ |
98 | 0 | if (logger.isDebugEnabled()) |
99 | |
{ |
100 | 0 | logger.debug("Event received: " + event); |
101 | |
} |
102 | 0 | return event.getMessage(); |
103 | |
} |
104 | |
else |
105 | |
{ |
106 | 0 | if (logger.isDebugEnabled()) |
107 | |
{ |
108 | 0 | logger.debug("No event received after " + timeout + " ms"); |
109 | |
} |
110 | 0 | return null; |
111 | |
} |
112 | |
} |
113 | |
} |
114 | 0 | catch (Exception e) |
115 | |
{ |
116 | 0 | throw e; |
117 | |
} |
118 | |
} |
119 | |
|
120 | |
protected void doDispatch(UMOEvent event) throws Exception |
121 | |
{ |
122 | 0 | UMOEndpointURI endpointUri = event.getEndpoint().getEndpointURI(); |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | 0 | if (endpointUri == null) |
131 | |
{ |
132 | 0 | throw new DispatchException( |
133 | |
CoreMessages.objectIsNull("Endpoint"), event.getMessage(), event.getEndpoint()); |
134 | |
} |
135 | 0 | if (connector.isQueueEvents()) |
136 | |
{ |
137 | 0 | QueueSession session = connector.getQueueSession(); |
138 | 0 | Queue queue = session.getQueue(endpointUri.getAddress()); |
139 | 0 | queue.put(event); |
140 | |
} |
141 | |
else |
142 | |
{ |
143 | 0 | VMMessageReceiver receiver = connector.getReceiver(event.getEndpoint().getEndpointURI()); |
144 | 0 | if (receiver == null) |
145 | |
{ |
146 | 0 | logger.warn("No receiver for endpointUri: " + event.getEndpoint().getEndpointURI()); |
147 | 0 | return; |
148 | |
} |
149 | |
|
150 | 0 | if (event.isStreaming()) |
151 | |
{ |
152 | 0 | PipedInputStream in = new PipedInputStream(); |
153 | 0 | PipedOutputStream out = new PipedOutputStream(in); |
154 | 0 | UMOStreamMessageAdapter sma = connector.getStreamMessageAdapter(in, out); |
155 | 0 | sma.write(event); |
156 | |
} |
157 | 0 | receiver.onEvent(event); |
158 | |
} |
159 | 0 | if (logger.isDebugEnabled()) |
160 | |
{ |
161 | 0 | logger.debug("dispatched Event on endpointUri: " + endpointUri); |
162 | |
} |
163 | 0 | } |
164 | |
|
165 | |
protected UMOMessage doSend(UMOEvent event) throws Exception |
166 | |
{ |
167 | |
UMOMessage retMessage; |
168 | 0 | UMOEndpointURI endpointUri = event.getEndpoint().getEndpointURI(); |
169 | 0 | VMMessageReceiver receiver = connector.getReceiver(endpointUri); |
170 | 0 | if (receiver == null) |
171 | |
{ |
172 | 0 | if (connector.isQueueEvents()) |
173 | |
{ |
174 | 0 | if (logger.isDebugEnabled()) |
175 | |
{ |
176 | 0 | logger.debug("Writing to queue as there is no receiver on connector: " |
177 | |
+ connector.getName() + ", for endpointUri: " |
178 | |
+ event.getEndpoint().getEndpointURI()); |
179 | |
} |
180 | 0 | doDispatch(event); |
181 | 0 | return null; |
182 | |
} |
183 | |
else |
184 | |
{ |
185 | 0 | throw new NoReceiverForEndpointException( |
186 | |
VMMessages.noReceiverForEndpoint(connector.getName(), |
187 | |
event.getEndpoint().getEndpointURI())); |
188 | |
} |
189 | |
} |
190 | 0 | if (event.isStreaming()) |
191 | |
{ |
192 | 0 | PipedInputStream in = new PipedInputStream(); |
193 | 0 | PipedOutputStream out = new PipedOutputStream(in); |
194 | 0 | UMOStreamMessageAdapter sma = connector.getStreamMessageAdapter(in, out); |
195 | 0 | sma.write(event); |
196 | |
} |
197 | |
|
198 | 0 | retMessage = (UMOMessage)receiver.onCall(event); |
199 | |
|
200 | 0 | if (event.isStreaming() && retMessage != null) |
201 | |
{ |
202 | |
InputStream in; |
203 | 0 | if (retMessage.getPayload() instanceof InputStream) |
204 | |
{ |
205 | 0 | in = (InputStream)retMessage.getPayload(); |
206 | |
} |
207 | |
else |
208 | |
{ |
209 | 0 | in = new ByteArrayInputStream((byte[])objectToByteArray.transform(retMessage.getPayload())); |
210 | |
} |
211 | 0 | UMOStreamMessageAdapter sma = connector.getStreamMessageAdapter(in, null); |
212 | 0 | retMessage = new MuleMessage(sma, retMessage); |
213 | |
} |
214 | |
|
215 | 0 | if (logger.isDebugEnabled()) |
216 | |
{ |
217 | 0 | logger.debug("sent event on endpointUri: " + event.getEndpoint().getEndpointURI()); |
218 | |
} |
219 | |
|
220 | 0 | return retMessage; |
221 | |
} |
222 | |
|
223 | |
protected void doDispose() |
224 | |
{ |
225 | |
|
226 | 0 | } |
227 | |
|
228 | |
protected void doConnect() throws Exception |
229 | |
{ |
230 | 0 | if (connector.isQueueEvents()) |
231 | |
{ |
232 | |
|
233 | |
|
234 | 0 | MuleManager.getConfiguration().getQueueProfile().configureQueue( |
235 | |
endpoint.getEndpointURI().getAddress()); |
236 | |
} |
237 | 0 | } |
238 | |
|
239 | |
protected void doDisconnect() throws Exception |
240 | |
{ |
241 | |
|
242 | 0 | } |
243 | |
|
244 | |
} |