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.cache.jcache;
018
019import java.net.URI;
020import java.util.Properties;
021
022import javax.cache.CacheManager;
023import javax.cache.Caching;
024
025import org.springframework.beans.factory.BeanClassLoaderAware;
026import org.springframework.beans.factory.DisposableBean;
027import org.springframework.beans.factory.FactoryBean;
028import org.springframework.beans.factory.InitializingBean;
029import org.springframework.lang.Nullable;
030
031/**
032 * {@link FactoryBean} for a JCache {@link CacheManager javax.cache.CacheManager},
033 * obtaining a pre-defined {@code CacheManager} by name through the standard
034 * JCache {@link Caching javax.cache.Caching} class.
035 *
036 * <p>Note: This class has been updated for JCache 1.0, as of Spring 4.0.
037 *
038 * @author Juergen Hoeller
039 * @since 3.2
040 * @see javax.cache.Caching#getCachingProvider()
041 * @see javax.cache.spi.CachingProvider#getCacheManager()
042 */
043public class JCacheManagerFactoryBean
044                implements FactoryBean<CacheManager>, BeanClassLoaderAware, InitializingBean, DisposableBean {
045
046        @Nullable
047        private URI cacheManagerUri;
048
049        @Nullable
050        private Properties cacheManagerProperties;
051
052        @Nullable
053        private ClassLoader beanClassLoader;
054
055        @Nullable
056        private CacheManager cacheManager;
057
058
059        /**
060         * Specify the URI for the desired {@code CacheManager}.
061         * <p>Default is {@code null} (i.e. JCache's default).
062         */
063        public void setCacheManagerUri(@Nullable URI cacheManagerUri) {
064                this.cacheManagerUri = cacheManagerUri;
065        }
066
067        /**
068         * Specify properties for the to-be-created {@code CacheManager}.
069         * <p>Default is {@code null} (i.e. no special properties to apply).
070         * @see javax.cache.spi.CachingProvider#getCacheManager(URI, ClassLoader, Properties)
071         */
072        public void setCacheManagerProperties(@Nullable Properties cacheManagerProperties) {
073                this.cacheManagerProperties = cacheManagerProperties;
074        }
075
076        @Override
077        public void setBeanClassLoader(ClassLoader classLoader) {
078                this.beanClassLoader = classLoader;
079        }
080
081        @Override
082        public void afterPropertiesSet() {
083                this.cacheManager = Caching.getCachingProvider().getCacheManager(
084                                this.cacheManagerUri, this.beanClassLoader, this.cacheManagerProperties);
085        }
086
087
088        @Override
089        @Nullable
090        public CacheManager getObject() {
091                return this.cacheManager;
092        }
093
094        @Override
095        public Class<?> getObjectType() {
096                return (this.cacheManager != null ? this.cacheManager.getClass() : CacheManager.class);
097        }
098
099        @Override
100        public boolean isSingleton() {
101                return true;
102        }
103
104
105        @Override
106        public void destroy() {
107                if (this.cacheManager != null) {
108                        this.cacheManager.close();
109                }
110        }
111
112}