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:30 UTC

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

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;
+        }
+    }
+}