1 /*
   2  * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  *
  26  * @bug 4087295
  27  * @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
  28  * @summary Enable resolveClass() to accommodate package renaming.
  29  * This fix enables one to implement a resolveClass method that maps a
  30  * Serialiazable class within a serialization stream to the same class
  31  * in a different package within the JVM runtime. See run shell script
  32  * for instructions on how to run this test.
  33  */
  34 
  35 package install;
  36 
  37 import java.io.*;
  38 import extension.ExtendedObjectInputStream;
  39 
  40 public class SerialDriver implements Serializable {
  41     private static final long serialVersionUID = 1L;
  42 
  43     String name;
  44     SerialDriver next;
  45     transient Object objarray[];
  46 
  47     public SerialDriver() {
  48         name = "<terminator>";
  49         next = null;
  50     }
  51 
  52     public SerialDriver(String name, SerialDriver next) {
  53         this.name = name;
  54         this.next = next;
  55     }
  56 
  57     static boolean serialize;
  58     static boolean deserialize;
  59 
  60     public static void main(String args[]) throws Exception  {
  61         SerialDriver obj = new SerialDriver("SerialDriver_2",
  62                                             new SerialDriver());
  63         SerialDriver[] array = new SerialDriver[5];
  64         for (int i = 0; i < array.length; i++)
  65             array[i] = new SerialDriver("SerialDriver_1_" + i, new SerialDriver());
  66 
  67         /*
  68          * see if we are serializing or deserializing.
  69          * The ability to deserialize or serialize allows
  70          * us to see the bidirectional readability and writeability
  71          */
  72         if (args.length == 1) {
  73             if (args[0].equals("-d")) {
  74                 deserialize = true;
  75             } else if (args[0].equals("-s")) {
  76                 serialize = true;
  77             } else {
  78                 usage();
  79                 throw new Exception("incorrect command line arguments");
  80             }
  81         } else {
  82             usage();
  83             throw new Exception("incorrect command line arguments");
  84         }
  85 
  86         File f = new File("stream.ser");
  87         if (serialize) {
  88             // Serialize the subclass
  89             try {
  90                 FileOutputStream fo = new FileOutputStream(f);
  91                 ObjectOutputStream so = new ObjectOutputStream(fo);
  92                 so.writeObject(obj);
  93                 /* Skip arrays since they do not work with rename yet.
  94                    The serialVersionUID changes due to the name change
  95                    and there is no way to set the serialVersionUID for an
  96                    array. */
  97                 so.writeObject(array);
  98                 so.flush();
  99             } catch (Exception e) {
 100                 System.out.println(e);
 101                 throw e;
 102             }
 103         }
 104         if (deserialize) {
 105             // Deserialize the subclass
 106             try {
 107                 FileInputStream fi = new FileInputStream(f);
 108                 ExtendedObjectInputStream si =
 109                     new ExtendedObjectInputStream(fi);
 110                 si.addRenamedClassName("test.SerialDriver", "install.SerialDriver");
 111                 si.addRenamedClassName("[Ltest.SerialDriver;",
 112                                        "[Linstall.SerialDriver");
 113                 obj = (SerialDriver) si.readObject();
 114                 array = (SerialDriver[]) si.readObject();
 115                 si.close();
 116             } catch (Exception e) {
 117                 System.out.println(e);
 118                 throw e;
 119             }
 120             System.out.println();
 121             System.out.println("Printing deserialized class: ");
 122             System.out.println();
 123             System.out.println(obj.toString());
 124             System.out.println();
 125         }
 126     }
 127 
 128 
 129     public String toString() {
 130         String nextString = next != null ? next.toString() : "<null>";
 131         return "name =" + name + " next = <" + nextString + ">";
 132     }
 133 
 134     /**
 135      * Prints out the usage
 136      */
 137     static void usage() {
 138         System.out.println("Usage:");
 139         System.out.println("      -s (in order to serialize)");
 140         System.out.println("      -d (in order to deserialize)");
 141     }
 142 }