Thanks Steve. Try as I might I can't get the audit logging interceptor to work.
Here is the interceptor code ...
Code:
package com.adcware.rcs.util;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.CallbackException;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.type.Type;
import com.adcware.rcs.model.AuditLogRecord;
import com.adcware.rcs.model.Auditable;
public class AuditLogInterceptor implements Interceptor {
private Session _session;
private String _userId;
private Set _inserts = new HashSet();
private Set _updates = new HashSet();
public void setSession(Session s) { _session = s; }
public void setUserId(String uid) { _userId = uid; }
public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, Type[] arg4) throws CallbackException {
return false;
}
public boolean onFlushDirty(
Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types
) throws CallbackException {
if (entity instanceof Auditable) {
_updates.add(entity);
}
return false;
}
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException {
if (entity instanceof Auditable) {
_inserts.add(entity);
}
return false;
}
public void onDelete(Object arg0, Serializable arg1, Object[] arg2, String[] arg3, Type[] arg4) throws CallbackException {
}
public void preFlush(Iterator iter) throws CallbackException {
}
public void postFlush(Iterator iter) throws CallbackException {
System.out.println("postFlush: session is "+_session);
System.out.println("postFlush: userId is "+_userId);
if (_session != null) {
System.out.println("postFlush: session is "+_session);
System.out.println("postFlush: userId is "+_userId);
try {
for (Iterator it = _inserts.iterator(); it.hasNext(); ) {
Auditable entity = (Auditable) it.next();
//AuditLog.logEvent("create", entity, _userId, _session.connection());
try {
AuditLogRecord record =
new AuditLogRecord("create",
entity.getId(),
entity.getClass(),
_userId );
System.out.println("..... entity class name "+entity.getClass().getName());
System.out.println("..... entity id "+entity.getId());
_session.save(record);
_session.flush();
} catch (Exception ex) {
throw new CallbackException(ex);
} finally {
try {
_session.close();
} catch (HibernateException ex) {
throw new CallbackException(ex);
}
}
}
for (Iterator it = _updates.iterator(); it.hasNext(); ) {
Auditable entity = (Auditable) it.next();
//AuditLog.logEvent("update", entity, _userId, _session.connection());
}
} catch (HibernateException ex) {
throw new CallbackException(ex);
} finally {
_inserts.clear();
_updates.clear();
}
}
}
public Boolean isTransient(Object arg0) {
return null;
}
public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2, Object[] arg3, String[] arg4, Type[] arg5) {
return null;
}
public Object instantiate(String arg0, EntityMode arg1, Serializable arg2) throws CallbackException {
return null;
}
public String getEntityName(Object arg0) throws CallbackException {
return null;
}
public Object getEntity(String arg0, Serializable arg1) throws CallbackException {
return null;
}
public void afterTransactionBegin(Transaction arg0) {
}
public void beforeTransactionCompletion(Transaction arg0) {
}
public void afterTransactionCompletion(Transaction arg0) {
}
}
And here is the session bean method which would enable it ...
Code:
public Referral createReferralForCustomer(Customer cust, ProductCategory prod,
Account orgAcct, Account asnAcct, ReferralSource source, String comment)
throws HibernateException, Exception {
Session hsession = _sessionFactory.getCurrentSession();
// enable audit logging ...
AuditLogInterceptor interceptor = new AuditLogInterceptor();
Session localSession = _sessionFactory.openSession( hsession.connection(), interceptor );
interceptor.setSession(localSession);
interceptor.setUserId(_ctx.getCallerPrincipal().getName());
Referral ref = new Referral();
//
// ref.set* omitted ...
//
hsession.save(ref);
return ref;
}
Here is what is displayed in the console log ...
Code:
19:52:44,299 INFO [STDOUT] postFlush: session is null
19:52:44,299 INFO [STDOUT] postFlush: userId is null
19:52:44,729 INFO [STDOUT] postFlush: session is null
19:52:44,729 INFO [STDOUT] postFlush: userId is null
19:52:44,860 INFO [STDOUT] postFlush: session is null
19:52:44,860 INFO [STDOUT] postFlush: userId is null
19:52:44,980 INFO [STDOUT] postFlush: session is null
19:52:44,980 INFO [STDOUT] postFlush: userId is null
19:52:45,080 INFO [STDOUT] postFlush: session is null
19:52:45,080 INFO [STDOUT] postFlush: userId is null
19:52:45,170 INFO [STDOUT] postFlush: session is null
19:52:45,170 INFO [STDOUT] postFlush: userId is null
19:52:45,370 INFO [STDOUT] postFlush: session is null
19:52:45,370 INFO [STDOUT] postFlush: userId is null
19:52:45,490 INFO [STDOUT] postFlush: session is null
19:52:45,490 INFO [STDOUT] postFlush: userId is null
19:52:45,751 INFO [STDOUT] postFlush: session is null
19:52:45,751 INFO [STDOUT] postFlush: userId is null
19:52:45,881 INFO [STDOUT] postFlush: session is null
19:52:45,881 INFO [STDOUT] postFlush: userId is null
19:52:46,542 INFO [STDOUT] postFlush: session is null
19:52:46,542 INFO [STDOUT] postFlush: userId is null
19:52:46,852 INFO [STDOUT] postFlush: session is null
19:52:46,852 INFO [STDOUT] postFlush: userId is null
19:52:47,103 INFO [STDOUT] postFlush: session is null
19:52:47,103 INFO [STDOUT] postFlush: userId is null
19:52:47,423 INFO [STDOUT] postFlush: session is null
19:52:47,423 INFO [STDOUT] postFlush: userId is null
19:52:47,583 INFO [STDOUT] postFlush: session is null
19:52:47,583 INFO [STDOUT] postFlush: userId is null
19:52:47,704 INFO [STDOUT] postFlush: session is null
19:52:47,704 INFO [STDOUT] postFlush: userId is null
19:52:48,034 INFO [STDOUT] postFlush: session is null
19:52:48,034 INFO [STDOUT] postFlush: userId is null
I don't know why the session and userid are always null. If I remove the the SessionFactoryInterceptor from the hibernate-service.xml then I get no postFlush callbacks at all.
I am using hibernate 3.0.5 and Jboss 4.0.2. The classes for AuditLogRecord are as defined in the book "Hibernate in Action".
Many thanks for your prompt reply and help.
Joe