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