Skip to content

Commit 84530ce

Browse files
committed
Merge pull request #16632 from leszko
* pr/16632: Polish "Add support for Hazelcast YAML configuration" Add support for Hazelcast YAML configuration
2 parents b527d36 + 704da17 commit 84530ce

File tree

12 files changed

+147
-43
lines changed

12 files changed

+147
-43
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
7979

8080
ConfigAvailableCondition() {
8181
super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml",
82-
"classpath:/hazelcast-client.xml");
82+
"classpath:/hazelcast-client.xml", "file:./hazelcast-client.yaml",
83+
"classpath:/hazelcast-client.yaml");
8384
}
8485

8586
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -22,6 +22,7 @@
2222
import com.hazelcast.client.HazelcastClient;
2323
import com.hazelcast.client.config.ClientConfig;
2424
import com.hazelcast.client.config.XmlClientConfigBuilder;
25+
import com.hazelcast.client.config.YamlClientConfigBuilder;
2526
import com.hazelcast.core.HazelcastInstance;
2627

2728
import org.springframework.core.io.Resource;
@@ -59,6 +60,10 @@ public HazelcastClientFactory(ClientConfig clientConfig) {
5960
private ClientConfig getClientConfig(Resource clientConfigLocation)
6061
throws IOException {
6162
URL configUrl = clientConfigLocation.getURL();
63+
String configFileName = configUrl.getPath();
64+
if (configFileName.endsWith(".yaml")) {
65+
return new YamlClientConfigBuilder(configUrl).build();
66+
}
6267
return new XmlClientConfigBuilder(configUrl).build();
6368
}
6469

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -21,6 +21,7 @@
2121

2222
import com.hazelcast.config.Config;
2323
import com.hazelcast.config.XmlConfigBuilder;
24+
import com.hazelcast.config.YamlConfigBuilder;
2425
import com.hazelcast.core.Hazelcast;
2526
import com.hazelcast.core.HazelcastInstance;
2627

@@ -61,7 +62,7 @@ public HazelcastInstanceFactory(Config config) {
6162

6263
private Config getConfig(Resource configLocation) throws IOException {
6364
URL configUrl = configLocation.getURL();
64-
Config config = new XmlConfigBuilder(configUrl).build();
65+
Config config = createConfig(configUrl);
6566
if (ResourceUtils.isFileURL(configUrl)) {
6667
config.setConfigurationFile(configLocation.getFile());
6768
}
@@ -71,6 +72,14 @@ private Config getConfig(Resource configLocation) throws IOException {
7172
return config;
7273
}
7374

75+
private static Config createConfig(URL configUrl) throws IOException {
76+
String configFileName = configUrl.getPath();
77+
if (configFileName.endsWith(".yaml")) {
78+
return new YamlConfigBuilder(configUrl).build();
79+
}
80+
return new XmlConfigBuilder(configUrl).build();
81+
}
82+
7483
/**
7584
* Get the {@link HazelcastInstance}.
7685
* @return the {@link HazelcastInstance}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
7777

7878
ConfigAvailableCondition() {
7979
super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast.xml",
80-
"classpath:/hazelcast.xml");
80+
"classpath:/hazelcast.xml", "file:./hazelcast.yaml",
81+
"classpath:/hazelcast.yaml");
8182
}
8283

8384
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828

2929
import org.springframework.beans.factory.BeanCreationException;
3030
import org.springframework.boot.autoconfigure.AutoConfigurations;
31+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
3132
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
33+
import org.springframework.boot.test.context.runner.ContextConsumer;
3234
import org.springframework.context.annotation.Bean;
3335
import org.springframework.context.annotation.Configuration;
3436

@@ -63,35 +65,55 @@ public static void close() {
6365
.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class));
6466

6567
@Test
66-
public void systemProperty() {
68+
public void systemPropertyWithXml() {
6769
this.contextRunner
6870
.withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY
6971
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/"
7072
+ "hazelcast-client-specific.xml")
71-
.run((context) -> assertThat(context).getBean(HazelcastInstance.class)
72-
.isInstanceOf(HazelcastInstance.class)
73-
.has(nameStartingWith("hz.client_")));
73+
.run(assertSpecificHazelcastClient("explicit-xml"));
7474
}
7575

7676
@Test
77-
public void explicitConfigFile() {
77+
public void systemPropertyWithYaml() {
78+
this.contextRunner
79+
.withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY
80+
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/"
81+
+ "hazelcast-client-specific.yaml")
82+
.run(assertSpecificHazelcastClient("explicit-yaml"));
83+
}
84+
85+
@Test
86+
public void explicitConfigFileWithXml() {
7887
this.contextRunner
7988
.withPropertyValues(
8089
"spring.hazelcast.config=org/springframework/boot/autoconfigure/"
8190
+ "hazelcast/hazelcast-client-specific.xml")
82-
.run((context) -> assertThat(context).getBean(HazelcastInstance.class)
83-
.isInstanceOf(HazelcastClientProxy.class)
84-
.has(nameStartingWith("hz.client_")));
91+
.run(assertSpecificHazelcastClient("explicit-xml"));
8592
}
8693

8794
@Test
88-
public void explicitConfigUrl() {
95+
public void explicitConfigFileWithYaml() {
8996
this.contextRunner
9097
.withPropertyValues(
91-
"spring.hazelcast.config=hazelcast-client-default.xml")
92-
.run((context) -> assertThat(context).getBean(HazelcastInstance.class)
93-
.isInstanceOf(HazelcastClientProxy.class)
94-
.has(nameStartingWith("hz.client_")));
98+
"spring.hazelcast.config=org/springframework/boot/autoconfigure/"
99+
+ "hazelcast/hazelcast-client-specific.yaml")
100+
.run(assertSpecificHazelcastClient("explicit-yaml"));
101+
}
102+
103+
@Test
104+
public void explicitConfigUrlWithXml() {
105+
this.contextRunner.withPropertyValues(
106+
"spring.hazelcast.config=classpath:org/springframework/"
107+
+ "boot/autoconfigure/hazelcast/hazelcast-client-specific.xml")
108+
.run(assertSpecificHazelcastClient("explicit-xml"));
109+
}
110+
111+
@Test
112+
public void explicitConfigUrlWithYaml() {
113+
this.contextRunner.withPropertyValues(
114+
"spring.hazelcast.config=classpath:org/springframework/"
115+
+ "boot/autoconfigure/hazelcast/hazelcast-client-specific.yaml")
116+
.run(assertSpecificHazelcastClient("explicit-yaml"));
95117
}
96118

97119
@Test
@@ -111,9 +133,16 @@ public void clientConfigTakesPrecedence() {
111133
.isInstanceOf(HazelcastClientProxy.class));
112134
}
113135

114-
private Condition<HazelcastInstance> nameStartingWith(String prefix) {
115-
return new Condition<>((o) -> o.getName().startsWith(prefix),
116-
"Name starts with " + prefix);
136+
private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastClient(
137+
String label) {
138+
return (context) -> assertThat(context).getBean(HazelcastInstance.class)
139+
.isInstanceOf(HazelcastInstance.class).has(labelEqualTo(label));
140+
}
141+
142+
private static Condition<HazelcastInstance> labelEqualTo(String label) {
143+
return new Condition<>((o) -> ((HazelcastClientProxy) o).getClientConfig()
144+
.getLabels().stream().anyMatch((e) -> e.equals(label)),
145+
"Label equals to " + label);
117146
}
118147

119148
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828
import org.springframework.beans.factory.BeanCreationException;
2929
import org.springframework.boot.autoconfigure.AutoConfigurations;
30+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
3031
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
32+
import org.springframework.boot.test.context.runner.ContextConsumer;
3133
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
3234
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
3335
import org.springframework.context.annotation.Bean;
@@ -59,39 +61,71 @@ public void defaultConfigFile() {
5961
}
6062

6163
@Test
62-
public void systemProperty() {
64+
public void systemPropertyWithXml() {
6365
this.contextRunner
6466
.withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
6567
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml")
6668
.run((context) -> {
6769
Config config = context.getBean(HazelcastInstance.class).getConfig();
68-
assertThat(config.getQueueConfigs().keySet()).containsOnly("foobar");
70+
assertThat(config.getMapConfigs().keySet()).containsOnly("foobar");
6971
});
7072
}
7173

7274
@Test
73-
public void explicitConfigFile() {
74-
this.contextRunner.withPropertyValues(
75-
"spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
76-
+ "hazelcast-specific.xml")
75+
public void systemPropertyWithYaml() {
76+
this.contextRunner
77+
.withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
78+
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml")
7779
.run((context) -> {
7880
Config config = context.getBean(HazelcastInstance.class).getConfig();
79-
assertThat(config.getConfigurationFile())
80-
.isEqualTo(new ClassPathResource(
81-
"org/springframework/boot/autoconfigure/hazelcast"
82-
+ "/hazelcast-specific.xml").getFile());
81+
assertThat(config.getMapConfigs().keySet()).containsOnly("foobar");
8382
});
8483
}
8584

8685
@Test
87-
public void explicitConfigUrl() {
86+
public void explicitConfigFileWithXml() {
87+
this.contextRunner.withPropertyValues(
88+
"spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
89+
+ "hazelcast-specific.xml")
90+
.run(assertSpecificHazelcastServer(
91+
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"));
92+
}
93+
94+
@Test
95+
public void explicitConfigFileWithYaml() {
96+
this.contextRunner.withPropertyValues(
97+
"spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
98+
+ "hazelcast-specific.yaml")
99+
.run(assertSpecificHazelcastServer(
100+
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml"));
101+
}
102+
103+
@Test
104+
public void explicitConfigUrlWithXml() {
88105
this.contextRunner
89-
.withPropertyValues("spring.hazelcast.config=hazelcast-default.xml")
90-
.run((context) -> {
91-
Config config = context.getBean(HazelcastInstance.class).getConfig();
92-
assertThat(config.getConfigurationUrl()).isEqualTo(
93-
new ClassPathResource("hazelcast-default.xml").getURL());
94-
});
106+
.withPropertyValues(
107+
"spring.hazelcast.config=classpath:org/springframework/"
108+
+ "boot/autoconfigure/hazelcast/hazelcast-specific.xml")
109+
.run(assertSpecificHazelcastServer(
110+
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"));
111+
}
112+
113+
@Test
114+
public void explicitConfigUrlWithYaml() {
115+
this.contextRunner
116+
.withPropertyValues(
117+
"spring.hazelcast.config=classpath:org/springframework/"
118+
+ "boot/autoconfigure/hazelcast/hazelcast-specific.yaml")
119+
.run(assertSpecificHazelcastServer(
120+
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml"));
121+
}
122+
123+
private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastServer(
124+
String location) {
125+
return (context) -> {
126+
Config config = context.getBean(HazelcastInstance.class).getConfig();
127+
assertThat(config.getConfigurationUrl()).asString().endsWith(location);
128+
};
95129
}
96130

97131
@Test

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -39,6 +39,8 @@ public class HazelcastAutoConfigurationTests {
3939
@Test
4040
public void defaultConfigFile() {
4141
// no hazelcast-client.xml and hazelcast.xml is present in root classpath
42+
// this also asserts that XML has priority over YAML
43+
// as both hazelcast.yaml and hazelcast.xml in test classpath.
4244
this.contextRunner.run((context) -> {
4345
Config config = context.getBean(HazelcastInstance.class).getConfig();
4446
assertThat(config.getConfigurationUrl())
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
hazelcast:
2+
network:
3+
join:
4+
multicast:
5+
enabled: false

spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-3.12.xsd">
5+
<client-labels>
6+
<label>explicit-xml</label>
7+
</client-labels>
58

69
</hazelcast-client>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hazelcast-client:
2+
client-labels:
3+
- explicit-yaml

0 commit comments

Comments
 (0)