incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1001000 - in /incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303: ApacheFactoryContext.java ApacheValidatorConfiguration.java
Date Fri, 24 Sep 2010 17:59:07 GMT
Author: mbenson
Date: Fri Sep 24 17:59:07 2010
New Revision: 1001000

URL: http://svn.apache.org/viewvc?rev=1001000&view=rev
Log:
allow configuration of custom MetaBeanFactories

Modified:
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheFactoryContext.java
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorConfiguration.java

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheFactoryContext.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheFactoryContext.java?rev=1001000&r1=1000999&r2=1001000&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheFactoryContext.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheFactoryContext.java
Fri Sep 24 17:59:07 2010
@@ -19,11 +19,18 @@
 package org.apache.bval.jsr303;
 
 import org.apache.bval.*;
+import org.apache.bval.jsr303.util.SecureActions;
+import org.apache.bval.util.PrivilegedActions;
 import org.apache.bval.xml.XMLMetaBeanBuilder;
 import org.apache.bval.xml.XMLMetaBeanFactory;
 import org.apache.bval.xml.XMLMetaBeanManager;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.reflect.ConstructorUtils;
 
 import javax.validation.*;
+
+import java.lang.reflect.Constructor;
+import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -147,18 +154,53 @@ public class ApacheFactoryContext implem
     }
 
     /**
-     * Create MetaBeanManager that uses JSR303-XML + JSR303-Annotations to build
-     * meta-data from.
+     * Create MetaBeanManager that uses factories:
+     * <ol>
+     * <li>if enabled by
+     * {@link ApacheValidatorConfiguration.Properties#ENABLE_INTROSPECTOR}, an
+     * {@link IntrospectorMetaBeanFactory}</li>
+     * <li>{@link MetaBeanFactory} types (if any) specified by
+     * {@link ApacheValidatorConfiguration.Properties#METABEAN_FACTORY_CLASSNAMES}
+     * </li>
+     * <li>if no {@link Jsr303MetaBeanFactory} has yet been specified (this
+     * allows factory order customization), a {@link Jsr303MetaBeanFactory}
+     * which handles both JSR303-XML and JSR303-Annotations</li>
+     * <li>if enabled by
+     * {@link ApacheValidatorConfiguration.Properties#ENABLE_METABEANS_XML}, an
+     * {@link XMLMetaBeanFactory}</li>
+     * </ol>
      * 
      * @return a new instance of MetaBeanManager with adequate MetaBeanFactories
      */
+    @SuppressWarnings("deprecation")
     protected MetaBeanFinder buildMetaBeanFinder() {
-        List<MetaBeanFactory> builders = new ArrayList<MetaBeanFactory>(2);
+        List<MetaBeanFactory> builders = new ArrayList<MetaBeanFactory>();
         if (Boolean.parseBoolean(factory.getProperties().get(
             ApacheValidatorConfiguration.Properties.ENABLE_INTROSPECTOR))) {
             builders.add(new IntrospectorMetaBeanFactory());
         }
-        builders.add(new Jsr303MetaBeanFactory(this));
+        String[] factoryClassNames =
+            StringUtils.split(
+                factory.getProperties().get(ApacheValidatorConfiguration.Properties.METABEAN_FACTORY_CLASSNAMES),
", ");
+        if (factoryClassNames != null) {
+            for (String clsName : factoryClassNames) {
+                @SuppressWarnings("unchecked")
+                Class<? extends MetaBeanFactory> factoryClass =
+                    (Class<? extends MetaBeanFactory>) SecureActions.loadClass(clsName,
getClass());
+                builders.add(createMetaBeanFactory(factoryClass));
+            }
+        }
+        boolean jsr303Found = false;
+        for (MetaBeanFactory builder : builders) {
+            jsr303Found |= builder instanceof Jsr303MetaBeanFactory;
+        }
+        if (!jsr303Found) {
+            builders.add(new Jsr303MetaBeanFactory(this));
+        }
+        if (Boolean.parseBoolean(factory.getProperties().get(
+            ApacheValidatorConfiguration.Properties.ENABLE_METABEANS_XML))) {
+            XMLMetaBeanManagerCreator.addFactory(builders);
+        }
         return createMetaBeanManager(builders);
     }
 
@@ -179,6 +221,27 @@ public class ApacheFactoryContext implem
         return new MetaBeanManager(new MetaBeanBuilder(builders.toArray(new MetaBeanFactory[builders.size()])));
     }
 
+    private <F extends MetaBeanFactory> F createMetaBeanFactory(final Class<F>
cls) {
+        return PrivilegedActions.run(new PrivilegedAction<F>() {
+
+            public F run() {
+                try {
+                    Constructor<F> c = ConstructorUtils.getMatchingAccessibleConstructor(cls,
ApacheFactoryContext.this.getClass());
+                    if (c != null) {
+                        return c.newInstance(ApacheFactoryContext.this);
+                    }
+                    c = ConstructorUtils.getMatchingAccessibleConstructor(cls, getFactory().getClass());
+                    if (c != null) {
+                        return c.newInstance(getFactory());
+                    }
+                    return cls.newInstance();
+                } catch (Exception e) {
+                    throw new ValidationException(e);
+                }
+            }
+        });
+    }
+
     /**
      * separate class to prevent the classloader to immediately load optional
      * classes: XMLMetaBeanManager, XMLMetaBeanFactory, XMLMetaBeanBuilder that
@@ -186,6 +249,10 @@ public class ApacheFactoryContext implem
      */
     private static class XMLMetaBeanManagerCreator {
 
+        static void addFactory(List<MetaBeanFactory> builders) {
+            builders.add(new XMLMetaBeanFactory());
+        }
+
         /**
          * Create the {@link MetaBeanManager} to process JSR303 XML. Requires
          * bval-xstream at RT.
@@ -197,7 +264,6 @@ public class ApacheFactoryContext implem
         // keep
         // bval-xstream an optional module.
         protected static MetaBeanManager createXMLMetaBeanManager(List<MetaBeanFactory>
builders) {
-            builders.add(new XMLMetaBeanFactory());
             return new XMLMetaBeanManager(
                 new XMLMetaBeanBuilder(builders.toArray(new MetaBeanFactory[builders.size()])));
         }

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorConfiguration.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorConfiguration.java?rev=1001000&r1=1000999&r2=1001000&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorConfiguration.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorConfiguration.java
Fri Sep 24 17:59:07 2010
@@ -19,6 +19,7 @@
 package org.apache.bval.jsr303;
 
 import javax.validation.Configuration;
+import javax.validation.ValidatorContext;
 import javax.validation.ValidatorFactory;
 import javax.validation.spi.ConfigurationState;
 
@@ -72,5 +73,19 @@ public interface ApacheValidatorConfigur
          * {@link ConfigurationState} argument.
          */
          String VALIDATOR_FACTORY_CLASSNAME = "apache.bval.validator-factory-classname";
+
+        /**
+         * Specifies, delimited by whitespace/commas, the names of
+         * {@link MetaBeanFactory} classes that should be added to collaborate
+         * with an {@link ApacheFactoryContext}'s {@link MetaBeanFinder}. These
+         * are instantiated per {@link ValidatorContext}, attempting to use
+         * constructor arguments of decreasing specificity:
+         * <ol>
+         * <li>assignable from the creating {@link ApacheFactoryContext}</li>
+         * <li>assignable from the associated {@link ApacheValidatorFactory}</li>
+         * <li>default (no-args) constructor</li>
+         * </ol>
+         */
+         String METABEAN_FACTORY_CLASSNAMES = "apache.bval.metabean-factory-classnames";
     }
 }



Mime
View raw message