View Javadoc

1   /*
2    * $Id: Jms11Support.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.Session;
26  import javax.jms.Topic;
27  import javax.naming.NamingException;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * <code>Jms11Support</code> is a template class to provide an abstraction to to
34   * the JMS 1.1 API specification.
35   */
36  
37  public class Jms11Support implements JmsSupport
38  {
39  
40      /**
41       * logger used by this class
42       */
43      protected final Log logger = LogFactory.getLog(getClass());
44  
45      protected JmsConnector connector;
46  
47      public Jms11Support(JmsConnector connector)
48      {
49          this.connector = connector;
50      }
51  
52      public Connection createConnection(ConnectionFactory connectionFactory, String username, String password)
53          throws JMSException
54      {
55          if (connectionFactory == null)
56          {
57              throw new IllegalArgumentException("connectionFactory cannot be null");
58          }
59          return connectionFactory.createConnection(username, password);
60      }
61  
62      public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException
63      {
64          if (connectionFactory == null)
65          {
66              throw new IllegalArgumentException("connectionFactory cannot be null");
67          }
68          return connectionFactory.createConnection();
69      }
70  
71      public Session createSession(Connection connection,
72                                   boolean topic,
73                                   boolean transacted,
74                                   int ackMode,
75                                   boolean noLocal) throws JMSException
76      {
77          return connection.createSession(transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode));
78      }
79  
80      public MessageProducer createProducer(Session session, Destination destination, boolean topic)
81          throws JMSException
82      {
83          return session.createProducer(destination);
84      }
85  
86      public MessageConsumer createConsumer(Session session, Destination destination, boolean topic, ImmutableEndpoint endpoint)
87          throws JMSException
88      {
89          return createConsumer(session, destination, null, false, null, topic, endpoint);
90      }
91  
92      public MessageConsumer createConsumer(Session session,
93                                            Destination destination,
94                                            String messageSelector,
95                                            boolean noLocal,
96                                            String durableName,
97                                            boolean topic, ImmutableEndpoint endpoint) throws JMSException
98      {
99          if (durableName == null)
100         {
101             if (topic)
102             {
103                 return session.createConsumer(destination, messageSelector, noLocal);
104             }
105             else
106             {
107                 return session.createConsumer(destination, messageSelector);
108             }
109         }
110         else
111         {
112             if (topic)
113             {
114                 return session.createDurableSubscriber((Topic) destination, durableName, messageSelector,
115                     noLocal);
116             }
117             else
118             {
119                 throw new JMSException(
120                     "A durable subscriber name was set but the destination was not a Topic");
121             }
122         }
123     }
124 
125     public Destination createDestination(Session session, ImmutableEndpoint endpoint) throws JMSException
126     {
127         String address = endpoint.getEndpointURI().toString();
128         if (address.contains(JmsConstants.TOPIC_PROPERTY + ":"))
129         {
130             // cut prefixes
131             address = address.substring((connector.getProtocol() + "://" + JmsConstants.TOPIC_PROPERTY + ":").length());
132             // cut any endpoint uri params, if any
133             if (address.contains("?"))
134             {
135                 address = address.substring(0, address.indexOf('?'));
136             }
137         }
138         else
139         {
140             address = endpoint.getEndpointURI().getAddress();
141         }
142         return createDestination(session, address, connector.getTopicResolver().isTopic(endpoint), endpoint);
143     }
144 
145     public Destination createDestination(Session session, String name, boolean topic, ImmutableEndpoint endpoint) throws JMSException
146     {
147         if (connector.isJndiDestinations())
148         {
149             try
150             {
151                 Destination dest = getJndiDestination(name);
152                 if (dest != null)
153                 {
154                     if (logger.isDebugEnabled())
155                     {
156                         logger.debug(MessageFormat.format("Destination {0} located in JNDI, will use it now", name));
157                     }
158                     return dest;
159                 }
160                 else
161                 {
162                     throw new JMSException("JNDI destination not found with name: " + name);
163                 }
164             }
165             catch (JMSException e)
166             {
167                 if (connector.isForceJndiDestinations())
168                 {
169                     throw e;
170                 }
171                 else 
172                 {
173                     logger.warn("Unable to look up JNDI destination " + name + ": " + e.getMessage());
174                 }
175             }
176         }
177 
178         if (logger.isDebugEnabled())
179         {
180             logger.debug("Using non-JNDI destination " + name + ", will create one now");
181         }
182 
183         if (session == null)
184         {
185             throw new IllegalArgumentException("Session cannot be null when creating a destination");
186         }
187         if (name == null)
188         {
189             throw new IllegalArgumentException("Destination name cannot be null when creating a destination");
190         }
191 
192         if (topic)
193         {
194             return session.createTopic(name);
195         }
196         else
197         {
198             return session.createQueue(name);
199         }
200     }
201     
202     protected Destination getJndiDestination(String name) throws JMSException
203     {
204         Object temp;
205         try
206         {
207             if (logger.isDebugEnabled())
208             {
209                 logger.debug(MessageFormat.format("Looking up {0} from JNDI", name));
210             }
211             temp = connector.lookupFromJndi(name);
212         }
213         catch (NamingException e)
214         {
215             if (logger.isDebugEnabled())
216             {
217                 logger.debug(e);
218             }
219             String message = MessageFormat.format("Failed to look up destination {0}. Reason: {1}",
220                                                   name, e.getMessage());
221             throw new JMSException(message);
222         }
223         
224         if (temp != null)
225         {
226             if (temp instanceof Destination)
227             {
228                 return (Destination) temp;
229             }
230         }
231         return null;
232     }
233 
234     public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException
235     {
236         if (session == null)
237         {
238             throw new IllegalArgumentException("Session cannot be null when creating a destination");
239         }
240 
241         if (topic)
242         {
243             return session.createTemporaryTopic();
244         }
245         else
246         {
247             return session.createTemporaryQueue();
248         }
249     }
250 
251     public void send(MessageProducer producer, Message message, boolean topic, ImmutableEndpoint endpoint) throws JMSException
252     {
253         send(producer, message, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
254             Message.DEFAULT_TIME_TO_LIVE, topic, endpoint);
255     }
256 
257     public void send(MessageProducer producer, Message message, Destination dest, boolean topic, ImmutableEndpoint endpoint)
258         throws JMSException
259     {
260         send(producer, message, dest, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
261             Message.DEFAULT_TIME_TO_LIVE, topic, endpoint);
262     }
263 
264     public void send(MessageProducer producer,
265                      Message message,
266                      boolean persistent,
267                      int priority,
268                      long ttl,
269                      boolean topic, ImmutableEndpoint endpoint) throws JMSException
270     {
271         producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
272             priority, ttl);
273     }
274 
275     public void send(MessageProducer producer,
276                      Message message,
277                      Destination dest,
278                      boolean persistent,
279                      int priority,
280                      long ttl,
281                      boolean topic, ImmutableEndpoint endpoint) throws JMSException
282     {
283         producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
284             priority, ttl);
285     }
286 
287 }