Skip to content

Commit b9cb1c8

Browse files
committed
Merge pull request #20729 from gurbuzali
* pr/20729: Polish "Disable Hazelcast auto-configuration when Jet is present" Disable Hazelcast auto-configuration when Jet is present Closes gh-20729
2 parents e0b3916 + 617786e commit b9cb1c8

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -19,13 +19,22 @@
1919
import com.hazelcast.core.HazelcastInstance;
2020

2121
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
22+
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
23+
import org.springframework.boot.autoconfigure.condition.ConditionMessage.Builder;
24+
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
2225
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26+
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
27+
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration.HazelcastDataGridCondition;
2328
import org.springframework.boot.context.properties.EnableConfigurationProperties;
29+
import org.springframework.context.annotation.ConditionContext;
30+
import org.springframework.context.annotation.Conditional;
2431
import org.springframework.context.annotation.Configuration;
2532
import org.springframework.context.annotation.Import;
33+
import org.springframework.core.io.Resource;
34+
import org.springframework.core.type.AnnotatedTypeMetadata;
2635

2736
/**
28-
* {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a
37+
* {@link EnableAutoConfiguration Auto-configuration} for Hazelcast IMDG. Creates a
2938
* {@link HazelcastInstance} based on explicit configuration or when a default
3039
* configuration file is found in the environment.
3140
*
@@ -35,9 +44,26 @@
3544
* @see HazelcastConfigResourceCondition
3645
*/
3746
@Configuration(proxyBeanMethods = false)
47+
@Conditional(HazelcastDataGridCondition.class)
3848
@ConditionalOnClass(HazelcastInstance.class)
3949
@EnableConfigurationProperties(HazelcastProperties.class)
4050
@Import({ HazelcastClientConfiguration.class, HazelcastServerConfiguration.class })
4151
public class HazelcastAutoConfiguration {
4252

53+
static class HazelcastDataGridCondition extends SpringBootCondition {
54+
55+
private static final String HAZELCAST_JET_CONFIG_FILE = "classpath:/hazelcast-jet-default.yaml";
56+
57+
@Override
58+
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
59+
Builder message = ConditionMessage.forCondition(HazelcastDataGridCondition.class.getSimpleName());
60+
Resource resource = context.getResourceLoader().getResource(HAZELCAST_JET_CONFIG_FILE);
61+
if (resource.exists()) {
62+
return ConditionOutcome.noMatch(message.because("Found Hazelcast Jet on the classpath"));
63+
}
64+
return ConditionOutcome.match(message.because("Hazelcast Jet not found on the classpath"));
65+
}
66+
67+
}
68+
4369
}

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -16,13 +16,18 @@
1616

1717
package org.springframework.boot.autoconfigure.hazelcast;
1818

19+
import java.io.IOException;
20+
import java.net.URL;
21+
import java.net.URLClassLoader;
22+
1923
import com.hazelcast.config.Config;
2024
import com.hazelcast.core.HazelcastInstance;
2125
import org.junit.jupiter.api.Test;
2226

2327
import org.springframework.boot.autoconfigure.AutoConfigurations;
2428
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2529
import org.springframework.core.io.ClassPathResource;
30+
import org.springframework.core.io.Resource;
2631

2732
import static org.assertj.core.api.Assertions.assertThat;
2833

@@ -47,4 +52,41 @@ void defaultConfigFile() {
4752
});
4853
}
4954

55+
@Test
56+
void hazelcastInstanceNotCreatedWhenJetIsPresent() {
57+
this.contextRunner.withClassLoader(new JetConfigClassLoader())
58+
.run((context) -> assertThat(context).doesNotHaveBean(HazelcastInstance.class));
59+
}
60+
61+
/**
62+
* A test {link {@link URLClassLoader} that emulates the default Hazelcast Jet
63+
* configuration file exists on the classpath.
64+
*/
65+
static class JetConfigClassLoader extends URLClassLoader {
66+
67+
private static final Resource FALLBACK = new ClassPathResource("hazelcast.yaml");
68+
69+
JetConfigClassLoader() {
70+
super(new URL[0], JetConfigClassLoader.class.getClassLoader());
71+
}
72+
73+
@Override
74+
public URL getResource(String name) {
75+
if (name.equals("hazelcast-jet-default.yaml")) {
76+
return getEmulatedJestConfigUrl();
77+
}
78+
return super.getResource(name);
79+
}
80+
81+
private URL getEmulatedJestConfigUrl() {
82+
try {
83+
return FALLBACK.getURL();
84+
}
85+
catch (IOException ex) {
86+
throw new IllegalArgumentException(ex);
87+
}
88+
}
89+
90+
}
91+
5092
}

0 commit comments

Comments
 (0)