Class PersistenceService
- java.lang.Object
-
- org.zkoss.zkex.xml.PersistenceService
-
public class PersistenceService extends java.lang.ObjectProvides a collection of static methods to support comfortable loading and storing of objects as XML data.This class uses the classes
XMLEncoderandXMLDecoderto encode and decode an object to and from an XML file for long term persistence. It allows you to provide customPersistenceDelegateinstances for the serialisation of any classes which do not implement the JavaBean design pattern and are not supported byXMLEncoderas a default.For the latter case,
PersistenceServiceoffers thesetPersistenceDelegate(Class, PersistenceDelegate)method which could be called from a static initialiser block in the class which would like to usePersistenceService'sstoreandloadmethods.Note that the Java API already provides some default persistence delegates for some classes of its API which are not JavaBeans. If in doubt, simply test the class before writing a custom
PersistenceDelegate. If you see exceptions happening, you most probably need to provide aPersistenceDelegateand use thesetPersistenceDelegatemethod to install it.Note that the store and load methods in this class have been designed to deal with any kind of
Throwables throughout the course of (de)serialisation, evenOutOfMemoryErrors.This class is designed to be thread safe, i.e. when an object is persistet, it is put into a
synchronized (object) { ... }block.- Author:
- Christian Schlichtherle
- See Also:
XMLEncoder,XMLDecoder,PersistenceDelegate,DefaultPersistenceDelegate, Sun Developer Network Site: Using XMLEncoder
-
-
Field Summary
Fields Modifier and Type Field Description static intBUFSIZEThe buffer size for I/O used in the store and load methods.static intDEFAULT_BUFSIZE10KB - the default buffer size for buffered I/O.static java.lang.StringXML_CHARSET"UTF-8"- the character encoding used byXMLEncoderandXMLDecoder.
-
Constructor Summary
Constructors Modifier Constructor Description protectedPersistenceService()You cannot instantiate this class.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static voidinstallPersistenceDelegates(java.beans.Encoder encoder)Installs all persistence delegates registered via{@link #setPersistenceDelegate(Class, PersistenceDelegate)}inencoder.static java.lang.Objectload(byte[] encoded)Loads a single object, which may form the root of an entire object graph, from XML content in the UTF-8 encoded byte arrayencoded.static java.lang.Objectload(java.io.File file)Loads a single object, which may form the root of an entire object graph, from XML content in the given filefile.static java.lang.Objectload(java.io.InputStream xmlIn)Loads a single object, which may form the root of an entire object graph, from XML content in the given input streamxmlIn.static java.lang.Objectload(java.lang.String encoded)Loads a single object, which may form the root of an entire object graph, from XML content in the stringencoded.static voidsetPersistenceDelegate(java.lang.Class clazz, java.beans.PersistenceDelegate persistenceDelegate)Associates aPersistenceDelegateto the given classtype.static voidstore(java.lang.Object root, java.io.File file)Stores the objectroot, which may form the root of an entire object graph, as XML content to the filefilefor long term persistence.static voidstore(java.lang.Object root, java.io.OutputStream xmlOut)Stores the objectroot, which may form the root of an entire object graph, as XML content to the output streamxmlOutfor long term persistence.static byte[]store2ByteArray(java.lang.Object root)Stores the objectroot, which may form the root of an entire object graph, as XML content into a UTF-8 encoded byte array for long term persistence.static java.lang.Stringstore2String(java.lang.Object root)Stores the objectroot, which may form the root of an entire object graph, as XML content into a string for long term persistence.
-
-
-
Field Detail
-
BUFSIZE
public static int BUFSIZE
The buffer size for I/O used in the store and load methods. You may customise this to your needs - the default isDEFAULT_BUFSIZE.
-
XML_CHARSET
public static final java.lang.String XML_CHARSET
"UTF-8"- the character encoding used byXMLEncoderandXMLDecoder.- See Also:
- Constant Field Values
-
DEFAULT_BUFSIZE
public static final int DEFAULT_BUFSIZE
10KB - the default buffer size for buffered I/O.- See Also:
- Constant Field Values
-
-
Method Detail
-
setPersistenceDelegate
public static final void setPersistenceDelegate(java.lang.Class clazz, java.beans.PersistenceDelegate persistenceDelegate)Associates aPersistenceDelegateto the given classtype.This must be called prior to the
storemethods for each class which's instances are to be persisted. Thus, the best place to put this call in is a static initializer block for the corresponding class.Here is an example:
class PersistentObject { static { PersistenceService.setPersistenceDelegate( PersistentObject.class, new DefaultPersistenceDelegate( new String[] { "property" })); } public int property; public PersistentObject(int property) { this.property = property; } }Note that you should not use this method for any class which's source code you can control. The preferred way to associate a persistence delegate with a class is to write a
BeanInfoclass which'sBeanDescriptorhas an attribute set with"persistenceDelegate"as its name and the respective persistence delegate as its value (seeEncoder.getPersistenceDelegate(java.lang.Class<?>)). However, this method is still useful in case you can't control the source code, as then at least you can still associate a persistence delegate to this class.- See Also:
XMLEncoder,PersistenceDelegate,DefaultPersistenceDelegate
-
installPersistenceDelegates
protected static void installPersistenceDelegates(java.beans.Encoder encoder)
Installs all persistence delegates registered via{@link #setPersistenceDelegate(Class, PersistenceDelegate)}inencoder.- Parameters:
encoder- The encoder - may not benull.- Throws:
java.lang.RuntimeException- ifencoderisnull.
-
store
public static void store(java.lang.Object root, java.io.OutputStream xmlOut) throws java.lang.NullPointerException, PersistenceServiceExceptionStores the objectroot, which may form the root of an entire object graph, as XML content to the output streamxmlOutfor long term persistence.Please note the following:
- This method will not tolerate any I/O or other serialisation exceptions!
- The underlying stream is always closed (even if an exception is thrown).
- Parameters:
root- The object to store - may benull.xmlOut- The unbuffered stream to output the XML content - may not benull.- Throws:
java.lang.NullPointerException- IfxmlOutis }null}.PersistenceServiceException- If any throwable was thrown during serialisation. Its cause is always set.
-
store
public static void store(java.lang.Object root, java.io.File file) throws java.lang.NullPointerException, PersistenceServiceExceptionStores the objectroot, which may form the root of an entire object graph, as XML content to the filefilefor long term persistence. This method supports writing to a file located in a ZIP or JAR file.Please note the following:
- This method will not tolerate any I/O or other serialisation exceptions!
- This is a transaction, i.e. the method either completely succeeds with saving the file or the file is restored to its original state.
- Parameters:
root- The object to store - may benull.file- The file to output the XML content to - may not benull.- Throws:
java.lang.NullPointerException- Iffileis }null}.PersistenceServiceException- If any throwable was thrown during serialisation. Its cause is always set.
-
store2ByteArray
public static byte[] store2ByteArray(java.lang.Object root) throws PersistenceServiceExceptionStores the objectroot, which may form the root of an entire object graph, as XML content into a UTF-8 encoded byte array for long term persistence.Please note the following:
- This method will not tolerate any I/O or other serialisation exceptions!
- Parameters:
root- The object to store - may benull.- Returns:
- The XML with UTF-8 charset encoded byte array
representation of
root-nullis never returned. - Throws:
PersistenceServiceException- If any throwable was thrown during serialisation. Its cause is always set.
-
store2String
public static java.lang.String store2String(java.lang.Object root) throws PersistenceServiceExceptionStores the objectroot, which may form the root of an entire object graph, as XML content into a string for long term persistence.Please note the following:
- This method will not tolerate any I/O or other serialisation exceptions!
- Parameters:
root- The object to store - may benull.- Returns:
- The XML string encoded representation of
root-nullis never returned. - Throws:
PersistenceServiceException- If any throwable was thrown during serialisation. Its cause is always set.
-
load
public static java.lang.Object load(java.io.InputStream xmlIn) throws java.lang.NullPointerException, PersistenceServiceExceptionLoads a single object, which may form the root of an entire object graph, from XML content in the given input streamxmlIn.Please note the following:
- The stream is connected to a new
BufferedInputStreamwithBUFSIZEas its buffer size and is always closed (even if an exception is thrown). - This method will not tolerate any I/O or other deserialisation exceptions!
- Parameters:
xmlIn- The unbuffered stream to input the XML content - may not benull.- Returns:
- The root of the loaded object graph - may be
null. - Throws:
java.lang.NullPointerException- IfxmlInisnull.PersistenceServiceException- If any throwable was thrown during serialisation. Its cause is always set.
- The stream is connected to a new
-
load
public static java.lang.Object load(java.io.File file) throws java.lang.NullPointerException, PersistenceServiceExceptionLoads a single object, which may form the root of an entire object graph, from XML content in the given filefile.Please note the following:
- This method will not tolerate any I/O or other deserialisation exceptions!
- Parameters:
file- The file to load the XML content from - may not benull.- Returns:
- The root of the loaded object graph - may be
null. - Throws:
java.lang.NullPointerException- Iffileisnull.PersistenceServiceException- If any throwable was thrown during serialisation. Its cause is always set.
-
load
public static java.lang.Object load(byte[] encoded) throws java.lang.NullPointerException, PersistenceServiceExceptionLoads a single object, which may form the root of an entire object graph, from XML content in the UTF-8 encoded byte arrayencoded.Please note the following:
- This method will not tolerate any I/O or other deserialisation exceptions!
- Parameters:
encoded- The XML with UTF-8 charset encoded byte array representation of the root of an object graph - may not benull.- Returns:
- The root of the loaded object graph - may be
null. - Throws:
java.lang.NullPointerException- Ifencodedisnull.PersistenceServiceException- If any throwable was thrown during serialisation. Its cause is always set.
-
load
public static java.lang.Object load(java.lang.String encoded) throws java.lang.NullPointerException, PersistenceServiceExceptionLoads a single object, which may form the root of an entire object graph, from XML content in the stringencoded.Please note the following:
- This method will not tolerate any I/O or other deserialisation exceptions!
- Parameters:
encoded- The XML string encoded representation of the root of an object graph - may not benull.- Returns:
- The root of the loaded object graph - may be
null. - Throws:
java.lang.NullPointerException- Ifencodedisnull.PersistenceServiceException- If any throwable was thrown during serialisation. Its cause is always set.
-
-