2.17.2: 2011-06-17

net.sf.basedb.core.log
Class LoggingInterceptor

java.lang.Object
  extended by org.hibernate.EmptyInterceptor
      extended by net.sf.basedb.core.log.LoggingInterceptor
All Implemented Interfaces:
Serializable, org.hibernate.Interceptor

public class LoggingInterceptor
extends org.hibernate.EmptyInterceptor

Interceptor that attaches to Hibernate sessions to provide logging of items that are updated/created/deleted. Do not attach this interceptor to a SessionFactory since this class is not thread-safe.

The interceptor uses onSave, onDelete and onFlushDirty to collect information about what has happened. The LogManagerFactory is asked to create EntityLogger:s for each modified/created/deleted item. The actual logging takes place in the postFlush method that calls EntityLogger.logChanges(LogManager, EntityDetails) on each entity logger.

NOTE! For new items, the ID is not available until after the flush to the database has been done. Eg. new items has a 0 id when LogManagerFactory.getEntityLogger(LogManager, Object) is called.

Version:
2.13
Author:
Nicklas
See Also:
Serialized Form
Last modified
$Date: 2010-08-13 10:50:27 +0200 (Fri, 13 Aug 2010) $

Field Summary
private  List<EntityDetails> changes
           
private  LogControl logControl
           
private  LogManager logManager
           
private  LogManagerFactory logManagerFactory
           
private static long serialVersionUID
           
 
Fields inherited from class org.hibernate.EmptyInterceptor
INSTANCE
 
Constructor Summary
LoggingInterceptor(LogControl logControl, LogManagerFactory logManagerFactory)
          Creates a new interceptor.
 
Method Summary
private  void addChange(ChangeType action, Object entity, Object[] state, Object[] previousState, String[] propertyNames, org.hibernate.type.Type[] types)
          If the log manager factory creates an entity logger, add information about the change to the internal list.
 void afterTransactionCompletion(org.hibernate.Transaction tx)
          Signals successful commit or rollback to the log manager.
 void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, org.hibernate.type.Type[] types)
          Adds a DELETE event to the log.
 boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, org.hibernate.type.Type[] types)
          Adds an UPDATE event to the log.
 boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, org.hibernate.type.Type[] types)
          Adds a CREATE event to the log.
 void postFlush(Iterator entities)
          Collects all log events and sends them to the each EntityLogger.
private  void readObject(ObjectInputStream ois)
           
private  void writeObject(ObjectOutputStream ois)
           
 
Methods inherited from class org.hibernate.EmptyInterceptor
afterTransactionBegin, beforeTransactionCompletion, findDirty, getEntity, getEntityName, instantiate, isTransient, onCollectionRecreate, onCollectionRemove, onCollectionUpdate, onLoad, onPrepareStatement, preFlush
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

logControl

private final LogControl logControl

logManagerFactory

private final LogManagerFactory logManagerFactory

logManager

private LogManager logManager

changes

private List<EntityDetails> changes
Constructor Detail

LoggingInterceptor

public LoggingInterceptor(LogControl logControl,
                          LogManagerFactory logManagerFactory)
Creates a new interceptor.

Parameters:
logControl - The LogControl that is managing the current transaction
logManagerFactory - The log manager factory to use for logging
Method Detail

onSave

public boolean onSave(Object entity,
                      Serializable id,
                      Object[] state,
                      String[] propertyNames,
                      org.hibernate.type.Type[] types)
Adds a CREATE event to the log.

Specified by:
onSave in interface org.hibernate.Interceptor
Overrides:
onSave in class org.hibernate.EmptyInterceptor
Returns:
Always false, since the state is not modified

onFlushDirty

public boolean onFlushDirty(Object entity,
                            Serializable id,
                            Object[] currentState,
                            Object[] previousState,
                            String[] propertyNames,
                            org.hibernate.type.Type[] types)
Adds an UPDATE event to the log.

Specified by:
onFlushDirty in interface org.hibernate.Interceptor
Overrides:
onFlushDirty in class org.hibernate.EmptyInterceptor
Returns:
Always false, since the state is not changed

onDelete

public void onDelete(Object entity,
                     Serializable id,
                     Object[] state,
                     String[] propertyNames,
                     org.hibernate.type.Type[] types)
Adds a DELETE event to the log.

Specified by:
onDelete in interface org.hibernate.Interceptor
Overrides:
onDelete in class org.hibernate.EmptyInterceptor

postFlush

public void postFlush(Iterator entities)
Collects all log events and sends them to the each EntityLogger.

Specified by:
postFlush in interface org.hibernate.Interceptor
Overrides:
postFlush in class org.hibernate.EmptyInterceptor

afterTransactionCompletion

public void afterTransactionCompletion(org.hibernate.Transaction tx)
Signals successful commit or rollback to the log manager. Note that this happens after the transaction/connection has been closed and no communication with the database can take place here.

Specified by:
afterTransactionCompletion in interface org.hibernate.Interceptor
Overrides:
afterTransactionCompletion in class org.hibernate.EmptyInterceptor

addChange

private void addChange(ChangeType action,
                       Object entity,
                       Object[] state,
                       Object[] previousState,
                       String[] propertyNames,
                       org.hibernate.type.Type[] types)
If the log manager factory creates an entity logger, add information about the change to the internal list. The list is processed in the postFlush(Iterator) method.


readObject

private void readObject(ObjectInputStream ois)
                 throws ClassNotFoundException,
                        IOException
Throws:
ClassNotFoundException
IOException

writeObject

private void writeObject(ObjectOutputStream ois)
                  throws IOException
Throws:
IOException

2.17.2: 2011-06-17