camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hekon...@apache.org
Subject git commit: [bean-validator] Refactoring.
Date Mon, 24 Feb 2014 11:05:57 GMT
Repository: camel
Updated Branches:
  refs/heads/master ae4192246 -> 91e707837


[bean-validator] Refactoring.


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

Branch: refs/heads/master
Commit: 91e707837a09da94f6bf997043dc17df166484da
Parents: ae41922
Author: Henryk Konsek <hekonsek@gmail.com>
Authored: Mon Feb 24 12:05:36 2014 +0100
Committer: Henryk Konsek <hekonsek@gmail.com>
Committed: Mon Feb 24 12:05:36 2014 +0100

----------------------------------------------------------------------
 .../bean/validator/BeanValidatorComponent.java  | 61 +++----------
 .../bean/validator/ValidatorFactories.java      | 91 ++++++++++++++++++++
 2 files changed, 101 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/91e70783/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorComponent.java
b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorComponent.java
index c50d3b4..a70ab2e 100644
--- a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorComponent.java
+++ b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorComponent.java
@@ -18,20 +18,19 @@ package org.apache.camel.component.bean.validator;
 
 import java.util.Map;
 
-import javax.validation.Configuration;
 import javax.validation.ConstraintValidatorFactory;
 import javax.validation.MessageInterpolator;
 import javax.validation.TraversableResolver;
-import javax.validation.Validation;
 import javax.validation.ValidationProviderResolver;
 import javax.validation.ValidatorFactory;
-import javax.validation.bootstrap.GenericBootstrap;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.impl.ProcessorEndpoint;
 import org.apache.camel.util.PlatformHelper;
 
+import static org.apache.camel.component.bean.validator.ValidatorFactories.buildValidatorFactory;
+
 /**
  * Bean Validator Component for validating Java beans against reference implementation of
JSR 303 Validator (Hibernate
  * Validator).
@@ -42,33 +41,15 @@ public class BeanValidatorComponent extends DefaultComponent {
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
         BeanValidator beanValidator = new BeanValidator();
 
-        ValidationProviderResolver validationProviderResolver = resolveValidationProviderResolver(parameters);
-        MessageInterpolator messageInterpolator = resolveAndRemoveReferenceParameter(parameters,
"messageInterpolator", MessageInterpolator.class);
-        TraversableResolver traversableResolver = resolveAndRemoveReferenceParameter(parameters,
"traversableResolver", TraversableResolver.class);
-        ConstraintValidatorFactory constraintValidatorFactory = resolveAndRemoveReferenceParameter(parameters,
"constraintValidatorFactory", ConstraintValidatorFactory.class);
-        String group = getAndRemoveParameter(parameters, "group", String.class);
-        
-        GenericBootstrap bootstrap = Validation.byDefaultProvider();
-        if (validationProviderResolver != null) {
-            bootstrap.providerResolver(validationProviderResolver);
-        }
-        Configuration<?> configuration = bootstrap.configure();
-
-        if (messageInterpolator != null) {
-            configuration.messageInterpolator(messageInterpolator);
-        }
-        
-        if (traversableResolver != null) {
-            configuration.traversableResolver(traversableResolver);
-        }
-        
-        if (constraintValidatorFactory != null) {
-            configuration.constraintValidatorFactory(constraintValidatorFactory);       
    
-        }
-        
-        ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
+        ValidatorFactory validatorFactory = buildValidatorFactory(
+                isOsgiContext(),
+                resolveAndRemoveReferenceParameter(parameters, "validationProviderResolver",
ValidationProviderResolver.class),
+                resolveAndRemoveReferenceParameter(parameters, "messageInterpolator", MessageInterpolator.class),
+                resolveAndRemoveReferenceParameter(parameters, "traversableResolver", TraversableResolver.class),
+                resolveAndRemoveReferenceParameter(parameters, "constraintValidatorFactory",
ConstraintValidatorFactory.class));
         beanValidator.setValidatorFactory(validatorFactory);
-        
+
+        String group = getAndRemoveParameter(parameters, "group", String.class);
         if (group != null) {
             beanValidator.setGroup(getCamelContext().getClassResolver().resolveMandatoryClass(group));
         }
@@ -77,28 +58,6 @@ public class BeanValidatorComponent extends DefaultComponent {
     }
 
     /**
-     * Resolves optional custom {@code javax.validation.ValidationProviderResolver} to be
used by the component. By
-     * default component tries to use resolver instance bound to the Camel registry under
name
-     * {@code validationProviderResolver} . If there is no such resolver instance in the
registry and component is
-     * running in the OSGi environment, {@link HibernateValidationProviderResolver} will
be used. In all the other
-     * cases this method will return null.
-     *
-     * @param parameters endpoint parameters
-     * @return {@code javax.validation.ValidationProviderResolver} instance or null if no
custom resolver should
-     * be used by the component
-     */
-    protected ValidationProviderResolver resolveValidationProviderResolver(Map<String,
Object> parameters) {
-        ValidationProviderResolver validationProviderResolver = resolveAndRemoveReferenceParameter(parameters,
"validationProviderResolver", ValidationProviderResolver.class);
-        if (validationProviderResolver != null) {
-            return validationProviderResolver;
-        }
-        if (isOsgiContext()) {
-            return new HibernateValidationProviderResolver();
-        }
-        return null;
-    }
-
-    /**
      * Recognizes if component is executed in the OSGi environment.
      *
      * @return true if component is executed in the OSGi environment. False otherwise.

http://git-wip-us.apache.org/repos/asf/camel/blob/91e70783/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/ValidatorFactories.java
----------------------------------------------------------------------
diff --git a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/ValidatorFactories.java
b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/ValidatorFactories.java
new file mode 100644
index 0000000..4aefbb0
--- /dev/null
+++ b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/ValidatorFactories.java
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean.validator;
+
+import javax.validation.Configuration;
+import javax.validation.ConstraintValidatorFactory;
+import javax.validation.MessageInterpolator;
+import javax.validation.TraversableResolver;
+import javax.validation.Validation;
+import javax.validation.ValidationProviderResolver;
+import javax.validation.ValidatorFactory;
+import javax.validation.bootstrap.GenericBootstrap;
+
+/**
+ * Utility class dedicated to create new {@code javax.validation.ValidatorFactory} instances.
+ */
+public final class ValidatorFactories {
+
+    private ValidatorFactories() {
+    }
+
+    public static ValidatorFactory buildValidatorFactory(boolean osgi,
+                                                         ValidationProviderResolver validationProviderResolver,
+                                                         MessageInterpolator messageInterpolator,
+                                                         TraversableResolver traversableResolver,
+                                                         ConstraintValidatorFactory constraintValidatorFactory)
{
+
+        ValidationProviderResolver resolvedValidationProviderResolver =
+                resolveValidationProviderResolver(osgi, validationProviderResolver);
+
+        GenericBootstrap bootstrap = Validation.byDefaultProvider();
+        if (resolvedValidationProviderResolver != null) {
+            bootstrap.providerResolver(resolvedValidationProviderResolver);
+        }
+        Configuration<?> configuration = bootstrap.configure();
+
+        if (messageInterpolator != null) {
+            configuration.messageInterpolator(messageInterpolator);
+        }
+
+        if (traversableResolver != null) {
+            configuration.traversableResolver(traversableResolver);
+        }
+
+        if (constraintValidatorFactory != null) {
+            configuration.constraintValidatorFactory(constraintValidatorFactory);
+        }
+
+        return configuration.buildValidatorFactory();
+    }
+
+    /**
+     * Resolves optional custom {@code javax.validation.ValidationProviderResolver} to be
used by the component. By
+     * default component tries to use resolver instance bound to the Camel registry under
name
+     * {@code validationProviderResolver} . If there is no such resolver instance in the
registry and component is
+     * running in the OSGi environment, {@link HibernateValidationProviderResolver} will
be used. In all the other
+     * cases this method will return null.
+     *
+     * @param osgi specifies if validator factory should be OSGi-aware
+     * @param validationProviderResolver predefined provider resolver. This parameter overrides
the results of the
+     *                                   resolution.
+     * @return {@code javax.validation.ValidationProviderResolver} instance or null if no
custom resolver should
+     * be used by the component
+     */
+    private static ValidationProviderResolver resolveValidationProviderResolver(
+            boolean osgi,
+            ValidationProviderResolver validationProviderResolver) {
+        if (validationProviderResolver != null) {
+            return validationProviderResolver;
+        }
+        if (osgi) {
+            return new HibernateValidationProviderResolver();
+        }
+        return null;
+    }
+
+}


Mime
View raw message