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.messaging.core;
018
019import java.util.Map;
020
021import org.springframework.messaging.Message;
022import org.springframework.util.Assert;
023
024/**
025 * An extension of {@link AbstractMessagingTemplate} that adds operations for sending
026 * messages to a resolvable destination name as defined by the following interfaces:
027 * <ul>
028 * <li>{@link DestinationResolvingMessageSendingOperations}</li>
029 * <li>{@link DestinationResolvingMessageReceivingOperations}</li>
030 * <li>{@link DestinationResolvingMessageRequestReplyOperations}</li>
031 * </ul>
032 *
033 * @author Mark Fisher
034 * @author Rossen Stoyanchev
035 * @since 4.0
036 */
037public abstract class AbstractDestinationResolvingMessagingTemplate<D> extends AbstractMessagingTemplate<D>
038                implements DestinationResolvingMessageSendingOperations<D>,
039                DestinationResolvingMessageReceivingOperations<D>,
040                DestinationResolvingMessageRequestReplyOperations<D> {
041
042        private volatile DestinationResolver<D> destinationResolver;
043
044
045        /**
046         * Configure the {@link DestinationResolver} to use to resolve String destination
047         * names into actual destinations of type {@code <D>}.
048         * <p>This field does not have a default setting. If not configured, methods that
049         * require resolving a destination name will raise an {@link IllegalArgumentException}.
050         * @param destinationResolver the destination resolver to use
051         */
052        public void setDestinationResolver(DestinationResolver<D> destinationResolver) {
053                Assert.notNull(destinationResolver, "'destinationResolver' is required");
054                this.destinationResolver = destinationResolver;
055        }
056
057        /**
058         * Return the configured destination resolver.
059         */
060        public DestinationResolver<D> getDestinationResolver() {
061                return this.destinationResolver;
062        }
063
064
065        @Override
066        public void send(String destinationName, Message<?> message) {
067                D destination = resolveDestination(destinationName);
068                doSend(destination, message);
069        }
070
071        protected final D resolveDestination(String destinationName) {
072                Assert.state(this.destinationResolver != null, "DestinationResolver is required to resolve destination names");
073                return this.destinationResolver.resolveDestination(destinationName);
074        }
075
076        @Override
077        public <T> void convertAndSend(String destinationName, T payload) {
078                convertAndSend(destinationName, payload, null, null);
079        }
080
081        @Override
082        public <T> void convertAndSend(String destinationName, T payload, Map<String, Object> headers) {
083                convertAndSend(destinationName, payload, headers, null);
084        }
085
086        @Override
087        public <T> void convertAndSend(String destinationName, T payload, MessagePostProcessor postProcessor) {
088                convertAndSend(destinationName, payload, null, postProcessor);
089        }
090
091        @Override
092        public <T> void convertAndSend(String destinationName, T payload, Map<String, Object> headers, MessagePostProcessor postProcessor) {
093                D destination = resolveDestination(destinationName);
094                super.convertAndSend(destination, payload, headers, postProcessor);
095        }
096
097        @Override
098        public Message<?> receive(String destinationName) {
099                D destination = resolveDestination(destinationName);
100                return super.receive(destination);
101        }
102
103        @Override
104        public <T> T receiveAndConvert(String destinationName, Class<T> targetClass) {
105                D destination = resolveDestination(destinationName);
106                return super.receiveAndConvert(destination, targetClass);
107        }
108
109        @Override
110        public Message<?> sendAndReceive(String destinationName, Message<?> requestMessage) {
111                D destination = resolveDestination(destinationName);
112                return super.sendAndReceive(destination, requestMessage);
113        }
114
115        @Override
116        public <T> T convertSendAndReceive(String destinationName, Object request, Class<T> targetClass) {
117                D destination = resolveDestination(destinationName);
118                return super.convertSendAndReceive(destination, request, targetClass);
119        }
120
121        @Override
122        public <T> T convertSendAndReceive(String destinationName, Object request, Map<String, Object> headers,
123                        Class<T> targetClass) {
124
125                D destination = resolveDestination(destinationName);
126                return super.convertSendAndReceive(destination, request, headers, targetClass);
127        }
128
129        @Override
130        public <T> T convertSendAndReceive(String destinationName, Object request, Class<T> targetClass,
131                        MessagePostProcessor postProcessor) {
132
133                D destination = resolveDestination(destinationName);
134                return super.convertSendAndReceive(destination, request, targetClass, postProcessor);
135        }
136
137        @Override
138        public <T> T convertSendAndReceive(String destinationName, Object request, Map<String, Object> headers,
139                        Class<T> targetClass, MessagePostProcessor postProcessor) {
140
141                D destination = resolveDestination(destinationName);
142                return super.convertSendAndReceive(destination, request, headers, targetClass, postProcessor);
143        }
144
145}