Skip to content

Commit 617786e

Browse files
committed
Polish "Disable Hazelcast auto-configuration when Jet is present"
See gh-20729
1 parent 002a7c6 commit 617786e

File tree

3 files changed

+48
-61
lines changed

3 files changed

+48
-61
lines changed

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

Lines changed: 27 additions & 3 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,14 +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;
2430
import org.springframework.context.annotation.Conditional;
2531
import org.springframework.context.annotation.Configuration;
2632
import org.springframework.context.annotation.Import;
33+
import org.springframework.core.io.Resource;
34+
import org.springframework.core.type.AnnotatedTypeMetadata;
2735

2836
/**
29-
* {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a
37+
* {@link EnableAutoConfiguration Auto-configuration} for Hazelcast IMDG. Creates a
3038
* {@link HazelcastInstance} based on explicit configuration or when a default
3139
* configuration file is found in the environment.
3240
*
@@ -36,10 +44,26 @@
3644
* @see HazelcastConfigResourceCondition
3745
*/
3846
@Configuration(proxyBeanMethods = false)
39-
@Conditional(HazelcastJetConfigMissingCondition.class)
47+
@Conditional(HazelcastDataGridCondition.class)
4048
@ConditionalOnClass(HazelcastInstance.class)
4149
@EnableConfigurationProperties(HazelcastProperties.class)
4250
@Import({ HazelcastClientConfiguration.class, HazelcastServerConfiguration.class })
4351
public class HazelcastAutoConfiguration {
4452

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+
4569
}

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

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

Lines changed: 21 additions & 14 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.
@@ -16,17 +16,18 @@
1616

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

19-
import java.net.MalformedURLException;
19+
import java.io.IOException;
2020
import java.net.URL;
21+
import java.net.URLClassLoader;
2122

2223
import com.hazelcast.config.Config;
2324
import com.hazelcast.core.HazelcastInstance;
24-
import org.jetbrains.annotations.Nullable;
2525
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.boot.autoconfigure.AutoConfigurations;
2828
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2929
import org.springframework.core.io.ClassPathResource;
30+
import org.springframework.core.io.Resource;
3031

3132
import static org.assertj.core.api.Assertions.assertThat;
3233

@@ -52,34 +53,40 @@ void defaultConfigFile() {
5253
}
5354

5455
@Test
55-
void testHazelcastInstanceNotCreatedWhenJetIsPresent() {
56+
void hazelcastInstanceNotCreatedWhenJetIsPresent() {
5657
this.contextRunner.withClassLoader(new JetConfigClassLoader())
5758
.run((context) -> assertThat(context).doesNotHaveBean(HazelcastInstance.class));
5859
}
5960

6061
/**
61-
* A specific class loader which emulates that default Hazelcast Jet configuration
62-
* file exists on the classpath.
62+
* A test {link {@link URLClassLoader} that emulates the default Hazelcast Jet
63+
* configuration file exists on the classpath.
6364
*/
64-
static class JetConfigClassLoader extends ClassLoader {
65+
static class JetConfigClassLoader extends URLClassLoader {
66+
67+
private static final Resource FALLBACK = new ClassPathResource("hazelcast.yaml");
6568

6669
JetConfigClassLoader() {
67-
super(JetConfigClassLoader.class.getClassLoader());
70+
super(new URL[0], JetConfigClassLoader.class.getClassLoader());
6871
}
6972

70-
@Nullable
7173
@Override
7274
public URL getResource(String name) {
7375
if (name.equals("hazelcast-jet-default.yaml")) {
74-
try {
75-
return new URL("file://hazelcast-jet-default.yaml");
76-
}
77-
catch (MalformedURLException ignored) {
78-
}
76+
return getEmulatedJestConfigUrl();
7977
}
8078
return super.getResource(name);
8179
}
8280

81+
private URL getEmulatedJestConfigUrl() {
82+
try {
83+
return FALLBACK.getURL();
84+
}
85+
catch (IOException ex) {
86+
throw new IllegalArgumentException(ex);
87+
}
88+
}
89+
8390
}
8491

8592
}

0 commit comments

Comments
 (0)