From e07aff45dafc517f828159e95e47daed86163764 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Fri, 13 Jan 2012 03:44:30 +0800 Subject: [PATCH 1/2] HHH-6964 EnversIntegrator is always detected by default while initializing Serviceregistry using Native Hibernate API causing a MappingException even when Envers is not used --- .../configuration/AuditConfiguration.java | 68 ++++++++++++------- .../envers/event/EnversIntegrator.java | 3 +- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java index ef6a96ad2f..ac04986d5a 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java @@ -39,6 +39,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 +54,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,28 +89,39 @@ public class AuditConfiguration { } public AuditConfiguration(Configuration cfg) { - Properties properties = cfg.getProperties(); - - ReflectionManager reflectionManager = cfg.getReflectionManager(); - globalCfg = new GlobalConfiguration(properties); - RevisionInfoConfiguration revInfoCfg = new RevisionInfoConfiguration(globalCfg); - RevisionInfoConfigurationResult revInfoCfgResult = revInfoCfg.configure(cfg, reflectionManager); - auditEntCfg = new AuditEntitiesConfiguration(properties, revInfoCfgResult.getRevisionInfoEntityName()); - auditProcessManager = new AuditProcessManager(revInfoCfgResult.getRevisionInfoGenerator()); - 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(cfg, null); } + public AuditConfiguration(Configuration cfg, ClassLoaderService classLoaderService) { + Properties properties = cfg.getProperties(); + + ReflectionManager reflectionManager = cfg.getReflectionManager(); + globalCfg = new GlobalConfiguration(properties); + RevisionInfoConfiguration revInfoCfg = new RevisionInfoConfiguration(globalCfg); + RevisionInfoConfigurationResult revInfoCfgResult = revInfoCfg.configure(cfg, reflectionManager); + auditEntCfg = new AuditEntitiesConfiguration(properties, revInfoCfgResult.getRevisionInfoEntityName()); + auditProcessManager = new AuditProcessManager(revInfoCfgResult.getRevisionInfoGenerator()); + revisionInfoQueryCreator = revInfoCfgResult.getRevisionInfoQueryCreator(); + revisionInfoNumberReader = revInfoCfgResult.getRevisionInfoNumberReader(); + modifiedEntityNamesReader = revInfoCfgResult.getModifiedEntityNamesReader(); + 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) { throw new MappingException( @@ -131,15 +144,18 @@ public class AuditConfiguration { = new WeakHashMap(); public synchronized static AuditConfiguration getFor(Configuration cfg) { - AuditConfiguration verCfg = cfgs.get(cfg); - - if (verCfg == null) { - verCfg = new AuditConfiguration(cfg); - cfgs.put(cfg, verCfg); - - cfg.buildMappings(); - } - - return verCfg; + 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, classLoaderService); + cfgs.put(cfg, verCfg); + + cfg.buildMappings(); + } + + return verCfg; + } } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/event/EnversIntegrator.java b/hibernate-envers/src/main/java/org/hibernate/envers/event/EnversIntegrator.java index c4d740a013..a04d21fa1a 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/event/EnversIntegrator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/event/EnversIntegrator.java @@ -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 ) ); From 8f9d8b530e1925c116a99f0d905110814ada1208 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Fri, 13 Jan 2012 04:09:01 +0800 Subject: [PATCH 2/2] HHH-6964 EnversIntegrator is always detected by default while initializing Serviceregistry using Native Hibernate API causing a MappingException even when Envers is not used --- .../configuration/AuditConfiguration.java | 152 ++++++++++-------- 1 file changed, 82 insertions(+), 70 deletions(-) diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java index ac04986d5a..9ccd515913 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java @@ -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; @@ -46,68 +47,73 @@ import org.hibernate.service.classloading.spi.ClassLoaderService; * @author Stephanie Pau at Markit Group Plc */ public class AuditConfiguration { - private final GlobalConfiguration globalCfg; - private final AuditEntitiesConfiguration auditEntCfg; - private final AuditProcessManager auditProcessManager; - private final AuditStrategy auditStrategy; - private final EntitiesConfigurations entCfg; - private final RevisionInfoQueryCreator revisionInfoQueryCreator; - private final RevisionInfoNumberReader revisionInfoNumberReader; - private final ModifiedEntityNamesReader modifiedEntityNamesReader; + private final GlobalConfiguration globalCfg; + private final AuditEntitiesConfiguration auditEntCfg; + private final AuditProcessManager auditProcessManager; + private final AuditStrategy auditStrategy; + private final EntitiesConfigurations entCfg; + private final RevisionInfoQueryCreator revisionInfoQueryCreator; + private final RevisionInfoNumberReader revisionInfoNumberReader; + private final ModifiedEntityNamesReader modifiedEntityNamesReader; private final ClassLoaderService classLoaderService; - public AuditEntitiesConfiguration getAuditEntCfg() { - return auditEntCfg; - } + public AuditEntitiesConfiguration getAuditEntCfg() { + return auditEntCfg; + } - public AuditProcessManager getSyncManager() { - return auditProcessManager; - } + public AuditProcessManager getSyncManager() { + return auditProcessManager; + } - public GlobalConfiguration getGlobalCfg() { - return globalCfg; - } + public GlobalConfiguration getGlobalCfg() { + return globalCfg; + } - public EntitiesConfigurations getEntCfg() { - return entCfg; - } + public EntitiesConfigurations getEntCfg() { + return entCfg; + } - public RevisionInfoQueryCreator getRevisionInfoQueryCreator() { - return revisionInfoQueryCreator; - } + public RevisionInfoQueryCreator getRevisionInfoQueryCreator() { + return revisionInfoQueryCreator; + } - public RevisionInfoNumberReader getRevisionInfoNumberReader() { - return revisionInfoNumberReader; - } + public RevisionInfoNumberReader getRevisionInfoNumberReader() { + return revisionInfoNumberReader; + } - public ModifiedEntityNamesReader getModifiedEntityNamesReader() { - return modifiedEntityNamesReader; - } + public ModifiedEntityNamesReader getModifiedEntityNamesReader() { + return modifiedEntityNamesReader; + } - public AuditStrategy getAuditStrategy() { - return auditStrategy; - } + public AuditStrategy getAuditStrategy() { + return auditStrategy; + } + + public AuditConfiguration(Configuration cfg) { + this( cfg, null ); + } - public AuditConfiguration(Configuration cfg) { - this(cfg, null); - } public AuditConfiguration(Configuration cfg, ClassLoaderService classLoaderService) { Properties properties = cfg.getProperties(); ReflectionManager reflectionManager = cfg.getReflectionManager(); - globalCfg = new GlobalConfiguration(properties); - RevisionInfoConfiguration revInfoCfg = new RevisionInfoConfiguration(globalCfg); - RevisionInfoConfigurationResult revInfoCfgResult = revInfoCfg.configure(cfg, reflectionManager); - auditEntCfg = new AuditEntitiesConfiguration(properties, revInfoCfgResult.getRevisionInfoEntityName()); - auditProcessManager = new AuditProcessManager(revInfoCfgResult.getRevisionInfoGenerator()); + globalCfg = new GlobalConfiguration( properties ); + RevisionInfoConfiguration revInfoCfg = new RevisionInfoConfiguration( globalCfg ); + RevisionInfoConfigurationResult revInfoCfgResult = revInfoCfg.configure( cfg, reflectionManager ); + auditEntCfg = new AuditEntitiesConfiguration( properties, revInfoCfgResult.getRevisionInfoEntityName() ); + auditProcessManager = new AuditProcessManager( revInfoCfgResult.getRevisionInfoGenerator() ); revisionInfoQueryCreator = revInfoCfgResult.getRevisionInfoQueryCreator(); revisionInfoNumberReader = revInfoCfgResult.getRevisionInfoNumberReader(); modifiedEntityNamesReader = revInfoCfgResult.getModifiedEntityNamesReader(); this.classLoaderService = classLoaderService; - auditStrategy = initializeAuditStrategy(revInfoCfgResult.getRevisionInfoClass(), - revInfoCfgResult.getRevisionInfoTimestampData()); - entCfg = new EntitiesConfigurator().configure(cfg, reflectionManager, globalCfg, auditEntCfg, auditStrategy, - revInfoCfgResult.getRevisionInfoXmlMapping(), revInfoCfgResult.getRevisionInfoRelationMapping()); + 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) { @@ -115,43 +121,49 @@ public class AuditConfiguration { try { - Class auditStrategyClass = null; - if(classLoaderService!=null){ + Class auditStrategyClass = null; + if ( classLoaderService != null ) { auditStrategyClass = classLoaderService.classForName( auditEntCfg.getAuditStrategyName() ); - } else { - auditStrategyClass = Thread.currentThread().getContextClassLoader().loadClass(auditEntCfg.getAuditStrategyName()); + } + else { + auditStrategyClass = Thread.currentThread() + .getContextClassLoader() + .loadClass( auditEntCfg.getAuditStrategyName() ); } - strategy = (AuditStrategy) auditStrategyClass.newInstance(); - } catch (Exception e) { - throw new MappingException( - String.format("Unable to create AuditStrategy[%s] instance." , auditEntCfg.getAuditStrategyName()), - e); - } + strategy = (AuditStrategy) auditStrategyClass.newInstance(); + } + catch ( Exception e ) { + throw new MappingException( + String.format( "Unable to create AuditStrategy[%s] instance.", auditEntCfg.getAuditStrategyName() ), + e + ); + } - if (strategy instanceof ValidityAuditStrategy) { - // further initialization required - Getter revisionTimestampGetter = ReflectionTools.getGetter(revisionInfoClass, revisionInfoTimestampData); - ((ValidityAuditStrategy) strategy).setRevisionTimestampGetter(revisionTimestampGetter); - } + if ( strategy instanceof ValidityAuditStrategy ) { + // further initialization required + Getter revisionTimestampGetter = ReflectionTools.getGetter( revisionInfoClass, revisionInfoTimestampData ); + ( (ValidityAuditStrategy) strategy ).setRevisionTimestampGetter( revisionTimestampGetter ); + } - return strategy; + return strategy; } - // + // - private static Map cfgs - = new WeakHashMap(); + private static Map cfgs + = new WeakHashMap(); + + public synchronized static AuditConfiguration getFor(Configuration cfg) { + return getFor( cfg, null ); + } - 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); + AuditConfiguration verCfg = cfgs.get( cfg ); - if (verCfg == null) { - verCfg = new AuditConfiguration(cfg, classLoaderService); - cfgs.put(cfg, verCfg); + if ( verCfg == null ) { + verCfg = new AuditConfiguration( cfg, classLoaderService ); + cfgs.put( cfg, verCfg ); cfg.buildMappings(); }