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  * Serializable 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 (FileOutputStream fo = new FileOutputStream(f);
  87                  ObjectOutputStream so = new ObjectOutputStream(fo))
  88             {
  89                 so.writeObject(obj);
  90                 /* Comment out since renaming arrays does not work
  91                    since it changes the serialVersionUID. */
  92                 so.writeObject(array);
  93             } catch (Exception e) {
  94                 System.out.println(e);
  95                 throw e;
  96             }
  97         }
  98         if (deserialize) {
  99             // Deserialize the subclass
 100             try (FileInputStream fi = new FileInputStream(f);
 101                  ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))
 102             {
 103                 si.addRenamedClassName("install.SerialDriver",
 104                                        "test.SerialDriver");
 105                 si.addRenamedClassName("[Linstall.SerialDriver;",
 106                                        "[Ltest.SerialDriver");
 107                 obj = (SerialDriver) si.readObject();
 108                 array = (SerialDriver[]) si.readObject();
 109             } catch (Exception e) {
 110                 System.out.println(e);
 111                 throw e;
 112             }
 113             System.out.println();
 114             System.out.println("Printing deserialized class: ");
 115             System.out.println();
 116             System.out.println(obj.toString());
 117             System.out.println();
 118         }
 119     }
 120 
 121 
 122     public String toString() {
 123         String nextString = next != null ? next.toString() : "<null>";
 124         return "name =" + name + " next = <" + nextString + ">";
 125     }
 126 
 127     /**
 128      * Prints out the usage
 129      */
 130     static void usage() {
 131         System.out.println("Usage:");
 132         System.out.println("      -s (in order to serialize)");
 133         System.out.println("      -d (in order to deserialize)");
 134     }
 135 }