HowTos - Application Architecture: Persistence

Define persistence streams

Description:
The in- and output streams for making the application persistent or restoring it's state need to be defined. This is useful if you, for example, wish to use one and the same file for making your system persistent. In this case you wouldn't need a file choosing dialog to pop up everytime.

Used classes:

ToDo:

  1. Create a subclass of Shop.
  2. Override the methods retrievePersistenceOutStream and retrievePersistenceInStream according to your needs (normally you would have to override both in order to keep them in "synch"). In this example, both are set to returning streams on the file "persistence.dat".

Example Source Code:

Shop class:

// necessary imports
.
.
.
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
.
.
.
1
public class ArchitectureShop extends Shop
{
    .
    .
    .
    2
    protected OutputStream retrievePersistenceOutStream() throws IOException {
        return new FileOutputStream(new File("persistence.dat"));
    }

    protected InputStream retrievePersistenceInStream() throws IOException {
        return new FileInputStream(new File("persistence.dat"));
    }
    .
    .
    .
}
		

Back to:


Make additional objects persistent

Description:
When making the Shop persistent, only the objects the shop knows are written. These are, by default, it's members (all Catalogs, Stocks, UI elements and so on), the global user manager and the global password garbler. All other (global) objects that should be made persistent, have to be made "known" to the shop.

Used classes:

ToDo:

  1. Create a subclass of Shop.
  2. In order to make an additional object persistent while making the shop persistent, you can use it's setObjectPersistent(Object, Object) method, where the first parameter defines a key for the object and the second a reference to the object itself. A good place for doing so is using the makePersistent() method of the shop by overriding it. (In order to make the object transient again, you can use the setObjectTransient(Object) method.)
  3. When restoring the shop, you can get this object by using the getPersistentObject(Object) and the key you defined when setting it persistent.

Example Source Code:

Shop class:

// necessary imports
1
public class ArchitectureShop extends Shop
{
    .
    .
    .
    2
    public synchronized void makePersistent() throws CancelledException, IOException {
        setObjectPersistent("TheGlobalObject", SomeGlobalObject.getInstance());
        super.makePersistent();
    }
    
    3
    public synchronized void restore() throws CancelledException,
                                              ClassNotFoundException,
                                              IOException {
        super.restore();
        SomeGlobalObject.setInstance(getPersistentObject("TheGlobalObject"));
    }
    .
    .
    .
}
		

Back to:


previous Application Architecture: Time ManagementApplication Architecture: Common next



by Thomas Ryssel