View Javadoc

1   /*
2    * $Id: Jms102bSupport.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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.Queue;
26  import javax.jms.QueueConnection;
27  import javax.jms.QueueConnectionFactory;
28  import javax.jms.QueueSender;
29  import javax.jms.QueueSession;
30  import javax.jms.Session;
31  import javax.jms.Topic;
32  import javax.jms.TopicConnection;
33  import javax.jms.TopicConnectionFactory;
34  import javax.jms.TopicPublisher;
35  import javax.jms.TopicSession;
36  
37  /**
38   * <code>Jms102bSupport</code> is a template class to provide an absstraction
39   * to to the Jms 1.0.2b api specification.
40   *
41   */
42  
43  public class Jms102bSupport extends Jms11Support
44  {
45      public Jms102bSupport(JmsConnector connector)
46      {
47          super(connector);
48      }
49  
50      @Override
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          if (connectionFactory instanceof QueueConnectionFactory)
59          {
60              return ((QueueConnectionFactory) connectionFactory).createQueueConnection(username, password);
61          }
62          else if (connectionFactory instanceof TopicConnectionFactory)
63          {
64              return ((TopicConnectionFactory) connectionFactory).createTopicConnection(username, password);
65          }
66          else
67          {
68              throw new IllegalArgumentException("Unsupported ConnectionFactory type: "
69                      + connectionFactory.getClass().getName());
70          }
71      }
72  
73      @Override
74      public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException
75      {
76          if (connectionFactory == null)
77          {
78              throw new IllegalArgumentException("connectionFactory cannot be null");
79          }
80          if (connectionFactory instanceof QueueConnectionFactory)
81          {
82              return ((QueueConnectionFactory) connectionFactory).createQueueConnection();
83          }
84          else if (connectionFactory instanceof TopicConnectionFactory)
85          {
86              return ((TopicConnectionFactory) connectionFactory).createTopicConnection();
87          }
88          else
89          {
90              throw new IllegalArgumentException("Unsupported ConnectionFactory type: "
91                      + connectionFactory.getClass().getName());
92          }
93      }
94  
95      @Override
96      public Session createSession(Connection connection, boolean topic, boolean transacted,
97          int ackMode, boolean noLocal) throws JMSException
98      {
99          if (connection == null)
100         {
101             throw new IllegalArgumentException("Connection is null");
102         }
103         if (topic && connection instanceof TopicConnection)
104         {
105             return ((TopicConnection) connection).createTopicSession(noLocal, ackMode);
106         }
107         else if (connection instanceof QueueConnection)
108         {
109             // for transacted sessions the ackMode is always ignored, but
110             // set it for readability (SESSION_TRANSACTION is recommented
111             // for this case).
112             return ((QueueConnection) connection).createQueueSession(
113                     transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode));
114         }
115         else
116         {
117             throw new IllegalArgumentException("Connection and domain type do not match, connection is of type " + connection.getClass().getName());
118         }
119     }
120 
121     @Override
122     public MessageConsumer createConsumer(Session session,
123                                           Destination destination,
124                                           String messageSelector,
125                                           boolean noLocal,
126                                           String durableName,
127                                           boolean topic, ImmutableEndpoint endpoint) throws JMSException
128     {
129         if (topic && session instanceof TopicSession)
130         {
131             if (durableName == null)
132             {
133                 return ((TopicSession) session).createSubscriber((Topic) destination, messageSelector, noLocal);
134             }
135             else
136             {
137                 // DO NOT REMOVE THE CAST, breaks Weblogic
138                 return ((TopicSession) session).createDurableSubscriber((Topic) destination, durableName, messageSelector, noLocal);
139             }
140         }
141         else if (session instanceof QueueSession)
142         {
143             if (messageSelector != null)
144             {
145                 return ((QueueSession) session).createReceiver((Queue) destination, messageSelector);
146             }
147             else
148             {
149                 return ((QueueSession) session).createReceiver((Queue) destination);
150             }
151         }
152         else
153         {
154             throw new IllegalArgumentException("Session and domain type do not match");
155         }
156     }
157 
158     @Override
159     public MessageProducer createProducer(Session session, Destination dest, boolean topic) throws JMSException
160     {
161         if (topic && session instanceof TopicSession)
162         {
163             return ((TopicSession) session).createPublisher((Topic) dest);
164         }
165         else if (session instanceof QueueSession)
166         {
167             return ((QueueSession) session).createSender((Queue) dest);
168         }
169         else
170         {
171             throw new IllegalArgumentException("Session and domain type do not match");
172         }
173     }
174 
175     @Override
176     public Destination createDestination(Session session, String name, boolean topic, ImmutableEndpoint endpoint) throws JMSException
177     {
178         if (connector.isJndiDestinations())
179         {
180             try
181             {
182                 Destination dest = getJndiDestination(name);
183                 if (dest != null)
184                 {
185                     if (logger.isDebugEnabled())
186                     {
187                         logger.debug(MessageFormat.format("Destination {0} located in JNDI, will use it now", name));
188                     }
189                     return dest;
190                 }
191                 else
192                 {
193                     throw new JMSException("JNDI destination not found with name: " + name);
194                 }
195             }
196             catch (JMSException e)
197             {
198                 if (connector.isForceJndiDestinations())
199                 {
200                     throw e;
201                 }
202                 else
203                 {
204                     logger.warn("Unable to look up JNDI destination " + name + ": " + e.getMessage());
205                 }
206             }
207         }
208 
209         if (logger.isDebugEnabled())
210         {
211             logger.debug("Using non-JNDI destination " + name + ", will create one now");
212         }
213 
214         if (session == null)
215         {
216             throw new IllegalArgumentException("Session cannot be null when creating a destination");
217         }
218         if (name == null)
219         {
220             throw new IllegalArgumentException("Destination name cannot be null when creating a destination");
221         }
222 
223         if (topic)
224         {
225             // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
226             return ((TopicSession) session).createTopic(name);
227         }
228         else
229         {
230             // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
231             return ((QueueSession) session).createQueue(name);
232         }
233     }
234 
235     @Override
236     public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException
237     {
238         if (session == null)
239         {
240             throw new IllegalArgumentException("Session cannot be null when creating a destination");
241         }
242 
243         if (topic)
244         {
245             // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
246             return ((TopicSession) session).createTemporaryTopic();
247         }
248         else
249         {
250             // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
251             return ((QueueSession) session).createTemporaryQueue();
252         }
253     }
254 
255     @Override
256     public void send(MessageProducer producer, Message message, boolean persistent, int priority, long ttl, boolean topic, ImmutableEndpoint endpoint)
257             throws JMSException
258     {
259         if (topic && producer instanceof TopicPublisher)
260         {
261             ((TopicPublisher) producer).publish(
262                     message,
263                     (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
264                     priority,
265                     ttl);
266         }
267         else if (producer instanceof QueueSender)
268         {
269             // DO NOT REMOVE THIS CAST, it breaks Weblogic
270             ((QueueSender) producer).send(
271                     message,
272                     (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
273                     priority,
274                     ttl);
275         }
276         else
277         {
278             throw new IllegalArgumentException("Producer and domain type do not match");
279         }
280     }
281 
282     @Override
283     public void send(MessageProducer producer,
284                      Message message,
285                      Destination dest,
286                      boolean persistent,
287                      int priority,
288                      long ttl,
289                      boolean topic, ImmutableEndpoint endpoint) throws JMSException
290     {
291         if (topic && producer instanceof TopicPublisher)
292         {
293             ((TopicPublisher) producer).publish(
294                     (Topic) dest,
295                     message,
296                     (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
297                     priority,
298                     ttl);
299         }
300         else if (producer instanceof QueueSender)
301         {
302             ((QueueSender) producer).send(
303                     (Queue) dest,
304                     message,
305                     (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
306                     priority,
307                     ttl);
308         }
309         else
310         {
311             throw new IllegalArgumentException("Producer and domain type do not match");
312         }
313     }
314 }