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