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