1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package javax.jdo.listener;
23
24 import javax.jdo.spi.I18NHelper;
25
26 /***
27 * This is the event class used in life cycle event notifications.
28 * <P>Note that although InstanceLifecycleEvent inherits Serializable interface
29 * from EventObject, it is not intended to be Serializable. Appropriate
30 * serialization methods are implemented to throw NotSerializableException.
31 * @version 2.0
32 * @since 2.0
33 */
34 public class InstanceLifecycleEvent
35 extends java.util.EventObject {
36
37 private static final int FIRST_EVENT_TYPE = 0;
38 public static final int CREATE = 0;
39 public static final int LOAD = 1;
40 public static final int STORE = 2;
41 public static final int CLEAR = 3;
42 public static final int DELETE = 4;
43 public static final int DIRTY = 5;
44 public static final int DETACH = 6;
45 public static final int ATTACH = 7;
46 private static final int LAST_EVENT_TYPE = 7;
47
48 /*** The Internationalization message helper.
49 */
50 private final static I18NHelper msg = I18NHelper.getInstance ("javax.jdo.Bundle");
51
52 /***
53 * The event type that triggered the construction of this event object.
54 */
55 private final int eventType;
56
57 /***
58 * The "other" object associated with the event.
59 */
60 private final Object target;
61
62 /***
63 * Creates a new event object with the specified
64 * <code>source</code> and <code>type</code>.
65 * @param source the instance that triggered the event
66 * @param type the event type
67 * @since 2.0
68 */
69 public InstanceLifecycleEvent (Object source, int type) {
70 this(source, type, null);
71 }
72
73 /***
74 * Creates a new event object with the specified
75 * <code>source</code>, <code>type</code>, and <code>target</code>.
76 * @param source the instance that triggered the event
77 * @param type the event type
78 * @param target the "other" instance
79 * @since 2.0
80 */
81 public InstanceLifecycleEvent (Object source, int type, Object target) {
82 super (source);
83 if (type < FIRST_EVENT_TYPE || type > LAST_EVENT_TYPE) {
84 throw new IllegalArgumentException(msg.msg("EXC_IllegalEventType"));
85 }
86 eventType = type;
87 this.target = target;
88 }
89
90 /***
91 * Returns the event type that triggered this event.
92 * @return the event type
93 * @since 2.0
94 */
95 public int getEventType () {
96 return eventType;
97 }
98
99 /***
100 * The source object of the Event. Although not deprecated,
101 * it is recommended that the the methods
102 * <code>getPersistentInstance()</code> and
103 * <code>getDetachedInstance()</code> be used instead.
104 *
105 * @return The persistent instance on any pre- callback except preAttach,
106 * or the detached instance for a postDetach or preAttach callback.
107 *
108 * @see #getPersistentInstance()
109 * @see #getDetachedInstance()
110 * @see "Section 12.15, Java Data Objects 2.0 Specification"
111 */
112 public Object getSource() {
113 return super.getSource();
114 }
115
116 /***
117 * The target object of the Event. Although not deprecated,
118 * it is recommended that the the methods
119 * <code>getPersistentInstance()</code> and
120 * <code>getDetachedInstance()</code> be used instead.
121 *
122 * @return The detached instance for preDetach and postAttach, the persistent instance otherwise.
123 *
124 * @since 2.0
125 * @see #getPersistentInstance()
126 * @see #getDetachedInstance()
127 * @see "Section 12.15, Java Data Objects 2.0 Specification"
128 */
129 public Object getTarget () {
130 return target;
131 }
132
133 /***
134 * Returns the persistent instance involved in the event.
135 *
136 * @return The persistent instance involved in the event, or null if there was none.
137 *
138 * @see "Section 12.15, Java Data Objects 2.0 Specification"
139 */
140 public Object getPersistentInstance() {
141 switch (getEventType()) {
142 case DETACH:
143 return target == null
144 ? getSource()
145 : getTarget();
146 case ATTACH:
147 return target == null
148 ? null
149 : getSource();
150 }
151
152
153 return getSource();
154 }
155
156 /***
157 * Returns the detached instance involved in the event.
158 *
159 * @return The detached instance involved in the event, or null if there was none.
160 *
161 * @see "Section 12.15, Java Data Objects 2.0 Specification"
162 */
163 public Object getDetachedInstance() {
164 switch (getEventType()) {
165 case DETACH:
166 return target == null
167 ? null
168 : getSource();
169 case ATTACH:
170 return target == null
171 ? getSource()
172 : getTarget();
173 }
174
175
176 return null;
177 }
178
179 /***
180 * Serialization is not supported for InstanceLifecycleEvents.
181 * param out the output stream
182 * @since 2.0
183 */
184 private void writeObject(java.io.ObjectOutputStream out)
185 throws java.io.IOException {
186 throw new java.io.NotSerializableException();
187 }
188 }