org.dellroad.stuff.pobj
Class SpringPersistentObjectSchemaUpdater<T>

java.lang.Object
  extended by org.dellroad.stuff.schema.AbstractSchemaUpdater<File,PersistentFileTransaction>
      extended by org.dellroad.stuff.pobj.PersistentObjectSchemaUpdater<T>
          extended by org.dellroad.stuff.pobj.SpringPersistentObjectSchemaUpdater<T>
Type Parameters:
T - type of the root persistent object
All Implemented Interfaces:
BeanFactoryAware, DisposableBean, InitializingBean

public class SpringPersistentObjectSchemaUpdater<T>
extends PersistentObjectSchemaUpdater<T>
implements BeanFactoryAware, InitializingBean, DisposableBean

PersistentObjectSchemaUpdater optimized for use with Spring:

An example of how this class can be combined with custom XML to define an updater and all its updates:

  <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

      <!-- Persistent object delegate; you supply the XML (un)marshaller -->
      <bean id="delegate" class="org.dellroad.stuff.pobj.SpringDelegate"
          p:marshaller-ref="marshaller" p:unmarshaller-ref="unmarshaller"/>

      <!-- Persistent object schema updater -->
      <bean id="schemaUpdater" class="org.dellroad.stuff.pobj.SpringPersistentObjectSchemaUpdater"
          p:file="/var/example/pobj.xml" p:delegate-ref="delegate"
          p:initialXML="classpath:com/example/initial-pobj.xml"/>

      <!-- Persistent object bean -->
      <bean scope="prototype" factory-bean="schemaUpdater" factory-method="getPersistentObject"/>

      <!-- Define a common location for our schema update XSLTs -->
      <bean class="org.dellroad.stuff.pobj.SpringXSLUpdateTransformConfigurer"
          p:prefix="classpath:updates/" p:suffix=".xsl"/>

      <!-- Schema update #1 -->
      <bean id="update1" class="org.dellroad.stuff.pobj.SpringXSLPersistentObjectSchemaUpdate"
        transform="file:///usr/share/updates/SomeUpdate.xsl"/>

      <!-- Schema update #2: uses "classpath:updates/update2.xsl" thanks to TransformConfigurer -->
      <bean id="update2" class="org.dellroad.stuff.pobj.SpringXSLPersistentObjectSchemaUpdate"/>

      <!-- Add more schema updates over time as needed and everything just works... -->

  </beans>
 

The PersistentObject itself, fully updated, is accessible via PersistentObjectSchemaUpdater.getPersistentObject(). or, in the above example, via the persistentObject bean.


Field Summary
 
Fields inherited from class org.dellroad.stuff.pobj.PersistentObjectSchemaUpdater
allowEmptyStart, checkInterval, DEFAULT_CHECK_INTERVAL, delegate, file, NAMESPACE_URI, numBackups, UPDATE_ELEMENT_NAME, UPDATES_ELEMENT_NAME, writeDelay, XML_PREFIX, XMLNS_ATTRIBUTE_NAME
 
Fields inherited from class org.dellroad.stuff.schema.AbstractSchemaUpdater
log
 
Constructor Summary
SpringPersistentObjectSchemaUpdater()
           
 
Method Summary
 void afterPropertiesSet()
           
 void destroy()
           
protected  T getInitialValue()
          Get the initial value for the persistent object when no persistent file is found.
protected  Comparator<SchemaUpdate<PersistentFileTransaction>> getOrderingTieBreaker()
          Get the preferred ordering of two updates that do not have any predecessor constraints (including implied indirect constraints) between them.
 void setBeanFactory(BeanFactory beanFactory)
           
 void setInitialValue(T initialValue)
          Set the initial value to be used on an uninitialized persistent object.
 void setInitialXML(Resource resource)
          Set the resource containing the initial value, encoded as XML, to be used on an uninitialized persistent object.
 
Methods inherited from class org.dellroad.stuff.pobj.PersistentObjectSchemaUpdater
commitTransaction, databaseNeedsInitialization, getAppliedUpdateNames, getPersistentObject, initializeDatabase, openTransaction, recordUpdateApplied, rollbackTransaction, setAllowEmptyStart, setCheckInterval, setDelegate, setFile, setNumBackups, setWriteDelay, start, stop
 
Methods inherited from class org.dellroad.stuff.schema.AbstractSchemaUpdater
apply, applyInTransaction, generateMultiUpdateName, getUpdates, initializeAndUpdateDatabase, isIgnoreUnrecognizedUpdates, isValidUpdateName, setIgnoreUnrecognizedUpdates, setUpdates
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpringPersistentObjectSchemaUpdater

public SpringPersistentObjectSchemaUpdater()
Method Detail

setInitialValue

public void setInitialValue(T initialValue)
Set the initial value to be used on an uninitialized persistent object.

Either this or a initial XML resource should be configured to handle the case that no persistent file exists yet.


setInitialXML

public void setInitialXML(Resource resource)
Set the resource containing the initial value, encoded as XML, to be used on an uninitialized persistent object. This can be used as an alternative to setInitialValue(T).

Either this or an explicit initial value should be configured to handle the case that no persistent file exists yet.


getInitialValue

protected T getInitialValue()
Get the initial value for the persistent object when no persistent file is found.

The implementation in SpringPersistentObjectSchemaUpdater returns the initial value, if any, otherwise it falls back to decoding the initial value from the initial value resource, if any. If neither property is configured, null is returned.

Overrides:
getInitialValue in class PersistentObjectSchemaUpdater<T>

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
Specified by:
setBeanFactory in interface BeanFactoryAware

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

destroy

public void destroy()
Specified by:
destroy in interface DisposableBean

getOrderingTieBreaker

protected Comparator<SchemaUpdate<PersistentFileTransaction>> getOrderingTieBreaker()
Get the preferred ordering of two updates that do not have any predecessor constraints (including implied indirect constraints) between them.

In the case no schema updates are explicitly configured, the Comparator returned by the implementation in SpringPersistentObjectSchemaUpdater sorts updates in the same order that they appear in the containing ListableBeanFactory. Otherwise, the superclass method is used.

Overrides:
getOrderingTieBreaker in class AbstractSchemaUpdater<File,PersistentFileTransaction>
Returns:
a Comparator that sorts incomparable updates in the order they should be applied