bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject [32/50] bval git commit: lazily load the validator factory and validator for the cdi extension - note: we can surely lazy load the config as well if needed
Date Tue, 16 Oct 2018 17:35:33 GMT
lazily load the validator factory and validator for the cdi extension - note: we can surely
lazy load the config as well if needed


Project: http://git-wip-us.apache.org/repos/asf/bval/repo
Commit: http://git-wip-us.apache.org/repos/asf/bval/commit/2f193bc3
Tree: http://git-wip-us.apache.org/repos/asf/bval/tree/2f193bc3
Diff: http://git-wip-us.apache.org/repos/asf/bval/diff/2f193bc3

Branch: refs/heads/master
Commit: 2f193bc31133dba16af2174bede156970ea647fa
Parents: e5e22d3
Author: Romain Manni-Bucau <rmannibucau@gmail.com>
Authored: Thu Apr 19 10:16:21 2018 +0200
Committer: Matt Benson <mbenson@apache.org>
Committed: Tue Oct 16 12:28:20 2018 -0500

----------------------------------------------------------------------
 .../java/org/apache/bval/cdi/BValExtension.java | 44 +++++++++-----------
 .../java/org/apache/bval/cdi/ValidatorBean.java | 17 ++------
 .../apache/bval/cdi/ValidatorFactoryBean.java   |  7 ++--
 bval-tck/work-tests-suite.xml                   |  2 +-
 4 files changed, 28 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/bval/blob/2f193bc3/bval-jsr/src/main/java/org/apache/bval/cdi/BValExtension.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/BValExtension.java b/bval-jsr/src/main/java/org/apache/bval/cdi/BValExtension.java
index 8aa216f..c564823 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/BValExtension.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/BValExtension.java
@@ -52,6 +52,7 @@ import javax.validation.metadata.MethodType;
 
 import org.apache.bval.jsr.ConfigurationImpl;
 import org.apache.bval.jsr.util.ExecutableTypes;
+import org.apache.bval.util.Lazy;
 import org.apache.bval.util.Validate;
 
 /**
@@ -73,8 +74,8 @@ public class BValExtension implements Extension {
     private boolean validatorFactoryFound = Boolean.getBoolean("bval.in-container");
 
     private final Configuration<?> config;
-    private ValidatorFactory factory;
-    private Validator validator;
+    private Lazy<ValidatorFactory> factory;
+    private Lazy<Validator> validator;
 
     private Set<ExecutableType> globalExecutableTypes;
     private boolean isExecutableValidationEnabled;
@@ -106,9 +107,9 @@ public class BValExtension implements Extension {
             ((ConfigurationImpl) config).deferBootstrapOverrides();
         }
         if (factory == null) {
-            factory = config.buildValidatorFactory();
+            factory = new Lazy<>(config::buildValidatorFactory);
         }
-        validator = factory.getValidator();
+        validator = new Lazy<>(() -> factory.get().getValidator());
     }
 
     public Set<ExecutableType> getGlobalExecutableTypes() {
@@ -136,7 +137,7 @@ public class BValExtension implements Extension {
             try {
                 ensureFactoryValidator();
                 try {
-                    final BeanDescriptor classConstraints = validator.getConstraintsForClass(javaClass);
+                    final BeanDescriptor classConstraints = validator.get().getConstraintsForClass(javaClass);
 
                     final boolean validConstructors = globalExecutableTypes.contains(ExecutableType.CONSTRUCTORS)
                         && !classConstraints.getConstrainedConstructors().isEmpty();
@@ -189,37 +190,30 @@ public class BValExtension implements Extension {
     }
 
     public void addBValBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery, final
BeanManager beanManager) {
-        if (factory != null) { // cleanup cache used to discover ValidateOnException before
factory is recreated
-            factory.close();
+        if (factory != null && factory.optional().isPresent()) { // cleanup cache
used to discover ValidateOnException before factory is recreated
+            factory.get().close();
         }
         if (config instanceof ConfigurationImpl) {
             ((ConfigurationImpl) config).releaseDeferredBootstrapOverrides();
         }
         if (!validatorFactoryFound) {
             try { // recreate the factory
-                afterBeanDiscovery.addBean(new ValidatorFactoryBean(factory = config.buildValidatorFactory()));
+                factory = new Lazy<>(config::buildValidatorFactory);
+                afterBeanDiscovery.addBean(new ValidatorFactoryBean(factory));
+                validatorFactoryFound = true;
+            } catch (final ValidationException ve) {
+                //throw ve;
             } catch (final Exception e) { // can throw an exception with custom providers
-                if (e instanceof ValidationException) {
-                    throw e;
-                }
                 LOGGER.log(Level.SEVERE, e.getMessage(), e);
             }
         }
-        if (!validatorFound) {
+        if (!validatorFound && validatorFactoryFound) {
             try {
-                if (validatorFactoryFound) {
-                    factory = config.buildValidatorFactory();
-                } // else fresh factory already created in previous if
-                if (factory != null) { // happens in TCKS
-                    afterBeanDiscovery.addBean(new ValidatorBean(factory, factory.getValidator()));
-                    validatorFound = true;
-                }
-            } catch (final Exception e) { // getValidator can throw an exception with custom
providers
-                if (e instanceof ValidationException) {
-                    throw e;
-                }
-                afterBeanDiscovery.addBean(new ValidatorBean(factory, null));
+                afterBeanDiscovery.addBean(new ValidatorBean(() -> CDI.current().select(ValidatorFactory.class).get().getValidator()));
                 validatorFound = true;
+            } catch (final ValidationException ve) {
+                throw ve;
+            } catch (final Exception e) {
                 LOGGER.log(Level.SEVERE, e.getMessage(), e);
             }
         }
@@ -243,7 +237,7 @@ public class BValExtension implements Extension {
             it.inject(instance, context);
             it.postConstruct(instance);
 
-            return new Releasable<T>(context, it, instance);
+            return new Releasable<>(context, it, instance);
         } catch (final Exception | NoClassDefFoundError error) {
             // no-op
         }

http://git-wip-us.apache.org/repos/asf/bval/blob/2f193bc3/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorBean.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorBean.java b/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorBean.java
index cd17fae..ee4f152 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorBean.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorBean.java
@@ -24,12 +24,12 @@ import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.PassivationCapable;
 import javax.validation.Validator;
-import javax.validation.ValidatorFactory;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.function.Supplier;
 
 /**
  * {@link Validator} CDI {@link Bean}.
@@ -37,11 +37,9 @@ import java.util.Set;
 public class ValidatorBean implements Bean<Validator>, PassivationCapable {
     private final Set<Type> types;
     private final Set<Annotation> qualifiers;
-    private final ValidatorFactory factory;
-    private volatile Validator instance;
+    private final Supplier<Validator> instance;
 
-    public ValidatorBean(final ValidatorFactory factory, final Validator validator) {
-        this.factory = factory;
+    public ValidatorBean(final Supplier<Validator> validator) {
         this.instance = validator;
 
         final Set<Type> t = new HashSet<>();
@@ -102,14 +100,7 @@ public class ValidatorBean implements Bean<Validator>, PassivationCapable
{
 
     @Override
     public Validator create(final CreationalContext<Validator> context) {
-        if (instance == null) {
-            synchronized (this) {
-                if (instance == null) {
-                    instance = factory.getValidator();
-                }
-            }
-        }
-        return instance;
+        return instance == null ? null : instance.get();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/bval/blob/2f193bc3/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java
----------------------------------------------------------------------
diff --git a/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java b/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java
index 0a8c2e5..8dd7073 100644
--- a/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java
+++ b/bval-jsr/src/main/java/org/apache/bval/cdi/ValidatorFactoryBean.java
@@ -29,6 +29,7 @@ import java.lang.reflect.Type;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.function.Supplier;
 
 /**
  * {@link ValidatorFactory} CDI {@link Bean}.
@@ -36,9 +37,9 @@ import java.util.Set;
 public class ValidatorFactoryBean implements Bean<ValidatorFactory>, PassivationCapable
{
     private final Set<Type> types;
     private final Set<Annotation> qualifiers;
-    private final ValidatorFactory instance;
+    private final Supplier<ValidatorFactory> instance;
 
-    public ValidatorFactoryBean(final ValidatorFactory validatorFactory) {
+    public ValidatorFactoryBean(final Supplier<ValidatorFactory> validatorFactory)
{
         this.instance = validatorFactory;
 
         final Set<Type> t = new HashSet<>();
@@ -99,7 +100,7 @@ public class ValidatorFactoryBean implements Bean<ValidatorFactory>,
Passivation
 
     @Override
     public ValidatorFactory create(final CreationalContext<ValidatorFactory> context)
{
-        return instance;
+        return instance.get();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/bval/blob/2f193bc3/bval-tck/work-tests-suite.xml
----------------------------------------------------------------------
diff --git a/bval-tck/work-tests-suite.xml b/bval-tck/work-tests-suite.xml
index 8bc248c..a6c08c6 100644
--- a/bval-tck/work-tests-suite.xml
+++ b/bval-tck/work-tests-suite.xml
@@ -22,7 +22,7 @@ think to add -Dvalidation.provider=org.apache.bval.jsr.ApacheValidationProvider
   <test name="tmp">
     <classes>
 <!--       <class name="org.hibernate.beanvalidation.tck.tests.xmlconfiguration.groupconversion.containerelement.XmlBasedContainerElementGroupConversionValidationTest"/>
-->
-      <class name="org.hibernate.beanvalidation.tck.tests.validation.CustomPropertyPathTest"/>
+      <class name="org.hibernate.beanvalidation.tck.tests.bootstrap.BootstrapConfigurationTest"/>
 <!--
       <class name="org.hibernate.beanvalidation.tck.tests.util.ConstraintViolationAssertTest"/>
 -->


Mime
View raw message