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 test;
  36 
  37 import java.io.*;
  38 import extension.ExtendedObjectInputStream;
  39 
  40 public class SerialDriver implements Serializable {
  41     private static final long serialVersionUID = 1L;
  42     String name;
  43     SerialDriver next;
  44 
  45     public SerialDriver() {
  46         name = "<terminator>";
  47         next = null;
  48     }
  49 
  50     public SerialDriver(String name, SerialDriver next) {
  51         this.name = name;
  52         this.next = next;
  53     }
  54 
  55     static boolean serialize = false;
  56     static boolean deserialize = false;
  57 
  58     public static void main(String args[])  throws Exception  {
  59         SerialDriver obj = new SerialDriver("SerialDriver_1", new SerialDriver());
  60         SerialDriver[] array = new SerialDriver[5];
  61         for (int i = 0; i < array.length; i++)
  62             array[i] = new SerialDriver("SerialDriver_1_" + i, new SerialDriver());
  63 
  64         /*
  65          * see if we are serializing or deserializing.
  66          * The ability to deserialize or serialize allows
  67          * us to see the bidirectional readability and writeability
  68          */
  69         if (args.length == 1) {
  70             if (args[0].equals("-d")) {
  71                 deserialize = true;
  72             } else if (args[0].equals("-s")) {
  73                 serialize = true;
  74             } else {
  75                 usage();
  76                 throw new Exception("incorrect command line arguments");
  77             }
  78         } else {
  79             usage();
  80             throw new Exception("incorrect command line arguments");
  81         }
  82 
  83         File f = new File("stream.ser");
  84         if (serialize) {
  85             // Serialize the subclass
  86             try {
  87                 FileOutputStream fo = new FileOutputStream(f);
  88                 ObjectOutputStream so = new ObjectOutputStream(fo);
  89                 so.writeObject(obj);
  90                 /* Comment out since renaming arrays does not work
  91                    since it changes the serialVersionUID. */
  92                 so.writeObject(array);
  93                 so.flush();
  94             } catch (Exception e) {
  95                 System.out.println(e);
  96                 throw e;
  97             }
  98         }
  99         if (deserialize) {
 100             // Deserialize the subclass
 101             try {
 102                 FileInputStream fi = new FileInputStream(f);
 103                 ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi);
 104                 si.addRenamedClassName("install.SerialDriver",
 105                                        "test.SerialDriver");
 106                 si.addRenamedClassName("[Linstall.SerialDriver;",
 107                                        "[Ltest.SerialDriver");
 108                 obj = (SerialDriver) si.readObject();
 109                 array = (SerialDriver[]) si.readObject();
 110                 si.close();
 111             } catch (Exception e) {
 112                 System.out.println(e);
 113                 throw e;
 114             }
 115             System.out.println();
 116             System.out.println("Printing deserialized class: ");
 117             System.out.println();
 118             System.out.println(obj.toString());
 119             System.out.println();
 120         }
 121     }
 122 
 123 
 124     public String toString() {
 125         String nextString = next != null ? next.toString() : "<null>";
 126         return "name =" + name + " next = <" + nextString + ">";
 127     }
 128 
 129     /**
 130      * Prints out the usage
 131      */
 132     static void usage() {
 133         System.out.println("Usage:");
 134         System.out.println("      -s (in order to serialize)");
 135         System.out.println("      -d (in order to deserialize)");
 136     }
 137 }