Polish DevToolsEnablementDeducer and OnEnabledDevToolsCondition
See gh-16732
This commit is contained in:
parent
ca51609cdf
commit
7c8bf48ebb
|
|
@ -21,11 +21,12 @@ import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility to deduce if Devtools should be enabled in the current context.
|
* Utility to deduce if DevTools should be enabled in the current context.
|
||||||
*
|
*
|
||||||
* @author Madhura Bhave
|
* @author Madhura Bhave
|
||||||
|
* @since 2.2.0
|
||||||
*/
|
*/
|
||||||
public final class DevtoolsEnablementDeducer {
|
public final class DevToolsEnablementDeducer {
|
||||||
|
|
||||||
private static final Set<String> SKIPPED_STACK_ELEMENTS;
|
private static final Set<String> SKIPPED_STACK_ELEMENTS;
|
||||||
|
|
||||||
|
|
@ -38,7 +39,7 @@ public final class DevtoolsEnablementDeducer {
|
||||||
SKIPPED_STACK_ELEMENTS = Collections.unmodifiableSet(skipped);
|
SKIPPED_STACK_ELEMENTS = Collections.unmodifiableSet(skipped);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DevtoolsEnablementDeducer() {
|
private DevToolsEnablementDeducer() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -55,7 +55,7 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
* @since 1.3.3
|
* @since 1.3.3
|
||||||
*/
|
*/
|
||||||
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
|
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
|
||||||
@Conditional({ OnEnabledDevtoolsCondition.class, DevToolsDataSourceCondition.class })
|
@Conditional({ OnEnabledDevToolsCondition.class, DevToolsDataSourceCondition.class })
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
public class DevToolsDataSourceAutoConfiguration {
|
public class DevToolsDataSourceAutoConfiguration {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ package org.springframework.boot.devtools.autoconfigure;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||||
import org.springframework.boot.devtools.DevtoolsEnablementDeducer;
|
import org.springframework.boot.devtools.DevToolsEnablementDeducer;
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
|
|
||||||
|
|
@ -27,14 +27,15 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
* A condition that checks if DevTools should be enabled.
|
* A condition that checks if DevTools should be enabled.
|
||||||
*
|
*
|
||||||
* @author Madhura Bhave
|
* @author Madhura Bhave
|
||||||
|
* @since 2.2.0
|
||||||
*/
|
*/
|
||||||
public class OnEnabledDevtoolsCondition extends SpringBootCondition {
|
public class OnEnabledDevToolsCondition extends SpringBootCondition {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
||||||
AnnotatedTypeMetadata metadata) {
|
AnnotatedTypeMetadata metadata) {
|
||||||
ConditionMessage.Builder message = ConditionMessage.forCondition("Devtools");
|
ConditionMessage.Builder message = ConditionMessage.forCondition("Devtools");
|
||||||
boolean shouldEnable = DevtoolsEnablementDeducer
|
boolean shouldEnable = DevToolsEnablementDeducer
|
||||||
.shouldEnable(Thread.currentThread());
|
.shouldEnable(Thread.currentThread());
|
||||||
if (!shouldEnable) {
|
if (!shouldEnable) {
|
||||||
return ConditionOutcome.noMatch(
|
return ConditionOutcome.noMatch(
|
||||||
|
|
@ -56,7 +56,7 @@ import org.springframework.http.server.ServerHttpRequest;
|
||||||
* @since 1.3.0
|
* @since 1.3.0
|
||||||
*/
|
*/
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@Conditional(OnEnabledDevtoolsCondition.class)
|
@Conditional(OnEnabledDevToolsCondition.class)
|
||||||
@ConditionalOnProperty(prefix = "spring.devtools.remote", name = "secret")
|
@ConditionalOnProperty(prefix = "spring.devtools.remote", name = "secret")
|
||||||
@ConditionalOnClass({ Filter.class, ServerHttpRequest.class })
|
@ConditionalOnClass({ Filter.class, ServerHttpRequest.class })
|
||||||
@EnableConfigurationProperties({ ServerProperties.class, DevToolsProperties.class })
|
@EnableConfigurationProperties({ ServerProperties.class, DevToolsProperties.class })
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ import java.io.IOException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.devtools.DevtoolsEnablementDeducer;
|
import org.springframework.boot.devtools.DevToolsEnablementDeducer;
|
||||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||||
import org.springframework.core.env.ConfigurableEnvironment;
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.PropertiesPropertySource;
|
import org.springframework.core.env.PropertiesPropertySource;
|
||||||
|
|
@ -44,7 +44,7 @@ public class DevToolsHomePropertiesPostProcessor implements EnvironmentPostProce
|
||||||
@Override
|
@Override
|
||||||
public void postProcessEnvironment(ConfigurableEnvironment environment,
|
public void postProcessEnvironment(ConfigurableEnvironment environment,
|
||||||
SpringApplication application) {
|
SpringApplication application) {
|
||||||
if (DevtoolsEnablementDeducer.shouldEnable(Thread.currentThread())) {
|
if (DevToolsEnablementDeducer.shouldEnable(Thread.currentThread())) {
|
||||||
File home = getHomeFolder();
|
File home = getHomeFolder();
|
||||||
File propertyFile = (home != null) ? new File(home, FILE_NAME) : null;
|
File propertyFile = (home != null) ? new File(home, FILE_NAME) : null;
|
||||||
if (propertyFile != null && propertyFile.exists() && propertyFile.isFile()) {
|
if (propertyFile != null && propertyFile.exists() && propertyFile.isFile()) {
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ import java.util.Map;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.devtools.DevtoolsEnablementDeducer;
|
import org.springframework.boot.devtools.DevToolsEnablementDeducer;
|
||||||
import org.springframework.boot.devtools.logger.DevToolsLogFactory;
|
import org.springframework.boot.devtools.logger.DevToolsLogFactory;
|
||||||
import org.springframework.boot.devtools.restart.Restarter;
|
import org.springframework.boot.devtools.restart.Restarter;
|
||||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||||
|
|
@ -80,7 +80,7 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
|
||||||
@Override
|
@Override
|
||||||
public void postProcessEnvironment(ConfigurableEnvironment environment,
|
public void postProcessEnvironment(ConfigurableEnvironment environment,
|
||||||
SpringApplication application) {
|
SpringApplication application) {
|
||||||
if (DevtoolsEnablementDeducer.shouldEnable(Thread.currentThread())
|
if (DevToolsEnablementDeducer.shouldEnable(Thread.currentThread())
|
||||||
&& isLocalApplication(environment)) {
|
&& isLocalApplication(environment)) {
|
||||||
if (canAddProperties(environment)) {
|
if (canAddProperties(environment)) {
|
||||||
logger.info("Devtools property defaults active! Set '" + ENABLED
|
logger.info("Devtools property defaults active! Set '" + ENABLED
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ package org.springframework.boot.devtools.restart;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
import org.springframework.boot.devtools.DevtoolsEnablementDeducer;
|
import org.springframework.boot.devtools.DevToolsEnablementDeducer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default {@link RestartInitializer} that only enable initial restart when running a
|
* Default {@link RestartInitializer} that only enable initial restart when running a
|
||||||
|
|
@ -36,7 +36,7 @@ public class DefaultRestartInitializer implements RestartInitializer {
|
||||||
if (!isMain(thread)) {
|
if (!isMain(thread)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!DevtoolsEnablementDeducer.shouldEnable(thread)) {
|
if (!DevToolsEnablementDeducer.shouldEnable(thread)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return getUrls(thread);
|
return getUrls(thread);
|
||||||
|
|
|
||||||
|
|
@ -27,11 +27,11 @@ import org.springframework.context.annotation.Configuration;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link OnEnabledDevtoolsCondition}.
|
* Tests for {@link OnEnabledDevToolsCondition}.
|
||||||
*
|
*
|
||||||
* @author Madhura Bhave
|
* @author Madhura Bhave
|
||||||
*/
|
*/
|
||||||
public class OnEnabledDevtoolsConditionTests {
|
public class OnEnabledDevToolsConditionTests {
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context;
|
private AnnotationConfigApplicationContext context;
|
||||||
|
|
||||||
|
|
@ -44,8 +44,8 @@ public class OnEnabledDevtoolsConditionTests {
|
||||||
@Test
|
@Test
|
||||||
public void outcomeWhenDevtoolsShouldBeEnabledIsTrueShouldMatch() throws Exception {
|
public void outcomeWhenDevtoolsShouldBeEnabledIsTrueShouldMatch() throws Exception {
|
||||||
Thread thread = new Thread(() -> {
|
Thread thread = new Thread(() -> {
|
||||||
OnEnabledDevtoolsConditionTests.this.context.refresh();
|
OnEnabledDevToolsConditionTests.this.context.refresh();
|
||||||
assertThat(OnEnabledDevtoolsConditionTests.this.context.containsBean("test"))
|
assertThat(OnEnabledDevToolsConditionTests.this.context.containsBean("test"))
|
||||||
.isTrue();
|
.isTrue();
|
||||||
});
|
});
|
||||||
thread.start();
|
thread.start();
|
||||||
|
|
@ -54,8 +54,8 @@ public class OnEnabledDevtoolsConditionTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void outcomeWhenDevtoolsShouldBeEnabledIsFalseShouldNotMatch() {
|
public void outcomeWhenDevtoolsShouldBeEnabledIsFalseShouldNotMatch() {
|
||||||
OnEnabledDevtoolsConditionTests.this.context.refresh();
|
OnEnabledDevToolsConditionTests.this.context.refresh();
|
||||||
assertThat(OnEnabledDevtoolsConditionTests.this.context.containsBean("test"))
|
assertThat(OnEnabledDevToolsConditionTests.this.context.containsBean("test"))
|
||||||
.isFalse();
|
.isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,7 +63,7 @@ public class OnEnabledDevtoolsConditionTests {
|
||||||
static class TestConfiguration {
|
static class TestConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Conditional(OnEnabledDevtoolsCondition.class)
|
@Conditional(OnEnabledDevToolsCondition.class)
|
||||||
public String test() {
|
public String test() {
|
||||||
return "hello";
|
return "hello";
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue