1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.jms; |
12 | |
|
13 | |
import java.text.MessageFormat; |
14 | |
|
15 | |
import javax.jms.Connection; |
16 | |
import javax.jms.ConnectionFactory; |
17 | |
import javax.jms.DeliveryMode; |
18 | |
import javax.jms.Destination; |
19 | |
import javax.jms.JMSException; |
20 | |
import javax.jms.Message; |
21 | |
import javax.jms.MessageConsumer; |
22 | |
import javax.jms.MessageProducer; |
23 | |
import javax.jms.Session; |
24 | |
import javax.jms.Topic; |
25 | |
import javax.naming.Context; |
26 | |
import javax.naming.NamingException; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class Jms11Support implements JmsSupport |
34 | |
{ |
35 | |
protected Context context; |
36 | 0 | protected boolean jndiDestinations = false; |
37 | 0 | protected boolean forceJndiDestinations = false; |
38 | |
protected JmsConnector connector; |
39 | |
|
40 | |
public Jms11Support(JmsConnector connector, |
41 | |
Context context, |
42 | |
boolean jndiDestinations, |
43 | |
boolean forceJndiDestinations) |
44 | 0 | { |
45 | 0 | this.connector = connector; |
46 | 0 | this.context = context; |
47 | 0 | this.jndiDestinations = jndiDestinations; |
48 | 0 | this.forceJndiDestinations = forceJndiDestinations; |
49 | 0 | } |
50 | |
|
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 | return connectionFactory.createConnection(username, password); |
59 | |
} |
60 | |
|
61 | |
public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException |
62 | |
{ |
63 | 0 | if (connectionFactory == null) |
64 | |
{ |
65 | 0 | throw new IllegalArgumentException("connectionFactory cannot be null"); |
66 | |
} |
67 | 0 | return connectionFactory.createConnection(); |
68 | |
} |
69 | |
|
70 | |
public Session createSession(Connection connection, |
71 | |
boolean topic, |
72 | |
boolean transacted, |
73 | |
int ackMode, |
74 | |
boolean noLocal) throws JMSException |
75 | |
{ |
76 | 0 | return connection.createSession(transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode)); |
77 | |
} |
78 | |
|
79 | |
public MessageProducer createProducer(Session session, Destination destination, boolean topic) |
80 | |
throws JMSException |
81 | |
{ |
82 | 0 | return session.createProducer(destination); |
83 | |
} |
84 | |
|
85 | |
public MessageConsumer createConsumer(Session session, Destination destination, boolean topic) |
86 | |
throws JMSException |
87 | |
{ |
88 | 0 | return createConsumer(session, destination, null, false, null, topic); |
89 | |
} |
90 | |
|
91 | |
public MessageConsumer createConsumer(Session session, |
92 | |
Destination destination, |
93 | |
String messageSelector, |
94 | |
boolean noLocal, |
95 | |
String durableName, |
96 | |
boolean topic) throws JMSException |
97 | |
{ |
98 | 0 | if (durableName == null) |
99 | |
{ |
100 | 0 | if (topic) |
101 | |
{ |
102 | 0 | return session.createConsumer(destination, messageSelector, noLocal); |
103 | |
} |
104 | |
else |
105 | |
{ |
106 | 0 | return session.createConsumer(destination, messageSelector); |
107 | |
} |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | 0 | if (topic) |
112 | |
{ |
113 | 0 | return session.createDurableSubscriber((Topic) destination, durableName, messageSelector, |
114 | |
noLocal); |
115 | |
} |
116 | |
else |
117 | |
{ |
118 | 0 | throw new JMSException( |
119 | |
"A durable subscriber name was set but the destination was not a Topic"); |
120 | |
} |
121 | |
} |
122 | |
} |
123 | |
|
124 | |
public Destination createDestination(Session session, String name, boolean topic) throws JMSException |
125 | |
{ |
126 | 0 | if (session == null) |
127 | |
{ |
128 | 0 | throw new IllegalArgumentException("Session cannot be null when creating a destination"); |
129 | |
} |
130 | 0 | if (name == null) |
131 | |
{ |
132 | 0 | throw new IllegalArgumentException("Destination name cannot be null when creating a destination"); |
133 | |
} |
134 | |
|
135 | 0 | if (jndiDestinations) |
136 | |
{ |
137 | 0 | if (context == null) |
138 | |
{ |
139 | 0 | throw new IllegalArgumentException( |
140 | |
"Jndi Context name cannot be null when looking up a destination"); |
141 | |
} |
142 | 0 | Destination dest = getJndiDestination(name); |
143 | 0 | if (dest != null) |
144 | |
{ |
145 | 0 | return dest; |
146 | |
} |
147 | 0 | else if (forceJndiDestinations) |
148 | |
{ |
149 | 0 | throw new JMSException("JNDI destination not found with name: " + name); |
150 | |
} |
151 | |
} |
152 | |
|
153 | 0 | if (topic) |
154 | |
{ |
155 | 0 | return session.createTopic(name); |
156 | |
} |
157 | |
else |
158 | |
{ |
159 | 0 | return session.createQueue(name); |
160 | |
} |
161 | |
} |
162 | |
|
163 | |
protected Destination getJndiDestination(String name) throws JMSException |
164 | |
{ |
165 | |
Object temp; |
166 | |
try |
167 | |
{ |
168 | 0 | temp = context.lookup(name); |
169 | |
} |
170 | 0 | catch (NamingException e) |
171 | |
{ |
172 | 0 | throw new JMSException(MessageFormat.format("Failed to look up destination {0}. Reason: {1}", |
173 | |
new Object[] {name, e.getMessage()})); |
174 | 0 | } |
175 | 0 | if (temp != null) |
176 | |
{ |
177 | 0 | if (temp instanceof Destination) |
178 | |
{ |
179 | 0 | return (Destination) temp; |
180 | |
} |
181 | 0 | else if (forceJndiDestinations) |
182 | |
{ |
183 | 0 | throw new JMSException("JNDI destination not found with name: " + name); |
184 | |
} |
185 | |
} |
186 | 0 | return null; |
187 | |
} |
188 | |
|
189 | |
public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException |
190 | |
{ |
191 | 0 | if (session == null) |
192 | |
{ |
193 | 0 | throw new IllegalArgumentException("Session cannot be null when creating a destination"); |
194 | |
} |
195 | |
|
196 | 0 | if (topic) |
197 | |
{ |
198 | 0 | return session.createTemporaryTopic(); |
199 | |
} |
200 | |
else |
201 | |
{ |
202 | 0 | return session.createTemporaryQueue(); |
203 | |
} |
204 | |
} |
205 | |
|
206 | |
public void send(MessageProducer producer, Message message, boolean topic) throws JMSException |
207 | |
{ |
208 | 0 | send(producer, message, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY, |
209 | |
Message.DEFAULT_TIME_TO_LIVE, topic); |
210 | 0 | } |
211 | |
|
212 | |
public void send(MessageProducer producer, Message message, Destination dest, boolean topic) |
213 | |
throws JMSException |
214 | |
{ |
215 | 0 | send(producer, message, dest, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY, |
216 | |
Message.DEFAULT_TIME_TO_LIVE, topic); |
217 | 0 | } |
218 | |
|
219 | |
public void send(MessageProducer producer, |
220 | |
Message message, |
221 | |
boolean persistent, |
222 | |
int priority, |
223 | |
long ttl, |
224 | |
boolean topic) throws JMSException |
225 | |
{ |
226 | 0 | producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), |
227 | |
priority, ttl); |
228 | 0 | } |
229 | |
|
230 | |
public void send(MessageProducer producer, |
231 | |
Message message, |
232 | |
Destination dest, |
233 | |
boolean persistent, |
234 | |
int priority, |
235 | |
long ttl, |
236 | |
boolean topic) throws JMSException |
237 | |
{ |
238 | 0 | producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), |
239 | |
priority, ttl); |
240 | 0 | } |
241 | |
|
242 | |
} |