Skip to content

Commit 207ab3e

Browse files
committed
Merge branch '1.4.x' into 1.5.x
2 parents 50c0204 + 07c56c6 commit 207ab3e

File tree

6 files changed

+239
-49
lines changed

6 files changed

+239
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2012-2017 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.io.IOException;
20+
import java.net.URI;
21+
import java.util.Properties;
22+
23+
import com.hazelcast.core.HazelcastInstance;
24+
25+
import org.springframework.beans.factory.ObjectProvider;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.core.io.Resource;
30+
31+
/**
32+
* JCache customization for Hazelcast.
33+
*
34+
* @author Stephane Nicoll
35+
*/
36+
@Configuration
37+
@ConditionalOnClass(HazelcastInstance.class)
38+
class HazelcastJCacheCustomizationConfiguration {
39+
40+
@Bean
41+
public HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(
42+
ObjectProvider<HazelcastInstance> hazelcastInstance) {
43+
return new HazelcastPropertiesCustomizer(
44+
hazelcastInstance.getIfUnique());
45+
}
46+
47+
private static class HazelcastPropertiesCustomizer
48+
implements JCachePropertiesCustomizer {
49+
50+
private final HazelcastInstance hazelcastInstance;
51+
52+
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance) {
53+
this.hazelcastInstance = hazelcastInstance;
54+
}
55+
56+
@Override
57+
public void customize(CacheProperties cacheProperties, Properties properties) {
58+
Resource configLocation = cacheProperties
59+
.resolveConfigLocation(cacheProperties.getJcache().getConfig());
60+
if (configLocation != null) {
61+
// Hazelcast does not use the URI as a mean to specify a custom config.
62+
properties.setProperty("hazelcast.config.location",
63+
toUri(configLocation).toString());
64+
}
65+
else if (this.hazelcastInstance != null) {
66+
properties.put("hazelcast.instance.itself", this.hazelcastInstance);
67+
}
68+
}
69+
70+
private static URI toUri(Resource config) {
71+
try {
72+
return config.getURI();
73+
}
74+
catch (IOException ex) {
75+
throw new IllegalArgumentException(
76+
"Could not get URI from " + config, ex);
77+
}
78+
}
79+
80+
}
81+
82+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.context.annotation.ConditionContext;
4141
import org.springframework.context.annotation.Conditional;
4242
import org.springframework.context.annotation.Configuration;
43+
import org.springframework.context.annotation.Import;
4344
import org.springframework.core.Ordered;
4445
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
4546
import org.springframework.core.annotation.Order;
@@ -59,6 +60,7 @@
5960
@ConditionalOnMissingBean(org.springframework.cache.CacheManager.class)
6061
@Conditional({ CacheCondition.class,
6162
JCacheCacheConfiguration.JCacheAvailableCondition.class })
63+
@Import(HazelcastJCacheCustomizationConfiguration.class)
6264
class JCacheCacheConfiguration {
6365

6466
private final CacheProperties cacheProperties;
@@ -69,14 +71,18 @@ class JCacheCacheConfiguration {
6971

7072
private final List<JCacheManagerCustomizer> cacheManagerCustomizers;
7173

74+
private final List<JCachePropertiesCustomizer> cachePropertiesCustomizers;
75+
7276
JCacheCacheConfiguration(CacheProperties cacheProperties,
7377
CacheManagerCustomizers customizers,
7478
ObjectProvider<javax.cache.configuration.Configuration<?, ?>> defaultCacheConfiguration,
75-
ObjectProvider<List<JCacheManagerCustomizer>> cacheManagerCustomizers) {
79+
ObjectProvider<List<JCacheManagerCustomizer>> cacheManagerCustomizers,
80+
ObjectProvider<List<JCachePropertiesCustomizer>> cachePropertiesCustomizers) {
7681
this.cacheProperties = cacheProperties;
7782
this.customizers = customizers;
7883
this.defaultCacheConfiguration = defaultCacheConfiguration.getIfAvailable();
7984
this.cacheManagerCustomizers = cacheManagerCustomizers.getIfAvailable();
85+
this.cachePropertiesCustomizers = cachePropertiesCustomizers.getIfAvailable();
8086
}
8187

8288
@Bean
@@ -102,14 +108,14 @@ public CacheManager jCacheCacheManager() throws IOException {
102108
private CacheManager createCacheManager() throws IOException {
103109
CachingProvider cachingProvider = getCachingProvider(
104110
this.cacheProperties.getJcache().getProvider());
111+
Properties properties = createCacheManagerProperties();
105112
Resource configLocation = this.cacheProperties
106113
.resolveConfigLocation(this.cacheProperties.getJcache().getConfig());
107114
if (configLocation != null) {
108115
return cachingProvider.getCacheManager(configLocation.getURI(),
109-
cachingProvider.getDefaultClassLoader(),
110-
createCacheManagerProperties(configLocation));
116+
cachingProvider.getDefaultClassLoader(), properties);
111117
}
112-
return cachingProvider.getCacheManager();
118+
return cachingProvider.getCacheManager(null, null, properties);
113119
}
114120

115121
private CachingProvider getCachingProvider(String cachingProviderFqn) {
@@ -119,12 +125,13 @@ private CachingProvider getCachingProvider(String cachingProviderFqn) {
119125
return Caching.getCachingProvider();
120126
}
121127

122-
private Properties createCacheManagerProperties(Resource configLocation)
123-
throws IOException {
128+
private Properties createCacheManagerProperties() {
124129
Properties properties = new Properties();
125-
// Hazelcast does not use the URI as a mean to specify a custom config.
126-
properties.setProperty("hazelcast.config.location",
127-
configLocation.getURI().toString());
130+
if (this.cachePropertiesCustomizers != null) {
131+
for (JCachePropertiesCustomizer customizer : this.cachePropertiesCustomizers) {
132+
customizer.customize(this.cacheProperties, properties);
133+
}
134+
}
128135
return properties;
129136
}
130137

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2017 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.Properties;
20+
21+
import javax.cache.CacheManager;
22+
import javax.cache.spi.CachingProvider;
23+
24+
/**
25+
* Callback interface that can be implemented by beans wishing to customize the properties
26+
* used by the {@link CachingProvider} to create the {@link CacheManager}.
27+
*
28+
* @author Stephane Nicoll
29+
*/
30+
interface JCachePropertiesCustomizer {
31+
32+
/**
33+
* Customize the properties.
34+
* @param cacheProperties the cache properties
35+
* @param properties the current properties
36+
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties)
37+
*/
38+
void customize(CacheProperties cacheProperties, Properties properties);
39+
40+
}

0 commit comments

Comments
 (0)