Commit 84530ce3 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #16632 from leszko

* pr/16632:
  Polish "Add support for Hazelcast YAML configuration"
  Add support for Hazelcast YAML configuration
parents b527d364 704da175
...@@ -79,7 +79,8 @@ class HazelcastClientConfiguration { ...@@ -79,7 +79,8 @@ class HazelcastClientConfiguration {
ConfigAvailableCondition() { ConfigAvailableCondition() {
super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml", super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml",
"classpath:/hazelcast-client.xml"); "classpath:/hazelcast-client.xml", "file:./hazelcast-client.yaml",
"classpath:/hazelcast-client.yaml");
} }
} }
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -22,6 +22,7 @@ import java.net.URL; ...@@ -22,6 +22,7 @@ import java.net.URL;
import com.hazelcast.client.HazelcastClient; import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig; import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.XmlClientConfigBuilder; import com.hazelcast.client.config.XmlClientConfigBuilder;
import com.hazelcast.client.config.YamlClientConfigBuilder;
import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.HazelcastInstance;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
...@@ -59,6 +60,10 @@ public class HazelcastClientFactory { ...@@ -59,6 +60,10 @@ public class HazelcastClientFactory {
private ClientConfig getClientConfig(Resource clientConfigLocation) private ClientConfig getClientConfig(Resource clientConfigLocation)
throws IOException { throws IOException {
URL configUrl = clientConfigLocation.getURL(); URL configUrl = clientConfigLocation.getURL();
String configFileName = configUrl.getPath();
if (configFileName.endsWith(".yaml")) {
return new YamlClientConfigBuilder(configUrl).build();
}
return new XmlClientConfigBuilder(configUrl).build(); return new XmlClientConfigBuilder(configUrl).build();
} }
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -21,6 +21,7 @@ import java.net.URL; ...@@ -21,6 +21,7 @@ import java.net.URL;
import com.hazelcast.config.Config; import com.hazelcast.config.Config;
import com.hazelcast.config.XmlConfigBuilder; import com.hazelcast.config.XmlConfigBuilder;
import com.hazelcast.config.YamlConfigBuilder;
import com.hazelcast.core.Hazelcast; import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.HazelcastInstance;
...@@ -61,7 +62,7 @@ public class HazelcastInstanceFactory { ...@@ -61,7 +62,7 @@ public class HazelcastInstanceFactory {
private Config getConfig(Resource configLocation) throws IOException { private Config getConfig(Resource configLocation) throws IOException {
URL configUrl = configLocation.getURL(); URL configUrl = configLocation.getURL();
Config config = new XmlConfigBuilder(configUrl).build(); Config config = createConfig(configUrl);
if (ResourceUtils.isFileURL(configUrl)) { if (ResourceUtils.isFileURL(configUrl)) {
config.setConfigurationFile(configLocation.getFile()); config.setConfigurationFile(configLocation.getFile());
} }
...@@ -71,6 +72,14 @@ public class HazelcastInstanceFactory { ...@@ -71,6 +72,14 @@ public class HazelcastInstanceFactory {
return config; return config;
} }
private static Config createConfig(URL configUrl) throws IOException {
String configFileName = configUrl.getPath();
if (configFileName.endsWith(".yaml")) {
return new YamlConfigBuilder(configUrl).build();
}
return new XmlConfigBuilder(configUrl).build();
}
/** /**
* Get the {@link HazelcastInstance}. * Get the {@link HazelcastInstance}.
* @return the {@link HazelcastInstance} * @return the {@link HazelcastInstance}
......
...@@ -77,7 +77,8 @@ class HazelcastServerConfiguration { ...@@ -77,7 +77,8 @@ class HazelcastServerConfiguration {
ConfigAvailableCondition() { ConfigAvailableCondition() {
super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast.xml", super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast.xml",
"classpath:/hazelcast.xml"); "classpath:/hazelcast.xml", "file:./hazelcast.yaml",
"classpath:/hazelcast.yaml");
} }
} }
......
...@@ -28,7 +28,9 @@ import org.junit.Test; ...@@ -28,7 +28,9 @@ import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -63,35 +65,55 @@ public class HazelcastAutoConfigurationClientTests { ...@@ -63,35 +65,55 @@ public class HazelcastAutoConfigurationClientTests {
.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class)); .withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class));
@Test @Test
public void systemProperty() { public void systemPropertyWithXml() {
this.contextRunner this.contextRunner
.withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY .withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/" + "=classpath:org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-client-specific.xml") + "hazelcast-client-specific.xml")
.run((context) -> assertThat(context).getBean(HazelcastInstance.class) .run(assertSpecificHazelcastClient("explicit-xml"));
.isInstanceOf(HazelcastInstance.class)
.has(nameStartingWith("hz.client_")));
} }
@Test @Test
public void explicitConfigFile() { public void systemPropertyWithYaml() {
this.contextRunner
.withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-client-specific.yaml")
.run(assertSpecificHazelcastClient("explicit-yaml"));
}
@Test
public void explicitConfigFileWithXml() {
this.contextRunner this.contextRunner
.withPropertyValues( .withPropertyValues(
"spring.hazelcast.config=org/springframework/boot/autoconfigure/" "spring.hazelcast.config=org/springframework/boot/autoconfigure/"
+ "hazelcast/hazelcast-client-specific.xml") + "hazelcast/hazelcast-client-specific.xml")
.run((context) -> assertThat(context).getBean(HazelcastInstance.class) .run(assertSpecificHazelcastClient("explicit-xml"));
.isInstanceOf(HazelcastClientProxy.class)
.has(nameStartingWith("hz.client_")));
} }
@Test @Test
public void explicitConfigUrl() { public void explicitConfigFileWithYaml() {
this.contextRunner this.contextRunner
.withPropertyValues( .withPropertyValues(
"spring.hazelcast.config=hazelcast-client-default.xml") "spring.hazelcast.config=org/springframework/boot/autoconfigure/"
.run((context) -> assertThat(context).getBean(HazelcastInstance.class) + "hazelcast/hazelcast-client-specific.yaml")
.isInstanceOf(HazelcastClientProxy.class) .run(assertSpecificHazelcastClient("explicit-yaml"));
.has(nameStartingWith("hz.client_"))); }
@Test
public void explicitConfigUrlWithXml() {
this.contextRunner.withPropertyValues(
"spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-client-specific.xml")
.run(assertSpecificHazelcastClient("explicit-xml"));
}
@Test
public void explicitConfigUrlWithYaml() {
this.contextRunner.withPropertyValues(
"spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-client-specific.yaml")
.run(assertSpecificHazelcastClient("explicit-yaml"));
} }
@Test @Test
...@@ -111,9 +133,16 @@ public class HazelcastAutoConfigurationClientTests { ...@@ -111,9 +133,16 @@ public class HazelcastAutoConfigurationClientTests {
.isInstanceOf(HazelcastClientProxy.class)); .isInstanceOf(HazelcastClientProxy.class));
} }
private Condition<HazelcastInstance> nameStartingWith(String prefix) { private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastClient(
return new Condition<>((o) -> o.getName().startsWith(prefix), String label) {
"Name starts with " + prefix); return (context) -> assertThat(context).getBean(HazelcastInstance.class)
.isInstanceOf(HazelcastInstance.class).has(labelEqualTo(label));
}
private static Condition<HazelcastInstance> labelEqualTo(String label) {
return new Condition<>((o) -> ((HazelcastClientProxy) o).getClientConfig()
.getLabels().stream().anyMatch((e) -> e.equals(label)),
"Label equals to " + label);
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
......
...@@ -27,7 +27,9 @@ import org.junit.runner.RunWith; ...@@ -27,7 +27,9 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -59,39 +61,71 @@ public class HazelcastAutoConfigurationServerTests { ...@@ -59,39 +61,71 @@ public class HazelcastAutoConfigurationServerTests {
} }
@Test @Test
public void systemProperty() { public void systemPropertyWithXml() {
this.contextRunner this.contextRunner
.withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY .withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml") + "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml")
.run((context) -> { .run((context) -> {
Config config = context.getBean(HazelcastInstance.class).getConfig(); Config config = context.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getQueueConfigs().keySet()).containsOnly("foobar"); assertThat(config.getMapConfigs().keySet()).containsOnly("foobar");
}); });
} }
@Test @Test
public void explicitConfigFile() { public void systemPropertyWithYaml() {
this.contextRunner.withPropertyValues( this.contextRunner
"spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" .withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
+ "hazelcast-specific.xml") + "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml")
.run((context) -> { .run((context) -> {
Config config = context.getBean(HazelcastInstance.class).getConfig(); Config config = context.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getConfigurationFile()) assertThat(config.getMapConfigs().keySet()).containsOnly("foobar");
.isEqualTo(new ClassPathResource(
"org/springframework/boot/autoconfigure/hazelcast"
+ "/hazelcast-specific.xml").getFile());
}); });
} }
@Test @Test
public void explicitConfigUrl() { public void explicitConfigFileWithXml() {
this.contextRunner.withPropertyValues(
"spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-specific.xml")
.run(assertSpecificHazelcastServer(
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"));
}
@Test
public void explicitConfigFileWithYaml() {
this.contextRunner.withPropertyValues(
"spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-specific.yaml")
.run(assertSpecificHazelcastServer(
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml"));
}
@Test
public void explicitConfigUrlWithXml() {
this.contextRunner this.contextRunner
.withPropertyValues("spring.hazelcast.config=hazelcast-default.xml") .withPropertyValues(
.run((context) -> { "spring.hazelcast.config=classpath:org/springframework/"
Config config = context.getBean(HazelcastInstance.class).getConfig(); + "boot/autoconfigure/hazelcast/hazelcast-specific.xml")
assertThat(config.getConfigurationUrl()).isEqualTo( .run(assertSpecificHazelcastServer(
new ClassPathResource("hazelcast-default.xml").getURL()); "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"));
}); }
@Test
public void explicitConfigUrlWithYaml() {
this.contextRunner
.withPropertyValues(
"spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-specific.yaml")
.run(assertSpecificHazelcastServer(
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml"));
}
private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastServer(
String location) {
return (context) -> {
Config config = context.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getConfigurationUrl()).asString().endsWith(location);
};
} }
@Test @Test
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -39,6 +39,8 @@ public class HazelcastAutoConfigurationTests { ...@@ -39,6 +39,8 @@ public class HazelcastAutoConfigurationTests {
@Test @Test
public void defaultConfigFile() { public void defaultConfigFile() {
// no hazelcast-client.xml and hazelcast.xml is present in root classpath // 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.
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
Config config = context.getBean(HazelcastInstance.class).getConfig(); Config config = context.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getConfigurationUrl()) assertThat(config.getConfigurationUrl())
......
...@@ -2,5 +2,8 @@ ...@@ -2,5 +2,8 @@
<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config" <hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-3.12.xsd"> xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-3.12.xsd">
<client-labels>
<label>explicit-xml</label>
</client-labels>
</hazelcast-client> </hazelcast-client>
hazelcast:
network:
join:
multicast:
enabled: false
map:
foobar:
time-to-live-seconds: 3600
max-idle-seconds: 600
...@@ -6539,8 +6539,8 @@ If you define a `com.hazelcast.config.Config` bean, Spring Boot uses that. If yo ...@@ -6539,8 +6539,8 @@ If you define a `com.hazelcast.config.Config` bean, Spring Boot uses that. If yo
configuration defines an instance name, Spring Boot tries to locate an existing instance configuration defines an instance name, Spring Boot tries to locate an existing instance
rather than creating a new one. rather than creating a new one.
You could also specify the `hazelcast.xml` configuration file to use through You could also specify the Hazelcast configuration file to use through configuration, as
configuration, as shown in the following example: shown in the following example:
[source,properties,indent=0] [source,properties,indent=0]
---- ----
...@@ -6548,8 +6548,9 @@ configuration, as shown in the following example: ...@@ -6548,8 +6548,9 @@ configuration, as shown in the following example:
---- ----
Otherwise, Spring Boot tries to find the Hazelcast configuration from the default 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. We locations: `hazelcast.xml` in the working directory or at the root of the classpath, or
also check if the `hazelcast.config` system property is set. See the a `.yaml` counterpart in the same locations. We also check if the `hazelcast.config`
system property is set. See the
https://docs.hazelcast.org/docs/latest/manual/html-single/[Hazelcast documentation] for https://docs.hazelcast.org/docs/latest/manual/html-single/[Hazelcast documentation] for
more details. more details.
...@@ -6560,6 +6561,7 @@ client by checking the following configuration options: ...@@ -6560,6 +6561,7 @@ client by checking the following configuration options:
* A configuration file defined by the `spring.hazelcast.config` property. * A configuration file defined by the `spring.hazelcast.config` property.
* The presence of the `hazelcast.client.config` system property. * The presence of the `hazelcast.client.config` system property.
* A `hazelcast-client.xml` in the working directory or at the root of the classpath. * A `hazelcast-client.xml` in the working directory or at the root of the classpath.
* A `hazelcast-client.yaml` in the working directory or at the root of the classpath.
NOTE: Spring Boot also has NOTE: Spring Boot also has
<<boot-features-caching-provider-hazelcast,explicit caching support for Hazelcast>>. If <<boot-features-caching-provider-hazelcast,explicit caching support for Hazelcast>>. If
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment