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