From 5eaafdee9a40fe555139ea440f7fdf3b38438a18 Mon Sep 17 00:00:00 2001 From: Neil Stevenson Date: Mon, 22 Aug 2022 18:23:27 +0100 Subject: [PATCH] Add support for detecting .yml Hazelcast config files See gh-32142 --- ...zelcastClientConfigAvailableCondition.java | 3 +- .../HazelcastClientConfiguration.java | 2 +- .../HazelcastServerConfiguration.java | 4 +-- ...HazelcastAutoConfigurationClientTests.java | 24 +++++++++++++++ ...HazelcastAutoConfigurationServerTests.java | 29 +++++++++++++++++++ .../HazelcastAutoConfigurationTests.java | 2 +- .../src/test/resources/hazelcast.yml | 7 +++++ .../hazelcast/hazelcast-client-specific.yml | 3 ++ .../hazelcast/hazelcast-specific.yml | 12 ++++++++ 9 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/resources/hazelcast.yml create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.yml create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yml diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfigAvailableCondition.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfigAvailableCondition.java index a08a40da1926..df05696a3b3c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfigAvailableCondition.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfigAvailableCondition.java @@ -39,7 +39,8 @@ class HazelcastClientConfigAvailableCondition extends HazelcastConfigResourceCon HazelcastClientConfigAvailableCondition() { super(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml", - "classpath:/hazelcast-client.xml", "file:./hazelcast-client.yaml", "classpath:/hazelcast-client.yaml"); + "classpath:/hazelcast-client.xml", "file:./hazelcast-client.yaml", "classpath:/hazelcast-client.yaml", + "file:./hazelcast-client.yml", "classpath:/hazelcast-client.yml"); } @Override diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java index 897085d6df37..8613de869509 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastClientConfiguration.java @@ -72,7 +72,7 @@ HazelcastInstance hazelcastInstance(HazelcastProperties properties, ResourceLoad private ClientConfig loadClientConfig(Resource configLocation) throws IOException { URL configUrl = configLocation.getURL(); String configFileName = configUrl.getPath(); - if (configFileName.endsWith(".yaml")) { + if (configFileName.endsWith(".yaml") || configFileName.endsWith(".yml")) { return new YamlClientConfigBuilder(configUrl).build(); } return new XmlClientConfigBuilder(configUrl).build(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java index 63fe94612e71..0fbe23300d6b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastServerConfiguration.java @@ -82,7 +82,7 @@ private Config loadConfig(Resource configLocation) throws IOException { private static Config loadConfig(URL configUrl) throws IOException { String configFileName = configUrl.getPath(); - if (configFileName.endsWith(".yaml")) { + if (configFileName.endsWith(".yaml") || configFileName.endsWith(".yml")) { return new YamlConfigBuilder(configUrl).build(); } return new XmlConfigBuilder(configUrl).build(); @@ -109,7 +109,7 @@ static class ConfigAvailableCondition extends HazelcastConfigResourceCondition { ConfigAvailableCondition() { super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast.xml", "classpath:/hazelcast.xml", "file:./hazelcast.yaml", - "classpath:/hazelcast.yaml"); + "classpath:/hazelcast.yaml", "file:./hazelcast.yml", "classpath:/hazelcast.yml"); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java index 27d98497a307..cc10dda71c2c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationClientTests.java @@ -81,6 +81,14 @@ void systemPropertyWithYaml() { .run(assertSpecificHazelcastClient("explicit-yaml")); } + @Test + void systemPropertyWithYml() { + this.contextRunner + .withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY + + "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.yml") + .run(assertSpecificHazelcastClient("explicit-yml")); + } + @Test void explicitConfigFileWithXml() { this.contextRunner.withPropertyValues("spring.hazelcast.config=org/springframework/boot/autoconfigure/" @@ -95,6 +103,14 @@ void explicitConfigFileWithYaml() { .run(assertSpecificHazelcastClient("explicit-yaml")); } + @Test + void explicitConfigFileWithYml() { + this.contextRunner + .withPropertyValues("spring.hazelcast.config=org/springframework/boot/autoconfigure/" + + "hazelcast/hazelcast-client-specific.yml") + .run(assertSpecificHazelcastClient("explicit-yml")); + } + @Test void explicitConfigUrlWithXml() { this.contextRunner @@ -111,6 +127,14 @@ void explicitConfigUrlWithYaml() { .run(assertSpecificHazelcastClient("explicit-yaml")); } + @Test + void explicitConfigUrlWithYml() { + this.contextRunner + .withPropertyValues("spring.hazelcast.config=classpath:org/springframework/" + + "boot/autoconfigure/hazelcast/hazelcast-client-specific.yml") + .run(assertSpecificHazelcastClient("explicit-yml")); + } + @Test void unknownConfigFile() { this.contextRunner.withPropertyValues("spring.hazelcast.config=foo/bar/unknown.xml") diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java index a1b92afa59d0..4cf523a2caeb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationServerTests.java @@ -79,6 +79,17 @@ void systemPropertyWithYaml() { }); } + @Test + void systemPropertyWithYml() { + this.contextRunner + .withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY + + "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yml") + .run((context) -> { + Config config = context.getBean(HazelcastInstance.class).getConfig(); + assertThat(config.getMapConfigs().keySet()).containsOnly("foobar"); + }); + } + @Test void explicitConfigFileWithXml() { this.contextRunner @@ -97,6 +108,15 @@ void explicitConfigFileWithYaml() { "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml")); } + @Test + void explicitConfigFileWithYml() { + this.contextRunner + .withPropertyValues("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" + + "hazelcast-specific.yml") + .run(assertSpecificHazelcastServer( + "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yml")); + } + @Test void explicitConfigUrlWithXml() { this.contextRunner @@ -115,6 +135,15 @@ void explicitConfigUrlWithYaml() { "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml")); } + @Test + void explicitConfigUrlWithYml() { + this.contextRunner + .withPropertyValues("spring.hazelcast.config=classpath:org/springframework/" + + "boot/autoconfigure/hazelcast/hazelcast-specific.yml") + .run(assertSpecificHazelcastServer( + "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yml")); + } + private ContextConsumer assertSpecificHazelcastServer(String location) { return (context) -> { Config config = context.getBean(HazelcastInstance.class).getConfig(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java index 6e941f928253..8c914217d4a2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfigurationTests.java @@ -45,7 +45,7 @@ class HazelcastAutoConfigurationTests { void defaultConfigFile() { // no hazelcast-client.xml and hazelcast.xml is present in root classpath // this also asserts that XML has priority over YAML - // as both hazelcast.yaml and hazelcast.xml in test classpath. + // as both hazelcast.yaml, hazelcast.yml and hazelcast.xml in test classpath. this.contextRunner.run((context) -> { Config config = context.getBean(HazelcastInstance.class).getConfig(); assertThat(config.getConfigurationUrl()).isEqualTo(new ClassPathResource("hazelcast.xml").getURL()); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/hazelcast.yml b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/hazelcast.yml new file mode 100644 index 000000000000..686d75638a9c --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/hazelcast.yml @@ -0,0 +1,7 @@ +hazelcast: + network: + join: + auto-detection: + enabled: false + multicast: + enabled: false diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.yml b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.yml new file mode 100644 index 000000000000..a3e491628063 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.yml @@ -0,0 +1,3 @@ +hazelcast-client: + client-labels: + - explicit-yml diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yml b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yml new file mode 100644 index 000000000000..933c34ffd0cd --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yml @@ -0,0 +1,12 @@ +hazelcast: + network: + join: + auto-detection: + enabled: false + multicast: + enabled: false + + map: + foobar: + time-to-live-seconds: 3600 + max-idle-seconds: 600