Skip to content

Commit 1f535a6

Browse files
committed
Merge pull request #32142 from neilstevenson
* pr/32142: Polish "Add support for detecting .yml Hazelcast config files" Add support for detecting .yml Hazelcast config files Closes gh-32142
2 parents cd61d69 + 19c69ff commit 1f535a6

File tree

10 files changed

+86
-12
lines changed

10 files changed

+86
-12
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 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,7 +39,8 @@ class HazelcastClientConfigAvailableCondition extends HazelcastConfigResourceCon
3939

4040
HazelcastClientConfigAvailableCondition() {
4141
super(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml",
42-
"classpath:/hazelcast-client.xml", "file:./hazelcast-client.yaml", "classpath:/hazelcast-client.yaml");
42+
"classpath:/hazelcast-client.xml", "file:./hazelcast-client.yaml", "classpath:/hazelcast-client.yaml",
43+
"file:./hazelcast-client.yml", "classpath:/hazelcast-client.yml");
4344
}
4445

4546
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -72,7 +72,7 @@ HazelcastInstance hazelcastInstance(HazelcastProperties properties, ResourceLoad
7272
private ClientConfig loadClientConfig(Resource configLocation) throws IOException {
7373
URL configUrl = configLocation.getURL();
7474
String configFileName = configUrl.getPath();
75-
if (configFileName.endsWith(".yaml")) {
75+
if (configFileName.endsWith(".yaml") || configFileName.endsWith(".yml")) {
7676
return new YamlClientConfigBuilder(configUrl).build();
7777
}
7878
return new XmlClientConfigBuilder(configUrl).build();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -82,7 +82,7 @@ private Config loadConfig(Resource configLocation) throws IOException {
8282

8383
private static Config loadConfig(URL configUrl) throws IOException {
8484
String configFileName = configUrl.getPath();
85-
if (configFileName.endsWith(".yaml")) {
85+
if (configFileName.endsWith(".yaml") || configFileName.endsWith(".yml")) {
8686
return new YamlConfigBuilder(configUrl).build();
8787
}
8888
return new XmlConfigBuilder(configUrl).build();
@@ -109,7 +109,7 @@ static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
109109

110110
ConfigAvailableCondition() {
111111
super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast.xml", "classpath:/hazelcast.xml", "file:./hazelcast.yaml",
112-
"classpath:/hazelcast.yaml");
112+
"classpath:/hazelcast.yaml", "file:./hazelcast.yml", "classpath:/hazelcast.yml");
113113
}
114114

115115
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -81,6 +81,14 @@ void systemPropertyWithYaml() {
8181
.run(assertSpecificHazelcastClient("explicit-yaml"));
8282
}
8383

84+
@Test
85+
void systemPropertyWithYml() {
86+
this.contextRunner
87+
.withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY
88+
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.yml")
89+
.run(assertSpecificHazelcastClient("explicit-yml"));
90+
}
91+
8492
@Test
8593
void explicitConfigFileWithXml() {
8694
this.contextRunner.withPropertyValues("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
@@ -95,6 +103,12 @@ void explicitConfigFileWithYaml() {
95103
.run(assertSpecificHazelcastClient("explicit-yaml"));
96104
}
97105

106+
@Test
107+
void explicitConfigFileWithYml() {
108+
this.contextRunner.withPropertyValues("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
109+
+ "hazelcast/hazelcast-client-specific.yml").run(assertSpecificHazelcastClient("explicit-yml"));
110+
}
111+
98112
@Test
99113
void explicitConfigUrlWithXml() {
100114
this.contextRunner
@@ -111,6 +125,14 @@ void explicitConfigUrlWithYaml() {
111125
.run(assertSpecificHazelcastClient("explicit-yaml"));
112126
}
113127

128+
@Test
129+
void explicitConfigUrlWithYml() {
130+
this.contextRunner
131+
.withPropertyValues("spring.hazelcast.config=classpath:org/springframework/"
132+
+ "boot/autoconfigure/hazelcast/hazelcast-client-specific.yml")
133+
.run(assertSpecificHazelcastClient("explicit-yml"));
134+
}
135+
114136
@Test
115137
void unknownConfigFile() {
116138
this.contextRunner.withPropertyValues("spring.hazelcast.config=foo/bar/unknown.xml")

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ void systemPropertyWithYaml() {
7979
});
8080
}
8181

82+
@Test
83+
void systemPropertyWithYml() {
84+
this.contextRunner
85+
.withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
86+
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yml")
87+
.run((context) -> {
88+
Config config = context.getBean(HazelcastInstance.class).getConfig();
89+
assertThat(config.getMapConfigs().keySet()).containsOnly("foobar");
90+
});
91+
}
92+
8293
@Test
8394
void explicitConfigFileWithXml() {
8495
this.contextRunner
@@ -97,6 +108,15 @@ void explicitConfigFileWithYaml() {
97108
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml"));
98109
}
99110

111+
@Test
112+
void explicitConfigFileWithYml() {
113+
this.contextRunner
114+
.withPropertyValues("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
115+
+ "hazelcast-specific.yml")
116+
.run(assertSpecificHazelcastServer(
117+
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yml"));
118+
}
119+
100120
@Test
101121
void explicitConfigUrlWithXml() {
102122
this.contextRunner
@@ -115,6 +135,15 @@ void explicitConfigUrlWithYaml() {
115135
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml"));
116136
}
117137

138+
@Test
139+
void explicitConfigUrlWithYml() {
140+
this.contextRunner
141+
.withPropertyValues("spring.hazelcast.config=classpath:org/springframework/"
142+
+ "boot/autoconfigure/hazelcast/hazelcast-specific.yml")
143+
.run(assertSpecificHazelcastServer(
144+
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yml"));
145+
}
146+
118147
private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastServer(String location) {
119148
return (context) -> {
120149
Config config = context.getBean(HazelcastInstance.class).getConfig();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -45,7 +45,7 @@ class HazelcastAutoConfigurationTests {
4545
void defaultConfigFile() {
4646
// no hazelcast-client.xml and hazelcast.xml is present in root classpath
4747
// this also asserts that XML has priority over YAML
48-
// as both hazelcast.yaml and hazelcast.xml in test classpath.
48+
// as hazelcast.yaml, hazelcast.yml, and hazelcast.xml are available.
4949
this.contextRunner.run((context) -> {
5050
Config config = context.getBean(HazelcastInstance.class).getConfig();
5151
assertThat(config.getConfigurationUrl()).isEqualTo(new ClassPathResource("hazelcast.xml").getURL());
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
hazelcast:
2+
network:
3+
join:
4+
auto-detection:
5+
enabled: false
6+
multicast:
7+
enabled: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hazelcast-client:
2+
client-labels:
3+
- explicit-yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
hazelcast:
2+
network:
3+
join:
4+
auto-detection:
5+
enabled: false
6+
multicast:
7+
enabled: false
8+
9+
map:
10+
foobar:
11+
time-to-live-seconds: 3600
12+
max-idle-seconds: 600

spring-boot-project/spring-boot-docs/src/docs/asciidoc/io/hazelcast.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Spring Boot first attempts to create a client by checking the following configur
88
* A configuration file defined by the configprop:spring.hazelcast.config[] property.
99
* The presence of the `hazelcast.client.config` system property.
1010
* A `hazelcast-client.xml` in the working directory or at the root of the classpath.
11-
* A `hazelcast-client.yaml` in the working directory or at the root of the classpath.
11+
* A `hazelcast-client.yaml` (or `hazelcast-client.yml`) in the working directory or at the root of the classpath.
1212

1313
NOTE: Spring Boot supports both Hazelcast 4 and Hazelcast 3.
1414
If you downgrade to Hazelcast 3, `hazelcast-client` should be added to the classpath to configure a client.
@@ -26,7 +26,7 @@ You could also specify the Hazelcast configuration file to use through configura
2626
config: "classpath:config/my-hazelcast.xml"
2727
----
2828

29-
Otherwise, Spring Boot tries to find the Hazelcast configuration from the default locations: `hazelcast.xml` in the working directory or at the root of the classpath, or a `.yaml` counterpart in the same locations.
29+
Otherwise, Spring Boot tries to find the Hazelcast configuration from the default locations: `hazelcast.xml` in the working directory or at the root of the classpath, or a `.yaml`/`.yml` counterpart in the same locations.
3030
We also check if the `hazelcast.config` system property is set.
3131
See the https://docs.hazelcast.org/docs/latest/manual/html-single/[Hazelcast documentation] for more details.
3232

0 commit comments

Comments
 (0)