001: /*
002: * $Id: DefaultJmsTopicResolver.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.jms;
012:
013: import org.mule.api.endpoint.ImmutableEndpoint;
014: import org.mule.util.MapUtils;
015: import org.mule.util.StringMessageUtils;
016:
017: import javax.jms.Destination;
018: import javax.jms.Queue;
019: import javax.jms.Topic;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /**
025: * A default implementation of the resolver uses endpoint's
026: * resource info and Java's {@code instanceof} operator to
027: * detect JMS topics.
028: */
029: public class DefaultJmsTopicResolver implements JmsTopicResolver {
030: /**
031: * logger used by this class
032: */
033: protected static final Log logger = LogFactory
034: .getLog(DefaultJmsTopicResolver.class);
035:
036: /**
037: * Connector back-reference.
038: */
039: private JmsConnector connector;
040:
041: /**
042: * Create an instance of the resolver.
043: * @param connector owning connector
044: */
045: public DefaultJmsTopicResolver(final JmsConnector connector) {
046: this .connector = connector;
047: }
048:
049: /**
050: * Getter for property 'connector'.
051: *
052: * @return Value for property 'connector'.
053: */
054: public JmsConnector getConnector() {
055: return connector;
056: }
057:
058: /**
059: * Will use endpoint's resource info to detect a topic,
060: * as in {@code jms://topic:trade.PriceUpdatesTopic}. This
061: * method will call {@link #isTopic(org.mule.api.endpoint.ImmutableEndpoint, boolean)}
062: * with fallback flag set to <strong>true</false>.
063: * <p/>
064: * <strong>NOTE:</strong> When using topics, use the '.' (dot) symbol for subcontext separation,
065: * as opposed to '/'. Otherwise the resource info may not get properly translated for the
066: * topic endpoint due to the way URI's are parsed.
067: * @param endpoint endpoint to test
068: * @return true if the endpoint has a topic configuration
069: * @see #isTopic(org.mule.api.endpoint.ImmutableEndpoint, boolean)
070: */
071: public boolean isTopic(ImmutableEndpoint endpoint) {
072: return isTopic(endpoint, true);
073: }
074:
075: /** {@inheritDoc} */
076: public boolean isTopic(ImmutableEndpoint endpoint,
077: boolean fallbackToEndpointProperties) {
078: final String resourceInfo = endpoint.getEndpointURI()
079: .getResourceInfo();
080: boolean topic = JmsConstants.TOPIC_PROPERTY
081: .equalsIgnoreCase(resourceInfo);
082: if (!topic && fallbackToEndpointProperties) {
083: topic = MapUtils.getBooleanValue(endpoint.getProperties(),
084: JmsConstants.TOPIC_PROPERTY, false);
085: }
086:
087: return topic;
088: }
089:
090: /**
091: * Will use an {@code instanceof} operator. Keep in mind
092: * that may fail for JMS systems implementing both a
093: * {@code javax.jms.Topic} and {@code javax.jms.Queue} in
094: * a single destination class implementation.
095: * @param destination a jms destination to test
096: * @return {@code true} if the destination is a topic
097: */
098: public boolean isTopic(Destination destination) {
099: checkInvariants(destination);
100:
101: return destination instanceof Topic;
102: }
103:
104: /**
105: * Perform some sanity checks, will complain in the log.
106: * @param destination destination to test
107: */
108: protected void checkInvariants(final Destination destination) {
109: if (destination instanceof Topic
110: && destination instanceof Queue
111: && connector.getJmsSupport() instanceof Jms102bSupport) {
112: logger
113: .error(StringMessageUtils
114: .getBoilerPlate("Destination implements both Queue and Topic "
115: + "while complying with JMS 1.0.2b specification. "
116: + "Please report your application server or JMS vendor name and version "
117: + "to dev<_at_>mule.codehaus.org or http://www.mulesource.org/jira"));
118: }
119: }
120: }
|