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

  1. /*
  2.  * @(#)EvolutionExampleOriginalClass.java    1.3 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. import java.util.*;
  24.  
  25. /** 
  26.  * When Java objects use serialization to store objects, the potential arises
  27.  * that the version of the class reading the data is different from the version
  28.  * of class that wrote that data
  29.  
  30.  *
  31.  * This example demonstrates some of the compatible changes that Serialization
  32.  * handles without class-specific methods
  33.  
  34.  * How to Run:
  35.  * 
  36.  * Compile Original Class: 
  37.  *         javac EvolutionExampleOriginalClass.java
  38.  * Run Original Class with serialization flag: 
  39.  *         java EvolutionExampleOriginalClass -s
  40.  * Compile Evolved Class:
  41.  *         javac EvolutionExampleEvolvedClass.java
  42.  * Run Evolved Class with deserialization flag:
  43.  *         java EvolutionExampleEvolvedClass -d
  44.  *
  45.  * This tests compatibility in one direction only. Perform the same steps in 
  46.  * the other direction to see bidirectional compatibility.
  47.  *
  48.  
  49.  * Compiled and Tested with JDK1.1.4 & JDK1.2
  50.  * This file contains the original class.
  51.  * The evolved class is in file called EvolutionExampleEvolvedClass.java
  52.  */
  53. public class EvolutionExampleOriginalClass {
  54.  
  55.  
  56.     /** 
  57.      *  There are two options: either a user can serialize an object or
  58.      *  deserialize it. (using the -s or -d flag). These options allow
  59.      *  for the demonstration of bidirection readability and writeability
  60.      *  between the original and the evolved class. In other words,
  61.      *  one can serialize an object here and deserialize it with the evolved
  62.      *  class or vice versa.
  63.      */
  64.     public static void main(String args[]) {
  65.     
  66.     boolean serialize = false;
  67.     boolean deserialize = false;
  68.     /* 
  69.      * see if we are serializing or deserializing.
  70.      * The ability to deserialize or serialize allows
  71.      * us to see the bidirectional readability and writeability
  72.      */
  73.     if (args.length == 1) {
  74.         if (args[0].equals("-d")) {
  75.         deserialize = true;
  76.         } else if (args[0].equals("-s")) {
  77.         serialize = true;
  78.         } else {
  79.         usage();
  80.         System.exit(0);
  81.         }
  82.     } else {
  83.         usage();
  84.         System.exit(0);
  85.     }
  86.         
  87.     AClass serializeclass = new AClass(10, "serializedByOriginalClass");
  88.     AClass deserializeclass = null;
  89.  
  90.     /* 
  91.      * Serialize the original class if that's the option chosen
  92.      */
  93.     if (serialize) {
  94.         try {
  95.         FileOutputStream fo = new FileOutputStream("evolve.tmp");
  96.         ObjectOutputStream so = new ObjectOutputStream(fo);
  97.         so.writeObject(serializeclass);
  98.         so.flush();
  99.         } catch (Exception e) {
  100.         System.out.println(e);
  101.         System.exit(1);
  102.         }
  103.     }
  104.     
  105.     /* 
  106.      * Deserialize, if that's the option chosen and print the name
  107.      * of the object, which will allow us to see who serialized the
  108.      * object, the original class or the evolved class file
  109.      */
  110.     if (deserialize) {
  111.         try {
  112.         FileInputStream fi = new FileInputStream("evolve.tmp");
  113.         ObjectInputStream si = new ObjectInputStream(fi);  
  114.         deserializeclass = (AClass) si.readObject();
  115.         } catch (Exception e) {
  116.         System.out.println(e);
  117.         System.exit(1);
  118.         }
  119.         /* 
  120.          * Print out to see that it is indeed the same object as it was
  121.          * when it was serialized (depending on whether it was the original
  122.          * class that serialized it or the evolved class)
  123.          */
  124.         System.out.println("Now printing deserialized object's name: ");
  125.         System.out.println();
  126.         System.out.println("name: " + deserializeclass.name);
  127.         System.out.println();
  128.     }
  129.     }
  130.  
  131.     /** 
  132.      * Prints out the usage
  133.      */
  134.     static void usage() {
  135.     System.out.println("Usage:");
  136.     System.out.println("      -s (in order to serialize)");
  137.     System.out.println("      -d (in order to deserialize)");
  138.     }
  139. }
  140.  
  141. /* 
  142.  * Original Class
  143.  * will evolve later (see the other file)
  144.  */
  145. class AClass implements Serializable {
  146.     
  147.     // some data fields.
  148.  
  149.     /** 
  150.      * @serial
  151.      */
  152.     private int num;
  153.  
  154.     /** 
  155.      * @serial
  156.      */
  157.     String name;
  158.  
  159.     static Hashtable ht = new Hashtable();
  160.  
  161.     // ...
  162.     // ...
  163.     // ...
  164.     
  165.     AClass(int n, String s) {
  166.     num = n;
  167.     name = s;
  168.     }
  169.    
  170.     // some methods...
  171.     // ...
  172.     // ...
  173.  
  174.     /** 
  175.      * These writeObject and readObject merely defaultwriteObject and
  176.      * defaultreadObject - so they don't do anything.. but they are placed
  177.      * here so that we can show that we can remove them in the evolved 
  178.      * class without any effect.
  179.      *
  180.      * @serialData Write serializable fields. No optional data written.
  181.      */
  182.     private void writeObject(ObjectOutputStream s)
  183.     throws IOException {    
  184.     s.defaultWriteObject();
  185.     }
  186.   
  187.     /** 
  188.      * readObject - just calls defaultreadObject()
  189.      *
  190.      * @serialData Read serializable fields. No optional data read.
  191.      */
  192.     private void readObject(ObjectInputStream s)
  193.     throws IOException, ClassNotFoundException {
  194.         s.defaultReadObject();
  195.     }
  196. }
  197.