SerialCallbackContext.java revision 2989
2989N/A /*
2989N/A * %W% %E%
2989N/A *
2989N/A * Copyright (c) 2006, 2010 Oracle and/or its affiliates. All rights reserved.
2989N/A * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
2989N/A */
2989N/A
2989N/A package java.io;
2989N/A
2989N/A /**
2989N/A * Context during upcalls from object stream to class-defined
2989N/A * readObject/writeObject methods.
2989N/A * Holds object currently being deserialized and descriptor for current class.
2989N/A *
2989N/A * This context keeps track of the thread it was constructed on, and allows
2989N/A * only a single call of defaultReadObject, readFields, defaultWriteObject
2989N/A * or writeFields which must be invoked on the same thread before the class's
2989N/A * readObject/writeObject method has returned.
2989N/A * If not set to the current thread, the getObj method throws NotActiveException.
2989N/A */
2989N/A final class SerialCallbackContext {
2989N/A private final Object obj;
2989N/A private final ObjectStreamClass desc;
2989N/A /**
2989N/A * Thread this context is in use by.
2989N/A * As this only works in one thread, we do not need to worry about thread-safety.
2989N/A */
2989N/A private Thread thread;
2989N/A
2989N/A public SerialCallbackContext(Object obj, ObjectStreamClass desc) {
2989N/A this.obj = obj;
2989N/A this.desc = desc;
2989N/A this.thread = Thread.currentThread();
2989N/A }
2989N/A
2989N/A public Object getObj() throws NotActiveException {
2989N/A checkAndSetUsed();
2989N/A return obj;
2989N/A }
2989N/A
2989N/A public ObjectStreamClass getDesc() {
2989N/A return desc;
2989N/A }
2989N/A
2989N/A private void checkAndSetUsed() throws NotActiveException {
2989N/A if (thread != Thread.currentThread()) {
2989N/A throw new NotActiveException(
2989N/A "not in readObject invocation or fields already read");
2989N/A }
2989N/A thread = null;
2989N/A }
2989N/A
2989N/A public void setUsed() {
2989N/A thread = null;
2989N/A }
2989N/A }
2989N/A
2989N/A