From 72707b9d5898f2164f88e3af5e30d49f062c84be Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Tue, 7 Aug 2018 20:37:28 +0200 Subject: [PATCH] Polish OnWebApplicationCondition Closes gh-14015 --- .../condition/OnWebApplicationCondition.java | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnWebApplicationCondition.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnWebApplicationCondition.java index 7ae98f285b..d6143221c9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnWebApplicationCondition.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnWebApplicationCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -62,8 +62,6 @@ class OnWebApplicationCondition extends SpringBootCondition { private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata, boolean required) { - ConditionMessage.Builder message = ConditionMessage.forCondition( - ConditionalOnWebApplication.class, required ? "(required)" : ""); Type type = deduceType(metadata); if (Type.SERVLET == type) { return isServletWebApplication(context); @@ -72,23 +70,30 @@ class OnWebApplicationCondition extends SpringBootCondition { return isReactiveWebApplication(context); } else { - ConditionOutcome servletOutcome = isServletWebApplication(context); - if (servletOutcome.isMatch() && required) { - return new ConditionOutcome(servletOutcome.isMatch(), - message.because(servletOutcome.getMessage())); - } - ConditionOutcome reactiveOutcome = isReactiveWebApplication(context); - if (reactiveOutcome.isMatch() && required) { - return new ConditionOutcome(reactiveOutcome.isMatch(), - message.because(reactiveOutcome.getMessage())); - } - boolean finalOutcome = (required - ? servletOutcome.isMatch() && reactiveOutcome.isMatch() - : servletOutcome.isMatch() || reactiveOutcome.isMatch()); - return new ConditionOutcome(finalOutcome, - message.because(servletOutcome.getMessage()).append("and") - .append(reactiveOutcome.getMessage())); + return isAnyWebApplication(context, required); + } + } + + private ConditionOutcome isAnyWebApplication(ConditionContext context, + boolean required) { + ConditionMessage.Builder message = ConditionMessage.forCondition( + ConditionalOnWebApplication.class, required ? "(required)" : ""); + ConditionOutcome servletOutcome = isServletWebApplication(context); + if (servletOutcome.isMatch() && required) { + return new ConditionOutcome(servletOutcome.isMatch(), + message.because(servletOutcome.getMessage())); + } + ConditionOutcome reactiveOutcome = isReactiveWebApplication(context); + if (reactiveOutcome.isMatch() && required) { + return new ConditionOutcome(reactiveOutcome.isMatch(), + message.because(reactiveOutcome.getMessage())); } + boolean finalOutcome = (required + ? servletOutcome.isMatch() && reactiveOutcome.isMatch() + : servletOutcome.isMatch() || reactiveOutcome.isMatch()); + return new ConditionOutcome(finalOutcome, + message.because(servletOutcome.getMessage()).append("and") + .append(reactiveOutcome.getMessage())); } private ConditionOutcome isServletWebApplication(ConditionContext context) {