001/*
002 * Copyright 2012-2016 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 *      http://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.boot.autoconfigure.cache;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.List;
022
023import org.infinispan.configuration.cache.ConfigurationBuilder;
024import org.infinispan.manager.DefaultCacheManager;
025import org.infinispan.manager.EmbeddedCacheManager;
026import org.infinispan.spring.provider.SpringEmbeddedCacheManager;
027
028import org.springframework.beans.factory.ObjectProvider;
029import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
030import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
031import org.springframework.cache.CacheManager;
032import org.springframework.context.annotation.Bean;
033import org.springframework.context.annotation.Conditional;
034import org.springframework.context.annotation.Configuration;
035import org.springframework.core.io.Resource;
036import org.springframework.util.CollectionUtils;
037
038/**
039 * Infinispan cache configuration.
040 *
041 * @author Eddú Meléndez
042 * @author Stephane Nicoll
043 * @since 1.3.0
044 */
045@Configuration
046@ConditionalOnClass(SpringEmbeddedCacheManager.class)
047@ConditionalOnMissingBean(CacheManager.class)
048@Conditional(CacheCondition.class)
049public class InfinispanCacheConfiguration {
050
051        private final CacheProperties cacheProperties;
052
053        private final CacheManagerCustomizers customizers;
054
055        private final ConfigurationBuilder defaultConfigurationBuilder;
056
057        public InfinispanCacheConfiguration(CacheProperties cacheProperties,
058                        CacheManagerCustomizers customizers,
059                        ObjectProvider<ConfigurationBuilder> defaultConfigurationBuilder) {
060                this.cacheProperties = cacheProperties;
061                this.customizers = customizers;
062                this.defaultConfigurationBuilder = defaultConfigurationBuilder.getIfAvailable();
063        }
064
065        @Bean
066        public SpringEmbeddedCacheManager cacheManager(
067                        EmbeddedCacheManager embeddedCacheManager) {
068                SpringEmbeddedCacheManager cacheManager = new SpringEmbeddedCacheManager(
069                                embeddedCacheManager);
070                return this.customizers.customize(cacheManager);
071        }
072
073        @Bean(destroyMethod = "stop")
074        @ConditionalOnMissingBean
075        public EmbeddedCacheManager infinispanCacheManager() throws IOException {
076                EmbeddedCacheManager cacheManager = createEmbeddedCacheManager();
077                List<String> cacheNames = this.cacheProperties.getCacheNames();
078                if (!CollectionUtils.isEmpty(cacheNames)) {
079                        for (String cacheName : cacheNames) {
080                                cacheManager.defineConfiguration(cacheName,
081                                                getDefaultCacheConfiguration());
082                        }
083                }
084                return cacheManager;
085        }
086
087        private EmbeddedCacheManager createEmbeddedCacheManager() throws IOException {
088                Resource location = this.cacheProperties
089                                .resolveConfigLocation(this.cacheProperties.getInfinispan().getConfig());
090                if (location != null) {
091                        InputStream in = location.getInputStream();
092                        try {
093                                return new DefaultCacheManager(in);
094                        }
095                        finally {
096                                in.close();
097                        }
098                }
099                return new DefaultCacheManager();
100        }
101
102        private org.infinispan.configuration.cache.Configuration getDefaultCacheConfiguration() {
103                if (this.defaultConfigurationBuilder != null) {
104                        return this.defaultConfigurationBuilder.build();
105                }
106                return new ConfigurationBuilder().build();
107        }
108
109}