public static GroupsPerOperation from(Map settings, ClassLoaderAccess classLoaderAccess) { GroupsPerOperation groupsPerOperation = new GroupsPerOperation(); applyOperationGrouping( groupsPerOperation, Operation.INSERT, settings, classLoaderAccess ); applyOperationGrouping( groupsPerOperation, Operation.UPDATE, settings, classLoaderAccess ); applyOperationGrouping( groupsPerOperation, Operation.DELETE, settings, classLoaderAccess ); applyOperationGrouping( groupsPerOperation, Operation.DDL, settings, classLoaderAccess ); return groupsPerOperation; }
@Override public EntityManagerFactoryBuilder withValidatorFactory(Object validatorFactory) { this.validatorFactory = validatorFactory; if ( validatorFactory != null ) { BeanValidationIntegrator.validateFactory( validatorFactory ); } return this; }
/** * Constructor used in an environment where validator factory is injected (JPA2). * * @param factory The {@code ValidatorFactory} to use to create {@code Validator} instance(s) * @param settings Configued properties */ public BeanValidationEventListener(ValidatorFactory factory, Map settings, ClassLoaderService classLoaderService) { init( factory, settings, classLoaderService ); }
@SuppressWarnings("UnusedDeclaration") public static void activate(ActivationContext activationContext) { final ValidatorFactory factory; try { factory = getValidatorFactory( activationContext ); } catch (IntegrationException e) { if ( activationContext.getValidationModes().contains( ValidationMode.CALLBACK ) ) { throw new IntegrationException( "Bean Validation provider was not available, but 'callback' validation was requested", e ); } if ( activationContext.getValidationModes().contains( ValidationMode.DDL ) ) { throw new IntegrationException( "Bean Validation provider was not available, but 'ddl' validation was requested", e ); } LOG.debug( "Unable to acquire Bean Validation ValidatorFactory, skipping activation" ); return; } applyRelationalConstraints( factory, activationContext ); applyCallbackListeners( factory, activationContext ); }
private static ValidatorFactory getValidatorFactory(ActivationContext activationContext) { // IMPL NOTE : We can either be provided a ValidatorFactory or make one. We can be provided // a ValidatorFactory in 2 different ways. So here we "get" a ValidatorFactory in the following order: // 1) Look into SessionFactoryOptions.getValidatorFactoryReference() // 2) Look into ConfigurationService // 3) build a new ValidatorFactory // 1 - look in SessionFactoryOptions.getValidatorFactoryReference() ValidatorFactory factory = resolveProvidedFactory( activationContext.getSessionFactory().getSessionFactoryOptions() ); if ( factory != null ) { return factory; } // 2 - look in ConfigurationService factory = resolveProvidedFactory( activationContext.getServiceRegistry().getService( ConfigurationService.class ) ); if ( factory != null ) { return factory; } // 3 - build our own try { return Validation.buildDefaultValidatorFactory(); } catch ( Exception e ) { throw new IntegrationException( "Unable to build the default ValidatorFactory", e ); } }
@SuppressWarnings({"unchecked", "UnusedParameters"}) private static void applyRelationalConstraints(ValidatorFactory factory, ActivationContext activationContext) { final ConfigurationService cfgService = activationContext.getServiceRegistry().getService( ConfigurationService.class ); if ( !cfgService.getSetting( BeanValidationIntegrator.APPLY_CONSTRAINTS, StandardConverters.BOOLEAN, true ) ) { LOG.debug( "Skipping application of relational constraints from legacy Hibernate Validator" ); return; } final Set<ValidationMode> modes = activationContext.getValidationModes(); if ( ! ( modes.contains( ValidationMode.DDL ) || modes.contains( ValidationMode.AUTO ) ) ) { return; } applyRelationalConstraints( factory, activationContext.getMetadata().getEntityBindings(), cfgService.getSettings(), activationContext.getServiceRegistry().getService( JdbcServices.class ).getDialect(), new ClassLoaderAccessImpl( null, activationContext.getServiceRegistry().getService( ClassLoaderService.class ) ) ); }
public boolean onPreUpdate(PreUpdateEvent event) { validate( event.getEntity(), event.getPersister().getEntityMode(), event.getPersister(), event.getSession().getFactory(), GroupsPerOperation.Operation.UPDATE ); return false; }
@Test public void test_bootstrap_bootstrap_native_registry_BootstrapServiceRegistry_example() { ClassLoader customClassLoader = Thread.currentThread().getContextClassLoader(); Integrator customIntegrator = new BeanValidationIntegrator(); //tag::bootstrap-bootstrap-native-registry-BootstrapServiceRegistry-example[] BootstrapServiceRegistryBuilder bootstrapRegistryBuilder = new BootstrapServiceRegistryBuilder(); // add a custom ClassLoader bootstrapRegistryBuilder.applyClassLoader( customClassLoader ); // manually add an Integrator bootstrapRegistryBuilder.applyIntegrator( customIntegrator ); BootstrapServiceRegistry bootstrapRegistry = bootstrapRegistryBuilder.build(); //end::bootstrap-bootstrap-native-registry-BootstrapServiceRegistry-example[] }
private void addAssociationsToTheSetForAllProperties(String[] names, Type[] types, String prefix, SessionFactoryImplementor factory) { final int length = names.length; for( int index = 0 ; index < length; index++ ) { addAssociationsToTheSetForOneProperty( names[index], types[index], prefix, factory ); } }
public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) { String path = getStringBasedPath( traversableProperty, pathToTraversableObject ); return ! associations.contains(path); } }
public HibernateTraversableResolver( EntityPersister persister, ConcurrentHashMap<EntityPersister, Set<String>> associationsPerEntityPersister, SessionFactoryImplementor factory) { this.associations = associationsPerEntityPersister.get( persister ); if (this.associations == null) { this.associations = new HashSet<String>(); addAssociationsToTheSetForAllProperties( persister.getPropertyNames(), persister.getPropertyTypes(), "", factory ); associationsPerEntityPersister.put( persister, associations ); } }
/** * Used to validate the case when the Bean Validation API is not available. * * @param modes The requested validation modes. */ private void validateMissingBeanValidationApi(Set<ValidationMode> modes) { if ( modes.contains( ValidationMode.CALLBACK ) ) { throw new IntegrationException( "Bean Validation API was not available, but 'callback' validation was requested" ); } if ( modes.contains( ValidationMode.DDL ) ) { throw new IntegrationException( "Bean Validation API was not available, but 'ddl' validation was requested" ); } }
private static void applyOperationGrouping( GroupsPerOperation groupsPerOperation, Operation operation, Map settings, ClassLoaderAccess classLoaderAccess) { groupsPerOperation.groupsPerOperation.put( operation, buildGroupsForOperation( operation, settings, classLoaderAccess ) ); }
private void init(ValidatorFactory factory, Map settings, ClassLoaderService classLoaderService) { this.factory = factory; groupsPerOperation = GroupsPerOperation.from( settings, new ClassLoaderAccessImpl( classLoaderService ) ); initialized = true; }
private static ValidationMode getMode(String modeProperty) { if (modeProperty == null || modeProperty.length() == 0) { return AUTO; } else { try { return valueOf( modeProperty.trim().toUpperCase(Locale.ROOT) ); } catch ( IllegalArgumentException e ) { throw new HibernateException( "Unknown validation mode in " + BeanValidationIntegrator.MODE_PROPERTY + ": " + modeProperty ); } } }
public boolean onPreDelete(PreDeleteEvent event) { validate( event.getEntity(), event.getPersister().getEntityMode(), event.getPersister(), event.getSession().getFactory(), GroupsPerOperation.Operation.DELETE ); return false; }
public void initialize(Map settings, ClassLoaderService classLoaderService) { if ( !initialized ) { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); init( factory, settings, classLoaderService ); } }
public IntegratorServiceImpl(LinkedHashSet<Integrator> providedIntegrators, ClassLoaderService classLoaderService) { // register standard integrators. Envers and JPA, for example, need to be handled by discovery because in // separate project/jars. addIntegrator( new BeanValidationIntegrator() ); addIntegrator( new JaccIntegrator() ); addIntegrator( new CollectionCacheInvalidator() ); // register provided integrators for ( Integrator integrator : providedIntegrators ) { addIntegrator( integrator ); } for ( Integrator integrator : classLoaderService.loadJavaServices( Integrator.class ) ) { addIntegrator( integrator ); } }
/** * Used to validate a supplied ValidatorFactory instance as being castable to ValidatorFactory. * * @param object The supplied ValidatorFactory instance. */ @SuppressWarnings( {"UnusedDeclaration"}) public static void validateSuppliedFactory(Object object) { if ( ! ValidatorFactory.class.isInstance( object ) ) { throw new IntegrationException( "Given object was not an instance of " + ValidatorFactory.class.getName() + "[" + object.getClass().getName() + "]" ); } }
public boolean onPreInsert(PreInsertEvent event) { validate( event.getEntity(), event.getPersister().getEntityMode(), event.getPersister(), event.getSession().getFactory(), GroupsPerOperation.Operation.INSERT ); return false; }