001/*
002 * Copyright 2002-2020 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.jcache;
018
019import java.util.Collection;
020import java.util.LinkedHashSet;
021import javax.cache.CacheManager;
022import javax.cache.Caching;
023
024import org.springframework.cache.Cache;
025import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;
026
027/**
028 * {@link org.springframework.cache.CacheManager} implementation
029 * backed by a JCache {@link CacheManager javax.cache.CacheManager}.
030 *
031 * <p>Note: This class has been updated for JCache 1.0, as of Spring 4.0.
032 *
033 * @author Juergen Hoeller
034 * @author Stephane Nicoll
035 * @since 3.2
036 */
037public class JCacheCacheManager extends AbstractTransactionSupportingCacheManager {
038
039        private CacheManager cacheManager;
040
041        private boolean allowNullValues = true;
042
043
044        /**
045         * Create a new {@code JCacheCacheManager} without a backing JCache
046         * {@link CacheManager javax.cache.CacheManager}.
047         * <p>The backing JCache {@code javax.cache.CacheManager} can be set via the
048         * {@link #setCacheManager} bean property.
049         */
050        public JCacheCacheManager() {
051        }
052
053        /**
054         * Create a new {@code JCacheCacheManager} for the given backing JCache
055         * {@link CacheManager javax.cache.CacheManager}.
056         * @param cacheManager the backing JCache {@code javax.cache.CacheManager}
057         */
058        public JCacheCacheManager(CacheManager cacheManager) {
059                this.cacheManager = cacheManager;
060        }
061
062
063        /**
064         * Set the backing JCache {@link CacheManager javax.cache.CacheManager}.
065         */
066        public void setCacheManager(CacheManager cacheManager) {
067                this.cacheManager = cacheManager;
068        }
069
070        /**
071         * Return the backing JCache {@link CacheManager javax.cache.CacheManager}.
072         */
073        public CacheManager getCacheManager() {
074                return this.cacheManager;
075        }
076
077        /**
078         * Specify whether to accept and convert {@code null} values for all caches
079         * in this cache manager.
080         * <p>Default is "true", despite JSR-107 itself not supporting {@code null} values.
081         * An internal holder object will be used to store user-level {@code null}s.
082         */
083        public void setAllowNullValues(boolean allowNullValues) {
084                this.allowNullValues = allowNullValues;
085        }
086
087        /**
088         * Return whether this cache manager accepts and converts {@code null} values
089         * for all of its caches.
090         */
091        public boolean isAllowNullValues() {
092                return this.allowNullValues;
093        }
094
095        @Override
096        public void afterPropertiesSet() {
097                if (getCacheManager() == null) {
098                        setCacheManager(Caching.getCachingProvider().getCacheManager());
099                }
100                super.afterPropertiesSet();
101        }
102
103
104        @Override
105        protected Collection<Cache> loadCaches() {
106                Collection<Cache> caches = new LinkedHashSet<Cache>();
107                for (String cacheName : getCacheManager().getCacheNames()) {
108                        javax.cache.Cache<Object, Object> jcache = getCacheManager().getCache(cacheName);
109                        caches.add(new JCacheCache(jcache, isAllowNullValues()));
110                }
111                return caches;
112        }
113
114        @Override
115        protected Cache getMissingCache(String name) {
116                // Check the JCache cache again (in case the cache was added at runtime)
117                javax.cache.Cache<Object, Object> jcache = getCacheManager().getCache(name);
118                if (jcache != null) {
119                        return new JCacheCache(jcache, isAllowNullValues());
120                }
121                return null;
122        }
123
124}