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
Modifier and TypeFieldDescriptionprotected final PersistentObjectDelegate<T>
static final String
XML namespace URI used for nested update elements.static final QName
XML element name for a single update.static final QName
XML element name for the updates list.static final String
Preferred XML namespace prefix forNAMESPACE_URI
elements.static final QName
XML namespace URI used for namespace declarations.Fields inherited from class org.dellroad.stuff.schema.AbstractSchemaUpdater
log
-
Constructor Summary
ConstructorDescriptionConstructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
commitTransaction
(PersistentFileTransaction transaction) Commit a previously opened transaction.Make a deep copy of the given object.protected boolean
databaseNeedsInitialization
(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.void
handleWritebackException
(PersistentObject<T> pobj, Throwable t) Handle an exception thrown during a delayed write-back attempt.protected void
initializeDatabase
(PersistentFileTransaction transaction) Initialize an uninitialized database.boolean
isSameGraph
(T root1, T root2) Compare two object graphs.protected PersistentFileTransaction
openTransaction
(PersistentFileTransaction transaction) Begin a transaction on the given database.protected void
recordUpdateApplied
(PersistentFileTransaction transaction, String name) Record an update as having been applied to the database.protected void
rollbackTransaction
(PersistentFileTransaction transaction) Roll back a previously opened transaction.void
Serialize object to XML.void
setTransformerFactory
(TransformerFactory transformerFactory) Configure theTransformerFactory
to 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_URI
elements.- 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
PersistentObjectDelegate
that 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
- ifdelegate
is null
-
-
Method Details
-
setTransformerFactory
Configure theTransformerFactory
to be used by this instance when reading the updates from the XML file. Must support reading from a StAXSource
.The implementation in
PersistentObjectSchemaUpdater
returns null.- Parameters:
transformerFactory
-TransformerFactory
to use or null for the platform default
-
copy
Make a deep copy of the given object.The implementation in
PersistentObjectSchemaUpdater
delegates to the delegate provided in the constructor.- Specified by:
copy
in interfacePersistentObjectDelegate<T>
- Parameters:
original
- original object- Returns:
- deep copy of the object graph rooted at
original
- Throws:
IllegalArgumentException
- iforiginal
is nullPersistentObjectException
- if an error occurs
-
isSameGraph
Compare two object graphs.The implementation in
PersistentObjectSchemaUpdater
delegates to the delegate provided in the constructor.- Specified by:
isSameGraph
in interfacePersistentObjectDelegate<T>
- Parameters:
root1
- root of first object graphroot2
- root of second object graph- Returns:
- true if
root1
androot2
represent equal object graphs
-
validate
Validate the given instance.The implementation in
PersistentObjectSchemaUpdater
delegates to the delegate provided in the constructor.- Specified by:
validate
in interfacePersistentObjectDelegate<T>
- Parameters:
obj
- root object- Returns:
- set of zero or more constraint violations
- Throws:
IllegalArgumentException
- ifobj
is null
-
handleWritebackException
Handle an exception thrown during a delayed write-back attempt.The implementation in
PersistentObjectSchemaUpdater
delegates to the delegate provided in the constructor.- Specified by:
handleWritebackException
in 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
PersistentObjectSchemaUpdater
delegates to the delegate provided in the constructor.- Specified by:
getDefaultValue
in interfacePersistentObjectDelegate<T>
- Returns:
- root object initial value, or null if there is no default value
-
serialize
Serialize object to XML.The implementation in
PersistentObjectSchemaUpdater
delegates to the delegate provided in the constructor but also adds the update list as the first XML tag.- Specified by:
serialize
in 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
PersistentObjectSchemaUpdater
delegates 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:
deserialize
in 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:AbstractSchemaUpdater
Determine if the database needs initialization.If so,
AbstractSchemaUpdater.initializeDatabase(T)
will eventually be invoked.- Specified by:
databaseNeedsInitialization
in 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:AbstractSchemaUpdater
Initialize 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:
initializeDatabase
in 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:AbstractSchemaUpdater
Begin a transaction on the given database. The transaction will always eventually either be committed or rolled back.- Specified by:
openTransaction
in classAbstractSchemaUpdater<PersistentFileTransaction,
PersistentFileTransaction> - Parameters:
transaction
- database- Returns:
- transaction handle
- Throws:
Exception
- if an error occurs while accessing the database
-
commitTransaction
Description copied from class:AbstractSchemaUpdater
Commit a previously opened transaction.- Specified by:
commitTransaction
in 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:AbstractSchemaUpdater
Roll back a previously opened transaction. This method will also be invoked ifcommitTransaction()
throws an exception.- Specified by:
rollbackTransaction
in 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:AbstractSchemaUpdater
Determine which updates have already been applied to the database.- Specified by:
getAppliedUpdateNames
in 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:AbstractSchemaUpdater
Record an update as having been applied to the database.- Specified by:
recordUpdateApplied
in 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
-