Package net.sf.basedb.core.log
Class LoggingInterceptor
java.lang.Object
org.hibernate.EmptyInterceptor
net.sf.basedb.core.log.LoggingInterceptor
- All Implemented Interfaces:
Serializable
,Interceptor
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:
- Last modified
- $Date: 2016-04-21 13:13:30 +0200 (to, 21 apr 2016) $
-
Nested Class Summary
Modifier and TypeClassDescription(package private) static class
(package private) static class
-
Field Summary
Modifier and TypeFieldDescriptionprivate List<LoggingInterceptor.LogDetails>
private static final Object[]
private static final String[]
private static final Type[]
private final LogControl
private final ExtensionsInvoker<LogManagerFactory>
private static final long
Fields inherited from class org.hibernate.EmptyInterceptor
INSTANCE
-
Constructor Summary
ConstructorDescriptionLoggingInterceptor
(LogControl logControl, ExtensionsInvoker<LogManagerFactory> logInvoker) Creates a new interceptor. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addChange
(ChangeType action, Object entity, Object[] state, Object[] previousState, String[] propertyNames, Type[] types, boolean logImmediately) If the log manager factory creates an entity logger, add information about the change to the internal list.void
addManualSingleChange
(ChangeType action, Object entity) Adds a manual non-Hibernate change to the log system.void
addManualSingleChange
(ChangeType action, Object entity, Object newValue, Object oldValue, String property, Type type) Adds a manual non-Hibernate change to the log system.void
Signals successful commit or rollback to the log manager.void
Flush remaining changes in the log control.void
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
Adds a CREATE event to the log.void
Collects all log events and sends them to the eachEntityLogger
.private void
private void
Methods inherited from class org.hibernate.EmptyInterceptor
afterTransactionBegin, findDirty, getEntity, getEntityName, instantiate, isTransient, onCollectionRecreate, onCollectionRemove, onCollectionUpdate, onLoad, onPrepareStatement, preFlush
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
EMTPY_OBJECT_ARRAY
-
EMTPY_STRING_ARRAY
-
EMTPY_TYPE_ARRAY
-
logControl
-
logInvoker
-
logHandlers
-
changes
-
-
Constructor Details
-
LoggingInterceptor
Creates a new interceptor.- Parameters:
logControl
- The LogControl that is managing the current transactionlogInvoker
- The extension invoker that create log manager factories
-
-
Method Details
-
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 interfaceInterceptor
- Overrides:
onSave
in classEmptyInterceptor
- 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 interfaceInterceptor
- Overrides:
onFlushDirty
in classEmptyInterceptor
- 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 interfaceInterceptor
- Overrides:
onDelete
in classEmptyInterceptor
-
postFlush
Collects all log events and sends them to the eachEntityLogger
.- Specified by:
postFlush
in interfaceInterceptor
- Overrides:
postFlush
in classEmptyInterceptor
-
beforeTransactionCompletion
Flush remaining changes in the log control.- Specified by:
beforeTransactionCompletion
in interfaceInterceptor
- Overrides:
beforeTransactionCompletion
in classEmptyInterceptor
-
afterTransactionCompletion
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 interfaceInterceptor
- Overrides:
afterTransactionCompletion
in classEmptyInterceptor
-
addManualSingleChange
public void addManualSingleChange(ChangeType action, Object entity, Object newValue, Object oldValue, String property, Type type) Adds a manual non-Hibernate change to the log system. The change is forwarded to the log handlers immediately. The log handlers should not store the entity object for later use since instance may be reused for other changes in the future.- Since:
- 3.3
-
addManualSingleChange
Adds a manual non-Hibernate change to the log system. The entity should be a special object that doesn't rely on the state arrays to convey what has changed. The change is forwarded to the log handlers immediately. The log handlers should not store the entity object for later use since instance may be reused for other changes in the future.- Since:
- 3.8
-
addChange
private void addChange(ChangeType action, Object entity, Object[] state, Object[] previousState, String[] propertyNames, Type[] types, boolean logImmediately) If the log manager factory creates an entity logger, add information about the change to the internal list. The list is processed in thepostFlush(Iterator)
method. -
readObject
- Throws:
ClassNotFoundException
IOException
-
writeObject
- Throws:
IOException
-