HHH-14274 Support for jakarta prefixed String properties for integrations

This commit is contained in:
Christian Beikov 2021-04-20 13:27:22 +02:00
parent 14b35bb3b6
commit eb8b8620d7
4 changed files with 62 additions and 14 deletions

View File

@ -33,8 +33,10 @@ public class BeanValidationIntegrator implements Integrator {
public static final String APPLY_CONSTRAINTS = "hibernate.validator.apply_to_ddl"; public static final String APPLY_CONSTRAINTS = "hibernate.validator.apply_to_ddl";
public static final String BV_CHECK_CLASS = "javax.validation.ConstraintViolation"; public static final String BV_CHECK_CLASS = "javax.validation.ConstraintViolation";
public static final String JAKARTA_BV_CHECK_CLASS = "jakarta.validation.ConstraintViolation";
public static final String MODE_PROPERTY = "javax.persistence.validation.mode"; public static final String MODE_PROPERTY = "javax.persistence.validation.mode";
public static final String JAKARTA_MODE_PROPERTY = "jakarta.persistence.validation.mode";
private static final String ACTIVATOR_CLASS_NAME = "org.hibernate.cfg.beanvalidation.TypeSafeActivator"; private static final String ACTIVATOR_CLASS_NAME = "org.hibernate.cfg.beanvalidation.TypeSafeActivator";
private static final String VALIDATE_SUPPLIED_FACTORY_METHOD_NAME = "validateSuppliedFactory"; private static final String VALIDATE_SUPPLIED_FACTORY_METHOD_NAME = "validateSuppliedFactory";
@ -87,7 +89,11 @@ public class BeanValidationIntegrator implements Integrator {
final SessionFactoryServiceRegistry serviceRegistry) { final SessionFactoryServiceRegistry serviceRegistry) {
final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class ); final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class );
// IMPL NOTE : see the comments on ActivationContext.getValidationModes() as to why this is multi-valued... // IMPL NOTE : see the comments on ActivationContext.getValidationModes() as to why this is multi-valued...
final Set<ValidationMode> modes = ValidationMode.getModes( cfgService.getSettings().get( MODE_PROPERTY ) ); Object modeSetting = cfgService.getSettings().get( MODE_PROPERTY );
if ( modeSetting == null ) {
modeSetting = cfgService.getSettings().get( JAKARTA_MODE_PROPERTY );
}
final Set<ValidationMode> modes = ValidationMode.getModes( modeSetting );
if ( modes.size() > 1 ) { if ( modes.size() > 1 ) {
LOG.multipleValidationModes( ValidationMode.loggable( modes ) ); LOG.multipleValidationModes( ValidationMode.loggable( modes ) );
} }
@ -157,7 +163,13 @@ public class BeanValidationIntegrator implements Integrator {
return true; return true;
} }
catch (Exception e) { catch (Exception e) {
return false; try {
classLoaderService.classForName( JAKARTA_BV_CHECK_CLASS );
return true;
}
catch (Exception e2) {
return false;
}
} }
} }

View File

@ -21,6 +21,7 @@ import org.hibernate.boot.spi.ClassLoaderAccess;
*/ */
public class GroupsPerOperation { public class GroupsPerOperation {
private static final String JPA_GROUP_PREFIX = "javax.persistence.validation.group."; private static final String JPA_GROUP_PREFIX = "javax.persistence.validation.group.";
private static final String JAKARTA_JPA_GROUP_PREFIX = "javax.persistence.validation.group.";
private static final String HIBERNATE_GROUP_PREFIX = "org.hibernate.validator.group."; private static final String HIBERNATE_GROUP_PREFIX = "org.hibernate.validator.group.";
private static final Class<?>[] DEFAULT_GROUPS = new Class<?>[] { Default.class }; private static final Class<?>[] DEFAULT_GROUPS = new Class<?>[] { Default.class };
@ -54,7 +55,10 @@ public class GroupsPerOperation {
} }
public static Class<?>[] buildGroupsForOperation(Operation operation, Map settings, ClassLoaderAccess classLoaderAccess) { public static Class<?>[] buildGroupsForOperation(Operation operation, Map settings, ClassLoaderAccess classLoaderAccess) {
final Object property = settings.get( operation.getGroupPropertyName() ); Object property = settings.get( operation.getGroupPropertyName() );
if ( property == null ) {
property = settings.get( operation.getJakartaGroupPropertyName() );
}
if ( property == null ) { if ( property == null ) {
return operation == Operation.DELETE ? EMPTY_GROUPS : DEFAULT_GROUPS; return operation == Operation.DELETE ? EMPTY_GROUPS : DEFAULT_GROUPS;
@ -95,18 +99,20 @@ public class GroupsPerOperation {
} }
public static enum Operation { public static enum Operation {
INSERT("persist", JPA_GROUP_PREFIX + "pre-persist"), INSERT( "persist", JPA_GROUP_PREFIX + "pre-persist", JAKARTA_JPA_GROUP_PREFIX + "pre-persist" ),
UPDATE("update", JPA_GROUP_PREFIX + "pre-update"), UPDATE( "update", JPA_GROUP_PREFIX + "pre-update", JAKARTA_JPA_GROUP_PREFIX + "pre-update" ),
DELETE("remove", JPA_GROUP_PREFIX + "pre-remove"), DELETE( "remove", JPA_GROUP_PREFIX + "pre-remove", JAKARTA_JPA_GROUP_PREFIX + "pre-remove" ),
DDL("ddl", HIBERNATE_GROUP_PREFIX + "ddl"); DDL( "ddl", HIBERNATE_GROUP_PREFIX + "ddl", HIBERNATE_GROUP_PREFIX + "ddl" );
private final String exposedName; private final String exposedName;
private final String groupPropertyName; private final String groupPropertyName;
private final String jakartaGroupPropertyName;
Operation(String exposedName, String groupProperty) { Operation(String exposedName, String groupProperty, String jakartaGroupPropertyName) {
this.exposedName = exposedName; this.exposedName = exposedName;
this.groupPropertyName = groupProperty; this.groupPropertyName = groupProperty;
this.jakartaGroupPropertyName = jakartaGroupPropertyName;
} }
public String getName() { public String getName() {
@ -116,6 +122,10 @@ public class GroupsPerOperation {
public String getGroupPropertyName() { public String getGroupPropertyName() {
return groupPropertyName; return groupPropertyName;
} }
public String getJakartaGroupPropertyName() {
return jakartaGroupPropertyName;
}
} }
} }

View File

@ -33,6 +33,7 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.boot.spi.ClassLoaderAccess; import org.hibernate.boot.spi.ClassLoaderAccess;
import org.hibernate.boot.spi.SessionFactoryOptions; import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.ConfigurationService;
@ -60,8 +61,6 @@ class TypeSafeActivator {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, TypeSafeActivator.class.getName()); private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, TypeSafeActivator.class.getName());
private static final String FACTORY_PROPERTY = "javax.persistence.validation.factory";
/** /**
* Used to validate a supplied ValidatorFactory instance as being castable to ValidatorFactory. * Used to validate a supplied ValidatorFactory instance as being castable to ValidatorFactory.
* *
@ -532,7 +531,7 @@ class TypeSafeActivator {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static ValidatorFactory resolveProvidedFactory(ConfigurationService cfgService) { private static ValidatorFactory resolveProvidedFactory(ConfigurationService cfgService) {
return cfgService.getSetting( return cfgService.getSetting(
FACTORY_PROPERTY, AvailableSettings.JPA_VALIDATION_FACTORY,
new ConfigurationService.Converter<ValidatorFactory>() { new ConfigurationService.Converter<ValidatorFactory>() {
@Override @Override
public ValidatorFactory convert(Object value) { public ValidatorFactory convert(Object value) {
@ -544,7 +543,7 @@ class TypeSafeActivator {
String.format( String.format(
Locale.ENGLISH, Locale.ENGLISH,
"ValidatorFactory reference (provided via `%s` setting) was not castable to %s : %s", "ValidatorFactory reference (provided via `%s` setting) was not castable to %s : %s",
FACTORY_PROPERTY, AvailableSettings.JPA_VALIDATION_FACTORY,
ValidatorFactory.class.getName(), ValidatorFactory.class.getName(),
value.getClass().getName() value.getClass().getName()
) )
@ -552,7 +551,29 @@ class TypeSafeActivator {
} }
} }
}, },
null cfgService.getSetting(
AvailableSettings.JAKARTA_JPA_VALIDATION_FACTORY,
new ConfigurationService.Converter<ValidatorFactory>() {
@Override
public ValidatorFactory convert(Object value) {
try {
return ValidatorFactory.class.cast( value );
}
catch ( ClassCastException e ) {
throw new IntegrationException(
String.format(
Locale.ENGLISH,
"ValidatorFactory reference (provided via `%s` setting) was not castable to %s : %s",
AvailableSettings.JAKARTA_JPA_VALIDATION_FACTORY,
ValidatorFactory.class.getName(),
value.getClass().getName()
)
);
}
}
},
null
)
); );
} }
} }

View File

@ -129,7 +129,12 @@ public class ManagedBeanRegistryInitiator implements StandardServiceInitiator<Ma
} }
public static Class cdiBeanManagerClass(ClassLoaderService classLoaderService) throws ClassLoadingException { public static Class cdiBeanManagerClass(ClassLoaderService classLoaderService) throws ClassLoadingException {
return classLoaderService.classForName( "javax.enterprise.inject.spi.BeanManager" ); try {
return classLoaderService.classForName( "javax.enterprise.inject.spi.BeanManager" );
}
catch (ClassLoadingException e) {
return classLoaderService.classForName( "jakarta.enterprise.inject.spi.BeanManager" );
}
} }
} }