Merge pull request #249 from stliu/HHH-6964

HHH-6964 Audit Strategy class can't be found by Thread.currentThread().getContextClassLoader() under AS7
This commit is contained in:
Adam Warski 2012-01-12 12:13:02 -08:00
commit d8003efcf6
2 changed files with 103 additions and 74 deletions

View File

@ -22,6 +22,7 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.configuration;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
@ -39,6 +40,7 @@ import org.hibernate.envers.strategy.ValidityAuditStrategy;
import org.hibernate.envers.synchronization.AuditProcessManager;
import org.hibernate.envers.tools.reflection.ReflectionTools;
import org.hibernate.property.Getter;
import org.hibernate.service.classloading.spi.ClassLoaderService;
/**
* @author Adam Warski (adam at warski dot org)
@ -53,6 +55,7 @@ public class AuditConfiguration {
private final RevisionInfoQueryCreator revisionInfoQueryCreator;
private final RevisionInfoNumberReader revisionInfoNumberReader;
private final ModifiedEntityNamesReader modifiedEntityNamesReader;
private final ClassLoaderService classLoaderService;
public AuditEntitiesConfiguration getAuditEntCfg() {
return auditEntCfg;
@ -87,6 +90,10 @@ public class AuditConfiguration {
}
public AuditConfiguration(Configuration cfg) {
this( cfg, null );
}
public AuditConfiguration(Configuration cfg, ClassLoaderService classLoaderService) {
Properties properties = cfg.getProperties();
ReflectionManager reflectionManager = cfg.getReflectionManager();
@ -98,22 +105,39 @@ public class AuditConfiguration {
revisionInfoQueryCreator = revInfoCfgResult.getRevisionInfoQueryCreator();
revisionInfoNumberReader = revInfoCfgResult.getRevisionInfoNumberReader();
modifiedEntityNamesReader = revInfoCfgResult.getModifiedEntityNamesReader();
auditStrategy = initializeAuditStrategy(revInfoCfgResult.getRevisionInfoClass(),
revInfoCfgResult.getRevisionInfoTimestampData());
entCfg = new EntitiesConfigurator().configure(cfg, reflectionManager, globalCfg, auditEntCfg, auditStrategy,
revInfoCfgResult.getRevisionInfoXmlMapping(), revInfoCfgResult.getRevisionInfoRelationMapping());
this.classLoaderService = classLoaderService;
auditStrategy = initializeAuditStrategy(
revInfoCfgResult.getRevisionInfoClass(),
revInfoCfgResult.getRevisionInfoTimestampData()
);
entCfg = new EntitiesConfigurator().configure(
cfg, reflectionManager, globalCfg, auditEntCfg, auditStrategy,
revInfoCfgResult.getRevisionInfoXmlMapping(), revInfoCfgResult.getRevisionInfoRelationMapping()
);
}
private AuditStrategy initializeAuditStrategy(Class<?> revisionInfoClass, PropertyData revisionInfoTimestampData) {
AuditStrategy strategy;
try {
Class<?> auditStrategyClass = Thread.currentThread().getContextClassLoader().loadClass(auditEntCfg.getAuditStrategyName());
Class<?> auditStrategyClass = null;
if ( classLoaderService != null ) {
auditStrategyClass = classLoaderService.classForName( auditEntCfg.getAuditStrategyName() );
}
else {
auditStrategyClass = Thread.currentThread()
.getContextClassLoader()
.loadClass( auditEntCfg.getAuditStrategyName() );
}
strategy = (AuditStrategy) auditStrategyClass.newInstance();
} catch (Exception e) {
}
catch ( Exception e ) {
throw new MappingException(
String.format( "Unable to create AuditStrategy[%s] instance.", auditEntCfg.getAuditStrategyName() ),
e);
e
);
}
if ( strategy instanceof ValidityAuditStrategy ) {
@ -131,10 +155,14 @@ public class AuditConfiguration {
= new WeakHashMap<Configuration, AuditConfiguration>();
public synchronized static AuditConfiguration getFor(Configuration cfg) {
return getFor( cfg, null );
}
public synchronized static AuditConfiguration getFor(Configuration cfg, ClassLoaderService classLoaderService) {
AuditConfiguration verCfg = cfgs.get( cfg );
if ( verCfg == null ) {
verCfg = new AuditConfiguration(cfg);
verCfg = new AuditConfiguration( cfg, classLoaderService );
cfgs.put( cfg, verCfg );
cfg.buildMappings();

View File

@ -34,6 +34,7 @@ import org.hibernate.integrator.spi.Integrator;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.metamodel.source.MetadataImplementor;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
/**
@ -60,7 +61,7 @@ public class EnversIntegrator implements Integrator {
EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
listenerRegistry.addDuplicationStrategy( EnversListenerDuplicationStrategy.INSTANCE );
final AuditConfiguration enversConfiguration = AuditConfiguration.getFor( configuration );
final AuditConfiguration enversConfiguration = AuditConfiguration.getFor( configuration, serviceRegistry.getService( ClassLoaderService.class ) );
if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
listenerRegistry.appendListeners( EventType.POST_DELETE, new EnversPostDeleteEventListenerImpl( enversConfiguration ) );