HHH-12169 - Support dependency injection for Envers revision listeners.
This commit is contained in:
parent
ddc1f03abc
commit
a5bce26262
|
@ -8,10 +8,8 @@ package org.hibernate.envers.internal.revisioninfo;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.hibernate.MappingException;
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.envers.EntityTrackingRevisionListener;
|
import org.hibernate.envers.EntityTrackingRevisionListener;
|
||||||
import org.hibernate.envers.RevisionListener;
|
import org.hibernate.envers.RevisionListener;
|
||||||
|
@ -21,15 +19,18 @@ import org.hibernate.envers.internal.synchronization.SessionCacheCleaner;
|
||||||
import org.hibernate.envers.internal.tools.ReflectionTools;
|
import org.hibernate.envers.internal.tools.ReflectionTools;
|
||||||
import org.hibernate.internal.util.ReflectHelper;
|
import org.hibernate.internal.util.ReflectHelper;
|
||||||
import org.hibernate.property.access.spi.Setter;
|
import org.hibernate.property.access.spi.Setter;
|
||||||
|
import org.hibernate.resource.beans.spi.ManagedBean;
|
||||||
|
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Adam Warski (adam at warski dot org)
|
* @author Adam Warski (adam at warski dot org)
|
||||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||||
|
* @author Chris Cranford
|
||||||
*/
|
*/
|
||||||
public class DefaultRevisionInfoGenerator implements RevisionInfoGenerator {
|
public class DefaultRevisionInfoGenerator implements RevisionInfoGenerator {
|
||||||
private final String revisionInfoEntityName;
|
private final String revisionInfoEntityName;
|
||||||
private final RevisionListener listener;
|
private final ManagedBean<? extends RevisionListener> listenerManagedBean;
|
||||||
private final Setter revisionTimestampSetter;
|
private final Setter revisionTimestampSetter;
|
||||||
private final boolean timestampAsDate;
|
private final boolean timestampAsDate;
|
||||||
private final Constructor<?> revisionInfoClassConstructor;
|
private final Constructor<?> revisionInfoClassConstructor;
|
||||||
|
@ -45,30 +46,12 @@ public class DefaultRevisionInfoGenerator implements RevisionInfoGenerator {
|
||||||
this.revisionInfoEntityName = revisionInfoEntityName;
|
this.revisionInfoEntityName = revisionInfoEntityName;
|
||||||
this.timestampAsDate = timestampAsDate;
|
this.timestampAsDate = timestampAsDate;
|
||||||
|
|
||||||
revisionInfoClassConstructor = ReflectHelper.getDefaultConstructor( revisionInfoClass );
|
this.revisionInfoClassConstructor = ReflectHelper.getDefaultConstructor( revisionInfoClass );
|
||||||
revisionTimestampSetter = ReflectionTools.getSetter( revisionInfoClass, revisionInfoTimestampData, serviceRegistry );
|
this.revisionTimestampSetter = ReflectionTools.getSetter( revisionInfoClass, revisionInfoTimestampData, serviceRegistry );
|
||||||
|
|
||||||
if ( !listenerClass.equals( RevisionListener.class ) ) {
|
this.listenerManagedBean = resolveRevisionListenerBean( listenerClass, serviceRegistry );
|
||||||
// This is not the default value.
|
|
||||||
try {
|
|
||||||
listener = (RevisionListener) ReflectHelper.getDefaultConstructor( listenerClass ).newInstance();
|
|
||||||
}
|
|
||||||
catch (InstantiationException e) {
|
|
||||||
throw new MappingException( e );
|
|
||||||
}
|
|
||||||
catch (IllegalAccessException e) {
|
|
||||||
throw new MappingException( e );
|
|
||||||
}
|
|
||||||
catch (InvocationTargetException e) {
|
|
||||||
throw new MappingException( e );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Default listener - none
|
|
||||||
listener = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionCacheCleaner = new SessionCacheCleaner();
|
this.sessionCacheCleaner = new SessionCacheCleaner();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -90,8 +73,8 @@ public class DefaultRevisionInfoGenerator implements RevisionInfoGenerator {
|
||||||
final long timestamp = System.currentTimeMillis();
|
final long timestamp = System.currentTimeMillis();
|
||||||
revisionTimestampSetter.set( revisionInfo, timestampAsDate ? new Date( timestamp ) : timestamp, null );
|
revisionTimestampSetter.set( revisionInfo, timestampAsDate ? new Date( timestamp ) : timestamp, null );
|
||||||
|
|
||||||
if ( listener != null ) {
|
if ( listenerManagedBean != null ) {
|
||||||
listener.newRevision( revisionInfo );
|
listenerManagedBean.getBeanInstance().newRevision( revisionInfo );
|
||||||
}
|
}
|
||||||
|
|
||||||
return revisionInfo;
|
return revisionInfo;
|
||||||
|
@ -104,14 +87,26 @@ public class DefaultRevisionInfoGenerator implements RevisionInfoGenerator {
|
||||||
Serializable entityId,
|
Serializable entityId,
|
||||||
RevisionType revisionType,
|
RevisionType revisionType,
|
||||||
Object revisionInfo) {
|
Object revisionInfo) {
|
||||||
if ( listener instanceof EntityTrackingRevisionListener ) {
|
if ( listenerManagedBean != null ) {
|
||||||
( (EntityTrackingRevisionListener) listener ).entityChanged(
|
final RevisionListener listener = listenerManagedBean.getBeanInstance();
|
||||||
entityClass,
|
if ( EntityTrackingRevisionListener.class.isInstance( listener ) ) {
|
||||||
entityName,
|
( (EntityTrackingRevisionListener) listener ).entityChanged(
|
||||||
entityId,
|
entityClass,
|
||||||
revisionType,
|
entityName,
|
||||||
revisionInfo
|
entityId,
|
||||||
);
|
revisionType,
|
||||||
|
revisionInfo
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ManagedBean<? extends RevisionListener> resolveRevisionListenerBean(
|
||||||
|
Class<? extends RevisionListener> listenerClass,
|
||||||
|
ServiceRegistry serviceRegistry) {
|
||||||
|
if ( !listenerClass.equals( RevisionListener.class ) ) {
|
||||||
|
return serviceRegistry.getService( ManagedBeanRegistry.class ).getBean( listenerClass, true );
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue