001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jms.support.destination;
018:
019: import javax.jms.Destination;
020: import javax.jms.JMSException;
021: import javax.jms.Queue;
022: import javax.jms.QueueSession;
023: import javax.jms.Session;
024: import javax.jms.Topic;
025: import javax.jms.TopicSession;
026:
027: import org.springframework.util.Assert;
028:
029: /**
030: * Simple {@link DestinationResolver} implementation resolving destination names
031: * as dynamic destinations.
032: *
033: * <p>This implementation will work on both JMS 1.1 and JMS 1.0.2,
034: * because it uses the {@link javax.jms.QueueSession} or {@link javax.jms.TopicSession}
035: * methods if possible, falling back to JMS 1.1's generic {@link javax.jms.Session}
036: * methods.
037: *
038: * @author Juergen Hoeller
039: * @since 1.1
040: * @see javax.jms.QueueSession#createQueue
041: * @see javax.jms.TopicSession#createTopic
042: * @see javax.jms.Session#createQueue
043: * @see javax.jms.Session#createTopic
044: */
045: public class DynamicDestinationResolver implements DestinationResolver {
046:
047: /**
048: * Resolve the specified destination name as a dynamic destination.
049: * @param session the current JMS Session
050: * @param destinationName the name of the destination
051: * @param pubSubDomain <code>true</code> if the domain is pub-sub, <code>false</code> if P2P
052: * @return the JMS destination (either a topic or a queue)
053: * @throws javax.jms.JMSException if resolution failed
054: * @see #resolveTopic(javax.jms.Session, String)
055: * @see #resolveQueue(javax.jms.Session, String)
056: */
057: public Destination resolveDestinationName(Session session,
058: String destinationName, boolean pubSubDomain)
059: throws JMSException {
060:
061: Assert.notNull(session, "Session must not be null");
062: Assert.notNull(destinationName,
063: "Destination name must not be null");
064: if (pubSubDomain) {
065: return resolveTopic(session, destinationName);
066: } else {
067: return resolveQueue(session, destinationName);
068: }
069: }
070:
071: /**
072: * Resolve the given destination name to a {@link Topic}.
073: * @param session the current JMS Session
074: * @param topicName the name of the desired {@link Topic}
075: * @return the JMS {@link Topic}
076: * @throws javax.jms.JMSException if resolution failed
077: * @see Session#createTopic(String)
078: */
079: protected Topic resolveTopic(Session session, String topicName)
080: throws JMSException {
081: if (session instanceof TopicSession) {
082: // Cast to TopicSession: will work on both JMS 1.1 and 1.0.2
083: return ((TopicSession) session).createTopic(topicName);
084: } else {
085: // Fall back to generic JMS Session: will only work on JMS 1.1
086: return session.createTopic(topicName);
087: }
088: }
089:
090: /**
091: * Resolve the given destination name to a {@link Queue}.
092: * @param session the current JMS Session
093: * @param queueName the name of the desired {@link Queue}
094: * @return the JMS {@link Queue}
095: * @throws javax.jms.JMSException if resolution failed
096: * @see Session#createQueue(String)
097: */
098: protected Queue resolveQueue(Session session, String queueName)
099: throws JMSException {
100: if (session instanceof QueueSession) {
101: // Cast to QueueSession: will work on both JMS 1.1 and 1.0.2
102: return ((QueueSession) session).createQueue(queueName);
103: } else {
104: // Fall back to generic JMS Session: will only work on JMS 1.1
105: return session.createQueue(queueName);
106: }
107: }
108:
109: }
|