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.appender.rolling.action; 018 019import java.nio.file.Path; 020import java.nio.file.attribute.BasicFileAttributes; 021import java.util.Arrays; 022import java.util.Objects; 023 024import org.apache.logging.log4j.core.Core; 025import org.apache.logging.log4j.core.config.plugins.Plugin; 026import org.apache.logging.log4j.core.config.plugins.PluginElement; 027import org.apache.logging.log4j.core.config.plugins.PluginFactory; 028 029/** 030 * Composite {@code PathCondition} that only accepts objects that are accepted by <em>all</em> component conditions. 031 * Corresponds to logical "AND". 032 */ 033@Plugin(name = "IfAll", category = Core.CATEGORY_NAME, printObject = true) 034public final class IfAll implements PathCondition { 035 036 private final PathCondition[] components; 037 038 private IfAll(final PathCondition... filters) { 039 this.components = Objects.requireNonNull(filters, "filters"); 040 } 041 042 public PathCondition[] getDeleteFilters() { 043 return components; 044 } 045 046 /* 047 * (non-Javadoc) 048 * 049 * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path, 050 * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes) 051 */ 052 @Override 053 public boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs) { 054 if (components == null || components.length == 0) { 055 return false; // unconditional delete not supported 056 } 057 return accept(components, baseDir, relativePath, attrs); 058 } 059 060 /** 061 * Returns {@code true} if all the specified conditions accept the specified path, {@code false} otherwise. 062 * 063 * @param list the array of conditions to evaluate 064 * @param baseDir the directory from where to start scanning for deletion candidate files 065 * @param relativePath the candidate for deletion. This path is relative to the baseDir. 066 * @param attrs attributes of the candidate path 067 * @return {@code true} if all the specified conditions accept the specified path, {@code false} otherwise 068 * @throws NullPointerException if any of the parameters is {@code null} 069 */ 070 public static boolean accept(final PathCondition[] list, final Path baseDir, final Path relativePath, 071 final BasicFileAttributes attrs) { 072 for (final PathCondition component : list) { 073 if (!component.accept(baseDir, relativePath, attrs)) { 074 return false; 075 } 076 } 077 return true; 078 } 079 080 /* 081 * (non-Javadoc) 082 * 083 * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk() 084 */ 085 @Override 086 public void beforeFileTreeWalk() { 087 beforeFileTreeWalk(components); 088 } 089 090 /** 091 * Calls {@link #beforeFileTreeWalk()} on all of the specified nested conditions. 092 * 093 * @param nestedConditions the conditions to call {@link #beforeFileTreeWalk()} on 094 */ 095 public static void beforeFileTreeWalk(final PathCondition[] nestedConditions) { 096 for (final PathCondition condition : nestedConditions) { 097 condition.beforeFileTreeWalk(); 098 } 099 } 100 101 /** 102 * Create a Composite PathCondition whose components all need to accept before this condition accepts. 103 * 104 * @param components The component filters. 105 * @return A Composite PathCondition. 106 */ 107 @PluginFactory 108 public static IfAll createAndCondition( 109 @PluginElement("PathConditions") final PathCondition... components) { 110 return new IfAll(components); 111 } 112 113 @Override 114 public String toString() { 115 return "IfAll" + Arrays.toString(components); 116 } 117}