HHH-12077 Support short-naming of audit strategies

This commit is contained in:
Chris Cranford 2021-11-26 02:15:00 -05:00
parent f379d51504
commit 26c3a1f32b
4 changed files with 156 additions and 30 deletions

View File

@ -0,0 +1,57 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.envers.boot.internal;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.boot.registry.selector.SimpleStrategyRegistrationImpl;
import org.hibernate.boot.registry.selector.StrategyRegistration;
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.envers.strategy.AuditStrategy;
import org.hibernate.envers.strategy.DefaultAuditStrategy;
import org.hibernate.envers.strategy.ValidityAuditStrategy;
/**
* Provides the audit strategy implementations to the {@link StrategySelector} service.
*
* @author Chris Cranford
* @since 6.0
*/
public class AuditStrategyRegistrationProvider implements StrategyRegistrationProvider {
private static final List<StrategyRegistration> STRATEGIES = new ArrayList<>();
static {
STRATEGIES.add(
new SimpleStrategyRegistrationImpl(
AuditStrategy.class,
DefaultAuditStrategy.class,
"default",
DefaultAuditStrategy.class.getName(),
DefaultAuditStrategy.class.getSimpleName(),
org.hibernate.envers.strategy.internal.DefaultAuditStrategy.class.getName()
)
);
STRATEGIES.add(
new SimpleStrategyRegistrationImpl(
AuditStrategy.class,
ValidityAuditStrategy.class,
"validity",
ValidityAuditStrategy.class.getName(),
ValidityAuditStrategy.class.getSimpleName(),
org.hibernate.envers.strategy.internal.ValidityAuditStrategy.class.getName()
)
);
}
@Override
public Iterable<StrategyRegistration> getStrategyRegistrations() {
return STRATEGIES;
}
}

View File

@ -7,7 +7,6 @@
package org.hibernate.envers.configuration; package org.hibernate.envers.configuration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -21,7 +20,6 @@ import org.hibernate.dialect.HSQLDialect;
import org.hibernate.envers.RevisionListener; import org.hibernate.envers.RevisionListener;
import org.hibernate.envers.boot.EnversMappingException; import org.hibernate.envers.boot.EnversMappingException;
import org.hibernate.envers.boot.internal.EnversService; import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.boot.internal.EnversServiceImpl;
import org.hibernate.envers.boot.internal.LegacyModifiedColumnNamingStrategy; import org.hibernate.envers.boot.internal.LegacyModifiedColumnNamingStrategy;
import org.hibernate.envers.boot.spi.ModifiedColumnNamingStrategy; import org.hibernate.envers.boot.spi.ModifiedColumnNamingStrategy;
import org.hibernate.envers.configuration.internal.RevisionInfoConfiguration; import org.hibernate.envers.configuration.internal.RevisionInfoConfiguration;
@ -29,7 +27,6 @@ import org.hibernate.envers.internal.tools.ReflectionTools;
import org.hibernate.envers.internal.tools.StringTools; import org.hibernate.envers.internal.tools.StringTools;
import org.hibernate.envers.strategy.AuditStrategy; import org.hibernate.envers.strategy.AuditStrategy;
import org.hibernate.envers.strategy.DefaultAuditStrategy; import org.hibernate.envers.strategy.DefaultAuditStrategy;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.internal.util.config.ConfigurationHelper;
/** /**
@ -109,8 +106,10 @@ public class Configuration {
modifiedFlagsSuffix = configProps.getString( EnversSettings.MODIFIED_FLAG_SUFFIX, DEFAULT_MODIFIED_FLAG_SUFFIX ); modifiedFlagsSuffix = configProps.getString( EnversSettings.MODIFIED_FLAG_SUFFIX, DEFAULT_MODIFIED_FLAG_SUFFIX );
revisionListenerClass = resolveRevisionListener( configProps, enversService ); revisionListenerClass = resolveRevisionListener( configProps, enversService );
modifiedColumnNamingStrategy = resolveModifiedColumnNamingStrategy( configProps, enversService );
auditStrategy = resolveAuditStrategy( configProps, enversService ); final StrategySelector strategySelector = enversService.getServiceRegistry().getService( StrategySelector.class );
modifiedColumnNamingStrategy = resolveModifiedColumnNamingStrategy( configProps, strategySelector );
auditStrategy = resolveAuditStrategy( configProps, strategySelector );
nativeIdEnabled = configProps.getBoolean( EnversSettings.USE_REVISION_ENTITY_WITH_NATIVE_ID, true ); nativeIdEnabled = configProps.getBoolean( EnversSettings.USE_REVISION_ENTITY_WITH_NATIVE_ID, true );
allowIdentifierReuse = configProps.getBoolean( EnversSettings.ALLOW_IDENTIFIER_REUSE, false ); allowIdentifierReuse = configProps.getBoolean( EnversSettings.ALLOW_IDENTIFIER_REUSE, false );
@ -373,8 +372,7 @@ public class Configuration {
private static ModifiedColumnNamingStrategy resolveModifiedColumnNamingStrategy( private static ModifiedColumnNamingStrategy resolveModifiedColumnNamingStrategy(
ConfigurationProperties configProps, ConfigurationProperties configProps,
EnversService enversService) { StrategySelector selector) {
final StrategySelector selector = enversService.getServiceRegistry().getService( StrategySelector.class );
return selector.resolveDefaultableStrategy( return selector.resolveDefaultableStrategy(
ModifiedColumnNamingStrategy.class, ModifiedColumnNamingStrategy.class,
configProps.getString( EnversSettings.MODIFIED_COLUMN_NAMING_STRATEGY ), configProps.getString( EnversSettings.MODIFIED_COLUMN_NAMING_STRATEGY ),
@ -386,29 +384,13 @@ public class Configuration {
); );
} }
private static AuditStrategy resolveAuditStrategy(ConfigurationProperties configProps, EnversService enversService) { private static AuditStrategy resolveAuditStrategy(ConfigurationProperties configProps, StrategySelector selector) {
final String className = configProps.getString( EnversSettings.AUDIT_STRATEGY, DefaultAuditStrategy.class.getName() ); return selector.resolveDefaultableStrategy(
try { AuditStrategy.class,
Class<?> auditStrategy = null; configProps.getString( EnversSettings.AUDIT_STRATEGY, DefaultAuditStrategy.class.getName() ),
try { (Callable<AuditStrategy>) () -> new DefaultAuditStrategy()
auditStrategy = EnversServiceImpl.class.getClassLoader().loadClass( className );
}
catch (Exception e) {
auditStrategy = ReflectionTools.loadClass( className, enversService.getClassLoaderService() );
}
return (AuditStrategy) ReflectHelper.getDefaultConstructor( auditStrategy ).newInstance();
}
catch (Exception e) {
throw new EnversMappingException(
String.format(
Locale.ENGLISH,
"Unable to create AuditStrategy [%s] instance.",
className
),
e
); );
} }
}
private static class ConfigurationProperties { private static class ConfigurationProperties {
private final Properties properties; private final Properties properties;

View File

@ -11,3 +11,4 @@
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. # See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
# #
org.hibernate.envers.boot.internal.ModifiedColumnNamingStrategyRegistrationProvider org.hibernate.envers.boot.internal.ModifiedColumnNamingStrategyRegistrationProvider
org.hibernate.envers.boot.internal.AuditStrategyRegistrationProvider

View File

@ -0,0 +1,86 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.envers.integration.strategy;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.configuration.Configuration;
import org.hibernate.envers.configuration.EnversSettings;
import org.hibernate.envers.strategy.AuditStrategy;
import org.hibernate.envers.strategy.DefaultAuditStrategy;
import org.hibernate.envers.strategy.ValidityAuditStrategy;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.testing.TestForIssue;
/**
* A set of unit tests that verify that the audit strategy selector appropriately selects and
* creators the correct strategy class.
*
* @author Chris Cranford
*/
@TestForIssue( jiraKey = "HHH-12077" )
public class AuditStraegySelectorTest {
@Test
public void testAuditStrategySelectorNoneSpecified() {
testAuditStrategySelector( null, DefaultAuditStrategy.class );
}
@Test
public void testAuditStrategySelectorDefaultSpecified() {
// test old implementations
testAuditStrategySelector( "default", DefaultAuditStrategy.class );
testAuditStrategySelector( DefaultAuditStrategy.class.getSimpleName(), DefaultAuditStrategy.class );
testAuditStrategySelector( DefaultAuditStrategy.class.getName(), DefaultAuditStrategy.class );
// test new implementation
testAuditStrategySelector(
org.hibernate.envers.strategy.internal.DefaultAuditStrategy.class.getName(),
org.hibernate.envers.strategy.internal.DefaultAuditStrategy.class
);
}
@Test
public void testAuditStrategySelectorValiditySpecified() {
// test old implementations
testAuditStrategySelector( "validity", ValidityAuditStrategy.class );
testAuditStrategySelector( ValidityAuditStrategy.class.getSimpleName(), ValidityAuditStrategy.class );
testAuditStrategySelector( ValidityAuditStrategy.class.getName(), ValidityAuditStrategy.class );
// test new implementation
testAuditStrategySelector(
org.hibernate.envers.strategy.internal.ValidityAuditStrategy.class.getName(),
org.hibernate.envers.strategy.internal.ValidityAuditStrategy.class
);
}
private void testAuditStrategySelector(String propertyValue, Class<? extends AuditStrategy> expectedStrategyClass) {
final Map<String, Object> properties = new HashMap<>();
if ( propertyValue != null ) {
properties.put( EnversSettings.AUDIT_STRATEGY, propertyValue );
}
final ServiceRegistry sr = ServiceRegistryBuilder.buildServiceRegistry( properties );
try {
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( sr ).buildMetadata();
final Configuration configuration = sr.getService( EnversService.class ).getConfig();
assertTyping( expectedStrategyClass, configuration.getAuditStrategy() );
}
finally {
ServiceRegistryBuilder.destroy( sr );
}
}
}