View Javadoc

1   /*
2    * $Id: Jms102bSupport.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.jms;
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.Queue;
22  import javax.jms.QueueConnection;
23  import javax.jms.QueueConnectionFactory;
24  import javax.jms.QueueSender;
25  import javax.jms.QueueSession;
26  import javax.jms.Session;
27  import javax.jms.Topic;
28  import javax.jms.TopicConnection;
29  import javax.jms.TopicConnectionFactory;
30  import javax.jms.TopicPublisher;
31  import javax.jms.TopicSession;
32  import javax.naming.Context;
33  
34  /**
35   * <code>Jms102bSupport</code> is a template class to provide an absstraction
36   * to to the Jms 1.0.2b api specification.
37   *
38   */
39  
40  public class Jms102bSupport extends Jms11Support
41  {
42      public Jms102bSupport(JmsConnector connector,
43                            Context context,
44                            boolean jndiDestinations,
45                            boolean forceJndiDestinations)
46      {
47          super(connector, context, jndiDestinations, forceJndiDestinations);
48      }
49  
50      public Connection createConnection(ConnectionFactory connectionFactory, String username, String password)
51              throws JMSException
52      {
53          if (connectionFactory == null)
54          {
55              throw new IllegalArgumentException("connectionFactory cannot be null");
56          }
57          if (connectionFactory instanceof QueueConnectionFactory)
58          {
59              return ((QueueConnectionFactory) connectionFactory).createQueueConnection(username, password);
60          }
61          else if (connectionFactory instanceof TopicConnectionFactory)
62          {
63              return ((TopicConnectionFactory) connectionFactory).createTopicConnection(username, password);
64          }
65          else
66          {
67              throw new IllegalArgumentException("Unsupported ConnectionFactory type: "
68                      + connectionFactory.getClass().getName());
69          }
70      }
71  
72      public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException
73      {
74          if (connectionFactory == null)
75          {
76              throw new IllegalArgumentException("connectionFactory cannot be null");
77          }
78          if (connectionFactory instanceof QueueConnectionFactory)
79          {
80              return ((QueueConnectionFactory) connectionFactory).createQueueConnection();
81          }
82          else if (connectionFactory instanceof TopicConnectionFactory)
83          {
84              return ((TopicConnectionFactory) connectionFactory).createTopicConnection();
85          }
86          else
87          {
88              throw new IllegalArgumentException("Unsupported ConnectionFactory type: "
89                      + connectionFactory.getClass().getName());
90          }
91      }
92  
93      public Session createSession(Connection connection, boolean topic, boolean transacted, int ackMode, boolean noLocal)
94              throws JMSException
95      {
96          if (topic && connection instanceof TopicConnection)
97          {
98              return ((TopicConnection) connection).createTopicSession(noLocal, ackMode);
99          }
100         else if (connection instanceof QueueConnection)
101         {
102             // for transacted sessions the ackMode is always ignored, but
103             // set it for readability (SESSION_TRANSACTION is recommented
104             // for this case).
105             return ((QueueConnection) connection).createQueueSession(
106                     transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode));
107         }
108         else
109         {
110             throw new IllegalArgumentException("Connection and domain type do not match");
111         }
112     }
113 
114     public MessageConsumer createConsumer(Session session,
115                                           Destination destination,
116                                           String messageSelector,
117                                           boolean noLocal,
118                                           String durableName,
119                                           boolean topic) throws JMSException
120     {
121         if (topic && session instanceof TopicSession)
122         {
123             if (durableName == null)
124             {
125                 return ((TopicSession) session).createSubscriber((Topic) destination, messageSelector, noLocal);
126             }
127             else
128             {
129                 // DO NOT REMOVE THE CAST, breaks Weblogic
130                 return ((TopicSession) session).createDurableSubscriber((Topic) destination, durableName, messageSelector, noLocal);
131             }
132         }
133         else if (session instanceof QueueSession)
134         {
135             if (messageSelector != null)
136             {
137                 return ((QueueSession) session).createReceiver((Queue) destination, messageSelector);
138             }
139             else
140             {
141                 return ((QueueSession) session).createReceiver((Queue) destination);
142             }
143         }
144         else
145         {
146             throw new IllegalArgumentException("Session and domain type do not match");
147         }
148     }
149 
150     public MessageProducer createProducer(Session session, Destination dest, boolean topic) throws JMSException
151     {
152         if (topic && session instanceof TopicSession)
153         {
154             return ((TopicSession) session).createPublisher((Topic) dest);
155         }
156         else if (session instanceof QueueSession)
157         {
158             return ((QueueSession) session).createSender((Queue) dest);
159         }
160         else
161         {
162             throw new IllegalArgumentException("Session and domain type do not match");
163         }
164     }
165 
166     public Destination createDestination(Session session, String name, boolean topic) throws JMSException
167     {
168         if (session == null)
169         {
170             throw new IllegalArgumentException("Session cannot be null when creating a destination");
171         }
172         if (name == null)
173         {
174             throw new IllegalArgumentException("Destination name cannot be null when creating a destination");
175         }
176 
177         if (jndiDestinations)
178         {
179             if (context == null)
180             {
181                 throw new IllegalArgumentException("Jndi Context name cannot be null when looking up a destination");
182             }
183             Destination dest = getJndiDestination(name);
184             if (dest != null)
185             {
186                 return dest;
187             }
188             else if (forceJndiDestinations)
189             {
190                 throw new JMSException("JNDI destination not found with name: " + name);
191             }
192         }
193 
194         if (topic)
195         {
196             // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
197             return ((TopicSession) session).createTopic(name);
198         }
199         else
200         {
201             // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
202             return ((QueueSession) session).createQueue(name);
203         }
204     }
205 
206     public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException
207     {
208         if (session == null)
209         {
210             throw new IllegalArgumentException("Session cannot be null when creating a destination");
211         }
212 
213         if (topic)
214         {
215             // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
216             return ((TopicSession) session).createTemporaryTopic();
217         }
218         else
219         {
220             // DO NOT REMOVE THE CAST, BREAKS WEBLOGIC 8.X
221             return ((QueueSession) session).createTemporaryQueue();
222         }
223     }
224 
225     public void send(MessageProducer producer, Message message, boolean persistent, int priority, long ttl, boolean topic)
226             throws JMSException
227     {
228         if (topic && producer instanceof TopicPublisher)
229         {
230             ((TopicPublisher) producer).publish(
231                     message,
232                     (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
233                     priority,
234                     ttl);
235         }
236         else if (producer instanceof QueueSender)
237         {
238             // DO NOT REMOVE THIS CAST, it breaks Weblogic
239             ((QueueSender) producer).send(
240                     message,
241                     (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
242                     priority,
243                     ttl);
244         }
245         else
246         {
247             throw new IllegalArgumentException("Producer and domain type do not match");
248         }
249     }
250 
251     public void send(MessageProducer producer,
252                      Message message,
253                      Destination dest,
254                      boolean persistent,
255                      int priority,
256                      long ttl,
257                      boolean topic) throws JMSException
258     {
259         if (topic && producer instanceof TopicPublisher)
260         {
261             ((TopicPublisher) producer).publish(
262                     (Topic) dest,
263                     message,
264                     (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
265                     priority,
266                     ttl);
267         }
268         else if (producer instanceof QueueSender)
269         {
270             ((QueueSender) producer).send(
271                     (Queue) dest,
272                     message,
273                     (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
274                     priority,
275                     ttl);
276         }
277         else
278         {
279             throw new IllegalArgumentException("Producer and domain type do not match");
280         }
281     }
282 }