001/*
002 * Copyright 2012-2017 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 * @author Raja Kolli
044 * @since 1.3.0
045 */
046@Configuration
047@ConditionalOnClass(SpringEmbeddedCacheManager.class)
048@ConditionalOnMissingBean(CacheManager.class)
049@Conditional(CacheCondition.class)
050public class InfinispanCacheConfiguration {
051
052        private final CacheProperties cacheProperties;
053
054        private final CacheManagerCustomizers customizers;
055
056        private final ConfigurationBuilder defaultConfigurationBuilder;
057
058        public InfinispanCacheConfiguration(CacheProperties cacheProperties,
059                        CacheManagerCustomizers customizers,
060                        ObjectProvider<ConfigurationBuilder> defaultConfigurationBuilder) {
061                this.cacheProperties = cacheProperties;
062                this.customizers = customizers;
063                this.defaultConfigurationBuilder = defaultConfigurationBuilder.getIfAvailable();
064        }
065
066        @Bean
067        public SpringEmbeddedCacheManager cacheManager(
068                        EmbeddedCacheManager embeddedCacheManager) {
069                SpringEmbeddedCacheManager cacheManager = new SpringEmbeddedCacheManager(
070                                embeddedCacheManager);
071                return this.customizers.customize(cacheManager);
072        }
073
074        @Bean(destroyMethod = "stop")
075        @ConditionalOnMissingBean
076        public EmbeddedCacheManager infinispanCacheManager() throws IOException {
077                EmbeddedCacheManager cacheManager = createEmbeddedCacheManager();
078                List<String> cacheNames = this.cacheProperties.getCacheNames();
079                if (!CollectionUtils.isEmpty(cacheNames)) {
080                        cacheNames.forEach((cacheName) -> cacheManager.defineConfiguration(cacheName,
081                                        getDefaultCacheConfiguration()));
082                }
083                return cacheManager;
084        }
085
086        private EmbeddedCacheManager createEmbeddedCacheManager() throws IOException {
087                Resource location = this.cacheProperties
088                                .resolveConfigLocation(this.cacheProperties.getInfinispan().getConfig());
089                if (location != null) {
090                        try (InputStream in = location.getInputStream()) {
091                                return new DefaultCacheManager(in);
092                        }
093                }
094                return new DefaultCacheManager();
095        }
096
097        private org.infinispan.configuration.cache.Configuration getDefaultCacheConfiguration() {
098                if (this.defaultConfigurationBuilder != null) {
099                        return this.defaultConfigurationBuilder.build();
100                }
101                return new ConfigurationBuilder().build();
102        }
103
104}