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