Skip to content

Commit db41fb1

Browse files
committed
Polish Hazelcast auto-configuration
Extract a HazelcastInstanceFactory class and cleanup some formatting. See gh-2942
1 parent 138d667 commit db41fb1

File tree

10 files changed

+176
-127
lines changed

10 files changed

+176
-127
lines changed

Diff for: spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import net.sf.ehcache.CacheManager;
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
23-
import org.springframework.boot.autoconfigure.condition.ResourceCondition;
2423
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2524
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25+
import org.springframework.boot.autoconfigure.condition.ResourceCondition;
2626
import org.springframework.cache.ehcache.EhCacheCacheManager;
2727
import org.springframework.cache.ehcache.EhCacheManagerUtils;
2828
import org.springframework.context.annotation.Bean;

Diff for: spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java

+25-24
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,38 @@
1919
import java.io.Closeable;
2020
import java.io.IOException;
2121

22-
import com.hazelcast.core.Hazelcast;
23-
import com.hazelcast.core.HazelcastInstance;
24-
import com.hazelcast.spring.cache.HazelcastCacheManager;
25-
2622
import org.springframework.beans.factory.annotation.Autowired;
2723
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2824
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2925
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3026
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
3127
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
3228
import org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition;
29+
import org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory;
3330
import org.springframework.cache.CacheManager;
3431
import org.springframework.context.annotation.Bean;
3532
import org.springframework.context.annotation.Conditional;
3633
import org.springframework.context.annotation.Configuration;
3734
import org.springframework.core.io.Resource;
3835

36+
import com.hazelcast.core.Hazelcast;
37+
import com.hazelcast.core.HazelcastInstance;
38+
import com.hazelcast.spring.cache.HazelcastCacheManager;
39+
3940
/**
40-
* Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that
41-
* has been configured by the general {@link HazelcastAutoConfiguration} or create
42-
* a separate one if the {@code spring.cache.hazelcast.config} property has been set.
41+
* Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that has
42+
* been configured by the general {@link HazelcastAutoConfiguration} or create a separate
43+
* one if the {@code spring.cache.hazelcast.config} property has been set.
4344
* <p>
44-
* If the {@link HazelcastAutoConfiguration} has been disabled, an attempt to configure
45-
* a default {@link HazelcastInstance} is still made, using the same defaults.
45+
* If the {@link HazelcastAutoConfiguration} has been disabled, an attempt to configure a
46+
* default {@link HazelcastInstance} is still made, using the same defaults.
4647
*
4748
* @author Stephane Nicoll
4849
* @since 1.3.0
4950
* @see HazelcastConfigResourceCondition
5051
*/
5152
@Configuration
52-
@ConditionalOnClass({HazelcastInstance.class, HazelcastCacheManager.class})
53+
@ConditionalOnClass({ HazelcastInstance.class, HazelcastCacheManager.class })
5354
@ConditionalOnMissingBean(CacheManager.class)
5455
@Conditional(CacheCondition.class)
5556
@AutoConfigureAfter(HazelcastAutoConfiguration.class)
@@ -63,19 +64,16 @@ static class ExistingHazelcastInstanceConfiguration {
6364
private CacheProperties cacheProperties;
6465

6566
@Bean
66-
public HazelcastCacheManager cacheManager(HazelcastInstance existingHazelcastInstance)
67-
throws IOException {
68-
Resource location = this.cacheProperties
69-
.resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig());
67+
public HazelcastCacheManager cacheManager(
68+
HazelcastInstance existingHazelcastInstance) throws IOException {
69+
Resource config = this.cacheProperties.getHazelcast().getConfig();
70+
Resource location = this.cacheProperties.resolveConfigLocation(config);
7071
if (location != null) {
71-
HazelcastInstance cacheHazelcastInstance =
72-
HazelcastAutoConfiguration.createHazelcastInstance(location);
72+
HazelcastInstance cacheHazelcastInstance = new HazelcastInstanceFactory(
73+
location).getHazelcastInstance();
7374
return new CloseableHazelcastCacheManager(cacheHazelcastInstance);
7475
}
75-
else {
76-
return new HazelcastCacheManager(existingHazelcastInstance);
77-
}
78-
76+
return new HazelcastCacheManager(existingHazelcastInstance);
7977
}
8078
}
8179

@@ -89,10 +87,10 @@ static class DefaultHazelcastInstanceConfiguration {
8987

9088
@Bean
9189
public HazelcastInstance hazelcastInstance() throws IOException {
92-
Resource location = this.cacheProperties
93-
.resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig());
90+
Resource config = this.cacheProperties.getHazelcast().getConfig();
91+
Resource location = this.cacheProperties.resolveConfigLocation(config);
9492
if (location != null) {
95-
HazelcastAutoConfiguration.createHazelcastInstance(location);
93+
new HazelcastInstanceFactory(location).getHazelcastInstance();
9694
}
9795
return Hazelcast.newHazelcastInstance();
9896
}
@@ -116,7 +114,9 @@ public ConfigAvailableCondition() {
116114

117115
}
118116

119-
private static class CloseableHazelcastCacheManager extends HazelcastCacheManager implements Closeable {
117+
private static class CloseableHazelcastCacheManager extends HazelcastCacheManager
118+
implements Closeable {
119+
120120
private final HazelcastInstance hazelcastInstance;
121121

122122
public CloseableHazelcastCacheManager(HazelcastInstance hazelcastInstance) {
@@ -128,6 +128,7 @@ public CloseableHazelcastCacheManager(HazelcastInstance hazelcastInstance) {
128128
public void close() throws IOException {
129129
this.hazelcastInstance.shutdown();
130130
}
131+
131132
}
132133

133134
}

Diff for: spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ResourceCondition.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public ConditionOutcome getMatchOutcome(ConditionContext context,
6060
AnnotatedTypeMetadata metadata) {
6161
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
6262
context.getEnvironment(), this.prefix);
63-
if (resolver.containsProperty(propertyName)) {
64-
return ConditionOutcome.match("A '" + this.prefix + propertyName +"' "
63+
if (resolver.containsProperty(this.propertyName)) {
64+
return ConditionOutcome.match("A '" + this.prefix + this.propertyName + "' "
6565
+ "property is specified");
6666
}
6767
return getResourceOutcome(context, metadata);

Diff for: spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java

+8-41
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
package org.springframework.boot.autoconfigure.hazelcast;
1818

1919
import java.io.IOException;
20-
import java.net.URL;
21-
22-
import com.hazelcast.config.Config;
23-
import com.hazelcast.config.XmlConfigBuilder;
24-
import com.hazelcast.core.Hazelcast;
25-
import com.hazelcast.core.HazelcastInstance;
2620

2721
import org.springframework.beans.factory.annotation.Autowired;
2822
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -34,9 +28,10 @@
3428
import org.springframework.context.annotation.Conditional;
3529
import org.springframework.context.annotation.Configuration;
3630
import org.springframework.core.io.Resource;
37-
import org.springframework.util.Assert;
38-
import org.springframework.util.ResourceUtils;
39-
import org.springframework.util.StringUtils;
31+
32+
import com.hazelcast.config.Config;
33+
import com.hazelcast.core.Hazelcast;
34+
import com.hazelcast.core.HazelcastInstance;
4035

4136
/**
4237
* {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a
@@ -53,37 +48,8 @@
5348
@EnableConfigurationProperties(HazelcastProperties.class)
5449
public class HazelcastAutoConfiguration {
5550

56-
57-
/**
58-
* Create a {@link HazelcastInstance} based on the specified configuration location.
59-
* @param location the location of the configuration file
60-
* @return a {@link HazelcastInstance} for the specified configuration
61-
* @throws IOException the configuration file could not be read
62-
*/
63-
public static HazelcastInstance createHazelcastInstance(Resource location)
64-
throws IOException {
65-
Assert.notNull(location, "Config must not be null");
66-
URL configUrl = location.getURL();
67-
Config config = new XmlConfigBuilder(configUrl).build();
68-
if (ResourceUtils.isFileURL(configUrl)) {
69-
config.setConfigurationFile(location.getFile());
70-
}
71-
else {
72-
config.setConfigurationUrl(configUrl);
73-
}
74-
return createHazelcastInstance(config);
75-
}
76-
77-
private static HazelcastInstance createHazelcastInstance(Config config) {
78-
if (StringUtils.hasText(config.getInstanceName())) {
79-
return Hazelcast.getOrCreateHazelcastInstance(config);
80-
}
81-
return Hazelcast.newHazelcastInstance(config);
82-
}
83-
84-
8551
@Configuration
86-
@ConditionalOnMissingBean({HazelcastInstance.class, Config.class})
52+
@ConditionalOnMissingBean({ HazelcastInstance.class, Config.class })
8753
@Conditional(ConfigAvailableCondition.class)
8854
static class HazelcastConfigFileConfiguration {
8955

@@ -95,7 +61,7 @@ static class HazelcastConfigFileConfiguration {
9561
public HazelcastInstance hazelcastInstance() throws IOException {
9662
Resource config = this.hazelcastProperties.resolveConfigLocation();
9763
if (config != null) {
98-
return createHazelcastInstance(config);
64+
return new HazelcastInstanceFactory(config).getHazelcastInstance();
9965
}
10066
return Hazelcast.newHazelcastInstance();
10167
}
@@ -109,7 +75,7 @@ static class HazelcastConfigConfiguration {
10975

11076
@Bean
11177
public HazelcastInstance hazelcastInstance(Config config) {
112-
return createHazelcastInstance(config);
78+
return new HazelcastInstanceFactory(config).getHazelcastInstance();
11379
}
11480

11581
}
@@ -123,6 +89,7 @@ static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
12389
public ConfigAvailableCondition() {
12490
super("spring.hazelcast", "config");
12591
}
92+
12693
}
12794

12895
}

Diff for: spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConfigResourceCondition.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import org.springframework.core.type.AnnotatedTypeMetadata;
2424

2525
/**
26-
* {@link SpringBootCondition} used to check if the Hazelcast configuration is
27-
* available. This either kicks in if a default configuration has been found or
28-
* if configurable property referring to the resource to use has been set.
26+
* {@link SpringBootCondition} used to check if the Hazelcast configuration is available.
27+
* This either kicks in if a default configuration has been found or if configurable
28+
* property referring to the resource to use has been set.
2929
*
3030
* @author Stephane Nicoll
3131
* @since 1.3.0
@@ -43,8 +43,8 @@ protected HazelcastConfigResourceCondition(String prefix, String propertyName) {
4343
protected ConditionOutcome getResourceOutcome(ConditionContext context,
4444
AnnotatedTypeMetadata metadata) {
4545
if (System.getProperty(CONFIG_SYSTEM_PROPERTY) != null) {
46-
return ConditionOutcome.match("System property '"
47-
+ CONFIG_SYSTEM_PROPERTY + "' is set.");
46+
return ConditionOutcome.match("System property '" + CONFIG_SYSTEM_PROPERTY
47+
+ "' is set.");
4848
}
4949
return super.getResourceOutcome(context, metadata);
5050
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2012-2015 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.hazelcast;
18+
19+
import java.io.IOException;
20+
import java.net.URL;
21+
22+
import org.springframework.core.io.Resource;
23+
import org.springframework.util.Assert;
24+
import org.springframework.util.ResourceUtils;
25+
import org.springframework.util.StringUtils;
26+
27+
import com.hazelcast.config.Config;
28+
import com.hazelcast.config.XmlConfigBuilder;
29+
import com.hazelcast.core.Hazelcast;
30+
import com.hazelcast.core.HazelcastInstance;
31+
32+
/**
33+
* Factory that can be used to create a {@link HazelcastInstance}.
34+
*
35+
* @author Stephane Nicoll
36+
* @author Phillip Webb
37+
* @since 1.3.0
38+
*/
39+
public class HazelcastInstanceFactory {
40+
41+
private Config config;
42+
43+
/**
44+
* Create a {@link HazelcastInstanceFactory} for the specified configuration location.
45+
* @param configLocation the location of the configuration file
46+
* @throws IOException if the configuration location could not be read
47+
*/
48+
public HazelcastInstanceFactory(Resource configLocation) throws IOException {
49+
Assert.notNull(configLocation, "ConfigLocation must not be null");
50+
this.config = getConfig(configLocation);
51+
}
52+
53+
/**
54+
* Create a {@link HazelcastInstanceFactory} for the specified configuration.
55+
* @param config the configuration
56+
*/
57+
public HazelcastInstanceFactory(Config config) {
58+
Assert.notNull(config, "Config must not be null");
59+
this.config = config;
60+
}
61+
62+
private Config getConfig(Resource configLocation) throws IOException {
63+
URL configUrl = configLocation.getURL();
64+
Config config = new XmlConfigBuilder(configUrl).build();
65+
if (ResourceUtils.isFileURL(configUrl)) {
66+
config.setConfigurationFile(configLocation.getFile());
67+
}
68+
else {
69+
config.setConfigurationUrl(configUrl);
70+
}
71+
return config;
72+
}
73+
74+
/**
75+
* Get the {@link HazelcastInstance}.
76+
* @return the {@link HazelcastInstance}
77+
*/
78+
public HazelcastInstance getHazelcastInstance() {
79+
if (StringUtils.hasText(this.config.getInstanceName())) {
80+
return Hazelcast.getOrCreateHazelcastInstance(this.config);
81+
}
82+
return Hazelcast.newHazelcastInstance(this.config);
83+
}
84+
85+
}

Diff for: spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastProperties.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public void setConfig(Resource config) {
4949
* location
5050
*/
5151
public Resource resolveConfigLocation() {
52-
if (this.config != null) {
53-
Assert.isTrue(this.config.exists(), "Hazelcast configuration does not exist '"
54-
+ this.config.getDescription() + "'");
55-
return this.config;
52+
if (this.config == null) {
53+
return null;
5654
}
57-
return null;
55+
Assert.isTrue(this.config.exists(), "Hazelcast configuration does not exist '"
56+
+ this.config.getDescription() + "'");
57+
return this.config;
5858
}
5959

6060
}

0 commit comments

Comments
 (0)