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 protected boolean jndiDestinations = false;
37 protected boolean forceJndiDestinations = false;
38 protected JmsConnector connector;
39
40 public Jms11Support(JmsConnector connector,
41 Context context,
42 boolean jndiDestinations,
43 boolean forceJndiDestinations)
44 {
45 this.connector = connector;
46 this.context = context;
47 this.jndiDestinations = jndiDestinations;
48 this.forceJndiDestinations = forceJndiDestinations;
49 }
50
51 public Connection createConnection(ConnectionFactory connectionFactory, String username, String password)
52 throws JMSException
53 {
54 if (connectionFactory == null)
55 {
56 throw new IllegalArgumentException("connectionFactory cannot be null");
57 }
58 return connectionFactory.createConnection(username, password);
59 }
60
61 public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException
62 {
63 if (connectionFactory == null)
64 {
65 throw new IllegalArgumentException("connectionFactory cannot be null");
66 }
67 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 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 return session.createProducer(destination);
83 }
84
85 public MessageConsumer createConsumer(Session session, Destination destination, boolean topic)
86 throws JMSException
87 {
88 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 if (durableName == null)
99 {
100 if (topic)
101 {
102 return session.createConsumer(destination, messageSelector, noLocal);
103 }
104 else
105 {
106 return session.createConsumer(destination, messageSelector);
107 }
108 }
109 else
110 {
111 if (topic)
112 {
113 return session.createDurableSubscriber((Topic) destination, durableName, messageSelector,
114 noLocal);
115 }
116 else
117 {
118 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 if (session == null)
127 {
128 throw new IllegalArgumentException("Session cannot be null when creating a destination");
129 }
130 if (name == null)
131 {
132 throw new IllegalArgumentException("Destination name cannot be null when creating a destination");
133 }
134
135 if (jndiDestinations)
136 {
137 if (context == null)
138 {
139 throw new IllegalArgumentException(
140 "Jndi Context name cannot be null when looking up a destination");
141 }
142 Destination dest = getJndiDestination(name);
143 if (dest != null)
144 {
145 return dest;
146 }
147 else if (forceJndiDestinations)
148 {
149 throw new JMSException("JNDI destination not found with name: " + name);
150 }
151 }
152
153 if (topic)
154 {
155 return session.createTopic(name);
156 }
157 else
158 {
159 return session.createQueue(name);
160 }
161 }
162
163 protected Destination getJndiDestination(String name) throws JMSException
164 {
165 Object temp;
166 try
167 {
168 temp = context.lookup(name);
169 }
170 catch (NamingException e)
171 {
172 throw new JMSException(MessageFormat.format("Failed to look up destination {0}. Reason: {1}",
173 new Object[] {name, e.getMessage()}));
174 }
175 if (temp != null)
176 {
177 if (temp instanceof Destination)
178 {
179 return (Destination) temp;
180 }
181 else if (forceJndiDestinations)
182 {
183 throw new JMSException("JNDI destination not found with name: " + name);
184 }
185 }
186 return null;
187 }
188
189 public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException
190 {
191 if (session == null)
192 {
193 throw new IllegalArgumentException("Session cannot be null when creating a destination");
194 }
195
196 if (topic)
197 {
198 return session.createTemporaryTopic();
199 }
200 else
201 {
202 return session.createTemporaryQueue();
203 }
204 }
205
206 public void send(MessageProducer producer, Message message, boolean topic) throws JMSException
207 {
208 send(producer, message, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
209 Message.DEFAULT_TIME_TO_LIVE, topic);
210 }
211
212 public void send(MessageProducer producer, Message message, Destination dest, boolean topic)
213 throws JMSException
214 {
215 send(producer, message, dest, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
216 Message.DEFAULT_TIME_TO_LIVE, topic);
217 }
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 producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
227 priority, ttl);
228 }
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 producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
239 priority, ttl);
240 }
241
242 }