001/*
002 * Copyright 2002-2012 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.concurrent;
018
019import java.util.concurrent.ConcurrentMap;
020
021import org.springframework.beans.factory.BeanNameAware;
022import org.springframework.beans.factory.FactoryBean;
023import org.springframework.beans.factory.InitializingBean;
024import org.springframework.util.StringUtils;
025
026/**
027 * {@link FactoryBean} for easy configuration of a {@link ConcurrentMapCache}
028 * when used within a Spring container. Can be configured through bean properties;
029 * uses the assigned Spring bean name as the default cache name.
030 *
031 * <p>Useful for testing or simple caching scenarios, typically in combination
032 * with {@link org.springframework.cache.support.SimpleCacheManager} or
033 * dynamically through {@link ConcurrentMapCacheManager}.
034 *
035 * @author Costin Leau
036 * @author Juergen Hoeller
037 * @since 3.1
038 */
039public class ConcurrentMapCacheFactoryBean
040                implements FactoryBean<ConcurrentMapCache>, BeanNameAware, InitializingBean {
041
042        private String name = "";
043
044        private ConcurrentMap<Object, Object> store;
045
046        private boolean allowNullValues = true;
047
048        private ConcurrentMapCache cache;
049
050
051        /**
052         * Specify the name of the cache.
053         * <p>Default is "" (empty String).
054         */
055        public void setName(String name) {
056                this.name = name;
057        }
058
059        /**
060         * Specify the ConcurrentMap to use as an internal store
061         * (possibly pre-populated).
062         * <p>Default is a standard {@link java.util.concurrent.ConcurrentHashMap}.
063         */
064        public void setStore(ConcurrentMap<Object, Object> store) {
065                this.store = store;
066        }
067
068        /**
069         * Set whether to allow {@code null} values
070         * (adapting them to an internal null holder value).
071         * <p>Default is "true".
072         */
073        public void setAllowNullValues(boolean allowNullValues) {
074                this.allowNullValues = allowNullValues;
075        }
076
077        @Override
078        public void setBeanName(String beanName) {
079                if (!StringUtils.hasLength(this.name)) {
080                        setName(beanName);
081                }
082        }
083
084        @Override
085        public void afterPropertiesSet() {
086                this.cache = (this.store != null ? new ConcurrentMapCache(this.name, this.store, this.allowNullValues) :
087                                new ConcurrentMapCache(this.name, this.allowNullValues));
088        }
089
090
091        @Override
092        public ConcurrentMapCache getObject() {
093                return this.cache;
094        }
095
096        @Override
097        public Class<?> getObjectType() {
098                return ConcurrentMapCache.class;
099        }
100
101        @Override
102        public boolean isSingleton() {
103                return true;
104        }
105
106}