Package org.dellroad.stuff.pobj
Class SpringPersistentObjectSchemaUpdater<T>
java.lang.Object
org.dellroad.stuff.schema.AbstractSchemaUpdater<PersistentFileTransaction,PersistentFileTransaction>
org.dellroad.stuff.pobj.PersistentObjectSchemaUpdater<T>
org.dellroad.stuff.pobj.SpringPersistentObjectSchemaUpdater<T>
- Type Parameters:
T- type of the root persistent object
- All Implemented Interfaces:
PersistentObjectDelegate<T>,Aware,BeanFactoryAware,InitializingBean
public class SpringPersistentObjectSchemaUpdater<T>
extends PersistentObjectSchemaUpdater<T>
implements BeanFactoryAware, InitializingBean
PersistentObjectSchemaUpdater optimized for use with Spring:
getOrderingTieBreaker()is overridden to break ties by ordering updates in the same order as they are defined in the bean factory.- This class implements
InitializingBeanand verifies all required properties are set. - If no updates are explicitly configured, then all
SpringPersistentObjectSchemaUpdates found in the containing bean factory are automatically configured; this requires that all of the schema updates are defined in the sameListableBeanFactory. - The default value may be configured as an XML resource
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">
<!-- Normal nested persistent object delegate. You supply the XML (un)marshaller (not shown). -->
<bean id="normalDelegate" class="org.dellroad.stuff.pobj.SpringDelegate"
p:marshaller-ref="marshaller" p:unmarshaller-ref="unmarshaller"/>
<!-- Schema updating persistent object delegate; the updates below will be auto-detected. -->
<bean id="updatingDelegate" class="org.dellroad.stuff.pobj.SpringPersistentObjectSchemaUpdater"
p:marshaller-ref="marshaller" p:unmarshaller-ref="unmarshaller" p:defaultXML="classpath:default.xml">
<constructor-arg ref="normalDelegate"/>
</bean>
<!-- Persistent object, configured to use our schema updating delegate -->
<bean id="persistentObject" class="org.dellroad.stuff.pobj.PersistentObject"
init-method="start" destroy-method="stop" p:file="/var/lib/pobj.xml" p:allowEmptyStart="true"
p:numBackups="3" p:delegate-ref="updatingDelegate"/>
<!-- Define a default location for schema update XSL files -->
<bean class="org.dellroad.stuff.pobj.SpringXSLUpdateTransformConfigurer"
p:prefix="classpath:updates/" p:suffix=".xsl"/>
<!-- Schema update #1 with an explicitly configured XSL resource -->
<bean id="update1" class="org.dellroad.stuff.pobj.SpringXSLPersistentObjectSchemaUpdate"
transform="file:///usr/share/updates/update1.xsl"/>
<!-- Schema update #2: implicitly uses "classpath:updates/update2.xsl" -->
<bean id="update2" class="org.dellroad.stuff.pobj.SpringXSLPersistentObjectSchemaUpdate"/>
<!-- Schema update #3: requires that update #1 be applied first -->
<bean id="update3" depends-on="update1"
class="org.dellroad.stuff.pobj.SpringXSLPersistentObjectSchemaUpdate"/>
<!-- Add more schema updates over time as needed and everything just works... -->
</beans>
-
Field Summary
Fields inherited from class org.dellroad.stuff.pobj.PersistentObjectSchemaUpdater
delegate, NAMESPACE_URI, UPDATE_ELEMENT_NAME, UPDATES_ELEMENT_NAME, XML_PREFIX, XMLNS_ATTRIBUTE_NAMEFields inherited from class org.dellroad.stuff.schema.AbstractSchemaUpdater
log -
Constructor Summary
ConstructorsConstructorDescriptionConstructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidGet the default value for the persistent object when no persistent file is found.protected Comparator<SchemaUpdate<PersistentFileTransaction>>Get the preferred ordering of two updates that do not have any predecessor constraints (including implied indirect constraints) between them.voidsetBeanFactory(BeanFactory beanFactory) voidsetDefaultXML(Resource resource) Set the resource containing the default value, encoded as XML, to be used on an uninitialized persistent object.Methods inherited from class org.dellroad.stuff.pobj.PersistentObjectSchemaUpdater
commitTransaction, copy, databaseNeedsInitialization, deserialize, getAppliedUpdateNames, handleWritebackException, initializeDatabase, isSameGraph, openTransaction, recordUpdateApplied, rollbackTransaction, serialize, setTransformerFactory, validateMethods inherited from class org.dellroad.stuff.schema.AbstractSchemaUpdater
apply, applyInTransaction, generateMultiUpdateName, getAllUpdateNames, getUpdates, initializeAndUpdateDatabase, isIgnoreUnrecognizedUpdates, isValidUpdateName, setIgnoreUnrecognizedUpdates, setUpdates
-
Constructor Details
-
SpringPersistentObjectSchemaUpdater
Constructor.- Parameters:
delegate- delegate that will be wrapped by this instance- Throws:
IllegalArgumentException- ifdelegateis null- See Also:
-
-
Method Details
-
setDefaultXML
Set the resource containing the default value, encoded as XML, to be used on an uninitialized persistent object. This will override whatever default value is returned by the nested delegate.- Parameters:
resource- default database content
-
getDefaultValue
Get the default value for the persistent object when no persistent file is found.The implementation in
SpringPersistentObjectSchemaUpdaterparses and returns the default value resource, if any; otherwise, the delegate provided to the constructor is queried for a default value.- Specified by:
getDefaultValuein interfacePersistentObjectDelegate<T>- Overrides:
getDefaultValuein classPersistentObjectSchemaUpdater<T>- Returns:
- root object initial value, or null if there is no default value
-
setBeanFactory
- Specified by:
setBeanFactoryin interfaceBeanFactoryAware
-
afterPropertiesSet
- Specified by:
afterPropertiesSetin interfaceInitializingBean- Throws:
Exception
-
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
Comparatorreturned by the implementation inSpringPersistentObjectSchemaUpdatersorts updates in the same order that they appear in the containingListableBeanFactory. Otherwise, the superclass method is used.- Overrides:
getOrderingTieBreakerin classAbstractSchemaUpdater<PersistentFileTransaction,PersistentFileTransaction> - Returns:
- a
Comparatorthat sorts incomparable updates in the order they should be applied
-