Coverage Report - org.mule.transport.jms.JmsConnector
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsConnector
0%
0/340
0%
0/118
0
JmsConnector$1
0%
0/4
0%
0/4
0
 
 1  
 /*
 2  
  * $Id: JmsConnector.java 19191 2010-08-25 21:05:23Z tcarlson $
 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.MuleContext;
 14  
 import org.mule.api.MuleException;
 15  
 import org.mule.api.MuleRuntimeException;
 16  
 import org.mule.api.construct.FlowConstruct;
 17  
 import org.mule.api.context.notification.ConnectionNotificationListener;
 18  
 import org.mule.api.endpoint.ImmutableEndpoint;
 19  
 import org.mule.api.endpoint.InboundEndpoint;
 20  
 import org.mule.api.lifecycle.InitialisationException;
 21  
 import org.mule.api.lifecycle.StartException;
 22  
 import org.mule.api.lifecycle.StopException;
 23  
 import org.mule.api.transaction.Transaction;
 24  
 import org.mule.api.transaction.TransactionException;
 25  
 import org.mule.api.transport.ReplyToHandler;
 26  
 import org.mule.config.ExceptionHelper;
 27  
 import org.mule.config.i18n.CoreMessages;
 28  
 import org.mule.config.i18n.MessageFactory;
 29  
 import org.mule.context.notification.ConnectionNotification;
 30  
 import org.mule.context.notification.NotificationException;
 31  
 import org.mule.transaction.TransactionCoordination;
 32  
 import org.mule.transport.AbstractConnector;
 33  
 import org.mule.transport.ConnectException;
 34  
 import org.mule.transport.jms.i18n.JmsMessages;
 35  
 import org.mule.transport.jms.xa.ConnectionFactoryWrapper;
 36  
 import org.mule.util.BeanUtils;
 37  
 
 38  
 import java.text.MessageFormat;
 39  
 import java.util.Hashtable;
 40  
 import java.util.Map;
 41  
 
 42  
 import javax.jms.Connection;
 43  
 import javax.jms.ConnectionFactory;
 44  
 import javax.jms.ExceptionListener;
 45  
 import javax.jms.JMSException;
 46  
 import javax.jms.MessageConsumer;
 47  
 import javax.jms.MessageProducer;
 48  
 import javax.jms.Session;
 49  
 import javax.jms.TemporaryQueue;
 50  
 import javax.jms.TemporaryTopic;
 51  
 import javax.jms.XAConnectionFactory;
 52  
 import javax.naming.CommunicationException;
 53  
 import javax.naming.Context;
 54  
 import javax.naming.InitialContext;
 55  
 import javax.naming.NamingException;
 56  
 
 57  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
 58  
 
 59  
 /**
 60  
  * <code>JmsConnector</code> is a JMS 1.0.2b compliant connector that can be used
 61  
  * by a Mule endpoint. The connector supports all JMS functionality including topics
 62  
  * and queues, durable subscribers, acknowledgement modes and local transactions.
 63  
  */
 64  
 
 65  0
 public class JmsConnector extends AbstractConnector implements ExceptionListener
 66  
 {
 67  
 
 68  
     public static final String JMS = "jms";
 69  
 
 70  
     /**
 71  
      * Indicates that Mule should throw an exception on any redelivery attempt.
 72  
      */
 73  
     public static final int REDELIVERY_FAIL_ON_FIRST = 0;
 74  
 
 75  
     public static final int REDELIVERY_IGNORE = -1;
 76  
 
 77  0
     private AtomicInteger receiverReportedExceptionCount = new AtomicInteger();
 78  
 
 79  
     ////////////////////////////////////////////////////////////////////////
 80  
     // Properties
 81  
     ////////////////////////////////////////////////////////////////////////
 82  
 
 83  0
     private int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;
 84  
 
 85  
     private String clientId;
 86  
 
 87  
     private boolean durable;
 88  
 
 89  
     private boolean noLocal;
 90  
 
 91  
     private boolean persistentDelivery;
 92  
 
 93  
     private boolean honorQosHeaders;
 94  
 
 95  0
     private int maxRedelivery = REDELIVERY_FAIL_ON_FIRST;
 96  
 
 97  0
     private boolean cacheJmsSessions = false;
 98  
 
 99  
     /**
 100  
      * Whether to create a consumer on connect.
 101  
      */
 102  0
     private boolean eagerConsumer = true;
 103  
 
 104  
     ////////////////////////////////////////////////////////////////////////
 105  
     // JMS Connection
 106  
     ////////////////////////////////////////////////////////////////////////
 107  
 
 108  
     /**
 109  
      * JMS Connection, not settable by the user.
 110  
      */
 111  
     private Connection connection;
 112  
 
 113  
     private ConnectionFactory connectionFactory;
 114  
 
 115  
     private Map connectionFactoryProperties;
 116  
 
 117  0
     public String username = null;
 118  
 
 119  0
     public String password = null;
 120  
 
 121  
     ////////////////////////////////////////////////////////////////////////
 122  
     // JNDI Connection
 123  
     ////////////////////////////////////////////////////////////////////////
 124  
 
 125  0
     private Context jndiContext = null;
 126  
 
 127  
     /**
 128  
      * This object guards all access to the jndiContext
 129  
      */
 130  0
     private final Object jndiLock = new Object();
 131  
 
 132  
     private String jndiProviderUrl;
 133  
 
 134  
     private String jndiInitialFactory;
 135  
 
 136  
     private Map jndiProviderProperties;
 137  
 
 138  
     private String connectionFactoryJndiName;
 139  
 
 140  0
     private boolean jndiDestinations = false;
 141  
 
 142  0
     private boolean forceJndiDestinations = false;
 143  
 
 144  
     ////////////////////////////////////////////////////////////////////////
 145  
     // Strategy classes
 146  
     ////////////////////////////////////////////////////////////////////////
 147  
 
 148  0
     private String specification = JmsConstants.JMS_SPECIFICATION_102B;
 149  
 
 150  
     private JmsSupport jmsSupport;
 151  
 
 152  
     private JmsTopicResolver topicResolver;
 153  
 
 154  
     private RedeliveryHandlerFactory redeliveryHandlerFactory;
 155  
 
 156  
     /**
 157  
      * determines whether a temporary JMSReplyTo destination will be used when using synchronous outbound JMS endpoints
 158  
      */
 159  0
     private boolean disableTemporaryReplyToDestinations = false;
 160  
 
 161  
     /**
 162  
      * If disableTemporaryReplyToDestinations = "true", this flag causes the original JMS Message to be returned as a
 163  
      * synchronous response with any properties set on it by the JMS Provider (e.g., JMSMessageID).
 164  
      *
 165  
      * @see EE-1688/MULE-3059
 166  
      */
 167  0
     private boolean returnOriginalMessageAsReply = false;
 168  
 
 169  
     /**
 170  
      * In-container embedded mode disables some features for strict Java EE compliance.
 171  
      */
 172  
     private boolean embeddedMode;
 173  
 
 174  
     /**
 175  
      * Overrides XaResource.isSameRM() result. Needed for IBM WMQ XA
 176  
      * implementation (set to 'false'). Default value is null (don't override).
 177  
      */
 178  
     private Boolean sameRMOverrideValue;
 179  
 
 180  
     ////////////////////////////////////////////////////////////////////////
 181  
     // Methods
 182  
     ////////////////////////////////////////////////////////////////////////
 183  
 
 184  
     /* Register the Jms Exception reader if this class gets loaded */
 185  
 
 186  
     static
 187  
     {
 188  0
         ExceptionHelper.registerExceptionReader(new JmsExceptionReader());
 189  0
     }
 190  
 
 191  
     public JmsConnector(MuleContext context)
 192  
     {
 193  0
         super(context);
 194  0
     }
 195  
     
 196  
     public String getProtocol()
 197  
     {
 198  0
         return JMS;
 199  
     }
 200  
 
 201  
     @Override
 202  
     protected void doInitialise() throws InitialisationException
 203  
     {        
 204  
         try
 205  
         {
 206  0
             connectionFactory = this.createConnectionFactory();
 207  
         }
 208  0
         catch (NamingException ne)
 209  
         {
 210  0
             throw new InitialisationException(JmsMessages.errorCreatingConnectionFactory(), ne, this);
 211  0
         }
 212  
 
 213  0
         if ((connectionFactoryProperties != null) && !connectionFactoryProperties.isEmpty())
 214  
         {
 215  
             // apply connection factory properties
 216  0
             BeanUtils.populateWithoutFail(connectionFactory, connectionFactoryProperties, true);
 217  
         }
 218  
 
 219  0
         if (topicResolver == null)
 220  
         {
 221  0
             topicResolver = new DefaultJmsTopicResolver(this);
 222  
         }
 223  0
         if (redeliveryHandlerFactory == null)
 224  
         {
 225  0
             redeliveryHandlerFactory = new AutoDiscoveryRedeliveryHandlerFactory(this);
 226  
         }
 227  
 
 228  
         try
 229  
         {
 230  0
             muleContext.registerListener(new ConnectionNotificationListener<ConnectionNotification>()
 231  0
             {
 232  
                 public void onNotification(ConnectionNotification notification)
 233  
                 {
 234  0
                     if (notification.getAction() == ConnectionNotification.CONNECTION_DISCONNECTED
 235  
                             || notification.getAction() == ConnectionNotification.CONNECTION_FAILED)
 236  
                     {
 237  
                         // Remove all dispatchers as any cached session will be invalidated
 238  0
                         clearDispatchers();
 239  
                         // TODO should we dispose receivers here as well (in case they are
 240  
                         // transactional)
 241  
                         // gives a harmless NPE at
 242  
                         // AbstractConnector.connect(AbstractConnector.java:927)
 243  
                         // disposeReceivers();
 244  
                     }
 245  0
                 }
 246  
 
 247  
             }, getName());
 248  
 
 249  
 
 250  
         }
 251  0
         catch (NotificationException nex)
 252  
         {
 253  0
             throw new InitialisationException(nex, this);
 254  0
         }
 255  
 
 256  0
         if (jmsSupport == null)
 257  
         {
 258  0
             jmsSupport = createJmsSupport();
 259  
         }
 260  0
     }
 261  
 
 262  
     /**
 263  
      * A factory method to create various JmsSupport class versions.
 264  
      *
 265  
      * @return JmsSupport instance
 266  
      * @see JmsSupport
 267  
      */
 268  
     protected JmsSupport createJmsSupport()
 269  
     {
 270  
         final JmsSupport result;
 271  0
         if (JmsConstants.JMS_SPECIFICATION_102B.equals(specification))
 272  
         {
 273  0
             result = new Jms102bSupport(this);
 274  
         }
 275  
         else
 276  
         {
 277  0
             result = new Jms11Support(this);
 278  
         }
 279  
 
 280  0
         return result;
 281  
     }
 282  
 
 283  
     protected ConnectionFactory createConnectionFactory() throws InitialisationException, NamingException
 284  
     {
 285  
         // if an initial factory class was configured that takes precedence over the 
 286  
         // spring-configured connection factory or the one that our subclasses may provide
 287  0
         if (jndiInitialFactory != null)
 288  
         {
 289  0
             this.initJndiContext();
 290  
 
 291  0
             Object temp = jndiContext.lookup(connectionFactoryJndiName);
 292  0
             if (temp instanceof ConnectionFactory)
 293  
             {
 294  0
                 return (ConnectionFactory) temp;
 295  
             }
 296  
             else
 297  
             {
 298  0
                 throw new InitialisationException(
 299  
                         JmsMessages.invalidResourceType(ConnectionFactory.class, temp), this);
 300  
             }
 301  
         }
 302  
         else
 303  
         {
 304  
             // don't look up objects from JNDI in any case
 305  0
             jndiDestinations = false;
 306  0
             forceJndiDestinations = false;
 307  
 
 308  
             // don't use JNDI. Use the spring-configured connection factory if that's provided
 309  0
             if (connectionFactory != null)
 310  
             {
 311  0
                 return connectionFactory;
 312  
             }
 313  
 
 314  
             // no spring-configured connection factory. See if there is a default one (e.g. from
 315  
             // subclass)
 316  
             ConnectionFactory factory;
 317  
             try
 318  
             {
 319  0
                 factory = getDefaultConnectionFactory();
 320  
             }
 321  0
             catch (Exception e)
 322  
             {
 323  0
                 throw new InitialisationException(e, this);
 324  0
             }
 325  0
             if (factory == null)
 326  
             {
 327  
                 // no connection factory ... give up
 328  0
                 throw new InitialisationException(JmsMessages.noConnectionFactoryConfigured(), this);
 329  
             }
 330  0
             return factory;
 331  
         }
 332  
     }
 333  
 
 334  
     /**
 335  
      * Override this method to provide a default ConnectionFactory for a vendor-specific JMS Connector.
 336  
      * @throws Exception 
 337  
      */
 338  
     protected ConnectionFactory getDefaultConnectionFactory() throws Exception
 339  
     {
 340  0
         return null;
 341  
     }
 342  
 
 343  
     @Override
 344  
     protected void doDispose()
 345  
     {
 346  0
         if (connection != null)
 347  
         {
 348  
             try
 349  
             {
 350  0
                 connection.close();
 351  
             }
 352  0
             catch (JMSException e)
 353  
             {
 354  0
                 logger.error("Jms connector failed to dispose properly: ", e);
 355  0
             }
 356  0
             connection = null;
 357  
         }
 358  
 
 359  0
         if (jndiContext != null)
 360  
         {
 361  
             try
 362  
             {
 363  0
                 jndiContext.close();
 364  
             }
 365  0
             catch (NamingException ne)
 366  
             {
 367  0
                 logger.error("Jms connector failed to dispose properly: ", ne);
 368  
             }
 369  
             finally
 370  
             {
 371  0
                 jndiContext = null;
 372  0
             }
 373  
         }
 374  0
     }
 375  
 
 376  
     protected void initJndiContext() throws NamingException, InitialisationException
 377  
     {
 378  0
         synchronized (jndiLock)
 379  
         {
 380  0
             Hashtable<String, Object> props = new Hashtable<String, Object>();
 381  
 
 382  0
             if (jndiInitialFactory != null)
 383  
             {
 384  0
                 props.put(Context.INITIAL_CONTEXT_FACTORY, jndiInitialFactory);
 385  
             }
 386  0
             else if (jndiProviderProperties == null
 387  
                     || !jndiProviderProperties.containsKey(Context.INITIAL_CONTEXT_FACTORY))
 388  
             {
 389  0
                 throw new InitialisationException(CoreMessages.objectIsNull("jndiInitialFactory"), this);
 390  
             }
 391  
 
 392  0
             if (jndiProviderUrl != null)
 393  
             {
 394  0
                 props.put(Context.PROVIDER_URL, jndiProviderUrl);
 395  
             }
 396  
 
 397  0
             if (jndiProviderProperties != null)
 398  
             {
 399  0
                 props.putAll(jndiProviderProperties);
 400  
             }
 401  
 
 402  0
             jndiContext = new InitialContext(props);
 403  0
         }
 404  0
     }
 405  
 
 406  
     protected Object lookupFromJndi(String jndiName) throws NamingException
 407  
     {
 408  0
         synchronized (jndiLock)
 409  
         {
 410  
             try
 411  
             {
 412  0
                 return jndiContext.lookup(jndiName);
 413  
             }
 414  0
             catch (CommunicationException ce)
 415  
             {
 416  
                 try
 417  
                 {
 418  0
                     final Transaction tx = TransactionCoordination.getInstance().getTransaction();
 419  0
                     if (tx != null)
 420  
                     {
 421  0
                         tx.setRollbackOnly();
 422  
                     }
 423  
                 }
 424  0
                 catch (TransactionException e)
 425  
                 {
 426  0
                     throw new MuleRuntimeException(
 427  
                             MessageFactory.createStaticMessage("Failed to mark transaction for rollback: "), e);
 428  0
                 }
 429  
 
 430  
                 // re-throw
 431  0
                 throw ce;
 432  
             }
 433  0
         }
 434  
     }
 435  
 
 436  
     protected Connection createConnection() throws NamingException, JMSException, InitialisationException
 437  
     {
 438  0
         ConnectionFactory cf = this.connectionFactory;
 439  
         Connection connection;
 440  
 
 441  
         try
 442  
         {
 443  0
             if (cf instanceof XAConnectionFactory && muleContext.getTransactionManager() != null)
 444  
             {
 445  0
                 cf = new ConnectionFactoryWrapper(cf, sameRMOverrideValue);
 446  
             }
 447  
         }
 448  0
         catch (Exception e)
 449  
         {
 450  0
             throw new InitialisationException(e, this);
 451  0
         }
 452  
 
 453  0
         if (username != null)
 454  
         {
 455  0
             connection = jmsSupport.createConnection(cf, username, password);
 456  
         }
 457  
         else
 458  
         {
 459  0
             connection = jmsSupport.createConnection(cf);
 460  
         }
 461  
 
 462  0
         if (connection != null)
 463  
         {
 464  
             // EE-1901: only sets the clientID if it was not already set
 465  0
             if (clientId != null && !clientId.equals(connection.getClientID()))
 466  
             {
 467  0
                 connection.setClientID(getClientId());
 468  
             }
 469  0
             if (!embeddedMode)
 470  
             {
 471  0
                 connection.setExceptionListener(this);
 472  
             }
 473  
         }
 474  
 
 475  
 
 476  0
         return connection;
 477  
     }
 478  
 
 479  
     public void onException(JMSException jmsException)
 480  
     {
 481  0
         final JmsConnector jmsConnector = JmsConnector.this;
 482  0
         Map receivers = jmsConnector.getReceivers();
 483  0
         boolean isMultiConsumerReceiver = false;
 484  
 
 485  0
         if (!receivers.isEmpty())
 486  
         {
 487  0
             Map.Entry entry = (Map.Entry) receivers.entrySet().iterator().next();
 488  0
             if (entry.getValue() instanceof MultiConsumerJmsMessageReceiver)
 489  
             {
 490  0
                 isMultiConsumerReceiver = true;
 491  
             }
 492  
         }
 493  
 
 494  0
         int expectedReceiverCount = isMultiConsumerReceiver ? 1 :
 495  
                 (jmsConnector.getReceivers().size() * jmsConnector.getNumberOfConcurrentTransactedReceivers());
 496  
 
 497  0
         if (logger.isDebugEnabled())
 498  
         {
 499  0
             logger.debug("About to recycle myself due to remote JMS connection shutdown but need "
 500  
                     + "to wait for all active receivers to report connection loss. Receiver count: "
 501  
                     + (receiverReportedExceptionCount.get() + 1) + '/' + expectedReceiverCount);
 502  
         }
 503  
 
 504  0
         if (receiverReportedExceptionCount.incrementAndGet() >= expectedReceiverCount)
 505  
         {
 506  0
             receiverReportedExceptionCount.set(0);
 507  
 
 508  0
             handleException(new ConnectException(jmsException, this));
 509  
         }
 510  0
     }
 511  
 
 512  
     // TODO This might make retry work a bit better w/ JMS
 513  
 //    @Override
 514  
 //    public boolean validateConnection() throws Exception
 515  
 //    {
 516  
 //        logger.debug("Creating a temporary session to verify that we have a healthy connection...");
 517  
 //
 518  
 //        Connection connection;
 519  
 //        Session session;
 520  
 //        try
 521  
 //        {
 522  
 //            connection = createConnection();
 523  
 //            if (connection == null)
 524  
 //            {
 525  
 //                return false;
 526  
 //            }
 527  
 //            session = connection.createSession(false, 1);
 528  
 //            if (session == null)
 529  
 //            {
 530  
 //                return false;
 531  
 //            }
 532  
 //            session.close();
 533  
 //            connection.close();
 534  
 //            return true;
 535  
 //        }
 536  
 //        finally
 537  
 //        {
 538  
 //            session = null;
 539  
 //            connection = null;
 540  
 //        }
 541  
 //    }
 542  
 
 543  
     @Override
 544  
     protected void doConnect() throws Exception
 545  
     {
 546  0
         connection = createConnection();
 547  0
         if (isStarted())
 548  
         {
 549  0
             connection.start();
 550  
         }
 551  0
     }
 552  
 
 553  
     @Override
 554  
     protected void doDisconnect() throws Exception
 555  
     {
 556  
         try
 557  
         {
 558  0
             if (connection != null)
 559  
             {
 560  
                 // Ignore exceptions while closing the connection
 561  0
                 if (!embeddedMode)
 562  
                 {
 563  0
                     connection.setExceptionListener(null);
 564  
                 }
 565  0
                 connection.close();
 566  
             }
 567  
         }
 568  
         finally
 569  
         {
 570  
             // connectionFactory = null;
 571  0
             connection = null;
 572  0
         }
 573  0
     }
 574  
 
 575  
     @Override
 576  
     protected Object getReceiverKey(FlowConstruct flowConstruct, InboundEndpoint endpoint)
 577  
     {
 578  0
         return flowConstruct.getName() + "~" + endpoint.getEndpointURI().getAddress();
 579  
     }
 580  
 
 581  
     public Session getSessionFromTransaction()
 582  
     {
 583  0
         Transaction tx = TransactionCoordination.getInstance().getTransaction();
 584  0
         if (tx != null)
 585  
         {
 586  0
             if (tx.hasResource(connection))
 587  
             {
 588  0
                 if (logger.isDebugEnabled())
 589  
                 {
 590  0
                     logger.debug("Retrieving jms session from current transaction " + tx);
 591  
                 }
 592  
 
 593  0
                 Session session = (Session) tx.getResource(connection);
 594  
 
 595  0
                 if (logger.isDebugEnabled())
 596  
                 {
 597  0
                     logger.debug("Using " + session + " bound to transaction " + tx);
 598  
                 }
 599  
 
 600  0
                 return session;
 601  
             }
 602  
         }
 603  0
         return null;
 604  
     }
 605  
 
 606  
     public Session getSession(ImmutableEndpoint endpoint) throws JMSException
 607  
     {
 608  0
         final boolean topic = getTopicResolver().isTopic(endpoint);
 609  0
         return getSession(endpoint.getTransactionConfig().isTransacted(), topic);
 610  
     }
 611  
 
 612  
     public Session getSession(boolean transacted, boolean topic) throws JMSException
 613  
     {
 614  0
         Session session = getSessionFromTransaction();
 615  0
         if (session != null)
 616  
         {
 617  0
             return session;
 618  
         }
 619  
 
 620  0
         Transaction tx = TransactionCoordination.getInstance().getTransaction();
 621  
 
 622  0
         session = jmsSupport.createSession(connection, topic, transacted, acknowledgementMode, noLocal);
 623  
 
 624  0
         if (logger.isDebugEnabled())
 625  
         {
 626  0
             logger.debug(MessageFormat.format(
 627  
                     "Retrieved new jms session from connection: " +
 628  
                             "topic={0}, transacted={1}, ack mode={2}, nolocal={3}: {4}",
 629  
                     topic, transacted, acknowledgementMode, noLocal, session));
 630  
         }
 631  
 
 632  0
         if (tx != null)
 633  
         {
 634  0
             logger.debug("Binding session " + session + " to current transaction " + tx);
 635  
             try
 636  
             {
 637  0
                 tx.bindResource(connection, session);
 638  
             }
 639  0
             catch (TransactionException e)
 640  
             {
 641  0
                 closeQuietly(session);
 642  0
                 throw new RuntimeException("Could not bind session to current transaction", e);
 643  0
             }
 644  
         }
 645  0
         return session;
 646  
     }
 647  
 
 648  
     @Override
 649  
     protected void doStart() throws MuleException
 650  
     {
 651  
         //TODO: This should never be null or an exception should be thrown
 652  0
         if (connection != null)
 653  
         {
 654  
             try
 655  
             {
 656  0
                 connection.start();
 657  
             }
 658  0
             catch (JMSException e)
 659  
             {
 660  0
                 throw new StartException(CoreMessages.failedToStart("Jms Connection"), e, this);
 661  0
             }
 662  
         }
 663  0
     }
 664  
 
 665  
 
 666  
     /**
 667  
      * Closes a session if there is no active transaction in the current thread, otherwise the
 668  
      * session will continue active until there is a direct call to close it.
 669  
      *
 670  
      * @param session the session that ill be closed if there is an active transaction.
 671  
      */
 672  
     public void closeSessionIfNoTransactionActive(Session session)
 673  
     {
 674  0
         final Transaction transaction = TransactionCoordination.getInstance().getTransaction();
 675  0
         if (transaction == null)
 676  
         {
 677  0
             if (logger.isDebugEnabled())
 678  
             {
 679  0
                 logger.error("Closing non-TX session: " + session);
 680  
             }
 681  0
             closeQuietly(session);
 682  
         }
 683  0
         else if (logger.isDebugEnabled())
 684  
         {
 685  0
             logger.error("Not closing TX session: " + session);
 686  
         }
 687  0
     }
 688  
 
 689  
     @Override
 690  
     protected void doStop() throws MuleException
 691  
     {
 692  0
         if (connection != null)
 693  
         {
 694  
             try
 695  
             {
 696  0
                 connection.stop();
 697  
             }
 698  0
             catch (JMSException e)
 699  
             {
 700  0
                 throw new StopException(CoreMessages.failedToStop("Jms Connection"), e, this);
 701  0
             }
 702  
         }
 703  0
     }
 704  
 
 705  
     @Override
 706  
     public ReplyToHandler getReplyToHandler(ImmutableEndpoint endpoint)
 707  
     {
 708  0
         return new JmsReplyToHandler(this, getDefaultResponseTransformers(endpoint));
 709  
     }
 710  
 
 711  
     /**
 712  
      * This method may be overridden in case a certain JMS implementation does not
 713  
      * support all the standard JMS properties.
 714  
      */
 715  
     public boolean supportsProperty(String property)
 716  
     {
 717  0
         return true;
 718  
     }
 719  
 
 720  
     /**
 721  
      * This method may be overridden in order to apply pre-processing to the message
 722  
      * as soon as it arrives.
 723  
      *
 724  
      * @param message - the incoming message
 725  
      * @param session - the JMS session
 726  
      * @return the preprocessed message
 727  
      */
 728  
     public javax.jms.Message preProcessMessage(javax.jms.Message message, Session session) throws Exception
 729  
     {
 730  0
         return message;
 731  
     }
 732  
 
 733  
     /**
 734  
      * Closes the MessageProducer
 735  
      *
 736  
      * @param producer
 737  
      * @throws JMSException
 738  
      */
 739  
     public void close(MessageProducer producer) throws JMSException
 740  
     {
 741  0
         if (producer != null)
 742  
         {
 743  0
             if (logger.isDebugEnabled())
 744  
             {
 745  0
                 logger.debug("Closing producer: " + producer);
 746  
             }
 747  0
             producer.close();
 748  
         }
 749  0
         else if (logger.isDebugEnabled())
 750  
         {
 751  0
             logger.debug("Producer is null, nothing to close");
 752  
         }
 753  0
     }
 754  
 
 755  
     /**
 756  
      * Closes the MessageProducer without throwing an exception (an error message is
 757  
      * logged instead).
 758  
      *
 759  
      * @param producer
 760  
      */
 761  
     public void closeQuietly(MessageProducer producer)
 762  
     {
 763  
         try
 764  
         {
 765  0
             close(producer);
 766  
         }
 767  0
         catch (JMSException e)
 768  
         {
 769  0
             logger.error("Failed to close jms message producer", e);
 770  0
         }
 771  0
     }
 772  
 
 773  
     /**
 774  
      * Closes the MessageConsumer
 775  
      *
 776  
      * @param consumer
 777  
      * @throws JMSException
 778  
      */
 779  
     public void close(MessageConsumer consumer) throws JMSException
 780  
     {
 781  0
         if (consumer != null)
 782  
         {
 783  0
             if (logger.isDebugEnabled())
 784  
             {
 785  0
                 logger.debug("Closing consumer: " + consumer);
 786  
             }
 787  0
             consumer.close();
 788  
         }
 789  0
         else if (logger.isDebugEnabled())
 790  
         {
 791  0
             logger.debug("Consumer is null, nothing to close");
 792  
         }
 793  0
     }
 794  
 
 795  
     /**
 796  
      * Closes the MessageConsumer without throwing an exception (an error message is
 797  
      * logged instead).
 798  
      *
 799  
      * @param consumer
 800  
      */
 801  
     public void closeQuietly(MessageConsumer consumer)
 802  
     {
 803  
         try
 804  
         {
 805  0
             close(consumer);
 806  
         }
 807  0
         catch (JMSException e)
 808  
         {
 809  0
             logger.error("Failed to close jms message consumer", e);
 810  0
         }
 811  0
     }
 812  
 
 813  
     /**
 814  
      * Closes the MuleSession
 815  
      *
 816  
      * @param session
 817  
      * @throws JMSException
 818  
      */
 819  
     public void close(Session session) throws JMSException
 820  
     {
 821  0
         if (session != null)
 822  
         {
 823  0
             if (logger.isDebugEnabled())
 824  
             {
 825  0
                 logger.debug("Closing session " + session);
 826  
             }
 827  0
             session.close();
 828  
         }
 829  0
     }
 830  
 
 831  
     /**
 832  
      * Closes the MuleSession without throwing an exception (an error message is logged
 833  
      * instead).
 834  
      *
 835  
      * @param session
 836  
      */
 837  
     public void closeQuietly(Session session)
 838  
     {
 839  
         try
 840  
         {
 841  0
             close(session);
 842  
         }
 843  0
         catch (JMSException e)
 844  
         {
 845  0
             logger.warn("Failed to close jms session consumer", e);
 846  0
         }
 847  0
     }
 848  
 
 849  
     /**
 850  
      * Closes the TemporaryQueue
 851  
      *
 852  
      * @param tempQueue
 853  
      * @throws JMSException
 854  
      */
 855  
     public void close(TemporaryQueue tempQueue) throws JMSException
 856  
     {
 857  0
         if (tempQueue != null)
 858  
         {
 859  0
             tempQueue.delete();
 860  
         }
 861  0
     }
 862  
 
 863  
     /**
 864  
      * Closes the TemporaryQueue without throwing an exception (an error message is
 865  
      * logged instead).
 866  
      *
 867  
      * @param tempQueue
 868  
      */
 869  
     public void closeQuietly(TemporaryQueue tempQueue)
 870  
     {
 871  
         try
 872  
         {
 873  0
             close(tempQueue);
 874  
         }
 875  0
         catch (JMSException e)
 876  
         {
 877  0
             if (logger.isWarnEnabled())
 878  
             {
 879  0
                 String queueName = "";
 880  
                 try
 881  
                 {
 882  0
                     queueName = tempQueue.getQueueName();
 883  
                 }
 884  0
                 catch (JMSException innerEx)
 885  
                 {
 886  
                     // ignore, we are just trying to get the queue name
 887  0
                 }
 888  0
                 logger.warn(MessageFormat.format(
 889  
                         "Failed to delete a temporary queue ''{0}'' Reason: {1}",
 890  
                         queueName, e.getMessage()));
 891  
             }
 892  0
         }
 893  0
     }
 894  
 
 895  
     /**
 896  
      * Closes the TemporaryTopic
 897  
      *
 898  
      * @param tempTopic
 899  
      * @throws JMSException
 900  
      */
 901  
     public void close(TemporaryTopic tempTopic) throws JMSException
 902  
     {
 903  0
         if (tempTopic != null)
 904  
         {
 905  0
             tempTopic.delete();
 906  
         }
 907  0
     }
 908  
 
 909  
     /**
 910  
      * Closes the TemporaryTopic without throwing an exception (an error message is
 911  
      * logged instead).
 912  
      *
 913  
      * @param tempTopic
 914  
      */
 915  
     public void closeQuietly(TemporaryTopic tempTopic)
 916  
     {
 917  
         try
 918  
         {
 919  0
             close(tempTopic);
 920  
         }
 921  0
         catch (JMSException e)
 922  
         {
 923  0
             if (logger.isWarnEnabled())
 924  
             {
 925  0
                 String topicName = "";
 926  
                 try
 927  
                 {
 928  0
                     topicName = tempTopic.getTopicName();
 929  
                 }
 930  0
                 catch (JMSException innerEx)
 931  
                 {
 932  
                     // ignore, we are just trying to get the topic name
 933  0
                 }
 934  0
                 logger.warn("Failed to delete a temporary topic " + topicName, e);
 935  
             }
 936  0
         }
 937  0
     }
 938  
 
 939  
     ////////////////////////////////////////////////////////////////////////
 940  
     // Getters and Setters
 941  
     ////////////////////////////////////////////////////////////////////////
 942  
 
 943  
     /**
 944  
      * @return Returns the connection.
 945  
      */
 946  
     public Connection getConnection()
 947  
     {
 948  0
         return connection;
 949  
     }
 950  
 
 951  
     protected void setConnection(Connection connection)
 952  
     {
 953  0
         this.connection = connection;
 954  0
     }
 955  
 
 956  
     /**
 957  
      * @return Returns the acknowledgeMode.
 958  
      */
 959  
     public int getAcknowledgementMode()
 960  
     {
 961  0
         return acknowledgementMode;
 962  
     }
 963  
 
 964  
     /**
 965  
      * @param acknowledgementMode The acknowledgementMode to set.
 966  
      */
 967  
     public void setAcknowledgementMode(int acknowledgementMode)
 968  
     {
 969  0
         this.acknowledgementMode = acknowledgementMode;
 970  0
     }
 971  
 
 972  
     /**
 973  
      * @return Returns the durable.
 974  
      */
 975  
     public boolean isDurable()
 976  
     {
 977  0
         return durable;
 978  
     }
 979  
 
 980  
     /**
 981  
      * @param durable The durable to set.
 982  
      */
 983  
     public void setDurable(boolean durable)
 984  
     {
 985  0
         this.durable = durable;
 986  0
     }
 987  
 
 988  
     /**
 989  
      * @return Returns the noLocal.
 990  
      */
 991  
     public boolean isNoLocal()
 992  
     {
 993  0
         return noLocal;
 994  
     }
 995  
 
 996  
     /**
 997  
      * @param noLocal The noLocal to set.
 998  
      */
 999  
     public void setNoLocal(boolean noLocal)
 1000  
     {
 1001  0
         this.noLocal = noLocal;
 1002  0
     }
 1003  
 
 1004  
     /**
 1005  
      * @return Returns the persistentDelivery.
 1006  
      */
 1007  
     public boolean isPersistentDelivery()
 1008  
     {
 1009  0
         return persistentDelivery;
 1010  
     }
 1011  
 
 1012  
     /**
 1013  
      * @param persistentDelivery The persistentDelivery to set.
 1014  
      */
 1015  
     public void setPersistentDelivery(boolean persistentDelivery)
 1016  
     {
 1017  0
         this.persistentDelivery = persistentDelivery;
 1018  0
     }
 1019  
 
 1020  
     public JmsSupport getJmsSupport()
 1021  
     {
 1022  0
         return jmsSupport;
 1023  
     }
 1024  
 
 1025  
     public void setJmsSupport(JmsSupport jmsSupport)
 1026  
     {
 1027  0
         this.jmsSupport = jmsSupport;
 1028  0
     }
 1029  
 
 1030  
     public String getSpecification()
 1031  
     {
 1032  0
         return specification;
 1033  
     }
 1034  
 
 1035  
     public void setSpecification(String specification)
 1036  
     {
 1037  0
         if (JmsConstants.JMS_SPECIFICATION_11.equals(specification)
 1038  
             || (JmsConstants.JMS_SPECIFICATION_102B.equals(specification)))
 1039  
         {
 1040  0
             this.specification = specification;
 1041  
         }
 1042  
         else
 1043  
         {
 1044  0
             throw new IllegalArgumentException(
 1045  
                 "JMS specification needs to be one of the defined values in JmsConstants but was: "
 1046  
                                 + specification);
 1047  
         }
 1048  0
     }
 1049  
 
 1050  
     public String getUsername()
 1051  
     {
 1052  0
         return username;
 1053  
     }
 1054  
 
 1055  
     public void setUsername(String username)
 1056  
     {
 1057  0
         this.username = username;
 1058  0
     }
 1059  
 
 1060  
     public String getPassword()
 1061  
     {
 1062  0
         return password;
 1063  
     }
 1064  
 
 1065  
     public void setPassword(String password)
 1066  
     {
 1067  0
         this.password = password;
 1068  0
     }
 1069  
 
 1070  
     public String getClientId()
 1071  
     {
 1072  0
         return clientId;
 1073  
     }
 1074  
 
 1075  
     public void setClientId(String clientId)
 1076  
     {
 1077  0
         this.clientId = clientId;
 1078  0
     }
 1079  
 
 1080  
     public int getMaxRedelivery()
 1081  
     {
 1082  0
         return maxRedelivery;
 1083  
     }
 1084  
 
 1085  
     public void setMaxRedelivery(int maxRedelivery)
 1086  
     {
 1087  0
         this.maxRedelivery = maxRedelivery;
 1088  0
     }
 1089  
 
 1090  
     @Override
 1091  
     public boolean isResponseEnabled()
 1092  
     {
 1093  0
         return true;
 1094  
     }
 1095  
 
 1096  
 
 1097  
     /**
 1098  
      * Getter for property 'topicResolver'.
 1099  
      *
 1100  
      * @return Value for property 'topicResolver'.
 1101  
      */
 1102  
     public JmsTopicResolver getTopicResolver()
 1103  
     {
 1104  0
         return topicResolver;
 1105  
     }
 1106  
 
 1107  
     /**
 1108  
      * Setter for property 'topicResolver'.
 1109  
      *
 1110  
      * @param topicResolver Value to set for property 'topicResolver'.
 1111  
      */
 1112  
     public void setTopicResolver(final JmsTopicResolver topicResolver)
 1113  
     {
 1114  0
         this.topicResolver = topicResolver;
 1115  0
     }
 1116  
 
 1117  
     /**
 1118  
      * Getter for property 'eagerConsumer'. Default
 1119  
      * is {@code true}.
 1120  
      *
 1121  
      * @return Value for property 'eagerConsumer'.
 1122  
      * @see #eagerConsumer
 1123  
      */
 1124  
     public boolean isEagerConsumer()
 1125  
     {
 1126  0
         return eagerConsumer;
 1127  
     }
 1128  
 
 1129  
     /**
 1130  
      * A value of {@code true} will create a consumer on
 1131  
      * connect, in contrast to lazy instantiation in the poll loop.
 1132  
      * This setting very much depends on the JMS vendor.
 1133  
      * Affects transactional receivers, typical symptoms are:
 1134  
      * <ul>
 1135  
      * <li> consumer thread hanging forever, though a message is
 1136  
      * available
 1137  
      * <li>failure to consume the first message (the rest
 1138  
      * are fine)
 1139  
      * </ul>
 1140  
      * <p/>
 1141  
      *
 1142  
      * @param eagerConsumer Value to set for property 'eagerConsumer'.
 1143  
      * @see #eagerConsumer
 1144  
      * @see org.mule.transport.jms.XaTransactedJmsMessageReceiver
 1145  
      */
 1146  
     public void setEagerConsumer(final boolean eagerConsumer)
 1147  
     {
 1148  0
         this.eagerConsumer = eagerConsumer;
 1149  0
     }
 1150  
 
 1151  
     public boolean isCacheJmsSessions()
 1152  
     {
 1153  0
         return cacheJmsSessions;
 1154  
     }
 1155  
 
 1156  
     public void setCacheJmsSessions(boolean cacheJmsSessions)
 1157  
     {
 1158  0
         this.cacheJmsSessions = cacheJmsSessions;
 1159  0
     }
 1160  
 
 1161  
     public ConnectionFactory getConnectionFactory()
 1162  
     {
 1163  0
         return connectionFactory;
 1164  
     }
 1165  
 
 1166  
     public void setConnectionFactory(ConnectionFactory connectionFactory)
 1167  
     {
 1168  0
         this.connectionFactory = connectionFactory;
 1169  0
     }
 1170  
 
 1171  
     public RedeliveryHandlerFactory getRedeliveryHandlerFactory()
 1172  
     {
 1173  0
         return redeliveryHandlerFactory;
 1174  
     }
 1175  
 
 1176  
     public void setRedeliveryHandlerFactory(RedeliveryHandlerFactory redeliveryHandlerFactory)
 1177  
     {
 1178  0
         this.redeliveryHandlerFactory = redeliveryHandlerFactory;
 1179  0
     }
 1180  
 
 1181  
     /**
 1182  
      * Sets the <code>honorQosHeaders</code> property, which determines whether
 1183  
      * {@link JmsMessageDispatcher} should honor incoming message's QoS headers
 1184  
      * (JMSPriority, JMSDeliveryMode).
 1185  
      *
 1186  
      * @param honorQosHeaders <code>true</code> if {@link JmsMessageDispatcher}
 1187  
      *                        should honor incoming message's QoS headers; otherwise
 1188  
      *                        <code>false</code> Default is <code>false</code>, meaning that
 1189  
      *                        connector settings will override message headers.
 1190  
      */
 1191  
     public void setHonorQosHeaders(boolean honorQosHeaders)
 1192  
     {
 1193  0
         this.honorQosHeaders = honorQosHeaders;
 1194  0
     }
 1195  
 
 1196  
     /**
 1197  
      * Gets the value of <code>honorQosHeaders</code> property.
 1198  
      *
 1199  
      * @return <code>true</code> if <code>JmsMessageDispatcher</code> should
 1200  
      *         honor incoming message's QoS headers; otherwise <code>false</code>
 1201  
      *         Default is <code>false</code>, meaning that connector settings will
 1202  
      *         override message headers.
 1203  
      */
 1204  
     public boolean isHonorQosHeaders()
 1205  
     {
 1206  0
         return honorQosHeaders;
 1207  
     }
 1208  
 
 1209  
     public Context getJndiContext()
 1210  
     {
 1211  0
         return jndiContext;
 1212  
     }
 1213  
 
 1214  
     public void setJndiContext(Context jndiContext)
 1215  
     {
 1216  0
         this.jndiContext = jndiContext;
 1217  0
     }
 1218  
 
 1219  
     public String getJndiInitialFactory()
 1220  
     {
 1221  0
         return jndiInitialFactory;
 1222  
     }
 1223  
 
 1224  
     public void setJndiInitialFactory(String jndiInitialFactory)
 1225  
     {
 1226  0
         this.jndiInitialFactory = jndiInitialFactory;
 1227  0
     }
 1228  
 
 1229  
     public String getJndiProviderUrl()
 1230  
     {
 1231  0
         return jndiProviderUrl;
 1232  
     }
 1233  
 
 1234  
     public void setJndiProviderUrl(String jndiProviderUrl)
 1235  
     {
 1236  0
         this.jndiProviderUrl = jndiProviderUrl;
 1237  0
     }
 1238  
 
 1239  
     public Map getJndiProviderProperties()
 1240  
     {
 1241  0
         return jndiProviderProperties;
 1242  
     }
 1243  
 
 1244  
     public void setJndiProviderProperties(Map jndiProviderProperties)
 1245  
     {
 1246  0
         this.jndiProviderProperties = jndiProviderProperties;
 1247  0
     }
 1248  
 
 1249  
     public String getConnectionFactoryJndiName()
 1250  
     {
 1251  0
         return connectionFactoryJndiName;
 1252  
     }
 1253  
 
 1254  
     public void setConnectionFactoryJndiName(String connectionFactoryJndiName)
 1255  
     {
 1256  0
         this.connectionFactoryJndiName = connectionFactoryJndiName;
 1257  0
     }
 1258  
 
 1259  
     public boolean isJndiDestinations()
 1260  
     {
 1261  0
         return jndiDestinations;
 1262  
     }
 1263  
 
 1264  
     public void setJndiDestinations(boolean jndiDestinations)
 1265  
     {
 1266  0
         this.jndiDestinations = jndiDestinations;
 1267  0
     }
 1268  
 
 1269  
     public boolean isForceJndiDestinations()
 1270  
     {
 1271  0
         return forceJndiDestinations;
 1272  
     }
 1273  
 
 1274  
     public void setForceJndiDestinations(boolean forceJndiDestinations)
 1275  
     {
 1276  0
         this.forceJndiDestinations = forceJndiDestinations;
 1277  0
     }
 1278  
 
 1279  
     public boolean isDisableTemporaryReplyToDestinations()
 1280  
     {
 1281  0
         return disableTemporaryReplyToDestinations;
 1282  
     }
 1283  
 
 1284  
     public void setDisableTemporaryReplyToDestinations(boolean disableTemporaryReplyToDestinations)
 1285  
     {
 1286  0
         this.disableTemporaryReplyToDestinations = disableTemporaryReplyToDestinations;
 1287  0
     }
 1288  
 
 1289  
     public boolean isReturnOriginalMessageAsReply()
 1290  
     {
 1291  0
         return returnOriginalMessageAsReply;
 1292  
     }
 1293  
 
 1294  
     public void setReturnOriginalMessageAsReply(boolean returnOriginalMessageAsReply)
 1295  
     {
 1296  0
         this.returnOriginalMessageAsReply = returnOriginalMessageAsReply;
 1297  0
     }
 1298  
 
 1299  
     /**
 1300  
      * @return Returns underlying connection factory properties.
 1301  
      */
 1302  
     public Map getConnectionFactoryProperties()
 1303  
     {
 1304  0
         return connectionFactoryProperties;
 1305  
     }
 1306  
 
 1307  
     /**
 1308  
      * @param connectionFactoryProperties properties to be set on the underlying
 1309  
      *                                    ConnectionFactory.
 1310  
      */
 1311  
     public void setConnectionFactoryProperties(Map connectionFactoryProperties)
 1312  
     {
 1313  0
         this.connectionFactoryProperties = connectionFactoryProperties;
 1314  0
     }
 1315  
 
 1316  
     /**
 1317  
      * A synonym for {@link #numberOfConcurrentTransactedReceivers}. Note that
 1318  
      * it affects both transactional and non-transactional scenarios.
 1319  
      *
 1320  
      * @param count number of consumers
 1321  
      */
 1322  
     public void setNumberOfConsumers(int count)
 1323  
     {
 1324  0
         this.numberOfConcurrentTransactedReceivers = count;
 1325  0
     }
 1326  
 
 1327  
     /**
 1328  
      * A synonym for {@link #numberOfConcurrentTransactedReceivers}.
 1329  
      *
 1330  
      * @return number of consumers
 1331  
      */
 1332  
     public int getNumberOfConsumers()
 1333  
     {
 1334  0
         return this.numberOfConcurrentTransactedReceivers;
 1335  
     }
 1336  
 
 1337  
     public boolean isEmbeddedMode()
 1338  
     {
 1339  0
         return embeddedMode;
 1340  
     }
 1341  
 
 1342  
     public void setEmbeddedMode(boolean embeddedMode)
 1343  
     {
 1344  0
         this.embeddedMode = embeddedMode;
 1345  0
     }
 1346  
 
 1347  
     public Boolean getSameRMOverrideValue()
 1348  
     {
 1349  0
         return sameRMOverrideValue;
 1350  
     }
 1351  
 
 1352  
     public void setSameRMOverrideValue(Boolean sameRMOverrideValue)
 1353  
     {
 1354  0
         this.sameRMOverrideValue = sameRMOverrideValue;
 1355  0
     }
 1356  
 }