FileDocCategorySizeDatePackage
ExternExampleOriginalClass.javaAPI DocExample5821Wed Apr 19 11:21:16 BST 2000None

ExternExampleOriginalClass

public class ExternExampleOriginalClass extends Object
This example shows how to evolve a persistent format using the Externalizable interface. A class using the Externalizable interface is responsible for saving its own state, for saving the state of its supertype, and for versioning including skipping over data. This example specifically deals with versioning - (To see how to save the state of the supertype, which can Externalizable or not, see relevant examples)... How to Run: Compile Original Class: javac ExternExampleOriginalClass.java Run Original Class with serialization flag: java ExternExampleOriginalClass -s Compile Evolved Class: javac ExternExampleEvolvedClass.java Run Evolved Class with deserialization flag: java ExternExampleEvolvedClass -d This tests compatibility in one direction. Do the same in other direction to see bidirectional compatibility. Compiled and Tested with JDK1.1.4 & JDK1.2

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
There are two options: either a user can serialize an object or deserialize it. (using the -s or -d flag). These options allow for the demonstration of bidirection readability and writeability between the original and the evolved class. In other words, one can serialize an object here and deserialize it with the evolved class or vice versa.

  
	
	boolean serialize = false;
	boolean deserialize = false;
	
	ExternVersioningClass wobj = new ExternVersioningClass(2, "oldclass");
	ExternVersioningClass robj = null;
	/* 
	 * see if we are serializing or deserializing.
	 * The ability to deserialize or serialize allows
	 * us to see the bidirectional readability and writeability
	 */
	if (args.length == 1) {
	    if (args[0].equals("-d")) {
		deserialize = true;
	    } else if (args[0].equals("-s")) {
		serialize = true;
	    } else {
                usage();
                System.exit(0);
	    }
	} else {
	    usage();
	    System.exit(0);
     }
	
	/* 
	 * serialize, if that's the chosen option
	 */
	if (serialize) {
	    try {
		FileOutputStream fo = new FileOutputStream("evolve.tmp");
		ObjectOutputStream so = new ObjectOutputStream(fo);
		so.writeObject(wobj);
		so.close();
	    } catch (Exception e) {
		e.printStackTrace();
		System.exit(1);
	    }
	}
	
	/* 
	 * deserialize, if that's the chosen option
	 */
	if (deserialize) {
	    try {
		FileInputStream fi = new FileInputStream("evolve.tmp");
		ObjectInputStream si = new ObjectInputStream(fi);
		robj = (ExternVersioningClass) si.readObject();
	    } catch (Exception e) {
		e.printStackTrace();
		System.exit(1);
	    }
	}
    
static voidusage()
Prints out the usage

        System.out.println("Usage:");
        System.out.println("      -s (in order to serialize)");
        System.out.println("      -d (in order to deserialize)");