1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.jms; |
12 | |
|
13 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
14 | |
|
15 | |
import java.text.MessageFormat; |
16 | |
|
17 | |
import javax.jms.Connection; |
18 | |
import javax.jms.ConnectionFactory; |
19 | |
import javax.jms.DeliveryMode; |
20 | |
import javax.jms.Destination; |
21 | |
import javax.jms.JMSException; |
22 | |
import javax.jms.Message; |
23 | |
import javax.jms.MessageConsumer; |
24 | |
import javax.jms.MessageProducer; |
25 | |
import javax.jms.Queue; |
26 | |
import javax.jms.QueueConnection; |
27 | |
import javax.jms.QueueConnectionFactory; |
28 | |
import javax.jms.QueueSender; |
29 | |
import javax.jms.QueueSession; |
30 | |
import javax.jms.Session; |
31 | |
import javax.jms.Topic; |
32 | |
import javax.jms.TopicConnection; |
33 | |
import javax.jms.TopicConnectionFactory; |
34 | |
import javax.jms.TopicPublisher; |
35 | |
import javax.jms.TopicSession; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class Jms102bSupport extends Jms11Support |
44 | |
{ |
45 | |
public Jms102bSupport(JmsConnector connector) |
46 | |
{ |
47 | 0 | super(connector); |
48 | 0 | } |
49 | |
|
50 | |
@Override |
51 | |
public Connection createConnection(ConnectionFactory connectionFactory, String username, String password) |
52 | |
throws JMSException |
53 | |
{ |
54 | 0 | if (connectionFactory == null) |
55 | |
{ |
56 | 0 | throw new IllegalArgumentException("connectionFactory cannot be null"); |
57 | |
} |
58 | 0 | if (connectionFactory instanceof QueueConnectionFactory) |
59 | |
{ |
60 | 0 | return ((QueueConnectionFactory) connectionFactory).createQueueConnection(username, password); |
61 | |
} |
62 | 0 | else if (connectionFactory instanceof TopicConnectionFactory) |
63 | |
{ |
64 | 0 | return ((TopicConnectionFactory) connectionFactory).createTopicConnection(username, password); |
65 | |
} |
66 | |
else |
67 | |
{ |
68 | 0 | throw new IllegalArgumentException("Unsupported ConnectionFactory type: " |
69 | |
+ connectionFactory.getClass().getName()); |
70 | |
} |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException |
75 | |
{ |
76 | 0 | if (connectionFactory == null) |
77 | |
{ |
78 | 0 | throw new IllegalArgumentException("connectionFactory cannot be null"); |
79 | |
} |
80 | 0 | if (connectionFactory instanceof QueueConnectionFactory) |
81 | |
{ |
82 | 0 | return ((QueueConnectionFactory) connectionFactory).createQueueConnection(); |
83 | |
} |
84 | 0 | else if (connectionFactory instanceof TopicConnectionFactory) |
85 | |
{ |
86 | 0 | return ((TopicConnectionFactory) connectionFactory).createTopicConnection(); |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | throw new IllegalArgumentException("Unsupported ConnectionFactory type: " |
91 | |
+ connectionFactory.getClass().getName()); |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
@Override |
96 | |
public Session createSession(Connection connection, boolean topic, boolean transacted, |
97 | |
int ackMode, boolean noLocal) throws JMSException |
98 | |
{ |
99 | 0 | if (connection == null) |
100 | |
{ |
101 | 0 | throw new IllegalArgumentException("Connection is null"); |
102 | |
} |
103 | 0 | if (topic && connection instanceof TopicConnection) |
104 | |
{ |
105 | 0 | return ((TopicConnection) connection).createTopicSession(noLocal, ackMode); |
106 | |
} |
107 | 0 | else if (connection instanceof QueueConnection) |
108 | |
{ |
109 | |
|
110 | |
|
111 | |
|
112 | 0 | return ((QueueConnection) connection).createQueueSession( |
113 | |
transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode)); |
114 | |
} |
115 | |
else |
116 | |
{ |
117 | 0 | throw new IllegalArgumentException("Connection and domain type do not match, connection is of type " + connection.getClass().getName()); |
118 | |
} |
119 | |
} |
120 | |
|
121 | |
@Override |
122 | |
public MessageConsumer createConsumer(Session session, |
123 | |
Destination destination, |
124 | |
String messageSelector, |
125 | |
boolean noLocal, |
126 | |
String durableName, |
127 | |
boolean topic, ImmutableEndpoint endpoint) throws JMSException |
128 | |
{ |
129 | 0 | if (topic && session instanceof TopicSession) |
130 | |
{ |
131 | 0 | if (durableName == null) |
132 | |
{ |
133 | 0 | return ((TopicSession) session).createSubscriber((Topic) destination, messageSelector, noLocal); |
134 | |
} |
135 | |
else |
136 | |
{ |
137 | |
|
138 | 0 | return ((TopicSession) session).createDurableSubscriber((Topic) destination, durableName, messageSelector, noLocal); |
139 | |
} |
140 | |
} |
141 | 0 | else if (session instanceof QueueSession) |
142 | |
{ |
143 | 0 | if (messageSelector != null) |
144 | |
{ |
145 | 0 | return ((QueueSession) session).createReceiver((Queue) destination, messageSelector); |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | 0 | return ((QueueSession) session).createReceiver((Queue) destination); |
150 | |
} |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | 0 | throw new IllegalArgumentException("Session and domain type do not match"); |
155 | |
} |
156 | |
} |
157 | |
|
158 | |
@Override |
159 | |
public MessageProducer createProducer(Session session, Destination dest, boolean topic) throws JMSException |
160 | |
{ |
161 | 0 | if (topic && session instanceof TopicSession) |
162 | |
{ |
163 | 0 | return ((TopicSession) session).createPublisher((Topic) dest); |
164 | |
} |
165 | 0 | else if (session instanceof QueueSession) |
166 | |
{ |
167 | 0 | return ((QueueSession) session).createSender((Queue) dest); |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | 0 | throw new IllegalArgumentException("Session and domain type do not match"); |
172 | |
} |
173 | |
} |
174 | |
|
175 | |
@Override |
176 | |
public Destination createDestination(Session session, String name, boolean topic, ImmutableEndpoint endpoint) throws JMSException |
177 | |
{ |
178 | 0 | if (connector.isJndiDestinations()) |
179 | |
{ |
180 | |
try |
181 | |
{ |
182 | 0 | Destination dest = getJndiDestination(name); |
183 | 0 | if (dest != null) |
184 | |
{ |
185 | 0 | if (logger.isDebugEnabled()) |
186 | |
{ |
187 | 0 | logger.debug(MessageFormat.format("Destination {0} located in JNDI, will use it now", name)); |
188 | |
} |
189 | 0 | return dest; |
190 | |
} |
191 | |
else |
192 | |
{ |
193 | 0 | throw new JMSException("JNDI destination not found with name: " + name); |
194 | |
} |
195 | |
} |
196 | 0 | catch (JMSException e) |
197 | |
{ |
198 | 0 | if (connector.isForceJndiDestinations()) |
199 | |
{ |
200 | 0 | throw e; |
201 | |
} |
202 | |
else |
203 | |
{ |
204 | 0 | logger.warn("Unable to look up JNDI destination " + name + ": " + e.getMessage()); |
205 | |
} |
206 | |
} |
207 | |
} |
208 | |
|
209 | 0 | if (logger.isDebugEnabled()) |
210 | |
{ |
211 | 0 | logger.debug("Using non-JNDI destination " + name + ", will create one now"); |
212 | |
} |
213 | |
|
214 | 0 | if (session == null) |
215 | |
{ |
216 | 0 | throw new IllegalArgumentException("Session cannot be null when creating a destination"); |
217 | |
} |
218 | 0 | if (name == null) |
219 | |
{ |
220 | 0 | throw new IllegalArgumentException("Destination name cannot be null when creating a destination"); |
221 | |
} |
222 | |
|
223 | 0 | if (topic) |
224 | |
{ |
225 | |
|
226 | 0 | return ((TopicSession) session).createTopic(name); |
227 | |
} |
228 | |
else |
229 | |
{ |
230 | |
|
231 | 0 | return ((QueueSession) session).createQueue(name); |
232 | |
} |
233 | |
} |
234 | |
|
235 | |
@Override |
236 | |
public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException |
237 | |
{ |
238 | 0 | if (session == null) |
239 | |
{ |
240 | 0 | throw new IllegalArgumentException("Session cannot be null when creating a destination"); |
241 | |
} |
242 | |
|
243 | 0 | if (topic) |
244 | |
{ |
245 | |
|
246 | 0 | return ((TopicSession) session).createTemporaryTopic(); |
247 | |
} |
248 | |
else |
249 | |
{ |
250 | |
|
251 | 0 | return ((QueueSession) session).createTemporaryQueue(); |
252 | |
} |
253 | |
} |
254 | |
|
255 | |
@Override |
256 | |
public void send(MessageProducer producer, Message message, boolean persistent, int priority, long ttl, boolean topic, ImmutableEndpoint endpoint) |
257 | |
throws JMSException |
258 | |
{ |
259 | 0 | if (topic && producer instanceof TopicPublisher) |
260 | |
{ |
261 | 0 | ((TopicPublisher) producer).publish( |
262 | |
message, |
263 | |
(persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), |
264 | |
priority, |
265 | |
ttl); |
266 | |
} |
267 | 0 | else if (producer instanceof QueueSender) |
268 | |
{ |
269 | |
|
270 | 0 | ((QueueSender) producer).send( |
271 | |
message, |
272 | |
(persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), |
273 | |
priority, |
274 | |
ttl); |
275 | |
} |
276 | |
else |
277 | |
{ |
278 | 0 | throw new IllegalArgumentException("Producer and domain type do not match"); |
279 | |
} |
280 | 0 | } |
281 | |
|
282 | |
@Override |
283 | |
public void send(MessageProducer producer, |
284 | |
Message message, |
285 | |
Destination dest, |
286 | |
boolean persistent, |
287 | |
int priority, |
288 | |
long ttl, |
289 | |
boolean topic, ImmutableEndpoint endpoint) throws JMSException |
290 | |
{ |
291 | 0 | if (topic && producer instanceof TopicPublisher) |
292 | |
{ |
293 | 0 | ((TopicPublisher) producer).publish( |
294 | |
(Topic) dest, |
295 | |
message, |
296 | |
(persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), |
297 | |
priority, |
298 | |
ttl); |
299 | |
} |
300 | 0 | else if (producer instanceof QueueSender) |
301 | |
{ |
302 | 0 | ((QueueSender) producer).send( |
303 | |
(Queue) dest, |
304 | |
message, |
305 | |
(persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), |
306 | |
priority, |
307 | |
ttl); |
308 | |
} |
309 | |
else |
310 | |
{ |
311 | 0 | throw new IllegalArgumentException("Producer and domain type do not match"); |
312 | |
} |
313 | 0 | } |
314 | |
} |