View Javadoc

1   /*
2    * $Id: Jms11Support.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.Session;
22  import javax.jms.Topic;
23  
24  /**
25   * <code>Jms11Support</code> is a template class to provide an abstraction to to
26   * the JMS 1.1 API specification.
27   */
28  
29  public class Jms11Support implements JmsSupport
30  {
31      protected JmsConnector connector;
32  
33      public Jms11Support(JmsConnector connector)
34      {
35          this.connector = connector;
36      }
37  
38      public Connection createConnection(ConnectionFactory connectionFactory, String username, String password)
39          throws JMSException
40      {
41          if (connectionFactory == null)
42          {
43              throw new IllegalArgumentException("connectionFactory cannot be null");
44          }
45          return connectionFactory.createConnection(username, password);
46      }
47  
48      public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException
49      {
50          if (connectionFactory == null)
51          {
52              throw new IllegalArgumentException("connectionFactory cannot be null");
53          }
54          return connectionFactory.createConnection();
55      }
56  
57      public Session createSession(Connection connection,
58                                   boolean topic,
59                                   boolean transacted,
60                                   int ackMode,
61                                   boolean noLocal) throws JMSException
62      {
63          return connection.createSession(transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode));
64      }
65  
66      public MessageProducer createProducer(Session session, Destination destination, boolean topic)
67          throws JMSException
68      {
69          return session.createProducer(destination);
70      }
71  
72      public MessageConsumer createConsumer(Session session, Destination destination, boolean topic)
73          throws JMSException
74      {
75          return createConsumer(session, destination, null, false, null, topic);
76      }
77  
78      public MessageConsumer createConsumer(Session session,
79                                            Destination destination,
80                                            String messageSelector,
81                                            boolean noLocal,
82                                            String durableName,
83                                            boolean topic) throws JMSException
84      {
85          if (durableName == null)
86          {
87              if (topic)
88              {
89                  return session.createConsumer(destination, messageSelector, noLocal);
90              }
91              else
92              {
93                  return session.createConsumer(destination, messageSelector);
94              }
95          }
96          else
97          {
98              if (topic)
99              {
100                 return session.createDurableSubscriber((Topic) destination, durableName, messageSelector,
101                     noLocal);
102             }
103             else
104             {
105                 throw new JMSException(
106                     "A durable subscriber name was set but the destination was not a Topic");
107             }
108         }
109     }
110 
111     public Destination createDestination(Session session, String name, boolean topic) throws JMSException
112     {
113         if (session == null)
114         {
115             throw new IllegalArgumentException("MuleSession cannot be null when creating a destination");
116         }
117         if (name == null)
118         {
119             throw new IllegalArgumentException("Destination name cannot be null when creating a destination");
120         }
121 
122         if (topic)
123         {
124             return session.createTopic(name);
125         }
126         else
127         {
128             return session.createQueue(name);
129         }
130     }
131 
132     public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException
133     {
134         if (session == null)
135         {
136             throw new IllegalArgumentException("MuleSession cannot be null when creating a destination");
137         }
138 
139         if (topic)
140         {
141             return session.createTemporaryTopic();
142         }
143         else
144         {
145             return session.createTemporaryQueue();
146         }
147     }
148 
149     public void send(MessageProducer producer, Message message, boolean topic) throws JMSException
150     {
151         send(producer, message, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
152             Message.DEFAULT_TIME_TO_LIVE, topic);
153     }
154 
155     public void send(MessageProducer producer, Message message, Destination dest, boolean topic)
156         throws JMSException
157     {
158         send(producer, message, dest, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
159             Message.DEFAULT_TIME_TO_LIVE, topic);
160     }
161 
162     public void send(MessageProducer producer,
163                      Message message,
164                      boolean persistent,
165                      int priority,
166                      long ttl,
167                      boolean topic) throws JMSException
168     {
169         producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
170             priority, ttl);
171     }
172 
173     public void send(MessageProducer producer,
174                      Message message,
175                      Destination dest,
176                      boolean persistent,
177                      int priority,
178                      long ttl,
179                      boolean topic) throws JMSException
180     {
181         producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
182             priority, ttl);
183     }
184 
185 }