1818
1919import java .util .Map ;
2020
21+ import org .springframework .boot .autoconfigure .AutoConfigurationMetadata ;
2122import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication .Type ;
2223import org .springframework .boot .web .reactive .context .ConfigurableReactiveWebEnvironment ;
2324import org .springframework .boot .web .reactive .context .ReactiveWebApplicationContext ;
3637 * {@link WebApplicationContext}.
3738 *
3839 * @author Dave Syer
40+ * @author Phillip Webb
3941 * @see ConditionalOnWebApplication
4042 * @see ConditionalOnNotWebApplication
4143 */
4244@ Order (Ordered .HIGHEST_PRECEDENCE + 20 )
43- class OnWebApplicationCondition extends SpringBootCondition {
45+ class OnWebApplicationCondition extends FilteringSpringBootCondition {
4446
45- private static final String WEB_CONTEXT_CLASS = "org.springframework.web.context."
46- + "support.GenericWebApplicationContext" ;
47+ private static final String SERVLET_WEB_APPLICATION_CLASS = "org.springframework.web.context.support.GenericWebApplicationContext" ;
48+
49+ private static final String REACTIVE_WEB_APPLICATION_CLASS = "org.springframework.web.reactive.HandlerResult" ;
50+
51+ @ Override
52+ protected ConditionOutcome [] getOutcomes (String [] autoConfigurationClasses ,
53+ AutoConfigurationMetadata autoConfigurationMetadata ) {
54+ ConditionOutcome [] outcomes = new ConditionOutcome [autoConfigurationClasses .length ];
55+ for (int i = 0 ; i < outcomes .length ; i ++) {
56+ String autoConfigurationClass = autoConfigurationClasses [i ];
57+ if (autoConfigurationClass != null ) {
58+ outcomes [i ] = getOutcome (autoConfigurationMetadata
59+ .get (autoConfigurationClass , "ConditionalOnWebApplication" ));
60+ }
61+ }
62+ return outcomes ;
63+ }
64+
65+ private ConditionOutcome getOutcome (String type ) {
66+ if (type == null ) {
67+ return null ;
68+ }
69+ ConditionMessage .Builder message = ConditionMessage
70+ .forCondition (ConditionalOnWebApplication .class );
71+ if (ConditionalOnWebApplication .Type .SERVLET .name ().equals (type )) {
72+ if (!ClassNameFilter .isPresent (SERVLET_WEB_APPLICATION_CLASS ,
73+ getBeanClassLoader ())) {
74+ return ConditionOutcome .noMatch (
75+ message .didNotFind ("servlet web application classes" ).atAll ());
76+ }
77+ }
78+ if (ConditionalOnWebApplication .Type .REACTIVE .name ().equals (type )) {
79+ if (!ClassNameFilter .isPresent (REACTIVE_WEB_APPLICATION_CLASS ,
80+ getBeanClassLoader ())) {
81+ return ConditionOutcome .noMatch (
82+ message .didNotFind ("reactive web application classes" ).atAll ());
83+ }
84+ }
85+ if (!ClassNameFilter .isPresent (SERVLET_WEB_APPLICATION_CLASS ,
86+ getBeanClassLoader ())
87+ && !ClassUtils .isPresent (REACTIVE_WEB_APPLICATION_CLASS ,
88+ getBeanClassLoader ())) {
89+ return ConditionOutcome .noMatch (message
90+ .didNotFind ("reactive or servlet web application classes" ).atAll ());
91+ }
92+ return null ;
93+ }
4794
4895 @ Override
4996 public ConditionOutcome getMatchOutcome (ConditionContext context ,
@@ -93,9 +140,10 @@ private ConditionOutcome isAnyWebApplication(ConditionContext context,
93140
94141 private ConditionOutcome isServletWebApplication (ConditionContext context ) {
95142 ConditionMessage .Builder message = ConditionMessage .forCondition ("" );
96- if (!ClassUtils .isPresent (WEB_CONTEXT_CLASS , context .getClassLoader ())) {
97- return ConditionOutcome
98- .noMatch (message .didNotFind ("web application classes" ).atAll ());
143+ if (!ClassNameFilter .isPresent (SERVLET_WEB_APPLICATION_CLASS ,
144+ context .getClassLoader ())) {
145+ return ConditionOutcome .noMatch (
146+ message .didNotFind ("servlet web application classes" ).atAll ());
99147 }
100148 if (context .getBeanFactory () != null ) {
101149 String [] scopes = context .getBeanFactory ().getRegisteredScopeNames ();
@@ -115,6 +163,11 @@ private ConditionOutcome isServletWebApplication(ConditionContext context) {
115163
116164 private ConditionOutcome isReactiveWebApplication (ConditionContext context ) {
117165 ConditionMessage .Builder message = ConditionMessage .forCondition ("" );
166+ if (!ClassNameFilter .isPresent (REACTIVE_WEB_APPLICATION_CLASS ,
167+ context .getClassLoader ())) {
168+ return ConditionOutcome .noMatch (
169+ message .didNotFind ("reactive web application classes" ).atAll ());
170+ }
118171 if (context .getEnvironment () instanceof ConfigurableReactiveWebEnvironment ) {
119172 return ConditionOutcome
120173 .match (message .foundExactly ("ConfigurableReactiveWebEnvironment" ));
0 commit comments