jdk/src/share/classes/java/io/SerialCallbackContext.java
author skoppar
Tue, 28 Sep 2010 01:09:10 -0700
changeset 7030 90cf66131063
child 8158 77d9c0f1c19f
permissions -rw-r--r--
6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization Reviewed-by: hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
     1
  /*
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
     2
   * %W% %E%
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
     3
   *
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
     4
   * Copyright (c) 2006, 2010  Oracle and/or its affiliates. All rights reserved.
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
     5
   * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
     6
   */
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
     7
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
     8
  package java.io;
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
     9
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    10
  /**
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    11
   * Context during upcalls from object stream to class-defined
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    12
   * readObject/writeObject methods.
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    13
   * Holds object currently being deserialized and descriptor for current class.
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    14
   *
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    15
   * This context keeps track of the thread it was constructed on, and allows
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    16
   * only a single call of defaultReadObject, readFields, defaultWriteObject
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    17
   * or writeFields which must be invoked on the same thread before the class's
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    18
   * readObject/writeObject method has returned.
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    19
   * If not set to the current thread, the getObj method throws NotActiveException.
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    20
   */
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    21
  final class SerialCallbackContext {
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    22
      private final Object obj;
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    23
      private final ObjectStreamClass desc;
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    24
      /**
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    25
       * Thread this context is in use by.
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    26
       * As this only works in one thread, we do not need to worry about thread-safety.
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    27
       */
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    28
      private Thread thread;
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    29
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    30
      public SerialCallbackContext(Object obj, ObjectStreamClass desc) {
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    31
          this.obj = obj;
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    32
          this.desc = desc;
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    33
          this.thread = Thread.currentThread();
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    34
      }
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    35
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    36
      public Object getObj() throws NotActiveException {
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    37
          checkAndSetUsed();
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    38
          return obj;
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    39
      }
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    40
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    41
      public ObjectStreamClass getDesc() {
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    42
          return desc;
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    43
      }
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    44
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    45
      private void checkAndSetUsed() throws NotActiveException {
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    46
          if (thread != Thread.currentThread()) {
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    47
               throw new NotActiveException(
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    48
                "not in readObject invocation or fields already read");
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    49
          }
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    50
          thread = null;
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    51
      }
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    52
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    53
      public void setUsed() {
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    54
          thread = null;
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    55
      }
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    56
  }
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    57
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents:
diff changeset
    58