home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / serialization / examples / externpers / ExternExampleEvolvedClass.java next >
Encoding:
Java Source  |  1999-09-19  |  6.2 KB  |  214 lines

  1. /*
  2.  * @(#)ExternExampleEvolvedClass.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.  * This is a continuation of the Externalizable Persistence Example.
  27.  * This file contains the evolved version of the ExternVersioningClass.
  28.  * Of course, normally, the evolved class would actually take place of the
  29.  * original class but we just keep both of them to demonstrate the evolution
  30.  *
  31.  * For information on how to run, see file ExternExampleOriginalClass.java
  32.  */
  33. public class ExternExampleEvolvedClass {
  34.  
  35.     /** 
  36.      *  There are two options: either a user can serialize an object or
  37.      *  deserialize it. (using the -s or -d flag). These options allow
  38.      *  for the demonstration of bidirection readability and writeability
  39.      *  between the original and the evolved class. In other words,
  40.      *  one can serialize an object here and deserialize it with the evolved
  41.      *  class or vice versa.
  42.      */
  43.     public static void main(String args[]) {  
  44.     
  45.     boolean serialize = false;
  46.     boolean deserialize = false;
  47.     
  48.     ExternVersioningClass wobj = new ExternVersioningClass
  49.         (1, "evolvedclass", true, 1.23);
  50.     ExternVersioningClass robj = null;
  51.     /* 
  52.      * see if we are serializing or deserializing.
  53.      * The ability to deserialize or serialize allows
  54.      * us to see the bidirectional readability and writeability
  55.      */
  56.     if (args.length == 1) {
  57.         if (args[0].equals("-d")) {
  58.         deserialize = true;
  59.         } else if (args[0].equals("-s")) {
  60.             serialize = true;
  61.         } else {
  62.             usage();
  63.             System.exit(0);
  64.         }
  65.     } else {
  66.         usage();
  67.         System.exit(0);
  68.     }
  69.     
  70.     /* 
  71.      * serialize, if that's the chosen option
  72.      */
  73.     if (serialize) {
  74.         try {
  75.         FileOutputStream fo = new FileOutputStream("evolve.tmp");
  76.         ObjectOutputStream so = new ObjectOutputStream(fo);
  77.         so.writeObject(wobj);
  78.         so.flush();
  79.         } catch (Exception e) {
  80.         System.out.println(e);
  81.         System.exit(1);
  82.         }
  83.     }
  84.     
  85.     /* 
  86.      * deserialize, if that's the chosen option
  87.      */
  88.     if (deserialize) {
  89.         try {
  90.         FileInputStream fi = new FileInputStream("evolve.tmp");
  91.         ObjectInputStream si = new ObjectInputStream(fi);
  92.         robj = (ExternVersioningClass) si.readObject();
  93.         } catch (Exception e) {
  94.         System.out.println(e);
  95.         System.exit(1);
  96.         }
  97.     }
  98.     }
  99.  
  100.     /** 
  101.      * Prints out the usage
  102.      */
  103.     static void usage() {
  104.     System.out.println("Usage:");
  105.     System.out.println("      -s (in order to serialize)");
  106.     System.out.println("      -d (in order to deserialize)");
  107.     }
  108. }
  109.     
  110.    
  111. /**
  112.  * The evolved class!
  113.  */
  114. class ExternVersioningClass implements Externalizable  {
  115.  
  116.     /* 
  117.      * Each versioned class must identify the original class version from which
  118.      * it evolved. This SUID is obtained using the command serialver on the 
  119.      * original class
  120.      */
  121.     static final long serialVersionUID = -6527577423406625824L;
  122.     public static final int version = 2;
  123.     
  124.     // the data that was part of the original class
  125.     int dimension;
  126.     int array[];
  127.     String name;
  128.     
  129.     // new data 
  130.     boolean b;
  131.     double d;
  132.         
  133.     // ***need to have a public-no-arg constructor***
  134.     public ExternVersioningClass() {}
  135.         
  136.     ExternVersioningClass(int dim, String n, boolean boo, double dou){
  137.     // initialize 
  138.     dimension = dim;
  139.     array = new int[dimension];
  140.     name = n;
  141.     b = boo;
  142.     d = dou;
  143.     }
  144.  
  145.     /** 
  146.      * the mandatory writeExternal method. First writes out the version number
  147.      * (ie. 2 because this is the evolved class.) Then writes out the fields
  148.      * that were a part of the original class and then the fields that are
  149.      * in this class. (since the original class assumes that the new data
  150.      * fields are appended)
  151.      *
  152.      * @serialData Writes out the int "class version number" 2. Next, the
  153.      *             dimension field is written as an int. Followed by
  154.      *             the array field written as an object. The
  155.      *             name field is written as an object. The new b field is
  156.      *             written as a boolean. Lastly, the new d field is written
  157.      *             as a double.
  158.      */
  159.     public void writeExternal(ObjectOutput out) throws IOException {
  160.     
  161.     // we first write out the class version number (ie. # 2)
  162.     out.writeInt(version);
  163.     
  164.     // We have to first write out the data of the original class because it
  165.     // it assumes that we appended the new data.
  166.     out.writeInt(dimension);
  167.     out.writeObject(array);
  168.     out.writeObject(name);
  169.     
  170.     // the original class will ignore this data.
  171.     out.writeBoolean(b);
  172.     out.writeDouble(d);
  173.     }
  174.  
  175.     /** 
  176.      * mandatory readExternal method. 
  177.      *
  178.      * @serialData First reads in the version number and
  179.      *             then if the version number suggests that the object
  180.      *             was written as an original class, gives the new fields
  181.      *             default values but if the object was written by this 
  182.      *             evolved class, then reads in all the fields accordingly.
  183.      */
  184.     public void readExternal(ObjectInput in) 
  185.     throws IOException, java.lang.ClassNotFoundException 
  186.     {
  187.     int version = in.readInt();
  188.     System.out.println
  189.         ("Reading an Object written by Version #: " + version);
  190.     
  191.     dimension = in.readInt();
  192.     
  193.     // need to allocate memory for the array we will read in
  194.     array = (int[]) in.readObject();
  195.     name = (String) in.readObject();
  196.     
  197.     
  198.     /*
  199.      * only if the object was written out by the evolved class, do we 
  200.      * read in the rest of the data.
  201.      */
  202.     if (version == 2) {
  203.         b = in.readBoolean();
  204.         d = in.readDouble();
  205.     }
  206.     
  207.     // otherwise, we give it default values
  208.     else if(version == 1) {
  209.         b = true;
  210.         d = 1.00;
  211.     }
  212.     }
  213. }
  214.