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 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
40 | |
|
41 | |
protected JmsConnector connector; |
42 | |
|
43 | |
public Jms11Support(JmsConnector connector) |
44 | 0 | { |
45 | 0 | this.connector = connector; |
46 | 0 | } |
47 | |
|
48 | |
public Connection createConnection(ConnectionFactory connectionFactory, String username, String password) |
49 | |
throws JMSException |
50 | |
{ |
51 | 0 | if (connectionFactory == null) |
52 | |
{ |
53 | 0 | throw new IllegalArgumentException("connectionFactory cannot be null"); |
54 | |
} |
55 | 0 | return connectionFactory.createConnection(username, password); |
56 | |
} |
57 | |
|
58 | |
public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException |
59 | |
{ |
60 | 0 | if (connectionFactory == null) |
61 | |
{ |
62 | 0 | throw new IllegalArgumentException("connectionFactory cannot be null"); |
63 | |
} |
64 | 0 | 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 | 0 | 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 | 0 | return session.createProducer(destination); |
80 | |
} |
81 | |
|
82 | |
public MessageConsumer createConsumer(Session session, Destination destination, boolean topic, ImmutableEndpoint endpoint) |
83 | |
throws JMSException |
84 | |
{ |
85 | 0 | 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 | 0 | if (durableName == null) |
96 | |
{ |
97 | 0 | if (topic) |
98 | |
{ |
99 | 0 | return session.createConsumer(destination, messageSelector, noLocal); |
100 | |
} |
101 | |
else |
102 | |
{ |
103 | 0 | return session.createConsumer(destination, messageSelector); |
104 | |
} |
105 | |
} |
106 | |
else |
107 | |
{ |
108 | 0 | if (topic) |
109 | |
{ |
110 | 0 | return session.createDurableSubscriber((Topic) destination, durableName, messageSelector, |
111 | |
noLocal); |
112 | |
} |
113 | |
else |
114 | |
{ |
115 | 0 | 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 | 0 | String address = endpoint.getEndpointURI().toString(); |
124 | 0 | if (address.contains(JmsConstants.TOPIC_PROPERTY + ":")) |
125 | |
{ |
126 | |
|
127 | 0 | address = address.substring((connector.getProtocol() + "://" + JmsConstants.TOPIC_PROPERTY + ":").length()); |
128 | |
|
129 | 0 | if (address.contains("?")) |
130 | |
{ |
131 | 0 | address = address.substring(0, address.indexOf('?')); |
132 | |
} |
133 | |
} |
134 | |
else |
135 | |
{ |
136 | 0 | address = endpoint.getEndpointURI().getAddress(); |
137 | |
} |
138 | 0 | 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 | 0 | if (connector.isJndiDestinations()) |
144 | |
{ |
145 | |
try |
146 | |
{ |
147 | 0 | Destination dest = getJndiDestination(name); |
148 | 0 | if (dest != null) |
149 | |
{ |
150 | 0 | if (logger.isDebugEnabled()) |
151 | |
{ |
152 | 0 | logger.debug(MessageFormat.format("Destination {0} located in JNDI, will use it now", name)); |
153 | |
} |
154 | 0 | return dest; |
155 | |
} |
156 | |
else |
157 | |
{ |
158 | 0 | throw new JMSException("JNDI destination not found with name: " + name); |
159 | |
} |
160 | |
} |
161 | 0 | catch (JMSException e) |
162 | |
{ |
163 | 0 | if (connector.isForceJndiDestinations()) |
164 | |
{ |
165 | 0 | throw e; |
166 | |
} |
167 | |
else |
168 | |
{ |
169 | 0 | logger.warn("Unable to look up JNDI destination " + name + ": " + e.getMessage()); |
170 | |
} |
171 | |
} |
172 | |
} |
173 | |
|
174 | 0 | if (logger.isDebugEnabled()) |
175 | |
{ |
176 | 0 | logger.debug("Using non-JNDI destination " + name + ", will create one now"); |
177 | |
} |
178 | |
|
179 | 0 | if (session == null) |
180 | |
{ |
181 | 0 | throw new IllegalArgumentException("Session cannot be null when creating a destination"); |
182 | |
} |
183 | 0 | if (name == null) |
184 | |
{ |
185 | 0 | throw new IllegalArgumentException("Destination name cannot be null when creating a destination"); |
186 | |
} |
187 | |
|
188 | 0 | if (topic) |
189 | |
{ |
190 | 0 | return session.createTopic(name); |
191 | |
} |
192 | |
else |
193 | |
{ |
194 | 0 | return session.createQueue(name); |
195 | |
} |
196 | |
} |
197 | |
|
198 | |
protected Destination getJndiDestination(String name) throws JMSException |
199 | |
{ |
200 | |
Object temp; |
201 | |
try |
202 | |
{ |
203 | 0 | if (logger.isDebugEnabled()) |
204 | |
{ |
205 | 0 | logger.debug(MessageFormat.format("Looking up {0} from JNDI", name)); |
206 | |
} |
207 | 0 | temp = connector.lookupFromJndi(name); |
208 | |
} |
209 | 0 | catch (NamingException e) |
210 | |
{ |
211 | 0 | if (logger.isDebugEnabled()) |
212 | |
{ |
213 | 0 | logger.debug(e); |
214 | |
} |
215 | 0 | String message = MessageFormat.format("Failed to look up destination {0}. Reason: {1}", |
216 | |
name, e.getMessage()); |
217 | 0 | throw new JMSException(message); |
218 | 0 | } |
219 | |
|
220 | 0 | if (temp != null) |
221 | |
{ |
222 | 0 | if (temp instanceof Destination) |
223 | |
{ |
224 | 0 | return (Destination) temp; |
225 | |
} |
226 | |
} |
227 | 0 | return null; |
228 | |
} |
229 | |
|
230 | |
public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException |
231 | |
{ |
232 | 0 | if (session == null) |
233 | |
{ |
234 | 0 | throw new IllegalArgumentException("Session cannot be null when creating a destination"); |
235 | |
} |
236 | |
|
237 | 0 | if (topic) |
238 | |
{ |
239 | 0 | return session.createTemporaryTopic(); |
240 | |
} |
241 | |
else |
242 | |
{ |
243 | 0 | return session.createTemporaryQueue(); |
244 | |
} |
245 | |
} |
246 | |
|
247 | |
public void send(MessageProducer producer, Message message, boolean topic, ImmutableEndpoint endpoint) throws JMSException |
248 | |
{ |
249 | 0 | send(producer, message, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY, |
250 | |
Message.DEFAULT_TIME_TO_LIVE, topic, endpoint); |
251 | 0 | } |
252 | |
|
253 | |
public void send(MessageProducer producer, Message message, Destination dest, boolean topic, ImmutableEndpoint endpoint) |
254 | |
throws JMSException |
255 | |
{ |
256 | 0 | send(producer, message, dest, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY, |
257 | |
Message.DEFAULT_TIME_TO_LIVE, topic, endpoint); |
258 | 0 | } |
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 | 0 | producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), |
268 | |
priority, ttl); |
269 | 0 | } |
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 | 0 | producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), |
280 | |
priority, ttl); |
281 | 0 | } |
282 | |
|
283 | |
} |