Add utility function in SpringUtils to determine the 'active' state of the given ApplicationContext.

This commit is contained in:
John Blum
2021-10-28 10:59:51 -07:00
parent 96e864c319
commit 2ae2f84779

View File

@@ -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}.
*