1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.vm; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MessagingException; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.endpoint.EndpointException; |
17 | |
import org.mule.api.endpoint.EndpointURI; |
18 | |
import org.mule.api.endpoint.InboundEndpoint; |
19 | |
import org.mule.api.lifecycle.InitialisationException; |
20 | |
import org.mule.api.service.Service; |
21 | |
import org.mule.api.transaction.Transaction; |
22 | |
import org.mule.api.transaction.TransactionException; |
23 | |
import org.mule.api.transport.MessageAdapter; |
24 | |
import org.mule.api.transport.MessageReceiver; |
25 | |
import org.mule.config.QueueProfile; |
26 | |
import org.mule.config.i18n.CoreMessages; |
27 | |
import org.mule.endpoint.DynamicURIInboundEndpoint; |
28 | |
import org.mule.endpoint.MuleEndpointURI; |
29 | |
import org.mule.routing.filters.WildcardFilter; |
30 | |
import org.mule.transaction.TransactionCoordination; |
31 | |
import org.mule.transport.AbstractConnector; |
32 | |
import org.mule.util.queue.QueueManager; |
33 | |
import org.mule.util.queue.QueueSession; |
34 | |
|
35 | |
import java.util.Iterator; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 210 | public class VMConnector extends AbstractConnector |
43 | |
{ |
44 | |
|
45 | |
public static final String VM = "vm"; |
46 | 210 | private boolean queueEvents = false; |
47 | |
private QueueProfile queueProfile; |
48 | 210 | private int queueTimeout = 1000; |
49 | |
|
50 | |
private QueueManager queueManager; |
51 | |
|
52 | |
protected void doInitialise() throws InitialisationException |
53 | |
{ |
54 | 210 | if (queueEvents) |
55 | |
{ |
56 | 104 | if (queueManager == null) |
57 | |
{ |
58 | 104 | queueManager = getMuleContext().getQueueManager(); |
59 | |
} |
60 | 104 | if (queueProfile == null) |
61 | |
{ |
62 | |
|
63 | 84 | queueProfile = new QueueProfile(); |
64 | 84 | if(logger.isDebugEnabled()) |
65 | |
{ |
66 | 0 | logger.debug("created default QueueProfile for VM connector: " + queueProfile); |
67 | |
} |
68 | |
} |
69 | |
} |
70 | |
|
71 | 210 | } |
72 | |
|
73 | |
protected void doDispose() |
74 | |
{ |
75 | |
|
76 | 226 | } |
77 | |
|
78 | |
protected void doConnect() throws Exception |
79 | |
{ |
80 | |
|
81 | 209 | } |
82 | |
|
83 | |
protected void doDisconnect() throws Exception |
84 | |
{ |
85 | |
|
86 | 209 | } |
87 | |
|
88 | |
protected void doStart() throws MuleException |
89 | |
{ |
90 | |
|
91 | 208 | } |
92 | |
|
93 | |
protected void doStop() throws MuleException |
94 | |
{ |
95 | |
|
96 | 208 | } |
97 | |
|
98 | |
public MessageReceiver createReceiver(Service service, InboundEndpoint endpoint) throws Exception |
99 | |
{ |
100 | 346 | if (queueEvents) |
101 | |
{ |
102 | 70 | queueProfile.configureQueue(endpoint.getEndpointURI().getAddress(), queueManager); |
103 | |
} |
104 | 346 | return serviceDescriptor.createMessageReceiver(this, service, endpoint); |
105 | |
} |
106 | |
|
107 | |
public MessageAdapter getMessageAdapter(Object message) throws MessagingException |
108 | |
{ |
109 | 2 | if (message == null) |
110 | |
{ |
111 | 0 | throw new NullPointerException(CoreMessages.objectIsNull("message").getMessage()); |
112 | |
} |
113 | 2 | else if (message instanceof DefaultMuleMessage) |
114 | |
{ |
115 | 2 | return ((DefaultMuleMessage)message).getAdapter(); |
116 | |
} |
117 | 0 | else if (message instanceof MessageAdapter) |
118 | |
{ |
119 | 0 | return (MessageAdapter)message; |
120 | |
} |
121 | |
else |
122 | |
{ |
123 | 0 | throw new MessagingException(CoreMessages.objectNotOfCorrectType(message.getClass(), MessageAdapter.class), null); |
124 | |
} |
125 | |
} |
126 | |
|
127 | |
public String getProtocol() |
128 | |
{ |
129 | 12796 | return "VM"; |
130 | |
} |
131 | |
|
132 | |
public boolean isQueueEvents() |
133 | |
{ |
134 | 9319 | return queueEvents; |
135 | |
} |
136 | |
|
137 | |
public void setQueueEvents(boolean queueEvents) |
138 | |
{ |
139 | 124 | this.queueEvents = queueEvents; |
140 | 124 | } |
141 | |
|
142 | |
public QueueProfile getQueueProfile() |
143 | |
{ |
144 | 231 | return queueProfile; |
145 | |
} |
146 | |
|
147 | |
public void setQueueProfile(QueueProfile queueProfile) |
148 | |
{ |
149 | 20 | this.queueProfile = queueProfile; |
150 | 20 | } |
151 | |
|
152 | |
VMMessageReceiver getReceiver(EndpointURI endpointUri) throws EndpointException |
153 | |
{ |
154 | 8132 | return (VMMessageReceiver)getReceiverByEndpoint(endpointUri); |
155 | |
} |
156 | |
|
157 | |
QueueSession getQueueSession() throws InitialisationException |
158 | |
{ |
159 | 4347 | Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
160 | 4347 | if (tx != null) |
161 | |
{ |
162 | 5 | if (tx.hasResource(queueManager)) |
163 | |
{ |
164 | 4 | final QueueSession queueSession = (QueueSession) tx.getResource(queueManager); |
165 | 5 | if (logger.isDebugEnabled()) |
166 | |
{ |
167 | 0 | logger.debug("Retrieved VM queue session " + queueSession + " from current transaction " + tx); |
168 | |
} |
169 | 5 | return queueSession; |
170 | |
} |
171 | |
} |
172 | |
|
173 | 4342 | if (logger.isDebugEnabled()) |
174 | |
{ |
175 | 0 | logger.debug("Retrieving new VM queue session from queue manager"); |
176 | |
} |
177 | |
|
178 | 4342 | QueueSession session = queueManager.getQueueSession(); |
179 | 4342 | if (tx != null) |
180 | |
{ |
181 | 0 | if (logger.isDebugEnabled()) |
182 | |
{ |
183 | 0 | logger.debug("Binding VM queue session " + session + " to current transaction " + tx); |
184 | |
} |
185 | |
try |
186 | |
{ |
187 | 0 | tx.bindResource(queueManager, session); |
188 | |
} |
189 | 0 | catch (TransactionException e) |
190 | |
{ |
191 | 0 | throw new RuntimeException("Could not bind queue session to current transaction", e); |
192 | 0 | } |
193 | |
} |
194 | 4342 | return session; |
195 | |
} |
196 | |
|
197 | |
protected MessageReceiver getReceiverByEndpoint(EndpointURI endpointUri) throws EndpointException |
198 | |
{ |
199 | 8132 | if (logger.isDebugEnabled()) |
200 | |
{ |
201 | 0 | logger.debug("Looking up vm receiver for address: " + endpointUri.toString()); |
202 | |
} |
203 | |
|
204 | |
MessageReceiver receiver; |
205 | |
|
206 | 8132 | receiver = (MessageReceiver)receivers.get(endpointUri.getAddress()); |
207 | 8132 | if (receiver != null) |
208 | |
{ |
209 | 8106 | if (logger.isDebugEnabled()) |
210 | |
{ |
211 | 0 | logger.debug("Found exact receiver match on endpointUri: " + endpointUri); |
212 | |
} |
213 | 8106 | return receiver; |
214 | |
} |
215 | |
|
216 | |
|
217 | 26 | for (Iterator iterator = receivers.values().iterator(); iterator.hasNext();) |
218 | |
{ |
219 | 44 | receiver = (MessageReceiver)iterator.next(); |
220 | 44 | String filterAddress = receiver.getEndpointURI().getAddress(); |
221 | 44 | WildcardFilter filter = new WildcardFilter(filterAddress); |
222 | 44 | if (filter.accept(endpointUri.getAddress())) |
223 | |
{ |
224 | 0 | InboundEndpoint endpoint = receiver.getEndpoint(); |
225 | 0 | EndpointURI newEndpointURI = new MuleEndpointURI(endpointUri, filterAddress); |
226 | 0 | receiver.setEndpoint(new DynamicURIInboundEndpoint(endpoint, newEndpointURI)); |
227 | |
|
228 | 0 | if (logger.isDebugEnabled()) |
229 | |
{ |
230 | 0 | logger.debug("Found receiver match on endpointUri: " + receiver.getEndpointURI() |
231 | |
+ " against " + endpointUri); |
232 | |
} |
233 | 0 | return receiver; |
234 | |
} |
235 | 44 | } |
236 | 26 | if (logger.isDebugEnabled()) |
237 | |
{ |
238 | 0 | logger.debug("No receiver found for endpointUri: " + endpointUri); |
239 | |
} |
240 | 26 | return null; |
241 | |
} |
242 | |
|
243 | |
|
244 | |
public boolean isRemoteSyncEnabled() |
245 | |
{ |
246 | 0 | return true; |
247 | |
} |
248 | |
|
249 | |
public int getQueueTimeout() |
250 | |
{ |
251 | 45 | return queueTimeout; |
252 | |
} |
253 | |
|
254 | |
public void setQueueTimeout(int queueTimeout) |
255 | |
{ |
256 | 12 | this.queueTimeout = queueTimeout; |
257 | 12 | } |
258 | |
|
259 | |
public QueueManager getQueueManager() |
260 | |
{ |
261 | 225 | return queueManager; |
262 | |
} |
263 | |
|
264 | |
} |