Polish "Add support for Hazelcast YAML configuration"

Closes gh-16632
pull/16684/head
Stephane Nicoll 6 years ago
parent 82e18ae88f
commit 704da1750b

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -60,14 +60,11 @@ public class HazelcastClientFactory {
private ClientConfig getClientConfig(Resource clientConfigLocation)
throws IOException {
URL configUrl = clientConfigLocation.getURL();
String configFileName = configUrl.getFile();
if (configFileName.endsWith(".xml")) {
return new XmlClientConfigBuilder(configUrl).build();
}
else {
String configFileName = configUrl.getPath();
if (configFileName.endsWith(".yaml")) {
return new YamlClientConfigBuilder(configUrl).build();
}
return new XmlClientConfigBuilder(configUrl).build();
}
/**

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
@ -73,12 +73,12 @@ public class HazelcastInstanceFactory {
}
private static Config createConfig(URL configUrl) throws IOException {
String configFileName = configUrl.getFile();
if (configFileName.endsWith(".xml")) {
return new XmlConfigBuilder(configUrl).build();
}
String configFileName = configUrl.getPath();
if (configFileName.endsWith(".yaml")) {
return new YamlConfigBuilder(configUrl).build();
}
return new XmlConfigBuilder(configUrl).build();
}
/**
* Get the {@link HazelcastInstance}.

@ -30,6 +30,7 @@ import org.springframework.beans.factory.BeanCreationException;
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.ContextConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -65,57 +66,54 @@ public class HazelcastAutoConfigurationClientTests {
@Test
public void systemPropertyWithXml() {
systemProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY
this.contextRunner
.withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-client-specific.xml");
+ "hazelcast-client-specific.xml")
.run(assertSpecificHazelcastClient("explicit-xml"));
}
@Test
public void systemPropertyWithYaml() {
systemProperty(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY
this.contextRunner
.withSystemProperties(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-client-specific.yaml");
}
private void systemProperty(String systemProperty) {
this.contextRunner.withSystemProperties(systemProperty)
.run((context) -> assertHazelcastClientSpecific(context));
+ "hazelcast-client-specific.yaml")
.run(assertSpecificHazelcastClient("explicit-yaml"));
}
@Test
public void explicitConfigFileWithXml() {
propertyValues("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
+ "hazelcast/hazelcast-client-specific.xml");
this.contextRunner
.withPropertyValues(
"spring.hazelcast.config=org/springframework/boot/autoconfigure/"
+ "hazelcast/hazelcast-client-specific.xml")
.run(assertSpecificHazelcastClient("explicit-xml"));
}
@Test
public void explicitConfigFileWithYaml() {
propertyValues("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
+ "hazelcast/hazelcast-client-specific.yaml");
this.contextRunner
.withPropertyValues(
"spring.hazelcast.config=org/springframework/boot/autoconfigure/"
+ "hazelcast/hazelcast-client-specific.yaml")
.run(assertSpecificHazelcastClient("explicit-yaml"));
}
@Test
public void explicitConfigUrlWithXml() {
propertyValues("spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-client-specific.xml");
this.contextRunner.withPropertyValues(
"spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-client-specific.xml")
.run(assertSpecificHazelcastClient("explicit-xml"));
}
@Test
public void explicitConfigUrlWithYaml() {
propertyValues("spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-client-specific.yaml");
}
private void propertyValues(String propertyValues) {
this.contextRunner.withPropertyValues(propertyValues)
.run((context) -> assertHazelcastClientSpecific(context));
}
private static void assertHazelcastClientSpecific(
AssertableApplicationContext context) {
assertThat(context).getBean(HazelcastInstance.class)
.isInstanceOf(HazelcastInstance.class)
.has(labelEqualTo("configured-client"));
this.contextRunner.withPropertyValues(
"spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-client-specific.yaml")
.run(assertSpecificHazelcastClient("explicit-yaml"));
}
@Test
@ -135,6 +133,12 @@ public class HazelcastAutoConfigurationClientTests {
.isInstanceOf(HazelcastClientProxy.class));
}
private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastClient(
String label) {
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)),

@ -29,6 +29,7 @@ import org.springframework.beans.factory.BeanCreationException;
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.ContextConsumer;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.context.annotation.Bean;
@ -61,55 +62,70 @@ public class HazelcastAutoConfigurationServerTests {
@Test
public void systemPropertyWithXml() {
systemProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml");
this.contextRunner
.withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml")
.run((context) -> {
Config config = context.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getMapConfigs().keySet()).containsOnly("foobar");
});
}
@Test
public void systemPropertyWithYaml() {
systemProperty(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml");
}
private void systemProperty(String systemProperties) {
this.contextRunner.withSystemProperties(systemProperties)
.run((context) -> assertHazelcastSpecific(context));
this.contextRunner
.withSystemProperties(HazelcastServerConfiguration.CONFIG_SYSTEM_PROPERTY
+ "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml")
.run((context) -> {
Config config = context.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getMapConfigs().keySet()).containsOnly("foobar");
});
}
@Test
public void explicitConfigFileWithXml() {
propertyValues(
this.contextRunner.withPropertyValues(
"spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-specific.xml");
+ "hazelcast-specific.xml")
.run(assertSpecificHazelcastServer(
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"));
}
@Test
public void explicitConfigFileWithYaml() {
propertyValues(
this.contextRunner.withPropertyValues(
"spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
+ "hazelcast-specific.yaml");
+ "hazelcast-specific.yaml")
.run(assertSpecificHazelcastServer(
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.yaml"));
}
@Test
public void explicitConfigUrlWithXml() {
propertyValues("spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-specific.xml");
this.contextRunner
.withPropertyValues(
"spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-specific.xml")
.run(assertSpecificHazelcastServer(
"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"));
}
@Test
public void explicitConfigUrlWithYaml() {
propertyValues("spring.hazelcast.config=classpath:org/springframework/"
+ "boot/autoconfigure/hazelcast/hazelcast-specific.yaml");
}
private void propertyValues(String propertyValues) {
this.contextRunner.withPropertyValues(propertyValues)
.run((context) -> assertHazelcastSpecific(context));
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 static void assertHazelcastSpecific(AssertableApplicationContext context) {
private ContextConsumer<AssertableApplicationContext> assertSpecificHazelcastServer(
String location) {
return (context) -> {
Config config = context.getBean(HazelcastInstance.class).getConfig();
assertThat(config.getMapConfigs().keySet()).containsOnly("foobar");
assertThat(config.getConfigurationUrl()).asString().endsWith(location);
};
}
@Test

@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.

@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/client-config hazelcast-client-config-3.12.xsd">
<client-labels>
<label>configured-client</label>
<label>explicit-xml</label>
</client-labels>
</hazelcast-client>

@ -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
rather than creating a new one.
You could also specify the `hazelcast.xml` configuration file to use through
configuration, as shown in the following example:
You could also specify the Hazelcast configuration file to use through configuration, as
shown in the following example:
[source,properties,indent=0]
----
@ -6548,8 +6548,9 @@ configuration, as shown in the following example:
----
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
also check if the `hazelcast.config` system property is set. See the
locations: `hazelcast.xml` in the working directory or at the root of the classpath, or
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
more details.
@ -6560,6 +6561,7 @@ client by checking the following configuration options:
* A configuration file defined by the `spring.hazelcast.config` 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.yaml` in the working directory or at the root of the classpath.
NOTE: Spring Boot also has
<<boot-features-caching-provider-hazelcast,explicit caching support for Hazelcast>>. If

Loading…
Cancel
Save