001/*
002 * Copyright 2002-2017 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.cache.transaction;
018
019import java.util.Collection;
020
021import org.springframework.beans.factory.InitializingBean;
022import org.springframework.cache.Cache;
023import org.springframework.cache.CacheManager;
024import org.springframework.lang.Nullable;
025import org.springframework.util.Assert;
026
027/**
028 * Proxy for a target {@link CacheManager}, exposing transaction-aware {@link Cache} objects
029 * which synchronize their {@link Cache#put} operations with Spring-managed transactions
030 * (through Spring's {@link org.springframework.transaction.support.TransactionSynchronizationManager},
031 * performing the actual cache put operation only in the after-commit phase of a successful transaction.
032 * If no transaction is active, {@link Cache#put} operations will be performed immediately, as usual.
033 *
034 * @author Juergen Hoeller
035 * @since 3.2
036 * @see #setTargetCacheManager
037 * @see TransactionAwareCacheDecorator
038 * @see org.springframework.transaction.support.TransactionSynchronizationManager
039 */
040public class TransactionAwareCacheManagerProxy implements CacheManager, InitializingBean {
041
042        @Nullable
043        private CacheManager targetCacheManager;
044
045
046        /**
047         * Create a new TransactionAwareCacheManagerProxy, setting the target CacheManager
048         * through the {@link #setTargetCacheManager} bean property.
049         */
050        public TransactionAwareCacheManagerProxy() {
051        }
052
053        /**
054         * Create a new TransactionAwareCacheManagerProxy for the given target CacheManager.
055         * @param targetCacheManager the target CacheManager to proxy
056         */
057        public TransactionAwareCacheManagerProxy(CacheManager targetCacheManager) {
058                Assert.notNull(targetCacheManager, "Target CacheManager must not be null");
059                this.targetCacheManager = targetCacheManager;
060        }
061
062
063        /**
064         * Set the target CacheManager to proxy.
065         */
066        public void setTargetCacheManager(CacheManager targetCacheManager) {
067                this.targetCacheManager = targetCacheManager;
068        }
069
070        @Override
071        public void afterPropertiesSet() {
072                if (this.targetCacheManager == null) {
073                        throw new IllegalArgumentException("Property 'targetCacheManager' is required");
074                }
075        }
076
077
078        @Override
079        @Nullable
080        public Cache getCache(String name) {
081                Assert.state(this.targetCacheManager != null, "No target CacheManager set");
082                Cache targetCache = this.targetCacheManager.getCache(name);
083                return (targetCache != null ? new TransactionAwareCacheDecorator(targetCache) : null);
084        }
085
086        @Override
087        public Collection<String> getCacheNames() {
088                Assert.state(this.targetCacheManager != null, "No target CacheManager set");
089                return this.targetCacheManager.getCacheNames();
090        }
091
092}