deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject deltaspike git commit: DELTASPIKE-992 optional support for bv 1.1
Date Mon, 21 Sep 2015 19:33:32 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 6f3e6a10f -> c9de75a81


DELTASPIKE-992 optional support for bv 1.1


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

Branch: refs/heads/master
Commit: c9de75a81f3e7fa49a89fd9ab760e85fe9e1e48d
Parents: 6f3e6a1
Author: gpetracek <gpetracek@apache.org>
Authored: Mon Sep 21 21:29:47 2015 +0200
Committer: gpetracek <gpetracek@apache.org>
Committed: Mon Sep 21 21:29:47 2015 +0200

----------------------------------------------------------------------
 .../CDIAwareConstraintValidatorFactory.java     | 49 +++++++++++++++++++-
 1 file changed, 47 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/c9de75a8/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanvalidation/impl/CDIAwareConstraintValidatorFactory.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanvalidation/impl/CDIAwareConstraintValidatorFactory.java
b/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanvalidation/impl/CDIAwareConstraintValidatorFactory.java
index d481081..e0f21b8 100644
--- a/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanvalidation/impl/CDIAwareConstraintValidatorFactory.java
+++ b/deltaspike/modules/bean-validation/impl/src/main/java/org/apache/deltaspike/beanvalidation/impl/CDIAwareConstraintValidatorFactory.java
@@ -18,6 +18,7 @@
  */
 package org.apache.deltaspike.beanvalidation.impl;
 
+import java.lang.reflect.Method;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -26,6 +27,7 @@ import javax.validation.ConstraintValidatorFactory;
 import javax.validation.Validation;
 
 import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.util.ReflectionUtils;
 
 /**
  * A factory for creating CDI Aware/Enabled ConstraintValidators.
@@ -34,6 +36,9 @@ import org.apache.deltaspike.core.api.provider.BeanProvider;
 public class CDIAwareConstraintValidatorFactory implements
         ConstraintValidatorFactory
 {
+    private static final String RELEASE_INSTANCE_METHOD_NAME = "releaseInstance";
+    private static Boolean releaseInstanceMethodFound;
+    private static Method releaseInstanceMethod;
 
     private final Logger log = Logger
             .getLogger(CDIAwareConstraintValidatorFactory.class.toString());
@@ -53,8 +58,7 @@ public class CDIAwareConstraintValidatorFactory implements
     @Override
     public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T>
validatorClass)
     {
-        T resolvedInst = null;
-        resolvedInst = BeanProvider.getContextualReference(validatorClass,true);
+        T resolvedInst = BeanProvider.getContextualReference(validatorClass, true);
         if (resolvedInst == null)
         {
             if (log.isLoggable(Level.CONFIG))
@@ -67,4 +71,45 @@ public class CDIAwareConstraintValidatorFactory implements
         return resolvedInst;
     }
 
+    //BV v1.1+
+    public void releaseInstance(ConstraintValidator<?, ?> constraintValidator)
+    {
+        if (releaseInstanceMethodFound == null)
+        {
+            lazyInit();
+        }
+        if (Boolean.TRUE.equals(releaseInstanceMethodFound))
+        {
+            ReflectionUtils.invokeMethod(this.delegate, releaseInstanceMethod, Void.class,
true, constraintValidator);
+        }
+    }
+
+    private synchronized void lazyInit()
+    {
+        if (releaseInstanceMethodFound != null)
+        {
+            return;
+        }
+
+        Class<?> currentClass = delegate.getClass();
+        while (currentClass != null && !Object.class.getName().equals(currentClass.getName()))
+        {
+            for (Method currentMethod : currentClass.getDeclaredMethods())
+            {
+                if (RELEASE_INSTANCE_METHOD_NAME.equals(currentMethod.getName()) &&
+                        currentMethod.getParameterTypes().length == 1 &&
+                        currentMethod.getParameterTypes()[0].equals(ConstraintValidator.class))
+                {
+                    releaseInstanceMethod = currentMethod;
+                    releaseInstanceMethodFound = true;
+                    return;
+                }
+
+            }
+
+            currentClass = currentClass.getSuperclass();
+        }
+
+        releaseInstanceMethodFound = false;
+    }
 }


Mime
View raw message