3.2.4: 2013-12-06

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, Interceptor

public class LoggingInterceptor
extends 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: 2013-05-24 13:46:08 +0200 (Fri, 24 May 2013) $

Nested Class Summary
(package private) static class LoggingInterceptor.LogDetails
           
(package private) static class LoggingInterceptor.LogManagerAndFactory
           
 
Field Summary
private  List<LoggingInterceptor.LogDetails> changes
           
private  LogControl logControl
           
private  List<LoggingInterceptor.LogManagerAndFactory> logHandlers
           
private  ExtensionsInvoker<LogManagerFactory> logInvoker
           
private static long serialVersionUID
           
 
Fields inherited from class org.hibernate.EmptyInterceptor
INSTANCE
 
Constructor Summary
LoggingInterceptor(LogControl logControl, ExtensionsInvoker<LogManagerFactory> logInvoker)
          Creates a new interceptor.
 
Method Summary
private  void addChange(ChangeType action, Object entity, Object[] state, Object[] previousState, String[] propertyNames, Type[] types)
          If the log manager factory creates an entity logger, add information about the change to the internal list.
 void afterTransactionCompletion(Transaction tx)
          Signals successful commit or rollback to the log manager.
 void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
          Adds a DELETE event to the log.
 boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)
          Adds an UPDATE event to the log.
 boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, 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

logInvoker

private final ExtensionsInvoker<LogManagerFactory> logInvoker

logHandlers

private List<LoggingInterceptor.LogManagerAndFactory> logHandlers

changes

private List<LoggingInterceptor.LogDetails> changes
Constructor Detail

LoggingInterceptor

public LoggingInterceptor(LogControl logControl,
                          ExtensionsInvoker<LogManagerFactory> logInvoker)
Creates a new interceptor.

Parameters:
logControl - The LogControl that is managing the current transaction
logInvoker - The extension invoker that create log manager factories
Method Detail

onSave

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

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

onFlushDirty

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

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

onDelete

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

Specified by:
onDelete in interface Interceptor
Overrides:
onDelete in class EmptyInterceptor

postFlush

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

Specified by:
postFlush in interface Interceptor
Overrides:
postFlush in class EmptyInterceptor

afterTransactionCompletion

public void afterTransactionCompletion(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 Interceptor
Overrides:
afterTransactionCompletion in class EmptyInterceptor

addChange

private void addChange(ChangeType action,
                       Object entity,
                       Object[] state,
                       Object[] previousState,
                       String[] propertyNames,
                       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

3.2.4: 2013-12-06