Skip to content

Commit 39a1c5e

Browse files
committed
Merge pull request #7469 from vpavic:gh-4918
* pr/7469: Polish "Add auto-configuration support for Hazelcast client" Add auto-configuration support for Hazelcast client
2 parents ae3225e + 0aded58 commit 39a1c5e

File tree

13 files changed

+640
-189
lines changed

13 files changed

+640
-189
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@
7575
<artifactId>hazelcast</artifactId>
7676
<optional>true</optional>
7777
</dependency>
78+
<dependency>
79+
<groupId>com.hazelcast</groupId>
80+
<artifactId>hazelcast-client</artifactId>
81+
<optional>true</optional>
82+
</dependency>
7883
<dependency>
7984
<groupId>com.hazelcast</groupId>
8085
<artifactId>hazelcast-spring</artifactId>

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

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,81 +16,28 @@
1616

1717
package org.springframework.boot.autoconfigure.hazelcast;
1818

19-
import java.io.IOException;
20-
21-
import com.hazelcast.config.Config;
22-
import com.hazelcast.core.Hazelcast;
2319
import com.hazelcast.core.HazelcastInstance;
2420

2521
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2622
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
27-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
28-
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
2923
import org.springframework.boot.context.properties.EnableConfigurationProperties;
30-
import org.springframework.context.annotation.Bean;
31-
import org.springframework.context.annotation.Conditional;
3224
import org.springframework.context.annotation.Configuration;
33-
import org.springframework.core.io.Resource;
25+
import org.springframework.context.annotation.Import;
3426

3527
/**
3628
* {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a
3729
* {@link HazelcastInstance} based on explicit configuration or when a default
3830
* configuration file is found in the environment.
3931
*
4032
* @author Stephane Nicoll
41-
* @author Madhura Bhave
33+
* @author Vedran Pavic
4234
* @since 1.3.0
4335
* @see HazelcastConfigResourceCondition
4436
*/
4537
@Configuration
4638
@ConditionalOnClass(HazelcastInstance.class)
47-
@ConditionalOnMissingBean(HazelcastInstance.class)
4839
@EnableConfigurationProperties(HazelcastProperties.class)
40+
@Import({ HazelcastClientConfiguration.class, HazelcastServerConfiguration.class })
4941
public class HazelcastAutoConfiguration {
5042

51-
@Configuration
52-
@ConditionalOnMissingBean(Config.class)
53-
@Conditional(ConfigAvailableCondition.class)
54-
static class HazelcastConfigFileConfiguration {
55-
56-
private final HazelcastProperties hazelcastProperties;
57-
58-
HazelcastConfigFileConfiguration(HazelcastProperties hazelcastProperties) {
59-
this.hazelcastProperties = hazelcastProperties;
60-
}
61-
62-
@Bean
63-
public HazelcastInstance hazelcastInstance() throws IOException {
64-
Resource config = this.hazelcastProperties.resolveConfigLocation();
65-
if (config != null) {
66-
return new HazelcastInstanceFactory(config).getHazelcastInstance();
67-
}
68-
return Hazelcast.newHazelcastInstance();
69-
}
70-
71-
}
72-
73-
@Configuration
74-
@ConditionalOnSingleCandidate(Config.class)
75-
static class HazelcastConfigConfiguration {
76-
77-
@Bean
78-
public HazelcastInstance hazelcastInstance(Config config) {
79-
return new HazelcastInstanceFactory(config).getHazelcastInstance();
80-
}
81-
82-
}
83-
84-
/**
85-
* {@link HazelcastConfigResourceCondition} that checks if the
86-
* {@code spring.hazelcast.config} configuration key is defined.
87-
*/
88-
static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
89-
90-
ConfigAvailableCondition() {
91-
super("spring.hazelcast.config");
92-
}
93-
94-
}
95-
9643
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.hazelcast;
18+
19+
import java.io.IOException;
20+
21+
import com.hazelcast.client.HazelcastClient;
22+
import com.hazelcast.client.config.ClientConfig;
23+
import com.hazelcast.core.HazelcastInstance;
24+
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Conditional;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.core.io.Resource;
32+
33+
/**
34+
* Configuration for Hazelcast client.
35+
*
36+
* @author Vedran Pavic
37+
* @author Stephane Nicoll
38+
*/
39+
@ConditionalOnClass(HazelcastClient.class)
40+
@ConditionalOnMissingBean(HazelcastInstance.class)
41+
class HazelcastClientConfiguration {
42+
43+
static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.client.config";
44+
45+
@Configuration
46+
@ConditionalOnMissingBean(ClientConfig.class)
47+
@Conditional(ConfigAvailableCondition.class)
48+
static class HazelcastClientConfigFileConfiguration {
49+
50+
@Bean
51+
public HazelcastInstance hazelcastInstance(HazelcastProperties properties)
52+
throws IOException {
53+
Resource config = properties.resolveConfigLocation();
54+
if (config != null) {
55+
return new HazelcastClientFactory(config).getHazelcastInstance();
56+
}
57+
return HazelcastClient.newHazelcastClient();
58+
}
59+
60+
}
61+
62+
@Configuration
63+
@ConditionalOnSingleCandidate(ClientConfig.class)
64+
static class HazelcastClientConfigConfiguration {
65+
66+
@Bean
67+
public HazelcastInstance hazelcastInstance(ClientConfig config) {
68+
return new HazelcastClientFactory(config).getHazelcastInstance();
69+
}
70+
71+
}
72+
73+
/**
74+
* {@link HazelcastConfigResourceCondition} that checks if the
75+
* {@code spring.hazelcast.config} configuration key is defined.
76+
*/
77+
static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
78+
79+
ConfigAvailableCondition() {
80+
super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml",
81+
"classpath:/hazelcast-client.xml");
82+
}
83+
84+
}
85+
86+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.hazelcast;
18+
19+
import java.io.IOException;
20+
import java.net.URL;
21+
22+
import com.hazelcast.client.HazelcastClient;
23+
import com.hazelcast.client.config.ClientConfig;
24+
import com.hazelcast.client.config.XmlClientConfigBuilder;
25+
import com.hazelcast.core.HazelcastInstance;
26+
27+
import org.springframework.core.io.Resource;
28+
import org.springframework.util.Assert;
29+
import org.springframework.util.StringUtils;
30+
31+
/**
32+
* Factory that can be used to create a client {@link HazelcastInstance}.
33+
*
34+
* @author Vedran Pavic
35+
* @since 2.0.0
36+
*/
37+
public class HazelcastClientFactory {
38+
39+
private final ClientConfig clientConfig;
40+
41+
/**
42+
* Create a {@link HazelcastClientFactory} for the specified configuration location.
43+
* @param clientConfigLocation the location of the configuration file
44+
* @throws IOException if the configuration location could not be read
45+
*/
46+
public HazelcastClientFactory(Resource clientConfigLocation) throws IOException {
47+
this.clientConfig = getClientConfig(clientConfigLocation);
48+
}
49+
50+
/**
51+
* Create a {@link HazelcastClientFactory} for the specified configuration.
52+
* @param clientConfig the configuration
53+
*/
54+
public HazelcastClientFactory(ClientConfig clientConfig) {
55+
Assert.notNull(clientConfig, "ClientConfig must not be null");
56+
this.clientConfig = clientConfig;
57+
}
58+
59+
60+
private ClientConfig getClientConfig(Resource clientConfigLocation)
61+
throws IOException {
62+
URL configUrl = clientConfigLocation.getURL();
63+
return new XmlClientConfigBuilder(configUrl).build();
64+
}
65+
66+
/**
67+
* Get the {@link HazelcastInstance}.
68+
* @return the {@link HazelcastInstance}
69+
*/
70+
public HazelcastInstance getHazelcastInstance() {
71+
if (StringUtils.hasText(this.clientConfig.getInstanceName())) {
72+
return HazelcastClient.getHazelcastClientByName(
73+
this.clientConfig.getInstanceName());
74+
}
75+
return HazelcastClient.newHazelcastClient(this.clientConfig);
76+
}
77+
78+
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
2222
import org.springframework.context.annotation.ConditionContext;
2323
import org.springframework.core.type.AnnotatedTypeMetadata;
24+
import org.springframework.util.Assert;
2425

2526
/**
2627
* {@link SpringBootCondition} used to check if the Hazelcast configuration is available.
@@ -29,22 +30,26 @@
2930
*
3031
* @author Stephane Nicoll
3132
* @author Madhura Bhave
33+
* @author Vedran Pavic
3234
* @since 1.3.0
3335
*/
3436
public abstract class HazelcastConfigResourceCondition extends ResourceCondition {
3537

36-
static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.config";
38+
private final String configSystemProperty;
3739

38-
protected HazelcastConfigResourceCondition(String property) {
39-
super("Hazelcast", property, "file:./hazelcast.xml", "classpath:/hazelcast.xml");
40+
protected HazelcastConfigResourceCondition(String configSystemProperty,
41+
String... resourceLocations) {
42+
super("Hazelcast", "spring.hazelcast.config", resourceLocations);
43+
Assert.notNull(configSystemProperty, "ConfigSystemProperty must not be null");
44+
this.configSystemProperty = configSystemProperty;
4045
}
4146

4247
@Override
4348
protected ConditionOutcome getResourceOutcome(ConditionContext context,
4449
AnnotatedTypeMetadata metadata) {
45-
if (System.getProperty(CONFIG_SYSTEM_PROPERTY) != null) {
50+
if (System.getProperty(this.configSystemProperty) != null) {
4651
return ConditionOutcome.match(startConditionMessage()
47-
.because("System property '" + CONFIG_SYSTEM_PROPERTY + "' is set."));
52+
.because("System property '" + this.configSystemProperty + "' is set."));
4853
}
4954
return super.getResourceOutcome(context, metadata);
5055
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@
3838
*/
3939
public class HazelcastInstanceFactory {
4040

41-
private Config config;
41+
private final Config config;
4242

4343
/**
4444
* Create a {@link HazelcastInstanceFactory} for the specified configuration location.

0 commit comments

Comments
 (0)