dsto.dfc.databeans.io
Class DataObjectPersister2

java.lang.Object
  extended by dsto.dfc.util.Scheduler.Task
      extended by dsto.dfc.databeans.io.DataObjectPersister2
All Implemented Interfaces:
PropertyListener, Runnable, EventListener

public class DataObjectPersister2
extends Scheduler.Task
implements PropertyListener

A task that saves a data object at periodic intervals when it has been modified. This mostly replaces the original DataObjectPersisterclass, which is only really useful if you want to use java.util.Timer.

NOTE: This task differs from the original DataObjectPersister in that it uses the DFC Scheduler's ability to re-schedule and thus avoids being in the task queue unless a change actually needs to be saved. This allows the persister to be automatically GC'd with the target object, or shut down cleanly when the scheduler is, removing the need to explictly dispose () this object.


Nested Class Summary
protected  class DataObjectPersister2.CleanupTask
          Cleans up old backups according to the following rules Preserve a trail of backups, daily backups for 1 week weekly backups of 1 month and monthly backups for 1 year.
 
Field Summary
protected  boolean backup
           
protected static int BACKUP_DAILY
           
protected static int BACKUP_MONTHLY
           
static String BACKUP_REGEX
           
protected static int BACKUP_WEEKLY
           
protected  File file
           
protected  long firstUnsavedChange
           
 boolean forceSaveOnExit
          Set to true to force the persister to save on exit (dispose ()) rather than rely on the modified flag.
protected  long lastBackedUp
           
protected  long maxBackupInterval
           
protected  long maxSaveInterval
           
protected  boolean modified
           
protected  IDataObject object
           
protected  long saveInterval
           
protected  Scheduler scheduler
           
protected  XmlOutput xmlOutput
           
 
Fields inherited from class dsto.dfc.util.Scheduler.Task
runAt
 
Constructor Summary
DataObjectPersister2(Scheduler scheduler, IDataObject object, File file)
           
DataObjectPersister2(Scheduler scheduler, IDataObject object, File file, boolean backup)
           
DataObjectPersister2(Scheduler scheduler, IDataObject object, File file, long saveInterval, long maxSaveInterval, long maxBackupInterval, boolean backup)
           
DataObjectPersister2(Scheduler scheduler, IDataObject object, String filename)
           
 
Method Summary
 void dispose()
          Called when the task is in the scheduling queue (but not currently running) and the scheduler is being shut down.
protected  String getBackupFilename(File f)
           
protected  String getBackupFileRegex(File f)
           
protected  File[] getBackups()
          Returns an array of back up files for the persistance file sorted by last modified in desceding order, ie the most recent backup file is at index 0;
 int getMaxSaveInterval()
           
 int getSaveInterval()
           
 XmlOutput getXmlOutput()
           
 void propertyValueChanged(PropertyEvent e)
           
 void run()
           
 void setMaxSaveInterval(int maxSaveInterval)
          Set the maximum the persister will allow a modified object to remain unsaved.
 void setModified(boolean value)
           
 void setSaveInterval(int saveInterval)
          Set the interval that must pass without modifications before a modified object is saved.
 
Methods inherited from class dsto.dfc.util.Scheduler.Task
scheduled
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BACKUP_REGEX

public static final String BACKUP_REGEX
See Also:
Constant Field Values

BACKUP_DAILY

protected static final int BACKUP_DAILY
See Also:
Constant Field Values

BACKUP_WEEKLY

protected static final int BACKUP_WEEKLY
See Also:
Constant Field Values

BACKUP_MONTHLY

protected static final int BACKUP_MONTHLY
See Also:
Constant Field Values

forceSaveOnExit

public boolean forceSaveOnExit
Set to true to force the persister to save on exit (dispose ()) rather than rely on the modified flag. Default is false.


scheduler

protected Scheduler scheduler

object

protected IDataObject object

file

protected File file

xmlOutput

protected XmlOutput xmlOutput

modified

protected boolean modified

firstUnsavedChange

protected long firstUnsavedChange

lastBackedUp

protected long lastBackedUp

saveInterval

protected long saveInterval

maxSaveInterval

protected long maxSaveInterval

maxBackupInterval

protected long maxBackupInterval

backup

protected boolean backup
Constructor Detail

DataObjectPersister2

public DataObjectPersister2(Scheduler scheduler,
                            IDataObject object,
                            String filename)

DataObjectPersister2

public DataObjectPersister2(Scheduler scheduler,
                            IDataObject object,
                            File file)

DataObjectPersister2

public DataObjectPersister2(Scheduler scheduler,
                            IDataObject object,
                            File file,
                            boolean backup)

DataObjectPersister2

public DataObjectPersister2(Scheduler scheduler,
                            IDataObject object,
                            File file,
                            long saveInterval,
                            long maxSaveInterval,
                            long maxBackupInterval,
                            boolean backup)
Method Detail

dispose

public void dispose()
Description copied from class: Scheduler.Task
Called when the task is in the scheduling queue (but not currently running) and the scheduler is being shut down. After this has been called, the task will not get another chance to run in the current scheduler.

The default implementation calls run () if the task is scheduled to be run now, does nothing if not. Subclasses may override as needed.

Overrides:
dispose in class Scheduler.Task

getXmlOutput

public XmlOutput getXmlOutput()

getMaxSaveInterval

public int getMaxSaveInterval()

setMaxSaveInterval

public void setMaxSaveInterval(int maxSaveInterval)
Set the maximum the persister will allow a modified object to remain unsaved.

Parameters:
maxSaveInterval - interval in seconds.

getSaveInterval

public int getSaveInterval()

setSaveInterval

public void setSaveInterval(int saveInterval)
Set the interval that must pass without modifications before a modified object is saved.

Parameters:
saveInterval - The interval in seconds.

run

public void run()
Specified by:
run in interface Runnable
Specified by:
run in class Scheduler.Task

setModified

public void setModified(boolean value)

propertyValueChanged

public void propertyValueChanged(PropertyEvent e)
Specified by:
propertyValueChanged in interface PropertyListener

getBackupFilename

protected String getBackupFilename(File f)

getBackups

protected File[] getBackups()
Returns an array of back up files for the persistance file sorted by last modified in desceding order, ie the most recent backup file is at index 0;


getBackupFileRegex

protected String getBackupFileRegex(File f)


Copyright © 2008 Commonwealth of Australia