Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dd501f52df2da29202cbc4117e8121f97224b034 (plain) (blame)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
 * <copyright>
 *
 * Copyright (c) 2012 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal
 * </copyright>
 *
 * $Id: AnyEObjectType.java,v 1.7 2010/11/12 09:33:33 mtaal Exp $
 */
package org.eclipse.emf.teneo.hibernate.auditing;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.extension.ExtensionPoint;
import org.eclipse.emf.teneo.hibernate.HbDataStore;
import org.eclipse.emf.teneo.hibernate.HbUtil;
import org.eclipse.emf.teneo.hibernate.auditing.model.teneoauditing.TeneoAuditCommitInfo;
import org.eclipse.emf.teneo.hibernate.auditing.model.teneoauditing.TeneoAuditEntry;
import org.eclipse.emf.teneo.hibernate.auditing.model.teneoauditing.TeneoAuditKind;
import org.eclipse.emf.teneo.hibernate.auditing.model.teneoauditing.TeneoauditingFactory;
import org.eclipse.emf.teneo.hibernate.auditing.model.teneoauditing.TeneoauditingPackage;
import org.eclipse.emf.teneo.util.StoreUtil;
import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.action.spi.AfterTransactionCompletionProcess;
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.event.spi.EventSource;
import org.hibernate.event.spi.PostDeleteEvent;
import org.hibernate.event.spi.PostDeleteEventListener;
import org.hibernate.event.spi.PostInsertEvent;
import org.hibernate.event.spi.PostInsertEventListener;
import org.hibernate.event.spi.PostUpdateEvent;
import org.hibernate.event.spi.PostUpdateEventListener;

/**
 * The main auditing logic used at runtime to create and persist auditing entries.
 * 
 * It listens to hibernate events and before the commit, creates audit entries to persist.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 */
public class AuditProcessHandler implements AfterTransactionCompletionProcess,
		BeforeTransactionCompletionProcess, PostDeleteEventListener, PostInsertEventListener,
		PostUpdateEventListener, ExtensionPoint {

	public static final long DEFAULT_END_TIMESTAMP = -1;

	private static final long serialVersionUID = 1L;
	private HbDataStore dataStore;

	private Map<Transaction, List<AuditWork>> workQueue = new ConcurrentHashMap<Transaction, List<AuditWork>>();

	private int pruneCounter = 0;
	private long pruneTime = 0;
	private List<EClass> auditEClasses = null;

	private boolean isAudited(Object entity) {
		if (!(entity instanceof EObject)) {
			// TODO: support featuremap entries
			return false;
		}
		final EObject eObject = (EObject) entity;
		final EClass auditEClass = dataStore.getAuditHandler()
				.getAuditingModelElement(eObject.eClass());
		return auditEClass != null;
	}

	private void addToAuditWorkQueue(EventSource session, TeneoAuditKind auditKind, Object entity) {
		if (!isAudited(entity)) {
			return;
		}

		final AuditWork auditWork = new AuditWork();
		auditWork.setAuditKind(auditKind);
		auditWork.setEntity(entity);

		final List<AuditWork> auditWorks;
		if (workQueue.containsKey(session.getTransaction())) {
			auditWorks = workQueue.get(session.getTransaction());
		} else {
			auditWorks = new ArrayList<AuditProcessHandler.AuditWork>();
			workQueue.put(session.getTransaction(), auditWorks);

			// let the handler be called at the end of the transaction
			// to do the transaction work
			session.getActionQueue().registerProcess((AfterTransactionCompletionProcess) this);
			session.getActionQueue().registerProcess((BeforeTransactionCompletionProcess) this);
		}

		// remove existing one if the same entity
		AuditWork existingAuditWork = null;
		for (AuditWork checkAuditWork : auditWorks) {
			if (checkAuditWork.getEntity() == auditWork.getEntity()) {
				existingAuditWork = checkAuditWork;
				break;
			}
		}

		// check if there is already an add
		if (existingAuditWork != null
				&& (existingAuditWork.getAuditKind() == TeneoAuditKind.ADD && auditWork.getAuditKind() == TeneoAuditKind.UPDATE)) {
			// don't add the audit work then, keep the add
		} else if (existingAuditWork != null
				&& (existingAuditWork.getAuditKind() == TeneoAuditKind.ADD && auditWork.getAuditKind() == TeneoAuditKind.DELETE)) {
			// delete with a preceding add, don't do anything, get rid of it
			auditWorks.remove(existingAuditWork);
		} else if (existingAuditWork != null
				&& (existingAuditWork.getAuditKind() == TeneoAuditKind.UPDATE && auditWork.getAuditKind() == TeneoAuditKind.ADD)) {
			// happens if the id has been set manually
			auditWorks.remove(existingAuditWork);
		} else {
			auditWorks.remove(existingAuditWork);

			// and add the new one
			auditWorks.add(auditWork);
		}
	}

	@Override
	public void onPostUpdate(PostUpdateEvent event) {
		addToAuditWorkQueue(event.getSession(), TeneoAuditKind.UPDATE, event.getEntity());
	}

	@Override
	public void onPostInsert(PostInsertEvent event) {
		addToAuditWorkQueue(event.getSession(), TeneoAuditKind.ADD, event.getEntity());
	}

	@Override
	public void onPostDelete(PostDeleteEvent event) {
		addToAuditWorkQueue(event.getSession(), TeneoAuditKind.DELETE, event.getEntity());
	}

	@Override
	public void doBeforeTransactionCompletion(SessionImplementor session) {
		// see: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178431
		if (!FlushMode.isManualFlushMode(session.getFlushMode())) {
			session.flush();
			final List<AuditWork> auditWorks = getRemoveQueue((Session) session, false);
			if (auditWorks == null) {
				return;
			}
			doAuditWorkInSession((Session) session, auditWorks);
		}
	}

	@Override
	public void doAfterTransactionCompletion(boolean success, SessionImplementor session) {
		if (!success) {
			return;
		}
		final List<AuditWork> auditWorks = getRemoveQueue((Session) session, true);
		if (auditWorks == null) {
			return;
		}
		// see: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178431
		Session tmpSession = null;
		boolean err = true;
		try {
			tmpSession = session.getFactory().openSession();
			tmpSession.beginTransaction();
			doAuditWorkInSession(tmpSession, auditWorks);
			tmpSession.getTransaction().commit();
			err = false;
		} finally {
			try {
				if (tmpSession != null && err) {
					tmpSession.getTransaction().rollback();
				}
			} finally {
				if (tmpSession != null) {
					tmpSession.close();
				}
			}
		}
	}

	private void doAuditWorkInSession(Session session, List<AuditWork> auditWorks) {
		final long commitTime = System.currentTimeMillis();

		final List<Object> evictObjects = new ArrayList<Object>();

		final TeneoAuditCommitInfo commitInfo = TeneoauditingFactory.eINSTANCE
				.createTeneoAuditCommitInfo();
		session.save(commitInfo);
		evictObjects.add(commitInfo);

		EClass lastEClass = null;
		EClass auditEntryEClass = null;
		final List<TeneoAuditEntry> toSaveEntries = new ArrayList<TeneoAuditEntry>();
		for (AuditWork auditWork : auditWorks) {
			final EObject eObject = (EObject) auditWork.getEntity();
			if (lastEClass != eObject.eClass()) {
				auditEntryEClass = dataStore.getAuditHandler().getAuditingModelElement(eObject.eClass());
				lastEClass = eObject.eClass();
			}
			final String auditEntryEntityName = HbUtil.getEntityName(auditEntryEClass);

			// create the auditEntry
			final TeneoAuditEntry auditEntry = (TeneoAuditEntry) auditEntryEClass.getEPackage()
					.getEFactoryInstance().create(auditEntryEClass);
			auditEntry.setTeneo_audit_kind(auditWork.getAuditKind());
			auditEntry.setTeneo_commit_info(commitInfo);
			auditEntry.setTeneo_end(DEFAULT_END_TIMESTAMP);
			auditEntry.setTeneo_start(commitTime);
			auditEntry.setTeneo_object_id(dataStore.getAuditHandler().entityToIdString(session,
					auditWork.getEntity()));

			if (auditWork.getEntity() instanceof EObject
					&& null != ((EObject) auditWork.getEntity()).eContainer()) {
				auditEntry.setTeneo_container_id(dataStore.getAuditHandler().entityToIdString(session,
						((EObject) auditWork.getEntity()).eContainer()));
				auditEntry.setTeneo_container_feature_id(((InternalEObject) auditWork.getEntity())
						.eContainerFeatureID());
			}

			dataStore.getAuditHandler().copyContentToAuditEntry(session, (EObject) auditWork.getEntity(),
					auditEntry, auditWork.getAuditKind() != TeneoAuditKind.DELETE);

			// note also do a query in case of ADD as an object can
			// be removed and then re-added, restore its link
			// to the history then
			// get info from the previous entry
			// Note: apparently hibernate has a query plan cache, so
			// it does not give a performance benefit to use a namedquery
			// at least not that much..
			// http://stackoverflow.com/questions/3578711/jpa-caching-queries
			final Query infoQuery = session.createQuery("select e from " + auditEntryEntityName
					+ " e where teneo_object_id=:objectId and teneo_end="
					+ AuditProcessHandler.DEFAULT_END_TIMESTAMP);
			infoQuery.setMaxResults(1);
			infoQuery.setString("objectId", auditEntry.getTeneo_object_id());

			@SuppressWarnings("unchecked")
			final List<Object> list = infoQuery.list();
			if (!list.isEmpty()) {
				// list can be empty this happens if the item has an id set, then hibernate fires
				// an update event and not an add event
				final TeneoAuditEntry previousEntry = (TeneoAuditEntry) list.get(0);
				previousEntry.setTeneo_end(commitTime - 1);
				setCommitInfoInReferencedObjects(previousEntry, evictObjects);
				evictObjects.add(previousEntry);

				auditEntry.setTeneo_previous_start(previousEntry.getTeneo_start());

				session.update(auditEntryEntityName, previousEntry);
			}
			// save later to not flush in the loop
			toSaveEntries.add(auditEntry);
		}
		// do flush here to ensure that the update is done before
		// the save, this to prevent unique constraint failures
		session.flush();

		for (TeneoAuditEntry auditEntry : toSaveEntries) {
			setCommitInfoInReferencedObjects(auditEntry, evictObjects);
			session.save(HbUtil.getEntityName(auditEntry.eClass()), auditEntry);
			evictObjects.add(auditEntry);
		}
		// and flush the saved stuff
		session.flush();

		// clear the cache from these audit objects
		for (Object object : evictObjects) {
			session.evict(object);
		}

		// clear the work queue
		getRemoveQueue(session, false);

		// remove any old entries
		pruneEntries((SessionImplementor) session);
	}

	// is called/used in case of emap entries which are themselves
	// audit objects
	private void setCommitInfoInReferencedObjects(TeneoAuditEntry source, List<Object> evictObjects) {
		for (EReference eReference : source.eClass().getEAllReferences()) {
			if (TeneoauditingPackage.eINSTANCE.getTeneoAuditEntry().isSuperTypeOf(
					eReference.getEReferenceType())) {
				if (eReference.isMany()) {
					int i = 0;
					for (Object value : (Collection<?>) source.eGet(eReference)) {
						final TeneoAuditEntry target = (TeneoAuditEntry) value;
						evictObjects.add(target);
						setAuditEntryValues(eReference.getName() + "_" + i++, source, target);
					}
				} else if (source.eIsSet(eReference) && null != source.eGet(eReference)) {
					setAuditEntryValues(eReference.getName() + "_", source,
							(TeneoAuditEntry) source.eGet(eReference));
					evictObjects.add((TeneoAuditEntry) source.eGet(eReference));
				}
			}
		}
	}

	private void setAuditEntryValues(String prefix, TeneoAuditEntry source, TeneoAuditEntry target) {
		target.setTeneo_commit_info(source.getTeneo_commit_info());
		target.setTeneo_audit_kind(source.getTeneo_audit_kind());
		target.setTeneo_start(source.getTeneo_start());
		target.setTeneo_end(source.getTeneo_end());
		target.setTeneo_object_id(prefix + "_" + source.getTeneo_object_id());
		target.setTeneo_previous_start(source.getTeneo_previous_start());
	}

	private synchronized List<AuditWork> getRemoveQueue(Session session, boolean remove) {
		final List<AuditWork> auditWorks = workQueue.get(session.getTransaction());
		if (auditWorks != null && remove) {
			workQueue.remove(session.getTransaction());
		} else {
			// set a new queue so that it won't be refilled during the audit process
			// any extra actions which are done during the audit process are illegal
			// (an error in hibernate dirty detection) this is for now ignored
			workQueue.put(session.getTransaction(), new ArrayList<AuditWork>());
		}
		return auditWorks;
	}

	public HbDataStore getDataStore() {
		return dataStore;
	}

	public void setDataStore(HbDataStore dataStore) {
		this.dataStore = dataStore;
		pruneTime = 1000 * 3600 * 24 * dataStore.getPersistenceOptions().getAuditingPruneDays();
	}

	private synchronized void pruneEntries(SessionImplementor session) {
		if (pruneTime == 0) {
			return;
		}
		pruneCounter++;
		if ((pruneCounter % 100) != 0) {
			return;
		}

		if (auditEClasses == null) {
			auditEClasses = new ArrayList<EClass>();
			for (EPackage ePackage : dataStore.getEPackages()) {
				for (EClassifier eClassifier : ePackage.getEClassifiers()) {
					if (eClassifier instanceof EClass && StoreUtil.isAuditEntryEClass((EClass) eClassifier)) {
						auditEClasses.add((EClass) eClassifier);
					}
				}
			}
		}

		Session tmpSession = null;
		boolean err = true;
		try {
			tmpSession = session.getFactory().openSession();
			tmpSession.beginTransaction();
			final long currentPruneTime = System.currentTimeMillis() - pruneTime;
			for (EClass auditEClass : auditEClasses) {
				final Query qry = tmpSession.createQuery("select e from "
						+ dataStore.getEntityNameStrategy().toEntityName(auditEClass)
						+ " e where e.teneo_start < :pruneTime");
				qry.setParameter("pruneTime", currentPruneTime);
				for (Object o : qry.list()) {
					tmpSession.delete(o);
				}
			}
			tmpSession.getTransaction().commit();
			err = false;
		} finally {
			try {
				if (tmpSession != null && err) {
					tmpSession.getTransaction().rollback();
				}
			} finally {
				if (tmpSession != null) {
					tmpSession.close();
				}
			}
		}
	}

	/**
	 * Used for testing, for setting the prune limit use the
	 * {@link PersistenceOptions#AUDITING_PRUNE_OLD_ENTRIES_DAYS} option.
	 */
	public void setPruneTime(long thePruneTime) {
		pruneTime = thePruneTime;
	}

	private class AuditWork {
		private Object entity;
		private TeneoAuditKind auditKind;

		public Object getEntity() {
			return entity;
		}

		public void setEntity(Object entity) {
			this.entity = entity;
		}

		public TeneoAuditKind getAuditKind() {
			return auditKind;
		}

		public void setAuditKind(TeneoAuditKind auditKind) {
			this.auditKind = auditKind;
		}

	}
}

Back to the top