Class PersistentObjectSchemaUpdater<T>
- Type Parameters:
T- type of the root persistent object
- All Implemented Interfaces:
PersistentObjectDelegate<T>
- Direct Known Subclasses:
SpringPersistentObjectSchemaUpdater
PersistentObjectDelegate that is also a AbstractSchemaUpdater that automatically
applies needed updates to the persistent XML file.
To use this class, wrap your normal delegate in an instance of this class. This will augment the serialization and deserialization process to keep track of which updates have been applied to the XML structure, and automatically and transparently apply any needed updates during deserialization.
Updates are tracked by inserting an element
into the serialized XML document; this update list is transparently removed when the document is read back,
and any missing updates are applied automatically.
In this way the document and its set of applied updates always travel together. For example:
<pobj:updates>
<MyConfig>
<pobj:updates xmlns:pobj="http://dellroad-stuff.googlecode.com/ns/persistentObject">
<pobj:update>some-update-name-1</pobj:update>
<pobj:update>some-update-name-2</pobj:update>
</pobj:updates>
<username>admin</username>
<password>secret</password>
</MyConfig>
For Spring applications, SpringPersistentObjectSchemaUpdater provides a convenient declarative way
to define your schema updates via XSLT files.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final PersistentObjectDelegate<T>static final StringXML namespace URI used for nested update elements.static final QNameXML element name for a single update.static final QNameXML element name for the updates list.static final StringPreferred XML namespace prefix forNAMESPACE_URIelements.static final QNameXML namespace URI used for namespace declarations.Fields inherited from class org.dellroad.stuff.schema.AbstractSchemaUpdater
log -
Constructor Summary
ConstructorsConstructorDescriptionConstructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcommitTransaction(PersistentFileTransaction transaction) Commit a previously opened transaction.Make a deep copy of the given object.protected booleandatabaseNeedsInitialization(PersistentFileTransaction transaction) Determine if the database needs initialization.deserialize(Source source) Deserialize object from XML.getAppliedUpdateNames(PersistentFileTransaction transaction) Determine which updates have already been applied to the database.Get the default value for the root object graph.voidhandleWritebackException(PersistentObject<T> pobj, Throwable t) Handle an exception thrown during a delayed write-back attempt.protected voidinitializeDatabase(PersistentFileTransaction transaction) Initialize an uninitialized database.booleanisSameGraph(T root1, T root2) Compare two object graphs.protected PersistentFileTransactionopenTransaction(PersistentFileTransaction transaction) Begin a transaction on the given database.protected voidrecordUpdateApplied(PersistentFileTransaction transaction, String name) Record an update as having been applied to the database.protected voidrollbackTransaction(PersistentFileTransaction transaction) Roll back a previously opened transaction.voidSerialize object to XML.voidsetTransformerFactory(TransformerFactory transformerFactory) Configure theTransformerFactoryto be used by this instance when reading the updates from the XML file.Validate the given instance.Methods inherited from class org.dellroad.stuff.schema.AbstractSchemaUpdater
apply, applyInTransaction, generateMultiUpdateName, getAllUpdateNames, getOrderingTieBreaker, getUpdates, initializeAndUpdateDatabase, isIgnoreUnrecognizedUpdates, isValidUpdateName, setIgnoreUnrecognizedUpdates, setUpdates
-
Field Details
-
NAMESPACE_URI
XML namespace URI used for nested update elements.- See Also:
-
XML_PREFIX
Preferred XML namespace prefix forNAMESPACE_URIelements.- See Also:
-
UPDATES_ELEMENT_NAME
XML element name for the updates list. -
UPDATE_ELEMENT_NAME
XML element name for a single update. -
XMLNS_ATTRIBUTE_NAME
XML namespace URI used for namespace declarations. -
delegate
-
-
Constructor Details
-
PersistentObjectSchemaUpdater
Constructor.Callers provide a
PersistentObjectDelegatethat will be used for all operations, with the exception that serialization and deserialization will add/remove the upate list, and deserialization will apply any needed updates as necessary.- Parameters:
delegate- delegate that will be wrapped by this instance- Throws:
IllegalArgumentException- ifdelegateis null
-
-
Method Details
-
setTransformerFactory
Configure theTransformerFactoryto be used by this instance when reading the updates from the XML file. Must support reading from a StAXSource.The implementation in
PersistentObjectSchemaUpdaterreturns null.- Parameters:
transformerFactory-TransformerFactoryto use or null for the platform default
-
copy
Make a deep copy of the given object.The implementation in
PersistentObjectSchemaUpdaterdelegates to the delegate provided in the constructor.- Specified by:
copyin interfacePersistentObjectDelegate<T>- Parameters:
original- original object- Returns:
- deep copy of the object graph rooted at
original - Throws:
IllegalArgumentException- iforiginalis nullPersistentObjectException- if an error occurs
-
isSameGraph
Compare two object graphs.The implementation in
PersistentObjectSchemaUpdaterdelegates to the delegate provided in the constructor.- Specified by:
isSameGraphin interfacePersistentObjectDelegate<T>- Parameters:
root1- root of first object graphroot2- root of second object graph- Returns:
- true if
root1androot2represent equal object graphs
-
validate
Validate the given instance.The implementation in
PersistentObjectSchemaUpdaterdelegates to the delegate provided in the constructor.- Specified by:
validatein interfacePersistentObjectDelegate<T>- Parameters:
obj- root object- Returns:
- set of zero or more constraint violations
- Throws:
IllegalArgumentException- ifobjis null
-
handleWritebackException
Handle an exception thrown during a delayed write-back attempt.The implementation in
PersistentObjectSchemaUpdaterdelegates to the delegate provided in the constructor.- Specified by:
handleWritebackExceptionin interfacePersistentObjectDelegate<T>- Parameters:
pobj- the instance that encountered the exceptiont- the exception thrown
-
getDefaultValue
Get the default value for the root object graph.The implementation in
PersistentObjectSchemaUpdaterdelegates to the delegate provided in the constructor.- Specified by:
getDefaultValuein interfacePersistentObjectDelegate<T>- Returns:
- root object initial value, or null if there is no default value
-
serialize
Serialize object to XML.The implementation in
PersistentObjectSchemaUpdaterdelegates to the delegate provided in the constructor but also adds the update list as the first XML tag.- Specified by:
serializein interfacePersistentObjectDelegate<T>- Parameters:
obj- object to serialize; must not be modifiedresult- XML destination- Throws:
PersistentObjectException- if an error occursIOException- if an I/O error occurs
-
deserialize
Deserialize object from XML.The implementation in
PersistentObjectSchemaUpdaterdelegates to the delegate provided in the constructor but also removes the update list as the first XML tag and applies any needed updates.- Specified by:
deserializein interfacePersistentObjectDelegate<T>- Parameters:
source- XML source- Returns:
- deserialized object
- Throws:
PersistentObjectException- if an error occursIOException- if an I/O error occurs
-
databaseNeedsInitialization
protected boolean databaseNeedsInitialization(PersistentFileTransaction transaction) throws Exception Description copied from class:AbstractSchemaUpdaterDetermine if the database needs initialization.If so,
AbstractSchemaUpdater.initializeDatabase(T)will eventually be invoked.- Specified by:
databaseNeedsInitializationin classAbstractSchemaUpdater<PersistentFileTransaction,PersistentFileTransaction> - Parameters:
transaction- open transaction- Returns:
- true if the database needs initialization
- Throws:
Exception- if an error occurs while accessing the database
-
initializeDatabase
Description copied from class:AbstractSchemaUpdaterInitialize an uninitialized database. This should create and initialize the database schema and content, including whatever portion of that is used to track schema updates.- Specified by:
initializeDatabasein classAbstractSchemaUpdater<PersistentFileTransaction,PersistentFileTransaction> - Parameters:
transaction- open transaction- Throws:
Exception- if an error occurs while accessing the database
-
openTransaction
protected PersistentFileTransaction openTransaction(PersistentFileTransaction transaction) throws Exception Description copied from class:AbstractSchemaUpdaterBegin a transaction on the given database. The transaction will always eventually either be committed or rolled back.- Specified by:
openTransactionin classAbstractSchemaUpdater<PersistentFileTransaction,PersistentFileTransaction> - Parameters:
transaction- database- Returns:
- transaction handle
- Throws:
Exception- if an error occurs while accessing the database
-
commitTransaction
Description copied from class:AbstractSchemaUpdaterCommit a previously opened transaction.- Specified by:
commitTransactionin classAbstractSchemaUpdater<PersistentFileTransaction,PersistentFileTransaction> - Parameters:
transaction- open transaction previously returned fromopenTransaction()- Throws:
Exception- if an error occurs while accessing the database
-
rollbackTransaction
Description copied from class:AbstractSchemaUpdaterRoll back a previously opened transaction. This method will also be invoked ifcommitTransaction()throws an exception.- Specified by:
rollbackTransactionin classAbstractSchemaUpdater<PersistentFileTransaction,PersistentFileTransaction> - Parameters:
transaction- open transaction previously returned fromopenTransaction()- Throws:
Exception- if an error occurs while accessing the database
-
getAppliedUpdateNames
Description copied from class:AbstractSchemaUpdaterDetermine which updates have already been applied to the database.- Specified by:
getAppliedUpdateNamesin classAbstractSchemaUpdater<PersistentFileTransaction,PersistentFileTransaction> - Parameters:
transaction- open transaction- Returns:
- set of already-applied updates
- Throws:
Exception- if an error occurs while accessing the database
-
recordUpdateApplied
protected void recordUpdateApplied(PersistentFileTransaction transaction, String name) throws Exception Description copied from class:AbstractSchemaUpdaterRecord an update as having been applied to the database.- Specified by:
recordUpdateAppliedin classAbstractSchemaUpdater<PersistentFileTransaction,PersistentFileTransaction> - Parameters:
transaction- open transactionname- update name- Throws:
IllegalStateException- if the update has already been recorded in the databaseException- if an error occurs while accessing the database
-