Skip to content

Commit 14c1b09

Browse files
committed
Polish
1 parent da69286 commit 14c1b09

File tree

10 files changed

+129
-84
lines changed

10 files changed

+129
-84
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.redis;
18+
19+
import java.util.Properties;
20+
21+
import org.springframework.boot.actuate.health.Health;
22+
import org.springframework.boot.actuate.health.Health.Builder;
23+
import org.springframework.data.redis.connection.ClusterInfo;
24+
25+
/**
26+
* Shared class used by {@link RedisHealthIndicator} and
27+
* {@link RedisReactiveHealthIndicator} to provide health details.
28+
*
29+
* @author Phillip Webb
30+
*/
31+
class RedisHealth {
32+
33+
static Builder up(Health.Builder builder, Properties info) {
34+
builder.withDetail("version", info.getProperty("redis_version"));
35+
return builder.up();
36+
}
37+
38+
static Builder up(Health.Builder builder, ClusterInfo clusterInfo) {
39+
builder.withDetail("cluster_size", clusterInfo.getClusterSize());
40+
builder.withDetail("slots_up", clusterInfo.getSlotsOk());
41+
builder.withDetail("slots_fail", clusterInfo.getSlotsFail());
42+
return builder.up();
43+
}
44+
45+
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/redis/RedisHealthIndicator.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
2020
import org.springframework.boot.actuate.health.Health;
2121
import org.springframework.boot.actuate.health.HealthIndicator;
22-
import org.springframework.data.redis.connection.ClusterInfo;
2322
import org.springframework.data.redis.connection.RedisClusterConnection;
2423
import org.springframework.data.redis.connection.RedisConnection;
2524
import org.springframework.data.redis.connection.RedisConnectionFactory;
@@ -37,8 +36,6 @@
3736
*/
3837
public class RedisHealthIndicator extends AbstractHealthIndicator {
3938

40-
private static final String REDIS_VERSION_PROPERTY = "redis_version";
41-
4239
private final RedisConnectionFactory redisConnectionFactory;
4340

4441
public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
@@ -60,14 +57,10 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
6057

6158
private void doHealthCheck(Health.Builder builder, RedisConnection connection) {
6259
if (connection instanceof RedisClusterConnection) {
63-
ClusterInfo clusterInfo = ((RedisClusterConnection) connection).clusterGetClusterInfo();
64-
builder.up().withDetail("cluster_size", clusterInfo.getClusterSize())
65-
.withDetail("slots_up", clusterInfo.getSlotsOk())
66-
.withDetail("slots_fail", clusterInfo.getSlotsFail());
60+
RedisHealth.up(builder, ((RedisClusterConnection) connection).clusterGetClusterInfo());
6761
}
6862
else {
69-
String version = connection.info().getProperty(REDIS_VERSION_PROPERTY);
70-
builder.up().withDetail("version", version);
63+
RedisHealth.up(builder, connection.info());
7164
}
7265
}
7366

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/redis/RedisReactiveHealthIndicator.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
*/
4141
public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
4242

43-
private static final String REDIS_VERSION_PROPERTY = "redis_version";
44-
4543
private final ReactiveRedisConnectionFactory connectionFactory;
4644

4745
public RedisReactiveHealthIndicator(ReactiveRedisConnectionFactory connectionFactory) {
@@ -54,35 +52,30 @@ protected Mono<Health> doHealthCheck(Health.Builder builder) {
5452
return getConnection().flatMap((connection) -> doHealthCheck(builder, connection));
5553
}
5654

55+
private Mono<ReactiveRedisConnection> getConnection() {
56+
return Mono.fromSupplier(this.connectionFactory::getReactiveConnection)
57+
.subscribeOn(Schedulers.boundedElastic());
58+
}
59+
5760
private Mono<Health> doHealthCheck(Health.Builder builder, ReactiveRedisConnection connection) {
58-
if (connection instanceof ReactiveRedisClusterConnection) {
59-
ReactiveRedisClusterConnection clusterConnection = (ReactiveRedisClusterConnection) connection;
60-
return clusterConnection.clusterGetClusterInfo().map((info) -> up(builder, info))
61-
.onErrorResume((ex) -> Mono.just(down(builder, ex)))
62-
.flatMap((health) -> clusterConnection.closeLater().thenReturn(health));
63-
}
64-
return connection.serverCommands().info().map((info) -> up(builder, info))
65-
.onErrorResume((ex) -> Mono.just(down(builder, ex)))
61+
return getHealth(builder, connection).onErrorResume((ex) -> Mono.just(builder.down(ex).build()))
6662
.flatMap((health) -> connection.closeLater().thenReturn(health));
6763
}
6864

69-
private Mono<ReactiveRedisConnection> getConnection() {
70-
return Mono.fromSupplier(this.connectionFactory::getReactiveConnection)
71-
.subscribeOn(Schedulers.boundedElastic());
65+
private Mono<Health> getHealth(Health.Builder builder, ReactiveRedisConnection connection) {
66+
if (connection instanceof ReactiveRedisClusterConnection) {
67+
return ((ReactiveRedisClusterConnection) connection).clusterGetClusterInfo()
68+
.map((info) -> up(builder, info));
69+
}
70+
return connection.serverCommands().info().map((info) -> up(builder, info));
7271
}
7372

7473
private Health up(Health.Builder builder, Properties info) {
75-
return builder.up().withDetail("version", info.getProperty(REDIS_VERSION_PROPERTY)).build();
74+
return RedisHealth.up(builder, info).build();
7675
}
7776

7877
private Health up(Health.Builder builder, ClusterInfo clusterInfo) {
79-
return builder.up().withDetail("cluster_size", clusterInfo.getClusterSize())
80-
.withDetail("slots_up", clusterInfo.getSlotsOk()).withDetail("slots_fail", clusterInfo.getSlotsFail())
81-
.build();
82-
}
83-
84-
private Health down(Health.Builder builder, Throwable cause) {
85-
return builder.down(cause).build();
78+
return RedisHealth.up(builder, clusterInfo).build();
8679
}
8780

8881
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ public void checkConfiguration() {
243243
throw new IncompatibleConfigurationException("spring.mvc.pathmatch.matching-strategy",
244244
"spring.mvc.pathmatch.use-suffix-pattern");
245245
}
246-
else if (this.getPathmatch().isUseRegisteredSuffixPattern()) {
246+
if (this.getPathmatch().isUseRegisteredSuffixPattern()) {
247247
throw new IncompatibleConfigurationException("spring.mvc.pathmatch.matching-strategy",
248248
"spring.mvc.pathmatch.use-registered-suffix-pattern");
249249
}
250-
else if (!this.getServlet().getServletMapping().equals("/")) {
250+
if (!this.getServlet().getServletMapping().equals("/")) {
251251
throw new IncompatibleConfigurationException("spring.mvc.pathmatch.matching-strategy",
252252
"spring.mvc.servlet.path");
253253
}

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222

2323
import org.springframework.beans.BeanUtils;
24+
import org.springframework.boot.ApplicationContextFactory;
2425
import org.springframework.boot.SpringApplication;
2526
import org.springframework.boot.WebApplicationType;
2627
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@@ -111,7 +112,7 @@ else if (config instanceof ReactiveWebMergedContextConfiguration) {
111112
application.setWebApplicationType(WebApplicationType.REACTIVE);
112113
if (!isEmbeddedWebEnvironment(config)) {
113114
application.setApplicationContextFactory(
114-
(webApplicationType) -> new GenericReactiveWebApplicationContext());
115+
ApplicationContextFactory.of(GenericReactiveWebApplicationContext::new));
115116
}
116117
}
117118
else {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationContextFactory.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
package org.springframework.boot;
1818

19+
import java.util.function.Supplier;
20+
1921
import org.springframework.beans.BeanUtils;
22+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
23+
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
2024
import org.springframework.context.ConfigurableApplicationContext;
25+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2126

2227
/**
2328
* Strategy interface for creating the {@link ConfigurableApplicationContext} used by a
@@ -26,11 +31,33 @@
2631
* context.
2732
*
2833
* @author Andy Wilkinson
34+
* @author Phillip Webb
2935
* @since 2.4.0
3036
*/
3137
@FunctionalInterface
3238
public interface ApplicationContextFactory {
3339

40+
/**
41+
* A default {@link ApplicationContextFactory} implementation that will create an
42+
* appropriate context for the {@link WebApplicationType}.
43+
*/
44+
static ApplicationContextFactory DEFAULT = (webApplicationType) -> {
45+
try {
46+
switch (webApplicationType) {
47+
case SERVLET:
48+
return new AnnotationConfigServletWebServerApplicationContext();
49+
case REACTIVE:
50+
return new AnnotationConfigReactiveWebServerApplicationContext();
51+
default:
52+
return new AnnotationConfigApplicationContext();
53+
}
54+
}
55+
catch (Exception ex) {
56+
throw new IllegalStateException("Unable create a default ApplicationContext instance, "
57+
+ "you may need a custom ApplicationContextFactory", ex);
58+
}
59+
};
60+
3461
/**
3562
* Creates the {@link ConfigurableApplicationContext application context} for a
3663
* {@link SpringApplication}, respecting the given {@code webApplicationType}.
@@ -46,8 +73,19 @@ public interface ApplicationContextFactory {
4673
* @return the factory that will instantiate the context class
4774
* @see BeanUtils#instantiateClass(Class)
4875
*/
49-
static ApplicationContextFactory forContextClass(Class<? extends ConfigurableApplicationContext> contextClass) {
50-
return (webApplicationType) -> BeanUtils.instantiateClass(contextClass);
76+
static ApplicationContextFactory ofContextClass(Class<? extends ConfigurableApplicationContext> contextClass) {
77+
return of(() -> BeanUtils.instantiateClass(contextClass));
78+
}
79+
80+
/**
81+
* Creates an {@code ApplicationContextFactory} that will create contexts by calling
82+
* the given {@link Supplier}.
83+
* @param supplier the context supplier, for example
84+
* {@code AnnotationConfigApplicationContext::new}
85+
* @return the factory that will instantiate the context class
86+
*/
87+
static ApplicationContextFactory of(Supplier<ConfigurableApplicationContext> supplier) {
88+
return (webApplicationType) -> supplier.get();
5189
}
5290

5391
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public class SpringApplication {
245245

246246
private boolean lazyInitialization = false;
247247

248-
private ApplicationContextFactory applicationContextFactory = new DefaultApplicationContextFactory();
248+
private ApplicationContextFactory applicationContextFactory = ApplicationContextFactory.DEFAULT;
249249

250250
/**
251251
* Create a new {@link SpringApplication} instance. The application context will load
@@ -1150,7 +1150,7 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
11501150
@Deprecated
11511151
public void setApplicationContextClass(Class<? extends ConfigurableApplicationContext> applicationContextClass) {
11521152
this.webApplicationType = WebApplicationType.deduceFromApplicationContext(applicationContextClass);
1153-
this.applicationContextFactory = ApplicationContextFactory.forContextClass(applicationContextClass);
1153+
this.applicationContextFactory = ApplicationContextFactory.ofContextClass(applicationContextClass);
11541154
}
11551155

11561156
/**
@@ -1164,7 +1164,8 @@ public void setApplicationContextClass(Class<? extends ConfigurableApplicationCo
11641164
* @since 2.4.0
11651165
*/
11661166
public void setApplicationContextFactory(ApplicationContextFactory applicationContextFactory) {
1167-
this.applicationContextFactory = applicationContextFactory;
1167+
this.applicationContextFactory = (applicationContextFactory != null) ? applicationContextFactory
1168+
: ApplicationContextFactory.DEFAULT;
11681169
}
11691170

11701171
/**
@@ -1309,26 +1310,4 @@ private static <E> Set<E> asUnmodifiableOrderedSet(Collection<E> elements) {
13091310
return new LinkedHashSet<>(list);
13101311
}
13111312

1312-
private static final class DefaultApplicationContextFactory implements ApplicationContextFactory {
1313-
1314-
@Override
1315-
public ConfigurableApplicationContext create(WebApplicationType webApplicationType) {
1316-
try {
1317-
switch (webApplicationType) {
1318-
case SERVLET:
1319-
return new AnnotationConfigServletWebServerApplicationContext();
1320-
case REACTIVE:
1321-
return new AnnotationConfigReactiveWebServerApplicationContext();
1322-
default:
1323-
return new AnnotationConfigApplicationContext();
1324-
}
1325-
}
1326-
catch (Exception ex) {
1327-
throw new IllegalStateException(
1328-
"Unable create a default ApplicationContext, please specify an ApplicationContextFactory", ex);
1329-
}
1330-
}
1331-
1332-
}
1333-
13341313
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationNoWebTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ void detectWebApplicationTypeToNone() {
4242
@Test
4343
void specificApplicationContextClass() {
4444
SpringApplication application = new SpringApplication(ExampleConfig.class);
45-
application.setApplicationContextFactory(
46-
ApplicationContextFactory.forContextClass(StaticApplicationContext.class));
45+
application
46+
.setApplicationContextFactory(ApplicationContextFactory.ofContextClass(StaticApplicationContext.class));
4747
ConfigurableApplicationContext context = application.run();
4848
assertThat(context).isInstanceOf(StaticApplicationContext.class);
4949
context.close();

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ void specificApplicationContextClass() {
331331
@Test
332332
void specificApplicationContextFactory() {
333333
SpringApplication application = new SpringApplication(ExampleConfig.class);
334-
application.setApplicationContextFactory(
335-
ApplicationContextFactory.forContextClass(StaticApplicationContext.class));
334+
application
335+
.setApplicationContextFactory(ApplicationContextFactory.ofContextClass(StaticApplicationContext.class));
336336
this.context = application.run();
337337
assertThat(this.context).isInstanceOf(StaticApplicationContext.class);
338338
}
@@ -889,8 +889,7 @@ void commandLineArgsApplyToSpringApplication() {
889889
@Test
890890
void registerShutdownHook() {
891891
SpringApplication application = new SpringApplication(ExampleConfig.class);
892-
application
893-
.setApplicationContextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class));
892+
application.setApplicationContextFactory(ApplicationContextFactory.ofContextClass(SpyApplicationContext.class));
894893
this.context = application.run();
895894
SpyApplicationContext applicationContext = (SpyApplicationContext) this.context;
896895
verify(applicationContext.getApplicationContext()).registerShutdownHook();
@@ -899,8 +898,7 @@ void registerShutdownHook() {
899898
@Test
900899
void registerListener() {
901900
SpringApplication application = new SpringApplication(ExampleConfig.class, ListenerConfig.class);
902-
application
903-
.setApplicationContextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class));
901+
application.setApplicationContextFactory(ApplicationContextFactory.ofContextClass(SpyApplicationContext.class));
904902
Set<ApplicationEvent> events = new LinkedHashSet<>();
905903
application.addListeners((ApplicationListener<ApplicationEvent>) events::add);
906904
this.context = application.run();
@@ -913,8 +911,7 @@ void registerListener() {
913911
void registerListenerWithCustomMulticaster() {
914912
SpringApplication application = new SpringApplication(ExampleConfig.class, ListenerConfig.class,
915913
Multicaster.class);
916-
application
917-
.setApplicationContextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class));
914+
application.setApplicationContextFactory(ApplicationContextFactory.ofContextClass(SpyApplicationContext.class));
918915
Set<ApplicationEvent> events = new LinkedHashSet<>();
919916
application.addListeners((ApplicationListener<ApplicationEvent>) events::add);
920917
this.context = application.run();
@@ -994,8 +991,7 @@ void applicationListenerFromContextIsCalledWhenContextFailsRefreshAfterListenerR
994991
@Test
995992
void registerShutdownHookOff() {
996993
SpringApplication application = new SpringApplication(ExampleConfig.class);
997-
application
998-
.setApplicationContextFactory(ApplicationContextFactory.forContextClass(SpyApplicationContext.class));
994+
application.setApplicationContextFactory(ApplicationContextFactory.ofContextClass(SpyApplicationContext.class));
999995
application.setRegisterShutdownHook(false);
1000996
this.context = application.run();
1001997
SpyApplicationContext applicationContext = (SpyApplicationContext) this.context;

0 commit comments

Comments
 (0)