home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / serialization / examples / evolvenewsuper / OriginalClass.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  5.3 KB  |  204 lines

  1. /*
  2.  * @(#)OriginalClass.java    1.2 98/10/01        
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * 
  20.  */
  21.  
  22. import java.io.*;
  23.  
  24. /**
  25.  * This example shows how to use versioning in serialization to evolve
  26.  * the class to have a new superclass. 
  27.  * 
  28.  * For example if initially the class structure was:
  29.  *
  30.  * class A {...};
  31.  * class C extends A {...};
  32.  *
  33.  * and the evolved clss structure looks like:
  34.  * 
  35.  * class A {...};
  36.  * class B extends A {...};
  37.  * class C extends B {...};
  38.  *
  39.  * then it should be possible to read the old version of C with
  40.  * the new version of C and vice versa.
  41.  *
  42.  * This example demonstrates this. 
  43.  *
  44.  * NOTE: In this example, the superclasses (A and B in the above example)
  45.  * implement the Serializable interface. If they did not, then it would
  46.  * be the responsibility of the subclass C to save and restore the fields of
  47.  * A and B. See the relevant example called: 
  48.  * "Serialization with a NonSerializable Superclass" 
  49.  *
  50.  * Looking at the code/comments is sufficient but if you want to run:
  51.  *
  52.  * How to Run:
  53.  * 
  54.  * Compile Original Class: 
  55.  *         javac OriginalClass.java
  56.  * Run Original Class with serialization flag: 
  57.  *         java OriginalClass -s
  58.  * Compile Evolved Class:
  59.  *         javac EvolvedClass.java
  60.  * Run Evolved Class with deserialization flag:
  61.  *         java EvolvedClass -d
  62.  *
  63.  * This tests compatibility in one direction. Do the same in other
  64.  * direction to see  bidirectional compatibility.
  65.  *
  66.  * Compiled and Tested with JDK1.2
  67.  * This file contains the original class.
  68.  * The original class is in file called EvolvedClass.java
  69.  */
  70.  
  71. public class OriginalClass {
  72.  
  73.     /** 
  74.      * There are two options: either a user can serialize an object or
  75.      *  deserialize it. (using the -s or -d flag). These options allow
  76.      *  for the demonstration of bidirection readability and writeability
  77.      *  between the original and the evolved class. In other words,
  78.      *  one can serialize an object here and deserialize it with the evolved
  79.      *  class or vice versa.
  80.      */
  81.     public static void main(String args[]) {
  82.     ASubClass corg = new ASubClass(1, "SerializedByOriginalClass");
  83.     ASubClass cnew = null;
  84.     boolean serialize = false;
  85.     boolean deserialize = false;
  86.  
  87.     /* 
  88.      * see if we are serializing or deserializing.
  89.          * The ability to deserialize or serialize allows
  90.          * us to see the bidirectional readability and writeability
  91.          */
  92.         if (args.length == 1) {
  93.             if (args[0].equals("-d")) {
  94.                 deserialize = true;
  95.             } else if (args[0].equals("-s")) {
  96.                 serialize = true;
  97.             } else {
  98.                 usage();
  99.                 System.exit(0);
  100.             }
  101.         } else {
  102.             usage();
  103.             System.exit(0);
  104.         }
  105.     
  106.     if (serialize) {
  107.         // Serialize the subclass
  108.         try {
  109.         FileOutputStream fo = new FileOutputStream("tmp");
  110.         ObjectOutputStream so = new ObjectOutputStream(fo);
  111.         so.writeObject(corg);
  112.         so.flush();
  113.         } catch (Exception e) {
  114.         System.out.println(e);
  115.         System.exit(1);
  116.         }
  117.     }
  118.     
  119.  
  120.     if (deserialize) {
  121.         // Deserialize the subclass
  122.         try {
  123.         FileInputStream fi = new FileInputStream("tmp");
  124.         ObjectInputStream si = new ObjectInputStream(fi);  
  125.         cnew = (ASubClass) si.readObject();
  126.         
  127.         } catch (Exception e) {
  128.         System.out.println(e);
  129.         System.exit(1);
  130.         }
  131.         System.out.println();
  132.         System.out.println("Printing deserialized class: ");
  133.         System.out.println();
  134.         System.out.println(cnew);
  135.         System.out.println();
  136.     }
  137.     }
  138.  
  139.     /** 
  140.      * Prints out the usage
  141.      */
  142.     static void usage() {
  143.         System.out.println("Usage:");
  144.         System.out.println("      -s (in order to serialize)");
  145.         System.out.println("      -d (in order to deserialize)");
  146.     }
  147. }
  148.  
  149.  
  150. /** 
  151.  * A simple superclass that implements Serializable
  152.  */
  153. class ASuperClass implements Serializable {
  154.  
  155.     /**
  156.      * @serial
  157.      */
  158.     String name;
  159.     
  160.     ASuperClass(String name) {
  161.     this.name = name;
  162.     }
  163.  
  164.     public String toString() {
  165.     return("Name: " + name);
  166.     }
  167. }
  168.  
  169. /** 
  170.  * A simple subclass that implements Serializable and extends
  171.  * a serializable superclass.
  172.  *
  173.  * Again note, if the superclass was not serializable, then it would
  174.  * be the responsibility of this subclass to save and restore the
  175.  * superclass's fields. See example called "Serialization with a
  176.  * NonSerializable Superclass" for more details.
  177.  */
  178. class ASubClass extends ASuperClass implements Serializable {
  179.     
  180.     /** 
  181.      * @serial
  182.      */
  183.     int num;
  184.  
  185.     ASubClass(int num, String name) {
  186.     super(name);
  187.     this.num = num;
  188.     }
  189.  
  190.     public String toString() {
  191.     return (super.toString() + "\nNum:  " + num);
  192.     }
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.