You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/09/15 05:34:42 UTC

[1/8] git commit: Wait to throw ConfigurationException until the end.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 64364232d -> cb63a15ea


Wait to throw ConfigurationException until the end.

  - Allows for all the validators to output an error message instead of just the first one.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/84e7fed6
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/84e7fed6
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/84e7fed6

Branch: refs/heads/master
Commit: 84e7fed68a6e0dea715461a26e1a973811758245
Parents: 6436423
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 20:24:39 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 20:24:39 2014 -0500

----------------------------------------------------------------------
 .../log4j/core/config/plugins/util/PluginBuilder.java   | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/84e7fed6/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
index ae8ccae..3826c56 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
@@ -170,6 +170,7 @@ public class PluginBuilder<T> implements Builder<T> {
         final Field[] fields = builder.getClass().getDeclaredFields();
         AccessibleObject.setAccessible(fields, true);
         final StringBuilder log = new StringBuilder();
+        boolean invalid = false;
         for (final Field field : fields) {
             log.append(log.length() == 0 ? "with params(" : ", ");
             final Annotation[] annotations = field.getDeclaredAnnotations();
@@ -198,7 +199,7 @@ public class PluginBuilder<T> implements Builder<T> {
             final Object value = field.get(builder);
             for (ConstraintValidator<Annotation, Object> validator : validators) {
                 if (!validator.isValid(value)) {
-                    throw new ConfigurationException("Invalid value [" + value + "] for field " + field.getName());
+                    invalid = true;
                 }
             }
         }
@@ -207,6 +208,9 @@ public class PluginBuilder<T> implements Builder<T> {
         }
         LOGGER.debug("Calling build() on class {} for element {} {}", builder.getClass(), node.getName(),
             log.toString());
+        if (invalid) {
+            throw new ConfigurationException("Arguments given for element " + node.getName() + " are invalid");
+        }
         checkForRemainingAttributes();
         verifyNodeChildrenUsed();
     }
@@ -228,6 +232,7 @@ public class PluginBuilder<T> implements Builder<T> {
         final Class<?>[] types = factory.getParameterTypes();
         final Annotation[][] annotations = factory.getParameterAnnotations();
         final Object[] args = new Object[annotations.length];
+        boolean invalid = false;
         for (int i = 0; i < annotations.length; i++) {
             log.append(log.length() == 0 ? "with params(" : ", ");
             final String[] aliases = extractPluginAliases(annotations[i]);
@@ -255,7 +260,7 @@ public class PluginBuilder<T> implements Builder<T> {
             final Object value = args[i];
             for (final ConstraintValidator<Annotation, Object> validator : validators) {
                 if (!validator.isValid(value)) {
-                    throw new ConfigurationException("Invalid value [" + value + "] for parameter " + i);
+                    invalid = true;
                 }
             }
         }
@@ -266,6 +271,9 @@ public class PluginBuilder<T> implements Builder<T> {
         verifyNodeChildrenUsed();
         LOGGER.debug("Calling {} on class {} for element {} {}", factory.getName(), clazz.getName(), node.getName(),
             log.toString());
+        if (invalid) {
+            throw new ConfigurationException("Arguments given for element " + node.getName() + " are invalid");
+        }
         return args;
     }
 


[3/8] git commit: Make line less dense.

Posted by ma...@apache.org.
Make line less dense.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/14dc8577
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/14dc8577
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/14dc8577

Branch: refs/heads/master
Commit: 14dc85772bef5f4a9f1764e794e17ae0f6f7c965
Parents: 20e739a
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 21:58:53 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 21:58:53 2014 -0500

----------------------------------------------------------------------
 .../core/config/plugins/convert/TypeConverterRegistry.java     | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/14dc8577/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverterRegistry.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverterRegistry.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverterRegistry.java
index dc9f9a6..e9a9576 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverterRegistry.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverterRegistry.java
@@ -110,10 +110,10 @@ public class TypeConverterRegistry {
 
     private void loadKnownTypeConverters(final Collection<PluginType<?>> knownTypes) {
         for (final PluginType<?> knownType : knownTypes) {
-            if (TypeConverter.class.isAssignableFrom(knownType.getPluginClass())) {
+            final Class<?> clazz = knownType.getPluginClass();
+            if (TypeConverter.class.isAssignableFrom(clazz)) {
                 @SuppressWarnings("unchecked") // but we just DID check it!
-                final Class<? extends TypeConverter<?>> pluginClass =
-                    (Class<? extends TypeConverter<?>>) knownType.getPluginClass();
+                final Class<? extends TypeConverter<?>> pluginClass = (Class<? extends TypeConverter<?>>) clazz;
                 final Type conversionType = getTypeConverterSupportedType(pluginClass);
                 final TypeConverter<?> converter = ReflectionUtil.instantiate(pluginClass);
                 if (registry.putIfAbsent(conversionType, converter) != null) {


[2/8] git commit: Add validation message to @RequiresNonNull.

Posted by ma...@apache.org.
Add validation message to @RequiresNonNull.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/20e739a5
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/20e739a5
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/20e739a5

Branch: refs/heads/master
Commit: 20e739a5308023e4f5b03a49ea12f473193dcbea
Parents: 84e7fed
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 20:26:48 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 20:26:48 2014 -0500

----------------------------------------------------------------------
 .../validation/constraints/RequiresNonNull.java       |  5 +++++
 .../validators/RequiresNonNullValidator.java          | 14 +++++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/20e739a5/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/RequiresNonNull.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/RequiresNonNull.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/RequiresNonNull.java
index b3ef11d..7eb83e2 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/RequiresNonNull.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/RequiresNonNull.java
@@ -36,4 +36,9 @@ import org.apache.logging.log4j.core.config.plugins.validation.validators.Requir
 @Target({ElementType.FIELD, ElementType.PARAMETER})
 @Constraint(RequiresNonNullValidator.class)
 public @interface RequiresNonNull {
+
+    /**
+     * The message to be logged if this constraint is violated. This should normally be overridden.
+     */
+    String message() default "The parameter is null";
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/20e739a5/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java
index 49c5806..9a39d4d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java
@@ -16,8 +16,10 @@
  */
 package org.apache.logging.log4j.core.config.plugins.validation.validators;
 
+import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
 import org.apache.logging.log4j.core.config.plugins.validation.constraints.RequiresNonNull;
+import org.apache.logging.log4j.status.StatusLogger;
 
 /**
  * Validator implementation for {@link RequiresNonNull}.
@@ -25,12 +27,22 @@ import org.apache.logging.log4j.core.config.plugins.validation.constraints.Requi
  * @since 2.1
  */
 public class RequiresNonNullValidator implements ConstraintValidator<RequiresNonNull, Object> {
+
+    private static final Logger LOGGER = StatusLogger.getLogger();
+
+    private RequiresNonNull annotation;
+
     @Override
     public void initialize(final RequiresNonNull annotation) {
+        this.annotation = annotation;
     }
 
     @Override
     public boolean isValid(final Object value) {
-        return value != null;
+        if (value != null) {
+            return true;
+        }
+        LOGGER.error(annotation.message());
+        return false;
     }
 }


[7/8] git commit: Add validation message overrides.

Posted by ma...@apache.org.
Add validation message overrides.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/8eebb063
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/8eebb063
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/8eebb063

Branch: refs/heads/master
Commit: 8eebb063dcf9bedf8ea8b75b53e0a7d2d78ead72
Parents: cf43135
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 22:31:01 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 22:31:01 2014 -0500

----------------------------------------------------------------------
 .../log4j/core/config/plugins/validation/ValidatingPlugin.java  | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8eebb063/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPlugin.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPlugin.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPlugin.java
index 6bf3f40..71bfbcc 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPlugin.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPlugin.java
@@ -40,7 +40,8 @@ public class ValidatingPlugin {
     }
 
     @PluginFactory
-    public static ValidatingPlugin newValidatingPlugin(@RequiresNonNull final String name) {
+    public static ValidatingPlugin newValidatingPlugin(
+        @RequiresNonNull(message = "The name given by the factory is null") final String name) {
         return new ValidatingPlugin(name);
     }
 
@@ -52,7 +53,7 @@ public class ValidatingPlugin {
     public static class Builder implements org.apache.logging.log4j.core.util.Builder<ValidatingPlugin> {
 
         @PluginBuilderAttribute
-        @RequiresNonNull
+        @RequiresNonNull(message = "The name given by the builder is null")
         private String name;
 
         public Builder setName(final String name) {


[5/8] git commit: Remove extra type parameter from ConstraintValidator.

Posted by ma...@apache.org.
Remove extra type parameter from ConstraintValidator.


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

Branch: refs/heads/master
Commit: dee7e9a13a16d55fccc609735f140efb9f199518
Parents: c666984
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 22:28:33 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 22:28:33 2014 -0500

----------------------------------------------------------------------
 .../log4j/core/config/plugins/util/PluginBuilder.java        | 8 ++++----
 .../log4j/core/config/plugins/validation/Constraint.java     | 2 +-
 .../core/config/plugins/validation/ConstraintValidator.java  | 5 ++---
 .../validation/validators/RequiresNonNullValidator.java      | 2 +-
 4 files changed, 8 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/dee7e9a1/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
index 3826c56..7d2f5bb 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/PluginBuilder.java
@@ -194,10 +194,10 @@ public class PluginBuilder<T> implements Builder<T> {
                     }
                 }
             }
-            final Collection<ConstraintValidator<Annotation, Object>> validators =
+            final Collection<ConstraintValidator<?>> validators =
                 ConstraintValidators.findValidators(annotations);
             final Object value = field.get(builder);
-            for (ConstraintValidator<Annotation, Object> validator : validators) {
+            for (ConstraintValidator<?> validator : validators) {
                 if (!validator.isValid(value)) {
                     invalid = true;
                 }
@@ -255,10 +255,10 @@ public class PluginBuilder<T> implements Builder<T> {
                     }
                 }
             }
-            final Collection<ConstraintValidator<Annotation, Object>> validators =
+            final Collection<ConstraintValidator<?>> validators =
                 ConstraintValidators.findValidators(annotations[i]);
             final Object value = args[i];
-            for (final ConstraintValidator<Annotation, Object> validator : validators) {
+            for (final ConstraintValidator<?> validator : validators) {
                 if (!validator.isValid(value)) {
                     invalid = true;
                 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/dee7e9a1/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/Constraint.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/Constraint.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/Constraint.java
index 12355c2..0ac2223 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/Constraint.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/Constraint.java
@@ -37,5 +37,5 @@ public @interface Constraint {
     /**
      * {@link ConstraintValidator} class that implements the validation logic for the annotated constraint annotation.
      */
-    Class<? extends ConstraintValidator<? extends Annotation, ?>> value();
+    Class<? extends ConstraintValidator<? extends Annotation>> value();
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/dee7e9a1/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidator.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidator.java
index 12a50c0..f94be5e 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidator.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidator.java
@@ -22,10 +22,9 @@ import java.lang.annotation.Annotation;
  * Interface that {@link Constraint} annotations must implement to perform validation logic.
  *
  * @param <A> the {@link Constraint} annotation this interface validates.
- * @param <T> the type of the object being validated
  * @since 2.1
  */
-public interface ConstraintValidator<A extends Annotation, T> {
+public interface ConstraintValidator<A extends Annotation> {
 
     /**
      * Called before this validator is used with the constraint annotation value.
@@ -40,5 +39,5 @@ public interface ConstraintValidator<A extends Annotation, T> {
      * @param value the value to validate.
      * @return {@code true} if the given value is valid.
      */
-    boolean isValid(T value);
+    boolean isValid(Object value);
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/dee7e9a1/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java
index 9a39d4d..13c926d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java
@@ -26,7 +26,7 @@ import org.apache.logging.log4j.status.StatusLogger;
  *
  * @since 2.1
  */
-public class RequiresNonNullValidator implements ConstraintValidator<RequiresNonNull, Object> {
+public class RequiresNonNullValidator implements ConstraintValidator<RequiresNonNull> {
 
     private static final Logger LOGGER = StatusLogger.getLogger();
 


[8/8] git commit: Add @RequiresNonEmpty annotation.

Posted by ma...@apache.org.
Add @RequiresNonEmpty annotation.


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

Branch: refs/heads/master
Commit: cb63a15eaedb8273479cf8adc7beb080537edddc
Parents: 8eebb06
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 22:34:27 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 22:34:27 2014 -0500

----------------------------------------------------------------------
 .../constraints/RequiresNonEmpty.java           | 44 +++++++++++
 .../validators/RequiresNonEmptyValidator.java   | 80 ++++++++++++++++++++
 2 files changed, 124 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/cb63a15e/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/RequiresNonEmpty.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/RequiresNonEmpty.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/RequiresNonEmpty.java
new file mode 100644
index 0000000..116aa9c
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/RequiresNonEmpty.java
@@ -0,0 +1,44 @@
+/*
+ * 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.logging.log4j.core.config.plugins.validation.constraints;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
+import org.apache.logging.log4j.core.config.plugins.validation.validators.RequiresNonEmptyValidator;
+
+/**
+ * Constraint to mark a field or parameter as requiring a non-empty value.
+ *
+ * @see RequiresNonEmptyValidator
+ * @since 2.1
+ */
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+@Constraint(RequiresNonEmptyValidator.class)
+public @interface RequiresNonEmpty {
+
+    /**
+     * The message to be logged if this constraint is violated. This should normally be overridden.
+     */
+    String message() default "The parameter is empty";
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/cb63a15e/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonEmptyValidator.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonEmptyValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonEmptyValidator.java
new file mode 100644
index 0000000..d8e8959
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonEmptyValidator.java
@@ -0,0 +1,80 @@
+/*
+ * 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.logging.log4j.core.config.plugins.validation.validators;
+
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
+import org.apache.logging.log4j.core.config.plugins.validation.constraints.RequiresNonEmpty;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ * Validator that checks an object for emptiness. Emptiness is defined here as:
+ * <ul>
+ * <li>The value {@code null}</li>
+ * <li>An object of type {@link CharSequence} with length 0</li>
+ * <li>An empty array</li>
+ * <li>An empty {@link Collection}</li>
+ * <li>An empty {@link Map}</li>
+ * </ul>
+ *
+ * @since 2.1
+ */
+public class RequiresNonEmptyValidator implements ConstraintValidator<RequiresNonEmpty> {
+
+    private static final Logger LOGGER = StatusLogger.getLogger();
+
+    private RequiresNonEmpty annotation;
+
+    @Override
+    public void initialize(final RequiresNonEmpty annotation) {
+        this.annotation = annotation;
+    }
+
+    @Override
+    public boolean isValid(final Object value) {
+        if (value == null) {
+            return err();
+        }
+        if (value instanceof CharSequence) {
+            final CharSequence sequence = (CharSequence) value;
+            return sequence.length() != 0 || err();
+        }
+        final Class<?> clazz = value.getClass();
+        if (clazz.isArray()) {
+            final Object[] array = (Object[]) value;
+            return array.length != 0 || err();
+        }
+        if (Collection.class.isAssignableFrom(clazz)) {
+            final Collection<?> collection = (Collection<?>) value;
+            return collection.size() != 0 || err();
+        }
+        if (Map.class.isAssignableFrom(clazz)) {
+            final Map<?, ?> map = (Map<?, ?>) value;
+            return map.size() != 0 || err();
+        }
+        LOGGER.warn("RequiresNonEmpty annotation applied to type [{}] that is unsupported.", clazz.getName());
+        return true;
+    }
+
+    private boolean err() {
+        LOGGER.error(annotation.message());
+        return false;
+    }
+}


[4/8] git commit: Enum generics dance updates.

Posted by ma...@apache.org.
Enum generics dance updates.


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

Branch: refs/heads/master
Commit: c6669849d030711ee6989b5ef5ec24d668653611
Parents: 14dc857
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 22:25:28 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 22:25:28 2014 -0500

----------------------------------------------------------------------
 .../config/plugins/convert/TypeConverterRegistry.java   | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c6669849/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverterRegistry.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverterRegistry.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverterRegistry.java
index e9a9576..47319e7 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverterRegistry.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverterRegistry.java
@@ -81,11 +81,8 @@ public class TypeConverterRegistry {
             return primary;
         }
         // dynamic enum support
-        if (type instanceof Class<?>) {
-            final Class<?> clazz = (Class<?>) type;
-            if (clazz.isEnum()) {
-                return registerEnumType(clazz.asSubclass(Enum.class));
-            }
+        if (type instanceof Class<?> && ((Class<?>) type).isEnum()) {
+            return registerEnumType(type);
         }
         // look for compatible converters
         for (final Map.Entry<Type, TypeConverter<?>> entry : registry.entrySet()) {
@@ -152,8 +149,9 @@ public class TypeConverterRegistry {
         registry.putIfAbsent(aliasType, registry.get(knownType));
     }
 
-    private <E extends Enum<E>> TypeConverter<E> registerEnumType(final Class<E> type) {
-        final TypeConverter<E> converter = new EnumConverter<E>(type);
+    private <E extends Enum<E>> TypeConverter<? extends E> registerEnumType(final Type type) {
+        @SuppressWarnings("unchecked")
+        final TypeConverter<E> converter = new EnumConverter<E>((Class<E>) type);
         registry.putIfAbsent(type, converter);
         return converter;
     }


[6/8] git commit: Drop unnecessary type param and improve type safety.

Posted by ma...@apache.org.
Drop unnecessary type param and improve type safety.

  - Performs some reflection on generics to verify the ConstraintValidator defined for a constraint annotation is appropriate for said annotation.


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

Branch: refs/heads/master
Commit: cf43135deef25c6a6329cf6ceba14ee343c64169
Parents: dee7e9a
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 22:30:30 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 22:30:30 2014 -0500

----------------------------------------------------------------------
 .../validation/ConstraintValidators.java        | 51 ++++++++++++--------
 1 file changed, 30 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/cf43135d/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidators.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidators.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidators.java
index 028f708..2429634 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidators.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidators.java
@@ -17,11 +17,12 @@
 package org.apache.logging.log4j.core.config.plugins.validation;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Collection;
 
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.core.util.ReflectionUtil;
 
 /**
  * Utility class to locate an appropriate {@link ConstraintValidator} implementation for an annotation.
@@ -30,8 +31,6 @@ import org.apache.logging.log4j.status.StatusLogger;
  */
 public final class ConstraintValidators {
 
-    private static final Logger LOGGER = StatusLogger.getLogger();
-
     private ConstraintValidators() {
     }
 
@@ -42,14 +41,13 @@ public final class ConstraintValidators {
      * @param annotations the annotations to find constraint validators for
      * @return a collection of ConstraintValidators for the given annotations
      */
-    public static Collection<ConstraintValidator<Annotation, Object>> findValidators(
-        final Annotation... annotations) {
-        final Collection<ConstraintValidator<Annotation, Object>> validators =
-            new ArrayList<ConstraintValidator<Annotation, Object>>();
+    public static Collection<ConstraintValidator<?>> findValidators(final Annotation... annotations) {
+        final Collection<ConstraintValidator<?>> validators =
+            new ArrayList<ConstraintValidator<?>>();
         for (final Annotation annotation : annotations) {
-            final Constraint constraint = annotation.annotationType().getAnnotation(Constraint.class);
-            if (constraint != null) {
-                final ConstraintValidator<Annotation, Object> validator = getValidatorFor(annotation, constraint);
+            final Class<? extends Annotation> type = annotation.annotationType();
+            if (type.isAnnotationPresent(Constraint.class)) {
+                final ConstraintValidator<?> validator = getValidator(annotation, type);
                 if (validator != null) {
                     validators.add(validator);
                 }
@@ -58,18 +56,29 @@ public final class ConstraintValidators {
         return validators;
     }
 
-    private static ConstraintValidator<Annotation, Object> getValidatorFor(final Annotation annotation,
-                                                                           final Constraint constraint) {
-        try {
-            // TODO: may want to cache these validator instances
-            @SuppressWarnings("unchecked")
-            final ConstraintValidator<Annotation, Object> validator =
-                (ConstraintValidator<Annotation, Object>) constraint.value().newInstance();
+    private static <A extends Annotation> ConstraintValidator<A> getValidator(final A annotation,
+                                                                              final Class<? extends A> type) {
+        final Constraint constraint = type.getAnnotation(Constraint.class);
+        final Class<? extends ConstraintValidator<?>> validatorClass = constraint.value();
+        if (type.equals(getConstraintValidatorAnnotationType(validatorClass))) {
+            @SuppressWarnings("unchecked") // I don't think we could be any more thorough in validation here
+            final ConstraintValidator<A> validator = (ConstraintValidator<A>)
+                ReflectionUtil.instantiate(validatorClass);
             validator.initialize(annotation);
             return validator;
-        } catch (final Exception e) {
-            LOGGER.error("Error loading ConstraintValidator [{}].", constraint.value(), e);
-            return null;
         }
+        return null;
+    }
+
+    private static Type getConstraintValidatorAnnotationType(final Class<? extends ConstraintValidator<?>> type) {
+        for (final Type parentType : type.getGenericInterfaces()) {
+            if (parentType instanceof ParameterizedType) {
+                final ParameterizedType parameterizedType = (ParameterizedType) parentType;
+                if (ConstraintValidator.class.equals(parameterizedType.getRawType())) {
+                    return parameterizedType.getActualTypeArguments()[0];
+                }
+            }
+        }
+        return Void.TYPE;
     }
 }