View Javadoc

1   /*
2    * $Id: AutoDiscoveryRedeliveryHandlerFactory.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.config.i18n.CoreMessages;
14  
15  import java.util.Enumeration;
16  import java.util.concurrent.atomic.AtomicReference;
17  
18  import javax.jms.ConnectionMetaData;
19  import javax.jms.JMSException;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * This factory will consult JMS connection metadata for supported optional properties and use
26   * those, if available, otherwise falling back to the manual counting of redeliveries.
27   *
28   * @see CountingRedeliveryHandlerFactory
29   * @see org.mule.transport.jms.JmsXRedeliveryHandlerFactory
30   * @see javax.jms.ConnectionMetaData
31   */
32  public class AutoDiscoveryRedeliveryHandlerFactory implements RedeliveryHandlerFactory
33  {
34      protected final Log logger = LogFactory.getLog(getClass());
35  
36      protected AtomicReference<RedeliveryHandler> delegateHandler = new AtomicReference<RedeliveryHandler>(null);
37  
38      protected JmsConnector connector;
39  
40      public AutoDiscoveryRedeliveryHandlerFactory(JmsConnector connector)
41      {
42          if (connector == null)
43          {
44              throw new IllegalArgumentException(CoreMessages.objectIsNull("connector").getMessage());
45          }
46          this.connector = connector;
47      }
48  
49      public RedeliveryHandler create()
50      {
51          RedeliveryHandler result;
52  
53          // initialize, accounting for concurrency
54          if (delegateHandler.get() == null)
55          {
56              RedeliveryHandler newInstance = createInstance();
57              boolean ok = delegateHandler.compareAndSet(null, newInstance);
58              if (!ok)
59              {
60                  // someone was faster to initialize it, use this ref instead
61                  result = delegateHandler.get();
62              }
63              else
64              {
65                  result = newInstance;
66              }
67          }
68          else
69          {
70              // just re-use existing handler
71              result = delegateHandler.get();
72          }
73  
74          return result;
75      }
76  
77      /**
78       * Create an instance using the discovery mechanism.
79       *
80       * @return an implementation based on the results of discovery
81       */
82      protected RedeliveryHandler createInstance()
83      {
84          RedeliveryHandler newInstance;
85          try
86          {
87              ConnectionMetaData metaData = connector.getConnection().getMetaData();
88              boolean supportsDeliveryCount = false;
89              final Enumeration propNames = metaData.getJMSXPropertyNames();
90              while (propNames.hasMoreElements())
91              {
92                  String p = (String) propNames.nextElement();
93                  if (JmsConstants.JMS_X_DELIVERY_COUNT.equals(p))
94                  {
95                      supportsDeliveryCount = true;
96                      break;
97                  }
98              }
99  
100             newInstance = (supportsDeliveryCount) ? new JmsXRedeliveryHandler() : new CountingRedeliveryHandler();
101         }
102         catch (JMSException e)
103         {
104             // fallback to defaults
105             newInstance = new CountingRedeliveryHandler();
106         }
107 
108         if (logger.isDebugEnabled())
109         {
110             logger.debug("Using " + newInstance.getClass().getName());
111         }
112 
113         return newInstance;
114     }
115 
116 }