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 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 JAKARTA_MODE_PROPERTY = "jakarta.persistence.validation.mode";
private static final String ACTIVATOR_CLASS_NAME = "org.hibernate.cfg.beanvalidation.TypeSafeActivator";
private static final String VALIDATE_SUPPLIED_FACTORY_METHOD_NAME = "validateSuppliedFactory";
@ -87,7 +89,11 @@ public class BeanValidationIntegrator implements Integrator {
final SessionFactoryServiceRegistry serviceRegistry) {
final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class );
// 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 ) {
LOG.multipleValidationModes( ValidationMode.loggable( modes ) );
}
@ -157,9 +163,15 @@ public class BeanValidationIntegrator implements Integrator {
return true;
}
catch (Exception e) {
try {
classLoaderService.classForName( JAKARTA_BV_CHECK_CLASS );
return true;
}
catch (Exception e2) {
return false;
}
}
}
/**
* Used to validate the case when the Bean Validation API is not available.

View File

@ -21,6 +21,7 @@ import org.hibernate.boot.spi.ClassLoaderAccess;
*/
public class GroupsPerOperation {
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 Class<?>[] DEFAULT_GROUPS = new Class<?>[] { Default.class };
@ -54,7 +55,10 @@ public class GroupsPerOperation {
}
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 ) {
return operation == Operation.DELETE ? EMPTY_GROUPS : DEFAULT_GROUPS;
@ -95,18 +99,20 @@ public class GroupsPerOperation {
}
public static enum Operation {
INSERT("persist", JPA_GROUP_PREFIX + "pre-persist"),
UPDATE("update", JPA_GROUP_PREFIX + "pre-update"),
DELETE("remove", JPA_GROUP_PREFIX + "pre-remove"),
DDL("ddl", HIBERNATE_GROUP_PREFIX + "ddl");
INSERT( "persist", JPA_GROUP_PREFIX + "pre-persist", JAKARTA_JPA_GROUP_PREFIX + "pre-persist" ),
UPDATE( "update", JPA_GROUP_PREFIX + "pre-update", JAKARTA_JPA_GROUP_PREFIX + "pre-update" ),
DELETE( "remove", JPA_GROUP_PREFIX + "pre-remove", JAKARTA_JPA_GROUP_PREFIX + "pre-remove" ),
DDL( "ddl", HIBERNATE_GROUP_PREFIX + "ddl", HIBERNATE_GROUP_PREFIX + "ddl" );
private final String exposedName;
private final String groupPropertyName;
private final String jakartaGroupPropertyName;
Operation(String exposedName, String groupProperty) {
Operation(String exposedName, String groupProperty, String jakartaGroupPropertyName) {
this.exposedName = exposedName;
this.groupPropertyName = groupProperty;
this.jakartaGroupPropertyName = jakartaGroupPropertyName;
}
public String getName() {
@ -116,6 +122,10 @@ public class GroupsPerOperation {
public String getGroupPropertyName() {
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.spi.ClassLoaderAccess;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
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 String FACTORY_PROPERTY = "javax.persistence.validation.factory";
/**
* Used to validate a supplied ValidatorFactory instance as being castable to ValidatorFactory.
*
@ -532,7 +531,7 @@ class TypeSafeActivator {
@SuppressWarnings("unchecked")
private static ValidatorFactory resolveProvidedFactory(ConfigurationService cfgService) {
return cfgService.getSetting(
FACTORY_PROPERTY,
AvailableSettings.JPA_VALIDATION_FACTORY,
new ConfigurationService.Converter<ValidatorFactory>() {
@Override
public ValidatorFactory convert(Object value) {
@ -544,7 +543,28 @@ class TypeSafeActivator {
String.format(
Locale.ENGLISH,
"ValidatorFactory reference (provided via `%s` setting) was not castable to %s : %s",
FACTORY_PROPERTY,
AvailableSettings.JPA_VALIDATION_FACTORY,
ValidatorFactory.class.getName(),
value.getClass().getName()
)
);
}
}
},
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()
)
@ -553,6 +573,7 @@ class TypeSafeActivator {
}
},
null
)
);
}
}

View File

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