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.Session;
22 import javax.jms.Topic;
23 import javax.naming.NamingException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28
29
30
31
32
33 public class Jms11Support implements JmsSupport
34 {
35
36
37
38
39 protected final Log logger = LogFactory.getLog(getClass());
40
41 protected JmsConnector connector;
42
43 public Jms11Support(JmsConnector connector)
44 {
45 this.connector = connector;
46 }
47
48 public Connection createConnection(ConnectionFactory connectionFactory, String username, String password)
49 throws JMSException
50 {
51 if (connectionFactory == null)
52 {
53 throw new IllegalArgumentException("connectionFactory cannot be null");
54 }
55 return connectionFactory.createConnection(username, password);
56 }
57
58 public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException
59 {
60 if (connectionFactory == null)
61 {
62 throw new IllegalArgumentException("connectionFactory cannot be null");
63 }
64 return connectionFactory.createConnection();
65 }
66
67 public Session createSession(Connection connection,
68 boolean topic,
69 boolean transacted,
70 int ackMode,
71 boolean noLocal) throws JMSException
72 {
73 return connection.createSession(transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode));
74 }
75
76 public MessageProducer createProducer(Session session, Destination destination, boolean topic)
77 throws JMSException
78 {
79 return session.createProducer(destination);
80 }
81
82 public MessageConsumer createConsumer(Session session, Destination destination, boolean topic, ImmutableEndpoint endpoint)
83 throws JMSException
84 {
85 return createConsumer(session, destination, null, false, null, topic, endpoint);
86 }
87
88 public MessageConsumer createConsumer(Session session,
89 Destination destination,
90 String messageSelector,
91 boolean noLocal,
92 String durableName,
93 boolean topic, ImmutableEndpoint endpoint) throws JMSException
94 {
95 if (durableName == null)
96 {
97 if (topic)
98 {
99 return session.createConsumer(destination, messageSelector, noLocal);
100 }
101 else
102 {
103 return session.createConsumer(destination, messageSelector);
104 }
105 }
106 else
107 {
108 if (topic)
109 {
110 return session.createDurableSubscriber((Topic) destination, durableName, messageSelector,
111 noLocal);
112 }
113 else
114 {
115 throw new JMSException(
116 "A durable subscriber name was set but the destination was not a Topic");
117 }
118 }
119 }
120
121 public Destination createDestination(Session session, ImmutableEndpoint endpoint) throws JMSException
122 {
123 String address = endpoint.getEndpointURI().toString();
124 if (address.contains(JmsConstants.TOPIC_PROPERTY + ":"))
125 {
126
127 address = address.substring((connector.getProtocol() + "://" + JmsConstants.TOPIC_PROPERTY + ":").length());
128
129 if (address.contains("?"))
130 {
131 address = address.substring(0, address.indexOf('?'));
132 }
133 }
134 else
135 {
136 address = endpoint.getEndpointURI().getAddress();
137 }
138 return createDestination(session, address, connector.getTopicResolver().isTopic(endpoint), endpoint);
139 }
140
141 public Destination createDestination(Session session, String name, boolean topic, ImmutableEndpoint endpoint) throws JMSException
142 {
143 if (connector.isJndiDestinations())
144 {
145 try
146 {
147 Destination dest = getJndiDestination(name);
148 if (dest != null)
149 {
150 if (logger.isDebugEnabled())
151 {
152 logger.debug(MessageFormat.format("Destination {0} located in JNDI, will use it now", name));
153 }
154 return dest;
155 }
156 else
157 {
158 throw new JMSException("JNDI destination not found with name: " + name);
159 }
160 }
161 catch (JMSException e)
162 {
163 if (connector.isForceJndiDestinations())
164 {
165 throw e;
166 }
167 else
168 {
169 logger.warn("Unable to look up JNDI destination " + name + ": " + e.getMessage());
170 }
171 }
172 }
173
174 if (logger.isDebugEnabled())
175 {
176 logger.debug("Using non-JNDI destination " + name + ", will create one now");
177 }
178
179 if (session == null)
180 {
181 throw new IllegalArgumentException("Session cannot be null when creating a destination");
182 }
183 if (name == null)
184 {
185 throw new IllegalArgumentException("Destination name cannot be null when creating a destination");
186 }
187
188 if (topic)
189 {
190 return session.createTopic(name);
191 }
192 else
193 {
194 return session.createQueue(name);
195 }
196 }
197
198 protected Destination getJndiDestination(String name) throws JMSException
199 {
200 Object temp;
201 try
202 {
203 if (logger.isDebugEnabled())
204 {
205 logger.debug(MessageFormat.format("Looking up {0} from JNDI", name));
206 }
207 temp = connector.lookupFromJndi(name);
208 }
209 catch (NamingException e)
210 {
211 if (logger.isDebugEnabled())
212 {
213 logger.debug(e);
214 }
215 String message = MessageFormat.format("Failed to look up destination {0}. Reason: {1}",
216 name, e.getMessage());
217 throw new JMSException(message);
218 }
219
220 if (temp != null)
221 {
222 if (temp instanceof Destination)
223 {
224 return (Destination) temp;
225 }
226 }
227 return null;
228 }
229
230 public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException
231 {
232 if (session == null)
233 {
234 throw new IllegalArgumentException("Session cannot be null when creating a destination");
235 }
236
237 if (topic)
238 {
239 return session.createTemporaryTopic();
240 }
241 else
242 {
243 return session.createTemporaryQueue();
244 }
245 }
246
247 public void send(MessageProducer producer, Message message, boolean topic, ImmutableEndpoint endpoint) throws JMSException
248 {
249 send(producer, message, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
250 Message.DEFAULT_TIME_TO_LIVE, topic, endpoint);
251 }
252
253 public void send(MessageProducer producer, Message message, Destination dest, boolean topic, ImmutableEndpoint endpoint)
254 throws JMSException
255 {
256 send(producer, message, dest, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
257 Message.DEFAULT_TIME_TO_LIVE, topic, endpoint);
258 }
259
260 public void send(MessageProducer producer,
261 Message message,
262 boolean persistent,
263 int priority,
264 long ttl,
265 boolean topic, ImmutableEndpoint endpoint) throws JMSException
266 {
267 producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
268 priority, ttl);
269 }
270
271 public void send(MessageProducer producer,
272 Message message,
273 Destination dest,
274 boolean persistent,
275 int priority,
276 long ttl,
277 boolean topic, ImmutableEndpoint endpoint) throws JMSException
278 {
279 producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
280 priority, ttl);
281 }
282
283 }