5134N/A/*
5134N/A * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
5134N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5134N/A *
5134N/A * This code is free software; you can redistribute it and/or modify it
5134N/A * under the terms of the GNU General Public License version 2 only, as
5134N/A * published by the Free Software Foundation. Oracle designates this
5134N/A * particular file as subject to the "Classpath" exception as provided
5134N/A * by Oracle in the LICENSE file that accompanied this code.
5134N/A *
5134N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5134N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5134N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5134N/A * version 2 for more details (a copy is included in the LICENSE file that
5134N/A * accompanied this code).
5134N/A *
5134N/A * You should have received a copy of the GNU General Public License version
5134N/A * 2 along with this work; if not, write to the Free Software Foundation,
5134N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5134N/A *
5134N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5134N/A * or visit www.oracle.com if you need additional information or have any
5134N/A * questions.
5134N/A */
2989N/A
5134N/Apackage java.io;
2989N/A
5134N/A/**
5134N/A * Context during upcalls from object stream to class-defined
5134N/A * readObject/writeObject methods.
5134N/A * Holds object currently being deserialized and descriptor for current class.
5134N/A *
5134N/A * This context keeps track of the thread it was constructed on, and allows
5134N/A * only a single call of defaultReadObject, readFields, defaultWriteObject
5134N/A * or writeFields which must be invoked on the same thread before the class's
5134N/A * readObject/writeObject method has returned.
5134N/A * If not set to the current thread, the getObj method throws NotActiveException.
5134N/A */
5134N/Afinal class SerialCallbackContext {
5134N/A private final Object obj;
5134N/A private final ObjectStreamClass desc;
5134N/A /**
5134N/A * Thread this context is in use by.
5134N/A * As this only works in one thread, we do not need to worry about thread-safety.
5134N/A */
5134N/A private Thread thread;
2989N/A
5134N/A public SerialCallbackContext(Object obj, ObjectStreamClass desc) {
5134N/A this.obj = obj;
5134N/A this.desc = desc;
5134N/A this.thread = Thread.currentThread();
5134N/A }
5134N/A
5134N/A public Object getObj() throws NotActiveException {
5134N/A checkAndSetUsed();
5134N/A return obj;
5134N/A }
2989N/A
5134N/A public ObjectStreamClass getDesc() {
5134N/A return desc;
5134N/A }
2989N/A
5134N/A private void checkAndSetUsed() throws NotActiveException {
5134N/A if (thread != Thread.currentThread()) {
5134N/A throw new NotActiveException(
5134N/A "not in readObject invocation or fields already read");
5134N/A }
5134N/A thread = null;
5134N/A }
5134N/A
5134N/A public void setUsed() {
5134N/A thread = null;
5134N/A }
5134N/A}