1 | /** |
---|
2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU Affero General Public License as |
---|
7 | * published by the Free Software Foundation, either version 3 of the |
---|
8 | * License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU Affero General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Affero General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | |
---|
19 | /******************************************************************************\ |
---|
20 | * |
---|
21 | * This product was developed by |
---|
22 | * |
---|
23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
24 | * |
---|
25 | * a government company established under Brazilian law (5.615/70), |
---|
26 | * at Department of Development of Porto Alegre. |
---|
27 | * |
---|
28 | \******************************************************************************/ |
---|
29 | |
---|
30 | package serpro.mailarchiver.util.jdo; |
---|
31 | |
---|
32 | import javax.jdo.JDOHelper; |
---|
33 | import javax.jdo.ObjectState; |
---|
34 | import javax.jdo.listener.AttachLifecycleListener; |
---|
35 | import javax.jdo.listener.ClearLifecycleListener; |
---|
36 | import javax.jdo.listener.CreateLifecycleListener; |
---|
37 | import javax.jdo.listener.DeleteLifecycleListener; |
---|
38 | import javax.jdo.listener.DetachLifecycleListener; |
---|
39 | import javax.jdo.listener.DirtyLifecycleListener; |
---|
40 | import javax.jdo.listener.InstanceLifecycleEvent; |
---|
41 | import javax.jdo.listener.LoadLifecycleListener; |
---|
42 | import javax.jdo.listener.StoreLifecycleListener; |
---|
43 | |
---|
44 | import com.google.common.base.Supplier; |
---|
45 | |
---|
46 | import serpro.mailarchiver.domain.BaseObject; |
---|
47 | import serpro.mailarchiver.util.Logger; |
---|
48 | |
---|
49 | public class InstanceLifecycleListener |
---|
50 | implements |
---|
51 | AttachLifecycleListener, |
---|
52 | ClearLifecycleListener, |
---|
53 | CreateLifecycleListener, |
---|
54 | DeleteLifecycleListener, |
---|
55 | DetachLifecycleListener, |
---|
56 | DirtyLifecycleListener, |
---|
57 | LoadLifecycleListener, |
---|
58 | StoreLifecycleListener |
---|
59 | { |
---|
60 | private static final Logger log = Logger.getLocalLogger(); |
---|
61 | |
---|
62 | private void logLifecycleEvent(final String method, final InstanceLifecycleEvent ile, final Object o1, final Object o2) { |
---|
63 | |
---|
64 | log.debug("%s", new Supplier<Object[]>() { |
---|
65 | @Override |
---|
66 | public Object[] get() { |
---|
67 | StringBuilder sb = new StringBuilder(); |
---|
68 | |
---|
69 | sb.append(">>").append(method).append("<<\n\n"); |
---|
70 | |
---|
71 | Object persistentInstance; |
---|
72 | Object detachedInstance; |
---|
73 | |
---|
74 | if(ile != null) { |
---|
75 | |
---|
76 | persistentInstance = ile.getPersistentInstance(); |
---|
77 | detachedInstance = ile.getDetachedInstance(); |
---|
78 | |
---|
79 | sb.append("eventType: "); |
---|
80 | |
---|
81 | switch(ile.getEventType()) { |
---|
82 | case InstanceLifecycleEvent.CREATE: |
---|
83 | sb.append("CREATE"); |
---|
84 | break; |
---|
85 | |
---|
86 | case InstanceLifecycleEvent.LOAD: |
---|
87 | sb.append("LOAD"); |
---|
88 | break; |
---|
89 | |
---|
90 | case InstanceLifecycleEvent.STORE: |
---|
91 | sb.append("STORE"); |
---|
92 | break; |
---|
93 | |
---|
94 | case InstanceLifecycleEvent.CLEAR: |
---|
95 | sb.append("CLEAR"); |
---|
96 | break; |
---|
97 | |
---|
98 | case InstanceLifecycleEvent.DELETE: |
---|
99 | sb.append("DELETE"); |
---|
100 | break; |
---|
101 | |
---|
102 | case InstanceLifecycleEvent.DIRTY: |
---|
103 | sb.append("DIRTY"); |
---|
104 | break; |
---|
105 | |
---|
106 | case InstanceLifecycleEvent.DETACH: |
---|
107 | sb.append("DETACH"); |
---|
108 | break; |
---|
109 | |
---|
110 | case InstanceLifecycleEvent.ATTACH: |
---|
111 | sb.append("ATTACH"); |
---|
112 | break; |
---|
113 | } |
---|
114 | } |
---|
115 | else { |
---|
116 | persistentInstance = o1; |
---|
117 | detachedInstance = o2; |
---|
118 | } |
---|
119 | |
---|
120 | ObjectState objectState = JDOHelper.getObjectState(persistentInstance); |
---|
121 | sb.append("\nobjectState: ").append(objectState.toString()); |
---|
122 | |
---|
123 | boolean isDeleted = JDOHelper.isDeleted(persistentInstance); |
---|
124 | sb.append("\nisDeleted: ").append(isDeleted); |
---|
125 | |
---|
126 | boolean isDetached = JDOHelper.isDetached(persistentInstance); |
---|
127 | sb.append("\nisDetached: ").append(isDetached); |
---|
128 | |
---|
129 | boolean isDirty = JDOHelper.isDirty(persistentInstance); |
---|
130 | sb.append("\nisDirty: ").append(isDirty); |
---|
131 | |
---|
132 | boolean isNew = JDOHelper.isNew(persistentInstance); |
---|
133 | sb.append("\nisNew: ").append(isNew); |
---|
134 | |
---|
135 | boolean isPersistent = JDOHelper.isPersistent(persistentInstance); |
---|
136 | sb.append("\nisPersistent: ").append(isPersistent); |
---|
137 | |
---|
138 | boolean isTransactional = JDOHelper.isTransactional(persistentInstance); |
---|
139 | sb.append("\nisTransactional: ").append(isTransactional); |
---|
140 | |
---|
141 | if(!isDeleted) { |
---|
142 | sb.append("\n\npersistentInstance:\n").append(persistentInstance.toString()); |
---|
143 | } |
---|
144 | |
---|
145 | if(detachedInstance != null) { |
---|
146 | sb.append("\n\ndetachedInstance:\n").append(detachedInstance.toString()); |
---|
147 | } |
---|
148 | |
---|
149 | return new Object[] { sb.toString() }; |
---|
150 | } |
---|
151 | }); |
---|
152 | } |
---|
153 | |
---|
154 | // |
---|
155 | // AttachLifecycleListener |
---|
156 | // |
---|
157 | |
---|
158 | @Override |
---|
159 | public void preAttach(InstanceLifecycleEvent ile) { |
---|
160 | logLifecycleEvent("preAttach", ile, null, null); |
---|
161 | } |
---|
162 | |
---|
163 | @Override |
---|
164 | public void postAttach(InstanceLifecycleEvent ile) { |
---|
165 | logLifecycleEvent("postAttach", ile, null, null); |
---|
166 | } |
---|
167 | |
---|
168 | // |
---|
169 | // ClearLifecycleListener |
---|
170 | // |
---|
171 | |
---|
172 | @Override |
---|
173 | public void preClear(InstanceLifecycleEvent ile) { |
---|
174 | logLifecycleEvent("preClear", ile, null, null); |
---|
175 | } |
---|
176 | |
---|
177 | @Override |
---|
178 | public void postClear(InstanceLifecycleEvent ile) { |
---|
179 | logLifecycleEvent("postClear", ile, null, null); |
---|
180 | } |
---|
181 | |
---|
182 | // |
---|
183 | // CreateLifecycleListener |
---|
184 | // |
---|
185 | |
---|
186 | @Override |
---|
187 | public void postCreate(InstanceLifecycleEvent ile) { |
---|
188 | logLifecycleEvent("postCreate", ile, null, null); |
---|
189 | } |
---|
190 | |
---|
191 | // |
---|
192 | // DeleteLifecycleListener |
---|
193 | // |
---|
194 | |
---|
195 | @Override |
---|
196 | public void preDelete(InstanceLifecycleEvent ile) { |
---|
197 | logLifecycleEvent("preDelete", ile, null, null); |
---|
198 | } |
---|
199 | |
---|
200 | @Override |
---|
201 | public void postDelete(InstanceLifecycleEvent ile) { |
---|
202 | logLifecycleEvent("postDelete", ile, null, null); |
---|
203 | } |
---|
204 | |
---|
205 | // |
---|
206 | // DetachLifecycleListener |
---|
207 | // |
---|
208 | |
---|
209 | @Override |
---|
210 | public void preDetach(InstanceLifecycleEvent ile) { |
---|
211 | logLifecycleEvent("preDetach", ile, null, null); |
---|
212 | } |
---|
213 | |
---|
214 | @Override |
---|
215 | public void postDetach(InstanceLifecycleEvent ile) { |
---|
216 | logLifecycleEvent("postDetach", ile, null, null); |
---|
217 | } |
---|
218 | |
---|
219 | // |
---|
220 | // DirtyLifecycleListener |
---|
221 | // |
---|
222 | |
---|
223 | @Override |
---|
224 | public void preDirty(InstanceLifecycleEvent ile) { |
---|
225 | logLifecycleEvent("preDirty", ile, null, null); |
---|
226 | } |
---|
227 | |
---|
228 | @Override |
---|
229 | public void postDirty(InstanceLifecycleEvent ile) { |
---|
230 | logLifecycleEvent("postDirty", ile, null, null); |
---|
231 | } |
---|
232 | |
---|
233 | // |
---|
234 | // LoadLifecycleListener |
---|
235 | // |
---|
236 | |
---|
237 | @Override |
---|
238 | public void postLoad(InstanceLifecycleEvent ile) { |
---|
239 | logLifecycleEvent("postLoad", ile, null, null); |
---|
240 | } |
---|
241 | |
---|
242 | // |
---|
243 | // StoreLifecycleListener |
---|
244 | // |
---|
245 | |
---|
246 | @Override |
---|
247 | public void preStore(InstanceLifecycleEvent ile) { |
---|
248 | logLifecycleEvent("preStore", ile, null, null); |
---|
249 | } |
---|
250 | |
---|
251 | @Override |
---|
252 | public void postStore(InstanceLifecycleEvent ile) { |
---|
253 | logLifecycleEvent("postStore", ile, null, null); |
---|
254 | } |
---|
255 | |
---|
256 | // |
---|
257 | // InstanceCallbacks |
---|
258 | // |
---|
259 | |
---|
260 | public void jdoPreClear(BaseObject o1) { |
---|
261 | logLifecycleEvent("jdoPreClear", null, o1, null); |
---|
262 | } |
---|
263 | |
---|
264 | public void jdoPreDelete(BaseObject o1) { |
---|
265 | logLifecycleEvent("jdoPreDelete", null, o1, null); |
---|
266 | } |
---|
267 | |
---|
268 | public void jdoPostLoad(BaseObject o1) { |
---|
269 | logLifecycleEvent("jdoPostLoad", null, o1, null); |
---|
270 | } |
---|
271 | |
---|
272 | public void jdoPreStore(BaseObject o1) { |
---|
273 | logLifecycleEvent("jdoPreStore", null, o1, null); |
---|
274 | } |
---|
275 | |
---|
276 | // |
---|
277 | // AttachCallbacks |
---|
278 | // |
---|
279 | |
---|
280 | public void jdoPreAttach(BaseObject o1) { |
---|
281 | logLifecycleEvent("jdoPreAttach", null, o1, null); |
---|
282 | } |
---|
283 | |
---|
284 | public void jdoPostAttach(BaseObject o1, Object o2) { |
---|
285 | logLifecycleEvent("jdoPostAttach", null, o1, o2); |
---|
286 | } |
---|
287 | |
---|
288 | // |
---|
289 | // DetachCallbacks |
---|
290 | // |
---|
291 | |
---|
292 | public void jdoPreDetach(BaseObject o1) { |
---|
293 | logLifecycleEvent("jdoPreDetach", null, o1, null); |
---|
294 | } |
---|
295 | |
---|
296 | public void jdoPostDetach(BaseObject o1, Object o2) { |
---|
297 | logLifecycleEvent("jdoPostDetach", null, o1, o2); |
---|
298 | } |
---|
299 | } |
---|