Coverage Report - org.mule.providers.jms.Jms11Support
 
Classes in this File Line Coverage Branch Coverage Complexity
Jms11Support
56%
35/62
43%
18/42
3.5
 
 1  
 /*
 2  
  * $Id: Jms11Support.java 7963 2007-08-21 08:53:15Z 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 java.text.MessageFormat;
 14  
 
 15  
 import javax.jms.Connection;
 16  
 import javax.jms.ConnectionFactory;
 17  
 import javax.jms.DeliveryMode;
 18  
 import javax.jms.Destination;
 19  
 import javax.jms.JMSException;
 20  
 import javax.jms.Message;
 21  
 import javax.jms.MessageConsumer;
 22  
 import javax.jms.MessageProducer;
 23  
 import javax.jms.Session;
 24  
 import javax.jms.Topic;
 25  
 import javax.naming.Context;
 26  
 import javax.naming.NamingException;
 27  
 
 28  
 /**
 29  
  * <code>Jms11Support</code> is a template class to provide an abstraction to to
 30  
  * the JMS 1.1 API specification.
 31  
  */
 32  
 
 33  
 public class Jms11Support implements JmsSupport
 34  
 {
 35  
     protected Context context;
 36  76
     protected boolean jndiDestinations = false;
 37  76
     protected boolean forceJndiDestinations = false;
 38  
     protected JmsConnector connector;
 39  
 
 40  
     public Jms11Support(JmsConnector connector,
 41  
                         Context context,
 42  
                         boolean jndiDestinations,
 43  
                         boolean forceJndiDestinations)
 44  76
     {
 45  76
         this.connector = connector;
 46  76
         this.context = context;
 47  76
         this.jndiDestinations = jndiDestinations;
 48  76
         this.forceJndiDestinations = forceJndiDestinations;
 49  76
     }
 50  
 
 51  
     public Connection createConnection(ConnectionFactory connectionFactory, String username, String password)
 52  
         throws JMSException
 53  
     {
 54  4
         if (connectionFactory == null)
 55  
         {
 56  0
             throw new IllegalArgumentException("connectionFactory cannot be null");
 57  
         }
 58  4
         return connectionFactory.createConnection(username, password);
 59  
     }
 60  
 
 61  
     public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException
 62  
     {
 63  57
         if (connectionFactory == null)
 64  
         {
 65  0
             throw new IllegalArgumentException("connectionFactory cannot be null");
 66  
         }
 67  57
         return connectionFactory.createConnection();
 68  
     }
 69  
 
 70  
     public Session createSession(Connection connection,
 71  
                                  boolean topic,
 72  
                                  boolean transacted,
 73  
                                  int ackMode,
 74  
                                  boolean noLocal) throws JMSException
 75  
     {
 76  201
         return connection.createSession(transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode));
 77  
     }
 78  
 
 79  
     public MessageProducer createProducer(Session session, Destination destination, boolean topic)
 80  
         throws JMSException
 81  
     {
 82  76
         return session.createProducer(destination);
 83  
     }
 84  
 
 85  
     public MessageConsumer createConsumer(Session session, Destination destination, boolean topic)
 86  
         throws JMSException
 87  
     {
 88  4
         return createConsumer(session, destination, null, false, null, topic);
 89  
     }
 90  
 
 91  
     public MessageConsumer createConsumer(Session session,
 92  
                                           Destination destination,
 93  
                                           String messageSelector,
 94  
                                           boolean noLocal,
 95  
                                           String durableName,
 96  
                                           boolean topic) throws JMSException
 97  
     {
 98  89
         if (durableName == null)
 99  
         {
 100  66
             if (topic)
 101  
             {
 102  10
                 return session.createConsumer(destination, messageSelector, noLocal);
 103  
             }
 104  
             else
 105  
             {
 106  56
                 return session.createConsumer(destination, messageSelector);
 107  
             }
 108  
         }
 109  
         else
 110  
         {
 111  23
             if (topic)
 112  
             {
 113  21
                 return session.createDurableSubscriber((Topic) destination, durableName, messageSelector,
 114  
                     noLocal);
 115  
             }
 116  
             else
 117  
             {
 118  2
                 throw new JMSException(
 119  
                     "A durable subscriber name was set but the destination was not a Topic");
 120  
             }
 121  
         }
 122  
     }
 123  
 
 124  
     public Destination createDestination(Session session, String name, boolean topic) throws JMSException
 125  
     {
 126  145
         if (session == null)
 127  
         {
 128  0
             throw new IllegalArgumentException("Session cannot be null when creating a destination");
 129  
         }
 130  144
         if (name == null)
 131  
         {
 132  0
             throw new IllegalArgumentException("Destination name cannot be null when creating a destination");
 133  
         }
 134  
 
 135  145
         if (jndiDestinations)
 136  
         {
 137  0
             if (context == null)
 138  
             {
 139  0
                 throw new IllegalArgumentException(
 140  
                     "Jndi Context name cannot be null when looking up a destination");
 141  
             }
 142  0
             Destination dest = getJndiDestination(name);
 143  0
             if (dest != null)
 144  
             {
 145  0
                 return dest;
 146  
             }
 147  0
             else if (forceJndiDestinations)
 148  
             {
 149  0
                 throw new JMSException("JNDI destination not found with name: " + name);
 150  
             }
 151  
         }
 152  
 
 153  145
         if (topic)
 154  
         {
 155  40
             return session.createTopic(name);
 156  
         }
 157  
         else
 158  
         {
 159  104
             return session.createQueue(name);
 160  
         }
 161  
     }
 162  
 
 163  
     protected Destination getJndiDestination(String name) throws JMSException
 164  
     {
 165  
         Object temp;
 166  
         try
 167  
         {
 168  0
             temp = context.lookup(name);
 169  
         }
 170  0
         catch (NamingException e)
 171  
         {
 172  0
             throw new JMSException(MessageFormat.format("Failed to look up destination {0}. Reason: {1}",
 173  
                                                         new Object[] {name, e.getMessage()}));
 174  0
         }
 175  0
         if (temp != null)
 176  
         {
 177  0
             if (temp instanceof Destination)
 178  
             {
 179  0
                 return (Destination) temp;
 180  
             }
 181  0
             else if (forceJndiDestinations)
 182  
             {
 183  0
                 throw new JMSException("JNDI destination not found with name: " + name);
 184  
             }
 185  
         }
 186  0
         return null;
 187  
     }
 188  
 
 189  
     public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException
 190  
     {
 191  4
         if (session == null)
 192  
         {
 193  0
             throw new IllegalArgumentException("Session cannot be null when creating a destination");
 194  
         }
 195  
 
 196  4
         if (topic)
 197  
         {
 198  0
             return session.createTemporaryTopic();
 199  
         }
 200  
         else
 201  
         {
 202  4
             return session.createTemporaryQueue();
 203  
         }
 204  
     }
 205  
 
 206  
     public void send(MessageProducer producer, Message message, boolean topic) throws JMSException
 207  
     {
 208  8
         send(producer, message, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
 209  
             Message.DEFAULT_TIME_TO_LIVE, topic);
 210  8
     }
 211  
 
 212  
     public void send(MessageProducer producer, Message message, Destination dest, boolean topic)
 213  
         throws JMSException
 214  
     {
 215  0
         send(producer, message, dest, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY,
 216  
             Message.DEFAULT_TIME_TO_LIVE, topic);
 217  0
     }
 218  
 
 219  
     public void send(MessageProducer producer,
 220  
                      Message message,
 221  
                      boolean persistent,
 222  
                      int priority,
 223  
                      long ttl,
 224  
                      boolean topic) throws JMSException
 225  
     {
 226  73
         producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
 227  
             priority, ttl);
 228  73
     }
 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  0
         producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT),
 239  
             priority, ttl);
 240  0
     }
 241  
 
 242  
 }