Class AbstractConfiguredBean<ROOT,T>

java.lang.Object
org.dellroad.stuff.spring.AbstractBean
org.dellroad.stuff.pobj.AbstractConfiguredBean<ROOT,T>
Type Parameters:
ROOT - type of the configuration object root
T - type of the sub-graph of ROOT that this bean is configured by
All Implemented Interfaces:
PersistentObjectListener<ROOT>, Aware, BeanNameAware, DisposableBean, InitializingBean

public abstract class AbstractConfiguredBean<ROOT,T> extends AbstractBean implements BeanNameAware, PersistentObjectListener<ROOT>
Support superclass for Spring beans that are configured by a PersistentObject root object, or some portion thereof.

This superclass handles registering as a listener on the PersistentObject, and invokes the start(), stop(), and reconfigure() lifecycle methods as needed. The subclass method getBeanConfig(ROOT) determines the portion of the configuration object of interest to the subclass. Subclasses may also override requiresReconfigure(T, T) to determine whether a configuration change requires a reconfigure() operation.

Empty starts and empty stops are supported; these are treated as an unconfigured state and the bean will be (or remain) stopped in those states.

At any point in time, getBeanConfig() (and it's alternate form getRequiredConfig()) access this bean's current configuration (if any).

  • Constructor Details

    • AbstractConfiguredBean

      protected AbstractConfiguredBean()
      Default constructor.
    • AbstractConfiguredBean

      protected AbstractConfiguredBean(PersistentObject<ROOT> persistentObject)
      Constructor.
      Parameters:
      persistentObject - keeper of the current configuration
  • Method Details

    • setPersistentObject

      protected void setPersistentObject(PersistentObject<ROOT> persistentObject)
      Configure the PersistentObject that this instance will monitor.
      Parameters:
      persistentObject - keeper of the this bean's configuration
    • setBeanName

      public void setBeanName(String beanName)
      Specified by:
      setBeanName in interface BeanNameAware
    • getBeanName

      protected String getBeanName()
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Start and configure this instance if the current configuration is valid; if not (i.e., "empty start"), then do nothing and wait until it becomes valid.
      Specified by:
      afterPropertiesSet in interface InitializingBean
      Overrides:
      afterPropertiesSet in class AbstractBean
      Throws:
      Exception
    • destroy

      public void destroy() throws Exception
      Stop this instance. Does nothing if already stopped.
      Specified by:
      destroy in interface DisposableBean
      Overrides:
      destroy in class AbstractBean
      Throws:
      Exception
    • isConfigured

      public boolean isConfigured()
      Is this bean configured? "Configured" means that either start() or reconfigure() has been invoked successfully since the most recent stop().
      Returns:
      true if this instance is configured
    • handleEvent

      public final void handleEvent(PersistentObjectEvent<ROOT> event)
      Description copied from interface: PersistentObjectListener
      Handle notification of an updated root object.
      Specified by:
      handleEvent in interface PersistentObjectListener<ROOT>
      Parameters:
      event - notification event
    • start

      protected void start(T beanConfig) throws Exception
      Start this bean. This instance's monitor will be locked when this method is invoked.

      The implementation in AbstractConfiguredBean does nothing.

      Parameters:
      beanConfig - configuration sub-tree object, never null
      Throws:
      Exception - upon failure; in this case the bean will be considered not configured
    • stop

      protected void stop()
      Stop this bean. This instance's monitor will be locked when this method is invoked.

      The implementation in AbstractConfiguredBean does nothing.

    • reconfigure

      protected void reconfigure(T oldConfig, T newConfig) throws Exception
      Reconfigure this bean. This instance's monitor will be locked when this method is invoked.

      The implementation in AbstractConfiguredBean invokes stop() followed by start(T).

      Parameters:
      oldConfig - old configuration sub-tree object, never null
      newConfig - new configuration sub-tree object, never null
      Throws:
      Exception - upon failure; in this case the bean will be considered not configured
    • requiresReconfigure

      protected boolean requiresReconfigure(T oldConfig, T newConfig)
      Determine if a change from oldConfig to newConfig requires a reconfigure() operation.

      The implementation in AbstractConfiguredBean invokes newConfig.equals(oldConfig) and returns true if they are not equal.

      Parameters:
      oldConfig - old configuration sub-tree object, never null
      newConfig - new configuration sub-tree object, never null
      Returns:
      true if reconfigure() needs to be invoked
    • getBeanConfig

      protected abstract T getBeanConfig(ROOT rootConfig)
      Extract the configuration sub-tree object that this node uses for its configuration given the root configuration object.
      Parameters:
      rootConfig - root config object, never null
      Returns:
      this bean's config object
    • getBeanConfig

      protected T getBeanConfig()
      Get the current configuration (sub-tree) object appropriate for this instance, or null if not configured.
      Returns:
      current bean configuration, or null if not configured or the PersistentObject has been stopped
    • getRequiredConfig

      protected T getRequiredConfig()
      Get the current configuration (sub-tree) object appropriate for this instance, and require that this instance also be configured at the time this method is invoked. This method is like getBeanConfig() but it throws an exception instead of returning null.
      Returns:
      current bean configuration, never null
      Throws:
      NotConfiguredException - if this instance is not configured
      NotConfiguredException - if the PersistentObject has been stopped