001/*
002 * Copyright 2002-2014 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 *      https://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
017package org.springframework.jms.support.destination;
018
019import javax.jms.Destination;
020import javax.jms.JMSException;
021import javax.jms.Queue;
022import javax.jms.Session;
023import javax.jms.Topic;
024
025import org.springframework.lang.Nullable;
026import org.springframework.util.Assert;
027
028/**
029 * Simple {@link DestinationResolver} implementation resolving destination names
030 * as dynamic destinations.
031 *
032 * @author Juergen Hoeller
033 * @since 1.1
034 * @see javax.jms.Session#createQueue
035 * @see javax.jms.Session#createTopic
036 */
037public class DynamicDestinationResolver implements DestinationResolver {
038
039        /**
040         * Resolve the specified destination name as a dynamic destination.
041         * @param session the current JMS Session
042         * @param destinationName the name of the destination
043         * @param pubSubDomain {@code true} if the domain is pub-sub, {@code false} if P2P
044         * @return the JMS destination (either a topic or a queue)
045         * @throws javax.jms.JMSException if resolution failed
046         * @see #resolveTopic(javax.jms.Session, String)
047         * @see #resolveQueue(javax.jms.Session, String)
048         */
049        @Override
050        public Destination resolveDestinationName(@Nullable Session session, String destinationName, boolean pubSubDomain)
051                        throws JMSException {
052
053                Assert.notNull(session, "Session must not be null");
054                Assert.notNull(destinationName, "Destination name must not be null");
055                if (pubSubDomain) {
056                        return resolveTopic(session, destinationName);
057                }
058                else {
059                        return resolveQueue(session, destinationName);
060                }
061        }
062
063
064        /**
065         * Resolve the given destination name to a {@link Topic}.
066         * @param session the current JMS Session
067         * @param topicName the name of the desired {@link Topic}
068         * @return the JMS {@link Topic}
069         * @throws javax.jms.JMSException if resolution failed
070         * @see Session#createTopic(String)
071         */
072        protected Topic resolveTopic(Session session, String topicName) throws JMSException {
073                return session.createTopic(topicName);
074        }
075
076        /**
077         * Resolve the given destination name to a {@link Queue}.
078         * @param session the current JMS Session
079         * @param queueName the name of the desired {@link Queue}
080         * @return the JMS {@link Queue}
081         * @throws javax.jms.JMSException if resolution failed
082         * @see Session#createQueue(String)
083         */
084        protected Queue resolveQueue(Session session, String queueName) throws JMSException {
085                return session.createQueue(queueName);
086        }
087
088}