Add support for Hazelcast

This commit upgrades to Hazelcast 4.0.3, yet keeping compatibility with
Hazelcast 3.x.

Closes gh-20856
Closes gh-23475
This commit is contained in:
Stephane Nicoll
2020-09-09 17:48:49 +02:00
parent 7b183ef99d
commit 4009acf025
32 changed files with 418 additions and 79 deletions

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2012-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.hazelcast;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import com.hazelcast.client.config.ClientConfigRecognizer;
import com.hazelcast.config.ConfigStream;
import org.springframework.boot.autoconfigure.condition.ConditionMessage.Builder;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* {@link HazelcastConfigResourceCondition} that checks if the
* {@code spring.hazelcast.config} configuration key is defined.
*/
class HazelcastClientConfigAvailableCondition extends HazelcastConfigResourceCondition {
HazelcastClientConfigAvailableCondition() {
super(HazelcastClientConfiguration.CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml",
"classpath:/hazelcast-client.xml", "file:./hazelcast-client.yaml", "classpath:/hazelcast-client.yaml");
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (context.getEnvironment().containsProperty(HAZELCAST_CONFIG_PROPERTY)) {
ConditionOutcome configValidationOutcome = Hazelcast4ClientValidation.clientConfigOutcome(context,
HAZELCAST_CONFIG_PROPERTY, startConditionMessage());
return (configValidationOutcome != null) ? configValidationOutcome : ConditionOutcome
.match(startConditionMessage().foundExactly("property " + HAZELCAST_CONFIG_PROPERTY));
}
return getResourceOutcome(context, metadata);
}
static class Hazelcast4ClientValidation {
static ConditionOutcome clientConfigOutcome(ConditionContext context, String propertyName, Builder builder) {
String resourcePath = context.getEnvironment().getProperty(propertyName);
Resource resource = context.getResourceLoader().getResource(resourcePath);
if (!resource.exists()) {
return ConditionOutcome.noMatch(builder.because("Hazelcast configuration does not exist"));
}
try (InputStream in = resource.getInputStream()) {
boolean clientConfig = new ClientConfigRecognizer().isRecognized(new ConfigStream(in));
return new ConditionOutcome(clientConfig, existingConfigurationOutcome(resource, clientConfig));
}
catch (Throwable ex) { // Hazelcast 4 specific API
return null;
}
}
private static String existingConfigurationOutcome(Resource resource, boolean client) throws IOException {
URL location = resource.getURL();
return client ? "Hazelcast client configuration detected at '" + location + "'"
: "Hazelcast server configuration detected at '" + location + "'";
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -45,7 +45,7 @@ class HazelcastClientConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(ClientConfig.class)
@Conditional(ConfigAvailableCondition.class)
@Conditional(HazelcastClientConfigAvailableCondition.class)
static class HazelcastClientConfigFileConfiguration {
@Bean
@@ -70,17 +70,4 @@ class HazelcastClientConfiguration {
}
/**
* {@link HazelcastConfigResourceCondition} that checks if the
* {@code spring.hazelcast.config} configuration key is defined.
*/
static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
ConfigAvailableCondition() {
super(CONFIG_SYSTEM_PROPERTY, "file:./hazelcast-client.xml", "classpath:/hazelcast-client.xml",
"file:./hazelcast-client.yaml", "classpath:/hazelcast-client.yaml");
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -35,10 +35,12 @@ import org.springframework.util.Assert;
*/
public abstract class HazelcastConfigResourceCondition extends ResourceCondition {
protected static final String HAZELCAST_CONFIG_PROPERTY = "spring.hazelcast.config";
private final String configSystemProperty;
protected HazelcastConfigResourceCondition(String configSystemProperty, String... resourceLocations) {
super("Hazelcast", "spring.hazelcast.config", resourceLocations);
super("Hazelcast", HAZELCAST_CONFIG_PROPERTY, resourceLocations);
Assert.notNull(configSystemProperty, "ConfigSystemProperty must not be null");
this.configSystemProperty = configSystemProperty;
}