From 2ae2f847799edb7bb44ba0f70ab6177a1d5fe550 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 28 Oct 2021 10:59:51 -0700 Subject: [PATCH] Add utility function in SpringUtils to determine the 'active' state of the given ApplicationContext. --- .../data/gemfire/tests/util/SpringUtils.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/SpringUtils.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/SpringUtils.java index fe3a26e..3034d39 100644 --- a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/SpringUtils.java +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/SpringUtils.java @@ -46,6 +46,29 @@ public abstract class SpringUtils extends org.springframework.data.gemfire.util. return false; }; + /** + * Determines whether the given {@link ApplicationContext} is still {@literal active}. + * + * An {@link ApplicationContext} is considered to be {@literal active} if the {@literal refresh} method + * has been called but the {@link ApplicationContext} has not be {@literal closed} yet. + * + * @param applicationContext {@link ApplicationContext} to evaluate. + * @return a boolean value indicating if the given {@link ApplicationContext} is still {@literal active}. + * Returns {@literal false} if the {@link ApplicationContext} is {@literal null}, is not an instance of + * {@link ConfigurableApplicationContext} (which is required to evaluate the {@literal active} state) + * or the {@link ConfigurableApplicationContext#isActive()} methods returns {@literal false} (which occurs after + * the {@link ConfigurableApplicationContext#close()} method has been called. + * @see org.springframework.context.ApplicationContext + */ + public static boolean isApplicationContextActive(@Nullable ApplicationContext applicationContext) { + + return Optional.ofNullable(applicationContext) + .filter(ConfigurableApplicationContext.class::isInstance) + .map(ConfigurableApplicationContext.class::cast) + .map(ConfigurableApplicationContext::isActive) + .orElse(false); + } + /** * Closes the given optionally provided {@link ApplicationContext}. *