001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.config.plugins.validation.validators; 018 019import java.util.Collection; 020import java.util.Map; 021 022import org.apache.logging.log4j.Logger; 023import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator; 024import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required; 025import org.apache.logging.log4j.core.util.Assert; 026import org.apache.logging.log4j.status.StatusLogger; 027 028/** 029 * Validator that checks an object for emptiness. Emptiness is defined here as: 030 * <ul> 031 * <li>The value {@code null}</li> 032 * <li>An object of type {@link CharSequence} with length 0</li> 033 * <li>An empty array</li> 034 * <li>An empty {@link Collection}</li> 035 * <li>An empty {@link Map}</li> 036 * </ul> 037 * 038 * @since 2.1 039 */ 040public class RequiredValidator implements ConstraintValidator<Required> { 041 042 private static final Logger LOGGER = StatusLogger.getLogger(); 043 044 private Required annotation; 045 046 @Override 047 public void initialize(final Required anAnnotation) { 048 this.annotation = anAnnotation; 049 } 050 051 @Override 052 public boolean isValid(final String name, final Object value) { 053 return Assert.isNonEmpty(value) || err(name); 054 } 055 056 private boolean err(final String name) { 057 LOGGER.error(annotation.message() + ": " + name); 058 return false; 059 } 060}