View Javadoc

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