001/*
002 * Copyright 2002-2018 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;
020import java.util.concurrent.ConcurrentHashMap;
021
022import org.springframework.beans.factory.InitializingBean;
023import org.springframework.lang.Nullable;
024import org.springframework.util.Assert;
025
026/**
027 * {@link DestinationResolver} implementation that proxies a target DestinationResolver,
028 * caching its {@link #resolveDestination} results. Such caching is particularly useful
029 * if the destination resolving process is expensive (e.g. the destination has to be
030 * resolved through an external system) and the resolution results are stable anyway.
031 *
032 * @author Agim Emruli
033 * @author Juergen Hoeller
034 * @since 4.1
035 * @param <D> the destination type
036 * @see DestinationResolver#resolveDestination
037 */
038public class CachingDestinationResolverProxy<D> implements DestinationResolver<D>, InitializingBean {
039
040        private final Map<String, D> resolvedDestinationCache = new ConcurrentHashMap<>();
041
042        @Nullable
043        private DestinationResolver<D> targetDestinationResolver;
044
045
046        /**
047         * Create a new CachingDestinationResolverProxy, setting the target DestinationResolver
048         * through the {@link #setTargetDestinationResolver} bean property.
049         */
050        public CachingDestinationResolverProxy() {
051        }
052
053        /**
054         * Create a new CachingDestinationResolverProxy using the given target
055         * DestinationResolver to actually resolve destinations.
056         * @param targetDestinationResolver the target DestinationResolver to delegate to
057         */
058        public CachingDestinationResolverProxy(DestinationResolver<D> targetDestinationResolver) {
059                Assert.notNull(targetDestinationResolver, "Target DestinationResolver must not be null");
060                this.targetDestinationResolver = targetDestinationResolver;
061        }
062
063
064        /**
065         * Set the target DestinationResolver to delegate to.
066         */
067        public void setTargetDestinationResolver(DestinationResolver<D> targetDestinationResolver) {
068                this.targetDestinationResolver = targetDestinationResolver;
069        }
070
071        @Override
072        public void afterPropertiesSet() {
073                if (this.targetDestinationResolver == null) {
074                        throw new IllegalArgumentException("Property 'targetDestinationResolver' is required");
075                }
076        }
077
078
079        /**
080         * Resolves and caches destinations if successfully resolved by the target
081         * DestinationResolver implementation.
082         * @param name the destination name to be resolved
083         * @return the currently resolved destination or an already cached destination
084         * @throws DestinationResolutionException if the target DestinationResolver
085         * reports an error during destination resolution
086         */
087        @Override
088        public D resolveDestination(String name) throws DestinationResolutionException {
089                D destination = this.resolvedDestinationCache.get(name);
090                if (destination == null) {
091                        Assert.state(this.targetDestinationResolver != null, "No target DestinationResolver set");
092                        destination = this.targetDestinationResolver.resolveDestination(name);
093                        this.resolvedDestinationCache.put(name, destination);
094                }
095                return destination;
096        }
097
098}