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