-
Notifications
You must be signed in to change notification settings - Fork 14
/
EntityLifecycleListenerManager.java
274 lines (240 loc) · 10.1 KB
/
EntityLifecycleListenerManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Copyright (C) 2019 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.model.metamodel;
import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
import cz.cvut.kbss.jopa.model.annotations.EntityListeners;
import cz.cvut.kbss.jopa.model.lifecycle.LifecycleEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
/**
* Manages entity lifecycle callbacks declared either in the entity (entity lifecycle callbacks) or in its entity
* listener (entity listener callbacks) and provides means for their invocation.
*/
public class EntityLifecycleListenerManager {
private static final EntityLifecycleListenerManager EMPTY = new EntityLifecycleListenerManager();
private EntityLifecycleListenerManager parent;
private final Map<LifecycleEvent, Method> lifecycleCallbacks = new EnumMap<>(LifecycleEvent.class);
private List<Object> entityListeners;
private Map<Object, Map<LifecycleEvent, Method>> entityListenerCallbacks;
private Map<Object, Object> instancesBeingProcessed = new IdentityHashMap<>();
/**
* Gets default instance of this manager, which contains no listeners and does nothing on invocation.
*
* @return Default {@link EntityLifecycleListenerManager} instance
*/
public static EntityLifecycleListenerManager empty() {
return EMPTY;
}
/**
* Calls pre-persist callbacks for the specified instance.
* <p>
* These include:
* <ul>
* <li>Lifecycle callbacks declared by the entity or its managed ancestors,</li>
* <li>Callbacks declared in classes referenced by {@link EntityListeners} on the entity or its ancestors.</li>
* </ul>
*
* @param instance The instance for persist
*/
public void invokePrePersistCallbacks(Object instance) {
invokeCallbacks(instance, LifecycleEvent.PRE_PERSIST);
}
private void invokeCallbacks(Object instance, LifecycleEvent lifecycleEvent) {
// The manager may be invoked from multiple threads (as each entity type has its listener and entity types are global for PU)
synchronized (this) {
if (instancesBeingProcessed.containsKey(instance)) {
return;
}
instancesBeingProcessed.put(instance, EMPTY);
}
try {
invokeEntityListenerCallbacks(instance, lifecycleEvent);
invokeInternalCallbacks(instance, lifecycleEvent);
} finally {
synchronized (this) {
instancesBeingProcessed.remove(instance);
}
}
}
private void invokeEntityListenerCallbacks(Object instance, LifecycleEvent lifecycleEvent) {
if (parent != null) {
parent.invokeEntityListenerCallbacks(instance, lifecycleEvent);
}
if (entityListeners != null) {
entityListeners
.forEach(listener -> getEntityListenerCallback(listener, lifecycleEvent).ifPresent(method -> {
if (!method.isAccessible()) {
method.setAccessible(true);
}
try {
method.invoke(listener, instance);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new OWLPersistenceException("Unable to invoke entity listener method " + method, e);
}
}));
}
}
private Optional<Method> getEntityListenerCallback(Object listener, LifecycleEvent lifecycleEvent) {
final Map<LifecycleEvent, Method> callbacks = entityListenerCallbacks.get(listener);
return Optional.ofNullable(callbacks.get(lifecycleEvent));
}
private void invokeInternalCallbacks(Object instance, LifecycleEvent lifecycleEvent) {
if (parent != null) {
parent.invokeInternalCallbacks(instance, lifecycleEvent);
}
if (lifecycleCallbacks.containsKey(lifecycleEvent)) {
final Method listener = lifecycleCallbacks.get(lifecycleEvent);
if (!listener.isAccessible()) {
listener.setAccessible(true);
}
try {
listener.invoke(instance);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new OWLPersistenceException("Unable to invoke method lifecycle listener " + listener, e);
}
}
}
/**
* Calls post-persist callbacks for the specified instance.
* <p>
* These include:
* <ul>
* <li>Lifecycle callbacks declared by the entity or its managed ancestors,</li>
* <li>Callbacks declared in classes referenced by {@link EntityListeners} on the entity or its ancestors.</li>
* </ul>
*
* @param instance The newly persisted instance
*/
public void invokePostPersistCallbacks(Object instance) {
invokeCallbacks(instance, LifecycleEvent.POST_PERSIST);
}
/**
* Calls post-load callbacks for the specified instance.
* <p>
* These include:
* <ul>
* <li>Lifecycle callbacks declared by the entity or its managed ancestors,</li>
* <li>Callbacks declared in classes referenced by {@link EntityListeners} on the entity or its ancestors.</li>
* </ul>
*
* @param instance The loaded instance
*/
public void invokePostLoadCallbacks(Object instance) {
invokeCallbacks(instance, LifecycleEvent.POST_LOAD);
}
/**
* Calls pre-update callbacks for the specified instance.
* <p>
* These include:
* <ul>
* <li>Lifecycle callbacks declared by the entity or its managed ancestors,</li>
* <li>Callbacks declared in classes referenced by {@link EntityListeners} on the entity or its ancestors.</li>
* </ul>
*
* @param instance The updated instance
*/
public void invokePreUpdateCallbacks(Object instance) {
invokeCallbacks(instance, LifecycleEvent.PRE_UPDATE);
}
/**
* Calls post-update callbacks for the specified instance.
* <p>
* These include:
* <ul>
* <li>Lifecycle callbacks declared by the entity or its managed ancestors,</li>
* <li>Callbacks declared in classes referenced by {@link EntityListeners} on the entity or its ancestors.</li>
* </ul>
*
* @param instance The updated instance
*/
public void invokePostUpdateCallbacks(Object instance) {
invokeCallbacks(instance, LifecycleEvent.POST_UPDATE);
}
/**
* Calls pre-remove callbacks for the specified instance.
* <p>
* These include:
* <ul>
* <li>Lifecycle callbacks declared by the entity or its managed ancestors,</li>
* <li>Callbacks declared in classes referenced by {@link EntityListeners} on the entity or its ancestors.</li>
* </ul>
*
* @param instance The instance for removal
*/
public void invokePreRemoveCallbacks(Object instance) {
invokeCallbacks(instance, LifecycleEvent.PRE_REMOVE);
}
/**
* Calls post-remove callbacks for the specified instance.
* <p>
* These include:
* <ul>
* <li>Lifecycle callbacks declared by the entity or its managed ancestors,</li>
* <li>Callbacks declared in classes referenced by {@link EntityListeners} on the entity or its ancestors.</li>
* </ul>
*
* @param instance The removed instance
*/
public void invokePostRemoveCallbacks(Object instance) {
invokeCallbacks(instance, LifecycleEvent.POST_REMOVE);
}
void setParent(EntityLifecycleListenerManager parent) {
this.parent = parent;
}
EntityLifecycleListenerManager getParent() {
return parent;
}
void addEntityListener(Object entityListener) {
assert entityListener != null;
if (entityListeners == null) {
this.entityListeners = new ArrayList<>();
}
entityListeners.add(entityListener);
if (entityListenerCallbacks == null) {
this.entityListenerCallbacks = new HashMap<>();
}
entityListenerCallbacks.put(entityListener, new EnumMap<>(LifecycleEvent.class));
}
void addLifecycleCallback(LifecycleEvent event, Method callback) {
assert event != null;
assert callback != null;
lifecycleCallbacks.put(event, callback);
}
Map<LifecycleEvent, Method> getLifecycleCallbacks() {
return Collections.unmodifiableMap(lifecycleCallbacks);
}
boolean hasLifecycleCallback(LifecycleEvent event) {
return lifecycleCallbacks.containsKey(event);
}
List<Object> getEntityListeners() {
return entityListeners != null ? Collections.unmodifiableList(entityListeners) : Collections.emptyList();
}
Map<Object, Map<LifecycleEvent, Method>> getEntityListenerCallbacks() {
return entityListenerCallbacks != null ? Collections.unmodifiableMap(entityListenerCallbacks) :
Collections.emptyMap();
}
void addEntityListenerCallback(Object listener, LifecycleEvent event, Method callback) {
assert listener != null;
assert event != null;
assert callback != null;
assert entityListenerCallbacks.containsKey(listener);
entityListenerCallbacks.get(listener).put(event, callback);
}
boolean hasEntityListenerCallback(Object listener, LifecycleEvent event) {
return entityListenerCallbacks != null && entityListenerCallbacks.containsKey(listener) &&
entityListenerCallbacks.get(listener).containsKey(event);
}
}