View Javadoc

1   /*
2    * $Id: DefaultJmsTopicResolver.java 7976 2007-08-21 14:26:13Z 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 org.mule.umo.endpoint.UMOImmutableEndpoint;
14  import org.mule.util.MapUtils;
15  import org.mule.util.StringMessageUtils;
16  
17  import javax.jms.Destination;
18  import javax.jms.Queue;
19  import javax.jms.Topic;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * A default implementation of the resolver uses endpoint's
26   * resource info and Java's {@code instanceof} operator to
27   * detect JMS topics.
28   */
29  public class DefaultJmsTopicResolver implements JmsTopicResolver
30  {
31      /**
32       * logger used by this class
33       */
34      protected static final Log logger = LogFactory.getLog(DefaultJmsTopicResolver.class);
35  
36      /**
37       * Connector back-reference.
38       */
39      private JmsConnector connector;
40  
41      /**
42       * Create an instance of the resolver.
43       * @param connector owning connector
44       */
45      public DefaultJmsTopicResolver (final JmsConnector connector)
46      {
47          this.connector = connector;
48      }
49  
50  
51      /**
52       * Getter for property 'connector'.
53       *
54       * @return Value for property 'connector'.
55       */
56      public JmsConnector getConnector ()
57      {
58          return connector;
59      }
60  
61      /**
62       * Will use endpoint's resource info to detect a topic,
63       * as in {@code jms://topic:trade.PriceUpdatesTopic}. This
64       * method will call {@link #isTopic(org.mule.umo.endpoint.UMOImmutableEndpoint, boolean)}
65       * with fallback flag set to <strong>false</false>.
66       * <p/>
67       * <strong>NOTE:</strong> When using topics, use the '.' (dot) symbol for subcontext separation,
68       * as opposed to '/'. Otherwise the resource info may not get properly translated for the
69       * topic endpoint due to the way URI's are parsed. 
70       * @param endpoint endpoint to test
71       * @return true if the endpoint has a topic configuration
72       * @see #isTopic(org.mule.umo.endpoint.UMOImmutableEndpoint, boolean) 
73       */
74      public boolean isTopic (UMOImmutableEndpoint endpoint)
75      {
76          return isTopic(endpoint, false);
77      }
78  
79      /** {@inheritDoc} */
80      public boolean isTopic (UMOImmutableEndpoint endpoint, boolean fallbackToEndpointProperties)
81      {
82          final String resourceInfo = endpoint.getEndpointURI().getResourceInfo();
83          boolean topic = JmsConstants.TOPIC_PROPERTY.equalsIgnoreCase(resourceInfo);
84          if (!topic && fallbackToEndpointProperties)
85          {
86              topic = MapUtils.getBooleanValue(endpoint.getProperties(), JmsConstants.TOPIC_PROPERTY, false);
87          }
88  
89          return topic;
90      }
91  
92      /**
93       * Will use an {@code instanceof} operator. Keep in mind
94       * that may fail for JMS systems implementing both a
95       * {@code javax.jms.Topic} and {@code javax.jms.Queue} in
96       * a single destination class implementation.
97       * @param destination a jms destination to test
98       * @return {@code true} if the destination is a topic
99       */
100     public boolean isTopic (Destination destination)
101     {
102         checkInvariants(destination);
103 
104         return destination instanceof Topic;
105     }
106 
107     /**
108      * Perform some sanity checks, will complain in the log.
109      * @param destination destination to test
110      */
111     protected void checkInvariants (final Destination destination)
112     {
113         if (destination instanceof Topic && destination instanceof Queue
114             && connector.getJmsSupport() instanceof Jms102bSupport)
115         {
116             logger.error(StringMessageUtils.getBoilerPlate(
117                     "Destination implements both Queue and Topic "
118                     + "while complying with JMS 1.0.2b specification. "
119                     + "Please report your application server or JMS vendor name and version "
120                     + "to dev<_at_>mule.codehaus.org or http://www.mulesource.org/jira"));
121         }
122     }
123 }