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