18
18
19
19
import java .util .Map ;
20
20
21
+ import org .springframework .boot .autoconfigure .AutoConfigurationMetadata ;
21
22
import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication .Type ;
22
23
import org .springframework .boot .web .reactive .context .ConfigurableReactiveWebEnvironment ;
23
24
import org .springframework .boot .web .reactive .context .ReactiveWebApplicationContext ;
36
37
* {@link WebApplicationContext}.
37
38
*
38
39
* @author Dave Syer
40
+ * @author Phillip Webb
39
41
* @see ConditionalOnWebApplication
40
42
* @see ConditionalOnNotWebApplication
41
43
*/
42
44
@ Order (Ordered .HIGHEST_PRECEDENCE + 20 )
43
- class OnWebApplicationCondition extends SpringBootCondition {
45
+ class OnWebApplicationCondition extends FilteringSpringBootCondition {
44
46
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
+ }
47
94
48
95
@ Override
49
96
public ConditionOutcome getMatchOutcome (ConditionContext context ,
@@ -93,9 +140,10 @@ private ConditionOutcome isAnyWebApplication(ConditionContext context,
93
140
94
141
private ConditionOutcome isServletWebApplication (ConditionContext context ) {
95
142
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 ());
99
147
}
100
148
if (context .getBeanFactory () != null ) {
101
149
String [] scopes = context .getBeanFactory ().getRegisteredScopeNames ();
@@ -115,6 +163,11 @@ private ConditionOutcome isServletWebApplication(ConditionContext context) {
115
163
116
164
private ConditionOutcome isReactiveWebApplication (ConditionContext context ) {
117
165
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
+ }
118
171
if (context .getEnvironment () instanceof ConfigurableReactiveWebEnvironment ) {
119
172
return ConditionOutcome
120
173
.match (message .foundExactly ("ConfigurableReactiveWebEnvironment" ));
0 commit comments