org.springframework.data.gemfire.support
Class SpringContextBootstrappingInitializer

java.lang.Object
  extended by org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer
All Implemented Interfaces:
com.gemstone.gemfire.cache.Declarable, EventListener, ApplicationListener<ContextRefreshedEvent>

public class SpringContextBootstrappingInitializer
extends Object
implements com.gemstone.gemfire.cache.Declarable, ApplicationListener<ContextRefreshedEvent>

The SpringContextBootstrappingInitializer class is a GemFire configuration initializer used to bootstrap a Spring ApplicationContext inside a GemFire Server JVM-based process. This enables a GemFire Cache Server resources to be mostly configured with Spring Data GemFire's XML namespace. The Cache itself is the only resource that cannot be configured and initialized in a Spring context since the initializer is not invoked until after GemFire creates and initializes the Cache for use.

Since:
1.4.0
See Also:
Properties, ApplicationContext, ApplicationListener, ConfigurableApplicationContext, AnnotationConfigApplicationContext, ApplicationEventMulticaster, ContextRefreshedEvent, SimpleApplicationEventMulticaster, ClassPathXmlApplicationContext, Declarable, Setting Cache Initializer, SGF-248

Field Summary
static String BASE_PACKAGES_PARAMETER
           
protected static String CHARS_TO_DELETE
           
protected static String COMMA_DELIMITER
           
static String CONTEXT_CONFIG_LOCATIONS_PARAMETER
           
 
Constructor Summary
SpringContextBootstrappingInitializer()
           
 
Method Summary
protected  ConfigurableApplicationContext createApplicationContext(String[] configLocations)
          Creates (constructs and configures) a ConfigurableApplicationContext instance based on the specified locations of the context configuration meta-data files.
protected  ConfigurableApplicationContext createApplicationContext(String[] basePackages, String[] configLocations)
          Creates (constructs and configures) an instance of the ConfigurableApplicationContext based on either the specified base packages containing @Configuration, @Component or JSR 330 annotated classes to scan, or the specified locations of context configuration meta-data files used to configure the context.
static ConfigurableApplicationContext getApplicationContext()
          Gets a reference to the Spring ApplicationContext constructed, configured and initialized inside the GemFire Server-based JVM process.
 void init(Properties parameters)
          Initializes a Spring ApplicationContext with the given parameters from a GemFire Initializer in GemFire native configuration meta-data (e.g. cache.xml).
protected static void notifyListenerOfExistingContextRefreshedEvent(ApplicationListener<ContextRefreshedEvent> listener)
          Notifies any Spring ApplicationListeners of a current and existing ContextRefreshedEvent if the ApplicationContext was previously created, initialized and refreshed before any ApplicationListeners interested in ContextRefreshedEvents get registered so that application components (such as LazyWiringDeclarableSupport objects) arriving late to the game that also require configuration (auto-wiring) get wired accordingly too.
protected  String nullSafeGetApplicationContextId(ApplicationContext applicationContext)
          Gets the the ID of the Spring ApplicationContext in a null-safe manner.
 void onApplicationEvent(ContextRefreshedEvent event)
          Gets notified when the Spring ApplicationContext gets created and refreshed by GemFire.
static
<T extends ApplicationListener<ContextRefreshedEvent>>
T
register(T listener)
          Registers a Spring ApplicationListener to be notified when the Spring ApplicationContext is created by GemFire when instantiating and initializing declared Initializers from the GemFire native configuration file (e.g. cache.xml).
static
<T extends ApplicationListener<ContextRefreshedEvent>>
T
unregister(T listener)
          Unregisters the Spring ApplicationListener from this SpringContextBootstrappingInitializer in order to stop receiving ApplicationEvents on Spring context refreshes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BASE_PACKAGES_PARAMETER

public static final String BASE_PACKAGES_PARAMETER
See Also:
Constant Field Values

CONTEXT_CONFIG_LOCATIONS_PARAMETER

public static final String CONTEXT_CONFIG_LOCATIONS_PARAMETER
See Also:
Constant Field Values

CHARS_TO_DELETE

protected static final String CHARS_TO_DELETE
See Also:
Constant Field Values

COMMA_DELIMITER

protected static final String COMMA_DELIMITER
See Also:
Constant Field Values
Constructor Detail

SpringContextBootstrappingInitializer

public SpringContextBootstrappingInitializer()
Method Detail

getApplicationContext

public static ConfigurableApplicationContext getApplicationContext()
Gets a reference to the Spring ApplicationContext constructed, configured and initialized inside the GemFire Server-based JVM process.

Returns:
a reference to the Spring ApplicationContext bootstrapped by GemFire.
See Also:
ConfigurableApplicationContext

register

public static <T extends ApplicationListener<ContextRefreshedEvent>> T register(T listener)
Registers a Spring ApplicationListener to be notified when the Spring ApplicationContext is created by GemFire when instantiating and initializing declared Initializers from the GemFire native configuration file (e.g. cache.xml).

Type Parameters:
T - the Class type of the Spring ApplicationListener.
Parameters:
listener - the ApplicationListener to register for ContextRefreshedEvents by this SpringContextBootstrappingInitializer.
Returns:
the reference to the ApplicationListener for method call chaining purposes.
See Also:
unregister(org.springframework.context.ApplicationListener), ApplicationListener, ContextRefreshedEvent, #addApplicationListener(org.springframework.context.ApplicationListener)

notifyListenerOfExistingContextRefreshedEvent

protected static void notifyListenerOfExistingContextRefreshedEvent(ApplicationListener<ContextRefreshedEvent> listener)
Notifies any Spring ApplicationListeners of a current and existing ContextRefreshedEvent if the ApplicationContext was previously created, initialized and refreshed before any ApplicationListeners interested in ContextRefreshedEvents get registered so that application components (such as LazyWiringDeclarableSupport objects) arriving late to the game that also require configuration (auto-wiring) get wired accordingly too.

Parameters:
listener - a Spring ApplicationListener requiring notification of any ContextRefreshedEvents after the ApplicationContext has already been created, initialized and/or refreshed.
See Also:
ApplicationListener.onApplicationEvent(org.springframework.context.ApplicationEvent), ContextRefreshedEvent

unregister

public static <T extends ApplicationListener<ContextRefreshedEvent>> T unregister(T listener)
Unregisters the Spring ApplicationListener from this SpringContextBootstrappingInitializer in order to stop receiving ApplicationEvents on Spring context refreshes.

Type Parameters:
T - the Class type of the Spring ApplicationListener.
Parameters:
listener - the ApplicationListener to unregister from receiving ContextRefreshedEvents by this SpringContextBootstrappingInitializer.
Returns:
the reference to the ApplicationListener for method call chaining purposes.
See Also:
register(org.springframework.context.ApplicationListener), ApplicationListener, ContextRefreshedEvent, #removeApplicationListener(org.springframework.context.ApplicationListener)

createApplicationContext

protected ConfigurableApplicationContext createApplicationContext(String[] configLocations)
Creates (constructs and configures) a ConfigurableApplicationContext instance based on the specified locations of the context configuration meta-data files. The created ConfigurableApplicationContext is not automatically "refreshed" and therefore must be "refreshed" by the caller manually.

Parameters:
configLocations - a String array indicating the locations of the context configuration meta-data files used to configure the ConfigurableApplicationContext instance.
Returns:
a newly constructed and configured instance of the ConfigurableApplicationContext class. Note, the "refresh" method must be called manually before using the context.
Throws:
IllegalArgumentException - if the configLocations parameter argument is null or empty.
See Also:
createApplicationContext(String[], String[]), ClassPathXmlApplicationContext

createApplicationContext

protected ConfigurableApplicationContext createApplicationContext(String[] basePackages,
                                                                  String[] configLocations)
Creates (constructs and configures) an instance of the ConfigurableApplicationContext based on either the specified base packages containing @Configuration, @Component or JSR 330 annotated classes to scan, or the specified locations of context configuration meta-data files used to configure the context. The created ConfigurableApplicationContext is not automatically "refreshed" and therefore must be "refreshed" by the caller manually. When basePackages are specified, an instance of AnnotationConfigApplicationContext is returned; otherwise an instance of the ClassPathXmlApplicationContext is initialized with the configLocations and returned. This method prefers the ClassPathXmlApplicationContext to the AnnotationConfigApplicationContext when both basePackages and configLocations are specified.

Parameters:
basePackages - the base application packages to scan for application @Components and @Configuration classes. *
configLocations - a String array indicating the locations of the context configuration meta-data files used to configure the ConfigurableApplicationContext instance.
Returns:
an instance of ConfigurableApplicationContext configured and initialized with either configLocations or the basePackages when configLocations is unspecified. Note, the "refresh" method must be called manually before using the context.
Throws:
IllegalArgumentException - if both the basePackages and configLocation parameter arguments are null or empty.
See Also:
createApplicationContext(String[]), AnnotationConfigApplicationContext, AnnotationConfigApplicationContext.scan(String...), ClassPathXmlApplicationContext

init

public void init(Properties parameters)
Initializes a Spring ApplicationContext with the given parameters from a GemFire Initializer in GemFire native configuration meta-data (e.g. cache.xml).

Specified by:
init in interface com.gemstone.gemfire.cache.Declarable
Parameters:
parameters - a Properties object containing the configuration parameters and settings defined in the GemFire cache.xml >initializer/< element.
See Also:
createApplicationContext(java.lang.String[]), Properties

nullSafeGetApplicationContextId

protected String nullSafeGetApplicationContextId(ApplicationContext applicationContext)
Gets the the ID of the Spring ApplicationContext in a null-safe manner.

Parameters:
applicationContext - the Spring ApplicationContext to retrieve the ID for.
Returns:
the ID of the given Spring ApplicationContext or null if the ApplicationContext reference is null.
See Also:
ApplicationContext.getId()

onApplicationEvent

public void onApplicationEvent(ContextRefreshedEvent event)
Gets notified when the Spring ApplicationContext gets created and refreshed by GemFire. The handler method proceeds in notifying any other GemFire components that need to be aware that the Spring ApplicationContext now exists and is ready for use, such as other Declarable GemFire objects requiring auto-wiring support, etc.

Specified by:
onApplicationEvent in interface ApplicationListener<ContextRefreshedEvent>
Parameters:
event - the ContextRefreshedEvent signaling that the Spring ApplicationContext has been created and refreshed by GemFire.
See Also:
ContextRefreshedEvent, #multicastEvent(org.springframework.context.ApplicationEvent)