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