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/14 07:01:19 UTC

[01/15] git commit: Add more log4j modules in dependencyManagement section.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master c16523762 -> 5533cae78


Add more log4j modules in dependencyManagement section.


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

Branch: refs/heads/master
Commit: b563ffeb702dfda7767de29d8ee46a9eb57656cc
Parents: c165237
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 00:41:14 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 00:41:14 2014 -0500

----------------------------------------------------------------------
 log4j-taglib/pom.xml |  5 +++++
 pom.xml              | 25 +++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b563ffeb/log4j-taglib/pom.xml
----------------------------------------------------------------------
diff --git a/log4j-taglib/pom.xml b/log4j-taglib/pom.xml
index 80c8846..54db02b 100644
--- a/log4j-taglib/pom.xml
+++ b/log4j-taglib/pom.xml
@@ -38,6 +38,11 @@
       <artifactId>log4j-api</artifactId>
     </dependency>
     <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-web</artifactId>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <version>2.5</version>

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b563ffeb/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index c0d83b5..24781ca 100644
--- a/pom.xml
+++ b/pom.xml
@@ -298,6 +298,31 @@
         <version>${project.version}</version>
       </dependency>
       <dependency>
+        <groupId>org.apache.logging.log4j</groupId>
+        <artifactId>log4j-flume-ng</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.logging.log4j</groupId>
+        <artifactId>log4j-iostreams</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.logging.log4j</groupId>
+        <artifactId>log4j-jul</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.logging.log4j</groupId>
+        <artifactId>log4j-taglib</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.apache.logging.log4j</groupId>
+        <artifactId>log4j-web</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+      <dependency>
         <groupId>com.sleepycat</groupId>
         <artifactId>je</artifactId>
         <version>5.0.73</version>


[06/15] git commit: Narrow scope of warning suppression.

Posted by ma...@apache.org.
Narrow scope of warning suppression.


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

Branch: refs/heads/master
Commit: d1766fcb1325c299fe3193c6c8c93cf601c36af8
Parents: 2f4a72c
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 18:31:00 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 18:31:00 2014 -0500

----------------------------------------------------------------------
 .../logging/log4j/core/config/plugins/visitors/PluginVisitors.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1766fcb/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
index 4203442..1392c60 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
@@ -42,13 +42,13 @@ public final class PluginVisitors {
      * @param <A>        the Plugin annotation type.
      * @return a PluginVisitor instance if one could be created, or {@code null} otherwise.
      */
-    @SuppressWarnings("unchecked") // we're keeping track of types, thanks
     public static <A extends Annotation> PluginVisitor<A> findVisitor(final Class<A> annotation) {
         final PluginVisitorStrategy strategy = annotation.getAnnotation(PluginVisitorStrategy.class);
         if (strategy == null) {
             LOGGER.debug("No PluginVisitorStrategy found on annotation [{}]. Ignoring.", annotation);
             return null;
         }
+        @SuppressWarnings("unchecked")
         final Class<? extends PluginVisitor<A>> visitorClass = (Class<? extends PluginVisitor<A>>) strategy.value();
         try {
             return visitorClass.newInstance();


[08/15] git commit: Only use plugin visitor value if non-null.

Posted by ma...@apache.org.
Only use plugin visitor value if non-null.

  - Makes behavior in factory method version match up better with builder class version.
  - Allows for multiple annotations on a single item where only one of them may return a value (e.g., for LOG4J2-825).


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

Branch: refs/heads/master
Commit: 73a12aa34d4494f87df6a03a6465c020f54a777e
Parents: 6ce4687
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 19:56:01 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 19:56:01 2014 -0500

----------------------------------------------------------------------
 .../logging/log4j/core/config/plugins/util/PluginBuilder.java  | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/73a12aa3/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 ba84690..08a85ff 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
@@ -225,12 +225,16 @@ public class PluginBuilder<T> implements Builder<T> {
                 final PluginVisitor<? extends Annotation> visitor = PluginVisitors.findVisitor(
                     a.annotationType());
                 if (visitor != null) {
-                    args[i] = visitor.setAliases(aliases)
+                    final Object value = visitor.setAliases(aliases)
                         .setAnnotation(a)
                         .setConversionType(types[i])
                         .setStrSubstitutor(configuration.getStrSubstitutor())
                         .setMember(factory)
                         .visit(configuration, node, event, log);
+                    // don't overwrite existing values if the visitor gives us no value to inject
+                    if (value != null) {
+                        args[i] = value;
+                    }
                 }
             }
         }


[07/15] git commit: Add fallback visitor strategy lookup.

Posted by ma...@apache.org.
Add fallback visitor strategy lookup.

  - Weaken findVisitor() return type due to lack of concrete generics usage.
  - Look for an annotated annotation visitor strategy if none is found for the main one.
  - Part of API improvements for LOG4J2-825.


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

Branch: refs/heads/master
Commit: 6ce46875270233da329afaa65cd37cde4f421277
Parents: d1766fc
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 19:54:51 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 19:54:51 2014 -0500

----------------------------------------------------------------------
 .../config/plugins/visitors/PluginVisitors.java | 26 +++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6ce46875/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
index 1392c60..3ef3203 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
@@ -39,17 +39,14 @@ public final class PluginVisitors {
      * data to be useful. Such data is passed through both the setters and the visit method.
      *
      * @param annotation the Plugin annotation class to find a PluginVisitor for.
-     * @param <A>        the Plugin annotation type.
      * @return a PluginVisitor instance if one could be created, or {@code null} otherwise.
      */
-    public static <A extends Annotation> PluginVisitor<A> findVisitor(final Class<A> annotation) {
-        final PluginVisitorStrategy strategy = annotation.getAnnotation(PluginVisitorStrategy.class);
-        if (strategy == null) {
+    public static PluginVisitor<? extends Annotation> findVisitor(final Class<? extends Annotation> annotation) {
+        final Class<? extends PluginVisitor<? extends Annotation>> visitorClass = findVisitorStrategy(annotation);
+        if (visitorClass == null) {
             LOGGER.debug("No PluginVisitorStrategy found on annotation [{}]. Ignoring.", annotation);
             return null;
         }
-        @SuppressWarnings("unchecked")
-        final Class<? extends PluginVisitor<A>> visitorClass = (Class<? extends PluginVisitor<A>>) strategy.value();
         try {
             return visitorClass.newInstance();
         } catch (final Exception e) {
@@ -57,4 +54,21 @@ public final class PluginVisitors {
             return null;
         }
     }
+
+    private static Class<? extends PluginVisitor<? extends Annotation>> findVisitorStrategy(
+        final Class<? extends Annotation> annotation) {
+        final PluginVisitorStrategy strategy = annotation.getAnnotation(PluginVisitorStrategy.class);
+        if (strategy != null) {
+            return strategy.value();
+        }
+        final Annotation[] annotations = annotation.getDeclaredAnnotations();
+        for (final Annotation a : annotations) {
+            final PluginVisitorStrategy fallbackStrategy = a.annotationType().getAnnotation(
+                PluginVisitorStrategy.class);
+            if (fallbackStrategy != null) {
+                return fallbackStrategy.value();
+            }
+        }
+        return null;
+    }
 }


[05/15] git commit: Add ReflectionUtil class for reflection operations.

Posted by ma...@apache.org.
Add ReflectionUtil class for reflection operations.

  - Even though every AccessibleObject is also a Member, this is not reflected in the JDK; thus, generics need to be used.
  - Inspired by Spring: https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java


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

Branch: refs/heads/master
Commit: 2f4a72cbd1eb706e63244fe5af8bef69ce60568f
Parents: 5b36078
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 18:23:44 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 18:23:44 2014 -0500

----------------------------------------------------------------------
 .../logging/log4j/core/util/ReflectionUtil.java | 141 +++++++++++++++++++
 1 file changed, 141 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2f4a72cb/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ReflectionUtil.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ReflectionUtil.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ReflectionUtil.java
new file mode 100644
index 0000000..bdd913a
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ReflectionUtil.java
@@ -0,0 +1,141 @@
+/*
+ * 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.util;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
+import java.lang.reflect.Member;
+import java.lang.reflect.Modifier;
+
+/**
+ * Utility class for performing common reflective operations.
+ *
+ * @since 2.1
+ */
+public final class ReflectionUtil {
+    private ReflectionUtil() {
+    }
+
+    /**
+     * Indicates whether or not a {@link Member} is both public and is contained in a public class.
+     *
+     * @param member the Member to check for public accessibility (must not be {@code null}).
+     * @return {@code true} if {@code member} is public and contained in a public class.
+     * @throws NullPointerException if {@code member} is {@code null}.
+     */
+    public static <T extends AccessibleObject & Member> boolean isAccessible(final T member) {
+        Assert.requireNonNull(member, "No member provided");
+        return Modifier.isPublic(member.getModifiers()) && Modifier.isPublic(member.getDeclaringClass().getModifiers());
+    }
+
+    /**
+     * Makes a {@link Member} {@link AccessibleObject#isAccessible() accessible} if the member is not public.
+     *
+     * @param member the Member to make accessible (must not be {@code null}).
+     * @throws NullPointerException if {@code member} is {@code null}.
+     */
+    public static <T extends AccessibleObject & Member> void makeAccessible(final T member) {
+        if (!isAccessible(member) && !member.isAccessible()) {
+            member.setAccessible(true);
+        }
+    }
+
+    /**
+     * Makes a {@link Field} {@link AccessibleObject#isAccessible() accessible} if it is not public or if it is final.
+     *
+     * <p>Note that using this method to make a {@code final} field writable will most likely not work very well due to
+     * compiler optimizations and the like.</p>
+     *
+     * @param field the Field to make accessible (must not be {@code null}).
+     * @throws NullPointerException if {@code field} is {@code null}.
+     */
+    public static void makeAccessible(final Field field) {
+        Assert.requireNonNull(field, "No field provided");
+        if ((!isAccessible(field) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
+            field.setAccessible(true);
+        }
+    }
+
+    /**
+     * Gets the value of a {@link Field}, making it accessible if required.
+     *
+     * @param field    the Field to obtain a value from (must not be {@code null}).
+     * @param instance the instance to obtain the field value from or {@code null} only if the field is static.
+     * @return the value stored by the field.
+     * @throws NullPointerException if {@code field} is {@code null}, or if {@code instance} is {@code null} but
+     *                              {@code field} is not {@code static}.
+     * @see Field#get(Object)
+     */
+    public static Object getFieldValue(final Field field, final Object instance) {
+        makeAccessible(field);
+        if (!Modifier.isStatic(field.getModifiers())) {
+            Assert.requireNonNull(instance, "No instance given for non-static field");
+        }
+        try {
+            return field.get(instance);
+        } catch (final IllegalAccessException e) {
+            throw new UnsupportedOperationException(e);
+        }
+    }
+
+    /**
+     * Gets the value of a static {@link Field}, making it accessible if required.
+     *
+     * @param field the Field to obtain a value from (must not be {@code null}).
+     * @return the value stored by the static field.
+     * @throws NullPointerException if {@code field} is {@code null}, or if {@code field} is not {@code static}.
+     * @see Field#get(Object)
+     */
+    public static Object getStaticFieldValue(final Field field) {
+        return getFieldValue(field, null);
+    }
+
+    /**
+     * Sets the value of a {@link Field}, making it accessible if required.
+     *
+     * @param field    the Field to write a value to (must not be {@code null}).
+     * @param instance the instance to write the value to or {@code null} only if the field is static.
+     * @param value    the (possibly wrapped) value to write to the field.
+     * @throws NullPointerException if {@code field} is {@code null}, or if {@code instance} is {@code null} but
+     *                              {@code field} is not {@code static}.
+     * @see Field#set(Object, Object)
+     */
+    public static void setFieldValue(final Field field, final Object instance, final Object value) {
+        makeAccessible(field);
+        if (!Modifier.isStatic(field.getModifiers())) {
+            Assert.requireNonNull(instance, "No instance given for non-static field");
+        }
+        try {
+            field.set(instance, value);
+        } catch (final IllegalAccessException e) {
+            throw new UnsupportedOperationException(e);
+        }
+    }
+
+    /**
+     * Sets the value of a static {@link Field}, making it accessible if required.
+     *
+     * @param field the Field to write a value to (must not be {@code null}).
+     * @param value the (possibly wrapped) value to write to the field.
+     * @throws NullPointerException if {@code field} is {@code null}, or if {@code field} is not {@code static}.
+     * @see Field#set(Object, Object)
+     */
+    public static void setStaticFieldValue(final Field field, final Object value) {
+        setFieldValue(field, null, value);
+    }
+}


[04/15] git commit: Note possible runtime exception thrown by Builders.

Posted by ma...@apache.org.
Note possible runtime exception thrown by Builders.


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

Branch: refs/heads/master
Commit: 5b36078b32b8e491defa1d27275ca1dd41d3a709
Parents: c7e2c83
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 18:18:18 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 18:18:18 2014 -0500

----------------------------------------------------------------------
 .../src/main/java/org/apache/logging/log4j/core/util/Builder.java  | 2 ++
 1 file changed, 2 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5b36078b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Builder.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Builder.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Builder.java
index e88b98f..b70993c 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Builder.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Builder.java
@@ -38,6 +38,8 @@ public interface Builder<T> {
      * unspecified attributes for the plugin.
      *
      * @return the configured plugin instance.
+     * @throws org.apache.logging.log4j.core.config.ConfigurationException if there was an error building the plugin
+     * object.
      */
     T build();
 }


[15/15] git commit: Add tests for required annotation.

Posted by ma...@apache.org.
Add tests for required annotation.

  - Part of LOG4J2-825.


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

Branch: refs/heads/master
Commit: 5533cae78ab9c0c88a8befd5b01c66b4b3e9e76f
Parents: 0cf76d6
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 00:00:16 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 00:00:16 2014 -0500

----------------------------------------------------------------------
 .../plugins/validation/ValidatingPlugin.java    | 68 ++++++++++++++++++++
 .../RequiresNonNullValidatorTest.java           | 63 ++++++++++++++++++
 2 files changed, 131 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5533cae7/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
new file mode 100644
index 0000000..6bf3f40
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/ValidatingPlugin.java
@@ -0,0 +1,68 @@
+/*
+ * 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;
+
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
+import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.config.plugins.validation.constraints.RequiresNonNull;
+import org.apache.logging.log4j.core.util.Assert;
+
+/**
+ *
+ */
+@Plugin(name = "Validator", category = "Test")
+public class ValidatingPlugin {
+
+    private final String name;
+
+    public ValidatingPlugin(final String name) {
+        this.name = Assert.requireNonNull(name, "name");
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    @PluginFactory
+    public static ValidatingPlugin newValidatingPlugin(@RequiresNonNull final String name) {
+        return new ValidatingPlugin(name);
+    }
+
+    @PluginBuilderFactory
+    public static Builder newBuilder() {
+        return new Builder();
+    }
+
+    public static class Builder implements org.apache.logging.log4j.core.util.Builder<ValidatingPlugin> {
+
+        @PluginBuilderAttribute
+        @RequiresNonNull
+        private String name;
+
+        public Builder setName(final String name) {
+            this.name = name;
+            return this;
+        }
+
+        @Override
+        public ValidatingPlugin build() {
+            return new ValidatingPlugin(name);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5533cae7/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidatorTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidatorTest.java
new file mode 100644
index 0000000..f685f04
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidatorTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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 org.apache.logging.log4j.core.config.Node;
+import org.apache.logging.log4j.core.config.NullConfiguration;
+import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
+import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
+import org.apache.logging.log4j.core.config.plugins.util.PluginType;
+import org.apache.logging.log4j.core.config.plugins.validation.ValidatingPlugin;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class RequiresNonNullValidatorTest {
+
+    private PluginType<ValidatingPlugin> plugin;
+    private Node node;
+
+    @SuppressWarnings("unchecked")
+    @Before
+    public void setUp() throws Exception {
+        final PluginManager manager = new PluginManager("Test");
+        manager.collectPlugins();
+        plugin = (PluginType<ValidatingPlugin>) manager.getPluginType("Validator");
+        node = new Node(null, "Validator", plugin);
+    }
+
+    @Test
+    public void testNullDefaultValue() throws Exception {
+        final ValidatingPlugin validatingPlugin = new PluginBuilder<ValidatingPlugin>(plugin)
+            .withConfiguration(new NullConfiguration())
+            .withConfigurationNode(node)
+            .build();
+        assertNull(validatingPlugin);
+    }
+
+    @Test
+    public void testNonNullValue() throws Exception {
+        node.getAttributes().put("name", "foo");
+        final ValidatingPlugin validatingPlugin = new PluginBuilder<ValidatingPlugin>(plugin)
+            .withConfiguration(new NullConfiguration())
+            .withConfigurationNode(node)
+            .build();
+        assertNotNull(validatingPlugin);
+        assertEquals("foo", validatingPlugin.getName());
+    }
+}
\ No newline at end of file


[03/15] git commit: Add field support for @PluginValue.

Posted by ma...@apache.org.
Add field support for @PluginValue.

  - As with another annotation recently updated, this one is technically already supported by PluginBuilder.


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

Branch: refs/heads/master
Commit: c7e2c83423e0a4f7c37e0dc14ef41c1c58b8c73d
Parents: bcd4cba
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 16:10:47 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 16:10:47 2014 -0500

----------------------------------------------------------------------
 .../org/apache/logging/log4j/core/config/plugins/PluginValue.java  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c7e2c834/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginValue.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginValue.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginValue.java
index 521de5e..9c20cc2 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginValue.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginValue.java
@@ -32,7 +32,7 @@ import org.apache.logging.log4j.core.config.plugins.visitors.PluginValueVisitor;
  */
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.PARAMETER)
+@Target({ElementType.PARAMETER, ElementType.FIELD})
 @PluginVisitorStrategy(PluginValueVisitor.class)
 public @interface PluginValue {
 


[11/15] git commit: Add link in javadoc.

Posted by ma...@apache.org.
Add link in javadoc.


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

Branch: refs/heads/master
Commit: 22620140879755771a64464440ed97d1cf98951e
Parents: 4f2c39c
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 23:31:33 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 23:31:33 2014 -0500

----------------------------------------------------------------------
 .../logging/log4j/core/config/plugins/visitors/PluginVisitors.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/22620140/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
index 9ca03d0..15a763d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
@@ -24,7 +24,7 @@ import org.apache.logging.log4j.core.config.plugins.PluginVisitorStrategy;
 import org.apache.logging.log4j.status.StatusLogger;
 
 /**
- * Utility class to locate an appropriate PluginVisitor implementation for an annotation.
+ * Utility class to locate an appropriate {@link PluginVisitor} implementation for an annotation.
  */
 public final class PluginVisitors {
 


[02/15] git commit: Use variable from requireNonNull.

Posted by ma...@apache.org.
Use variable from requireNonNull.


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

Branch: refs/heads/master
Commit: bcd4cba6624971760f62317a3a87b6b0538a0e6c
Parents: b563ffe
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 15:57:36 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 15:57:36 2014 -0500

----------------------------------------------------------------------
 .../core/config/plugins/visitors/AbstractPluginVisitor.java      | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bcd4cba6/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/AbstractPluginVisitor.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/AbstractPluginVisitor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/AbstractPluginVisitor.java
index 7870f33..2729fdc 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/AbstractPluginVisitor.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/AbstractPluginVisitor.java
@@ -58,8 +58,8 @@ public abstract class AbstractPluginVisitor<A extends Annotation> implements Plu
     @Override
     public PluginVisitor<A> setAnnotation(final Annotation annotation) {
         final Annotation a = Assert.requireNonNull(annotation, "No annotation was provided");
-        if (this.clazz.isInstance(annotation)) {
-            this.annotation = (A) annotation;
+        if (this.clazz.isInstance(a)) {
+            this.annotation = (A) a;
         }
         return this;
     }


[14/15] git commit: Add required annotation.

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

  - Part of LOG4J2-825.


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

Branch: refs/heads/master
Commit: 0cf76d6a5087f118e3ac38bd35e20793cf6b7448
Parents: 59b7833
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 14 00:00:02 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 14 00:00:02 2014 -0500

----------------------------------------------------------------------
 .../validation/constraints/RequiresNonNull.java | 39 ++++++++++++++++++++
 .../validators/RequiresNonNullValidator.java    | 36 ++++++++++++++++++
 2 files changed, 75 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0cf76d6a/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
new file mode 100644
index 0000000..b3ef11d
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/RequiresNonNull.java
@@ -0,0 +1,39 @@
+/*
+ * 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.RequiresNonNullValidator;
+
+/**
+ * Marks a plugin builder field or plugin factory parameter as required.
+ *
+ * @since 2.1
+ */
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+@Constraint(RequiresNonNullValidator.class)
+public @interface RequiresNonNull {
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0cf76d6a/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
new file mode 100644
index 0000000..49c5806
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/RequiresNonNullValidator.java
@@ -0,0 +1,36 @@
+/*
+ * 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 org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
+import org.apache.logging.log4j.core.config.plugins.validation.constraints.RequiresNonNull;
+
+/**
+ * Validator implementation for {@link RequiresNonNull}.
+ *
+ * @since 2.1
+ */
+public class RequiresNonNullValidator implements ConstraintValidator<RequiresNonNull, Object> {
+    @Override
+    public void initialize(final RequiresNonNull annotation) {
+    }
+
+    @Override
+    public boolean isValid(final Object value) {
+        return value != null;
+    }
+}


[09/15] git commit: Revert unnecessary annotation graph walking.

Posted by ma...@apache.org.
Revert unnecessary annotation graph walking.


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

Branch: refs/heads/master
Commit: aa62239f386a033a8c35b961b460b41464328431
Parents: 73a12aa
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 21:31:11 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 21:31:11 2014 -0500

----------------------------------------------------------------------
 .../config/plugins/visitors/PluginVisitors.java | 25 ++++----------------
 1 file changed, 4 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aa62239f/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
index 3ef3203..9ca03d0 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginVisitors.java
@@ -42,33 +42,16 @@ public final class PluginVisitors {
      * @return a PluginVisitor instance if one could be created, or {@code null} otherwise.
      */
     public static PluginVisitor<? extends Annotation> findVisitor(final Class<? extends Annotation> annotation) {
-        final Class<? extends PluginVisitor<? extends Annotation>> visitorClass = findVisitorStrategy(annotation);
-        if (visitorClass == null) {
+        final PluginVisitorStrategy strategy = annotation.getAnnotation(PluginVisitorStrategy.class);
+        if (strategy == null) {
             LOGGER.debug("No PluginVisitorStrategy found on annotation [{}]. Ignoring.", annotation);
             return null;
         }
         try {
-            return visitorClass.newInstance();
+            return strategy.value().newInstance();
         } catch (final Exception e) {
-            LOGGER.error("Error loading PluginVisitor [{}] for annotation [{}].", visitorClass, annotation, e);
+            LOGGER.error("Error loading PluginVisitor [{}] for annotation [{}].", strategy.value(), annotation, e);
             return null;
         }
     }
-
-    private static Class<? extends PluginVisitor<? extends Annotation>> findVisitorStrategy(
-        final Class<? extends Annotation> annotation) {
-        final PluginVisitorStrategy strategy = annotation.getAnnotation(PluginVisitorStrategy.class);
-        if (strategy != null) {
-            return strategy.value();
-        }
-        final Annotation[] annotations = annotation.getDeclaredAnnotations();
-        for (final Annotation a : annotations) {
-            final PluginVisitorStrategy fallbackStrategy = a.annotationType().getAnnotation(
-                PluginVisitorStrategy.class);
-            if (fallbackStrategy != null) {
-                return fallbackStrategy.value();
-            }
-        }
-        return null;
-    }
 }


[12/15] git commit: Add core constraint validation classes.

Posted by ma...@apache.org.
Add core constraint validation classes.

  - Part of LOG4J2-825.


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

Branch: refs/heads/master
Commit: bee6af97a72d49edde85d8f5550a4cdafbad6fe8
Parents: 2262014
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 23:57:03 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 23:57:03 2014 -0500

----------------------------------------------------------------------
 .../config/plugins/validation/Constraint.java   | 41 +++++++++++
 .../plugins/validation/ConstraintValidator.java | 44 ++++++++++++
 .../validation/ConstraintValidators.java        | 75 ++++++++++++++++++++
 3 files changed, 160 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bee6af97/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
new file mode 100644
index 0000000..12355c2
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/Constraint.java
@@ -0,0 +1,41 @@
+/*
+ * 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;
+
+import java.lang.annotation.Annotation;
+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;
+
+/**
+ * Meta annotation to mark an annotation as a validation constraint. This annotation must specify a
+ * {@link ConstraintValidator} implementation class that has a default constructor.
+ *
+ * @since 2.1
+ */
+@Documented
+@Target(ElementType.ANNOTATION_TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Constraint {
+
+    /**
+     * {@link ConstraintValidator} class that implements the validation logic for the annotated constraint annotation.
+     */
+    Class<? extends ConstraintValidator<? extends Annotation, ?>> value();
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bee6af97/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
new file mode 100644
index 0000000..12a50c0
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidator.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;
+
+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> {
+
+    /**
+     * Called before this validator is used with the constraint annotation value.
+     *
+     * @param annotation the annotation value this validator will be validating.
+     */
+    void initialize(A annotation);
+
+    /**
+     * Indicates if the given value is valid.
+     *
+     * @param value the value to validate.
+     * @return {@code true} if the given value is valid.
+     */
+    boolean isValid(T value);
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bee6af97/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
new file mode 100644
index 0000000..028f708
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/ConstraintValidators.java
@@ -0,0 +1,75 @@
+/*
+ * 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;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ * Utility class to locate an appropriate {@link ConstraintValidator} implementation for an annotation.
+ *
+ * @since 2.1
+ */
+public final class ConstraintValidators {
+
+    private static final Logger LOGGER = StatusLogger.getLogger();
+
+    private ConstraintValidators() {
+    }
+
+    /**
+     * Finds all relevant {@link ConstraintValidator} objects from an array of annotations. All validators will be
+     * {@link ConstraintValidator#initialize(java.lang.annotation.Annotation) initialized} before being returned.
+     *
+     * @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>>();
+        for (final Annotation annotation : annotations) {
+            final Constraint constraint = annotation.annotationType().getAnnotation(Constraint.class);
+            if (constraint != null) {
+                final ConstraintValidator<Annotation, Object> validator = getValidatorFor(annotation, constraint);
+                if (validator != null) {
+                    validators.add(validator);
+                }
+            }
+        }
+        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();
+            validator.initialize(annotation);
+            return validator;
+        } catch (final Exception e) {
+            LOGGER.error("Error loading ConstraintValidator [{}].", constraint.value(), e);
+            return null;
+        }
+    }
+}


[10/15] git commit: Add type converter for ConsoleAppender.Target enum.

Posted by ma...@apache.org.
Add type converter for ConsoleAppender.Target enum.


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

Branch: refs/heads/master
Commit: 4f2c39c2506713cb988e43470f8f39c060965ef7
Parents: aa62239
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 23:13:54 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 23:13:54 2014 -0500

----------------------------------------------------------------------
 .../logging/log4j/core/config/plugins/util/TypeConverters.java   | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4f2c39c2/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java
index 0fc2353..7bf1ec0 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java
@@ -30,12 +30,12 @@ import java.security.Security;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.regex.Pattern;
-
 import javax.xml.bind.DatatypeConverter;
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.appender.ConsoleAppender;
 import org.apache.logging.log4j.core.layout.GelfLayout;
 import org.apache.logging.log4j.core.layout.HtmlLayout;
 import org.apache.logging.log4j.core.net.Facility;
@@ -441,5 +441,7 @@ public final class TypeConverters {
         registry.put(GelfLayout.CompressionType.class, new EnumConverter<GelfLayout.CompressionType>(
                 GelfLayout.CompressionType.class));
         registry.put(HtmlLayout.FontSize.class, new EnumConverter<HtmlLayout.FontSize>(HtmlLayout.FontSize.class));
+        registry.put(ConsoleAppender.Target.class, new EnumConverter<ConsoleAppender.Target>(
+            ConsoleAppender.Target.class));
     }
 }


[13/15] git commit: Add in constraint validator logic to PluginBuilder.

Posted by ma...@apache.org.
Add in constraint validator logic to PluginBuilder.

  - Also includes a couple clean-up things.


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

Branch: refs/heads/master
Commit: 59b7833b4cc6d29fea90dcdf9ebd131f437bfecf
Parents: bee6af9
Author: Matt Sicker <ma...@apache.org>
Authored: Sat Sep 13 23:58:47 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sat Sep 13 23:58:47 2014 -0500

----------------------------------------------------------------------
 .../core/config/plugins/util/PluginBuilder.java | 37 +++++++++++++++-----
 1 file changed, 29 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/59b7833b/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 08a85ff..ae8ccae 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
@@ -18,25 +18,30 @@
 package org.apache.logging.log4j.core.config.plugins.util;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationException;
 import org.apache.logging.log4j.core.config.Node;
 import org.apache.logging.log4j.core.config.plugins.PluginAliases;
 import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
+import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidators;
 import org.apache.logging.log4j.core.config.plugins.visitors.PluginVisitor;
 import org.apache.logging.log4j.core.config.plugins.visitors.PluginVisitors;
 import org.apache.logging.log4j.core.util.Assert;
 import org.apache.logging.log4j.core.util.Builder;
+import org.apache.logging.log4j.core.util.ReflectionUtil;
 import org.apache.logging.log4j.status.StatusLogger;
 
 /**
@@ -119,9 +124,8 @@ public class PluginBuilder<T> implements Builder<T> {
                 return result;
             }
         } catch (final Exception e) {
-            LOGGER.catching(Level.ERROR, e);
             LOGGER.error("Unable to inject fields into builder class for plugin type {}, element {}.", this.clazz,
-                node.getName());
+                node.getName(), e);
         }
         // or fall back to factory method if no builder class is available
         try {
@@ -134,8 +138,8 @@ public class PluginBuilder<T> implements Builder<T> {
             LOGGER.debug("Built Plugin[name={}] OK from factory method.", pluginType.getElementName());
             return plugin;
         } catch (final Exception e) {
-            LOGGER.catching(Level.ERROR, e);
-            LOGGER.error("Unable to invoke factory method in class {} for element {}.", this.clazz, this.node.getName());
+            LOGGER.error("Unable to invoke factory method in class {} for element {}.", this.clazz, this.node.getName(),
+                e);
             return null;
         }
     }
@@ -150,6 +154,7 @@ public class PluginBuilder<T> implements Builder<T> {
         for (final Method method : clazz.getDeclaredMethods()) {
             if (method.isAnnotationPresent(PluginBuilderFactory.class) &&
                 Modifier.isStatic(method.getModifiers())) {
+                ReflectionUtil.makeAccessible(method);
                 @SuppressWarnings("unchecked")
                 final Builder<T> builder = (Builder<T>) method.invoke(null);
                 LOGGER.debug("Found builder factory method [{}]: {}.", method.getName(), method);
@@ -163,10 +168,10 @@ public class PluginBuilder<T> implements Builder<T> {
 
     private void injectFields(final Builder<T> builder) throws IllegalAccessException {
         final Field[] fields = builder.getClass().getDeclaredFields();
+        AccessibleObject.setAccessible(fields, true);
         final StringBuilder log = new StringBuilder();
         for (final Field field : fields) {
             log.append(log.length() == 0 ? "with params(" : ", ");
-            field.setAccessible(true);
             final Annotation[] annotations = field.getDeclaredAnnotations();
             final String[] aliases = extractPluginAliases(annotations);
             for (final Annotation a : annotations) {
@@ -188,6 +193,14 @@ public class PluginBuilder<T> implements Builder<T> {
                     }
                 }
             }
+            final Collection<ConstraintValidator<Annotation, Object>> validators =
+                ConstraintValidators.findValidators(annotations);
+            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());
+                }
+            }
         }
         if (log.length() > 0) {
             log.append(')');
@@ -203,11 +216,11 @@ public class PluginBuilder<T> implements Builder<T> {
             if (method.isAnnotationPresent(PluginFactory.class) &&
                 Modifier.isStatic(method.getModifiers())) {
                 LOGGER.debug("Found factory method [{}]: {}.", method.getName(), method);
+                ReflectionUtil.makeAccessible(method);
                 return method;
             }
         }
-        LOGGER.debug("No factory method found in class {}.", clazz.getName());
-        return null;
+        throw new IllegalStateException("No factory method found for class " + clazz.getName());
     }
 
     private Object[] generateParameters(final Method factory) {
@@ -237,6 +250,14 @@ public class PluginBuilder<T> implements Builder<T> {
                     }
                 }
             }
+            final Collection<ConstraintValidator<Annotation, Object>> validators =
+                ConstraintValidators.findValidators(annotations[i]);
+            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);
+                }
+            }
         }
         if (log.length() > 0) {
             log.append(')');