/**
   * Deserializes examplars stored in archives in getArchiveDirectory().
   *
   * @throws RuntimeException if clazz cannot be serialized. This exception has an informative
   *     message and wraps the originally thrown exception as root cause.
   * @see #getArchiveDirectory()
   */
  public void deserializeArchivedVersions() throws RuntimeException {
    System.out.println("Deserializing archived instances in " + getArchiveDirectory() + ".");

    File archive = new File(getArchiveDirectory());

    if (!archive.exists() || !archive.isDirectory()) {
      return;
    }

    String[] listing = archive.list();

    for (String archiveName : listing) {
      if (!(archiveName.endsWith(".zip"))) {
        continue;
      }

      try {
        File file = new File(getArchiveDirectory(), archiveName);
        ZipFile zipFile = new ZipFile(file);
        ZipEntry entry = zipFile.getEntry("class_fields.ser");
        InputStream inputStream = zipFile.getInputStream(entry);
        ObjectInputStream objectIn = new ObjectInputStream(inputStream);
        Map<String, List<String>> classFields = (Map<String, List<String>>) objectIn.readObject();
        zipFile.close();

        for (String className : classFields.keySet()) {

          //                    if (classFields.equals("HypotheticalGraph")) continue;

          List<String> fieldNames = classFields.get(className);
          Class<?> clazz = Class.forName(className);
          ObjectStreamClass streamClass = ObjectStreamClass.lookup(clazz);

          if (streamClass == null) {
            System.out.println();
          }

          for (String fieldName : fieldNames) {
            assert streamClass != null;
            ObjectStreamField field = streamClass.getField(fieldName);

            if (field == null) {
              throw new RuntimeException(
                  "Field '"
                      + fieldName
                      + "' was dropped from class '"
                      + className
                      + "' as a serializable field! Please "
                      + "put it back!!!"
                      + "\nIt used to be in "
                      + className
                      + " in this archive: "
                      + archiveName
                      + ".");
            }
          }
        }
      } catch (ClassNotFoundException e) {
        throw new RuntimeException(
            "Could not read class_fields.ser in archive + " + archiveName + " .", e);
      } catch (IOException e) {
        throw new RuntimeException("Problem reading archive" + archiveName + "; see cause.", e);
      }

      System.out.println("...Deserializing instances in " + archiveName + "...");
      ZipEntry zipEntry = null;

      try {
        File file = new File(getArchiveDirectory(), archiveName);
        FileInputStream in = new FileInputStream(file);
        ZipInputStream zipinputstream = new ZipInputStream(in);

        while ((zipEntry = zipinputstream.getNextEntry()) != null) {
          if (!zipEntry.getName().endsWith(".ser")) {
            continue;
          }

          ObjectInputStream objectIn = new ObjectInputStream(zipinputstream);
          objectIn.readObject();
          zipinputstream.closeEntry();
        }

        zipinputstream.close();
      } catch (ClassNotFoundException e) {
        throw new RuntimeException(
            "Could not read object zipped file "
                + zipEntry.getName()
                + " in archive "
                + archiveName
                + ". "
                + "Perhaps the class was renamed, moved to another package, or "
                + "removed. In any case, please put it back where it was.",
            e);
      } catch (IOException e) {
        throw new RuntimeException("Problem reading archive" + archiveName + "; see cause.", e);
      }
    }

    System.out.println("Finished deserializing archived instances.");
  }
Ejemplo n.º 2
0
 /** java.io.ObjectStreamClass#getField(java.lang.String) */
 public void test_getFieldLjava_lang_String() {
   ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class);
   assertEquals("getField did not return correct field", 'J', osc.getField("bam").getTypeCode());
   assertNull("getField did not null for non-existent field", osc.getField("wham"));
 }