View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.config.plugins.validation.validators;
18  
19  import java.util.Collection;
20  import java.util.Map;
21  
22  import org.apache.logging.log4j.Logger;
23  import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
24  import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
25  import org.apache.logging.log4j.status.StatusLogger;
26  
27  /**
28   * Validator that checks an object for emptiness. Emptiness is defined here as:
29   * <ul>
30   * <li>The value {@code null}</li>
31   * <li>An object of type {@link CharSequence} with length 0</li>
32   * <li>An empty array</li>
33   * <li>An empty {@link Collection}</li>
34   * <li>An empty {@link Map}</li>
35   * </ul>
36   *
37   * @since 2.1
38   */
39  public class RequiredValidator implements ConstraintValidator<Required> {
40  
41      private static final Logger LOGGER = StatusLogger.getLogger();
42  
43      private Required annotation;
44  
45      @Override
46      public void initialize(final Required annotation) {
47          this.annotation = annotation;
48      }
49  
50      @Override
51      public boolean isValid(final Object value) {
52          if (value == null) {
53              return err();
54          }
55          if (value instanceof CharSequence) {
56              final CharSequence sequence = (CharSequence) value;
57              return sequence.length() != 0 || err();
58          }
59          final Class<?> clazz = value.getClass();
60          if (clazz.isArray()) {
61              final Object[] array = (Object[]) value;
62              return array.length != 0 || err();
63          }
64          if (Collection.class.isAssignableFrom(clazz)) {
65              final Collection<?> collection = (Collection<?>) value;
66              return collection.size() != 0 || err();
67          }
68          if (Map.class.isAssignableFrom(clazz)) {
69              final Map<?, ?> map = (Map<?, ?>) value;
70              return map.size() != 0 || err();
71          }
72          LOGGER.debug("Encountered type [{}] which can only be checked for null.", clazz.getName());
73          return true;
74      }
75  
76      private boolean err() {
77          LOGGER.error(annotation.message());
78          return false;
79      }
80  }