|
| 1 | +/* |
| 2 | + * Copyright 2011-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.cache; |
| 18 | + |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import com.gemstone.gemfire.cache.Cache; |
| 24 | +import com.gemstone.gemfire.cache.GemFireCache; |
| 25 | +import com.gemstone.gemfire.cache.Region; |
| 26 | +import com.gemstone.gemfire.cache.client.ClientCache; |
| 27 | + |
| 28 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 31 | +import org.springframework.cache.CacheManager; |
| 32 | +import org.springframework.context.annotation.Bean; |
| 33 | +import org.springframework.context.annotation.Conditional; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | +import org.springframework.data.gemfire.CacheFactoryBean; |
| 36 | +import org.springframework.data.gemfire.client.ClientCacheFactoryBean; |
| 37 | +import org.springframework.data.gemfire.support.GemfireCacheManager; |
| 38 | +import org.springframework.util.Assert; |
| 39 | +import org.springframework.util.CollectionUtils; |
| 40 | + |
| 41 | +/** |
| 42 | + * GemFire cache configuration. |
| 43 | + * |
| 44 | + * @author John Blum |
| 45 | + * @see com.gemstone.gemfire.cache.Cache |
| 46 | + * @see com.gemstone.gemfire.cache.GemFireCache |
| 47 | + * @see com.gemstone.gemfire.cache.client.ClientCache |
| 48 | + * @see org.springframework.context.annotation.Bean |
| 49 | + * @see org.springframework.context.annotation.Configuration |
| 50 | + * @see org.springframework.data.gemfire.support.GemfireCacheManager |
| 51 | + * @since 1.5.0 |
| 52 | + */ |
| 53 | +@Configuration |
| 54 | +@ConditionalOnBean({ CacheFactoryBean.class, Cache.class, |
| 55 | + ClientCacheFactoryBean.class, ClientCache.class }) |
| 56 | +@ConditionalOnClass(GemfireCacheManager.class) |
| 57 | +@ConditionalOnMissingBean(CacheManager.class) |
| 58 | +@Conditional(CacheCondition.class) |
| 59 | +class GeodeCacheConfiguration { |
| 60 | + |
| 61 | + private final CacheProperties cacheProperties; |
| 62 | + |
| 63 | + private final CacheManagerCustomizers cacheManagerCustomizers; |
| 64 | + |
| 65 | + GeodeCacheConfiguration(CacheProperties cacheProperties, |
| 66 | + CacheManagerCustomizers cacheManagerCustomizers) { |
| 67 | + |
| 68 | + this.cacheProperties = cacheProperties; |
| 69 | + this.cacheManagerCustomizers = cacheManagerCustomizers; |
| 70 | + } |
| 71 | + |
| 72 | + @Bean |
| 73 | + public GemfireCacheManager cacheManager(GemFireCache gemfireCache) { |
| 74 | + GemfireCacheManager cacheManager = new GemfireCacheManager(); |
| 75 | + |
| 76 | + cacheManager.setCache((Cache) gemfireCache); |
| 77 | + |
| 78 | + Set<Region<?, ?>> regions = getRegions(gemfireCache); |
| 79 | + |
| 80 | + if (!CollectionUtils.isEmpty(regions)) { |
| 81 | + cacheManager.setRegions(regions); |
| 82 | + } |
| 83 | + |
| 84 | + return this.cacheManagerCustomizers.customize(cacheManager); |
| 85 | + } |
| 86 | + |
| 87 | + private Set<Region<?, ?>> getRegions(GemFireCache gemfireCache) { |
| 88 | + List<String> cacheNames = this.cacheProperties.getCacheNames(); |
| 89 | + Set<Region<?, ?>> regions = new HashSet<Region<?, ?>>(cacheNames.size()); |
| 90 | + |
| 91 | + for (String cacheName : cacheNames) { |
| 92 | + Region<?, ?> region = gemfireCache.getRegion(cacheName); |
| 93 | + Assert.notNull(region, String.format("No Region for cache name [%s] was found", cacheName)); |
| 94 | + regions.add(region); |
| 95 | + } |
| 96 | + |
| 97 | + return regions; |
| 98 | + } |
| 99 | +} |
0 commit comments