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