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.appender.rolling.action; 18 19 import java.nio.file.Path; 20 import java.nio.file.attribute.BasicFileAttributes; 21 import java.util.Arrays; 22 import java.util.Objects; 23 24 import org.apache.logging.log4j.core.Core; 25 import org.apache.logging.log4j.core.config.plugins.Plugin; 26 import org.apache.logging.log4j.core.config.plugins.PluginElement; 27 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 28 29 /** 30 * Composite {@code PathCondition} that only accepts objects that are accepted by <em>all</em> component conditions. 31 * Corresponds to logical "AND". 32 */ 33 @Plugin(name = "IfAll", category = Core.CATEGORY_NAME, printObject = true) 34 public final class IfAll implements PathCondition { 35 36 private final PathCondition[] components; 37 38 private IfAll(final PathCondition... filters) { 39 this.components = Objects.requireNonNull(filters, "filters"); 40 } 41 42 public PathCondition[] getDeleteFilters() { 43 return components; 44 } 45 46 /* 47 * (non-Javadoc) 48 * 49 * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path, 50 * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes) 51 */ 52 @Override 53 public boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs) { 54 if (components == null || components.length == 0) { 55 return false; // unconditional delete not supported 56 } 57 return accept(components, baseDir, relativePath, attrs); 58 } 59 60 /** 61 * Returns {@code true} if all the specified conditions accept the specified path, {@code false} otherwise. 62 * 63 * @param list the array of conditions to evaluate 64 * @param baseDir the directory from where to start scanning for deletion candidate files 65 * @param relativePath the candidate for deletion. This path is relative to the baseDir. 66 * @param attrs attributes of the candidate path 67 * @return {@code true} if all the specified conditions accept the specified path, {@code false} otherwise 68 * @throws NullPointerException if any of the parameters is {@code null} 69 */ 70 public static boolean accept(final PathCondition[] list, final Path baseDir, final Path relativePath, 71 final BasicFileAttributes attrs) { 72 for (final PathCondition component : list) { 73 if (!component.accept(baseDir, relativePath, attrs)) { 74 return false; 75 } 76 } 77 return true; 78 } 79 80 /* 81 * (non-Javadoc) 82 * 83 * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk() 84 */ 85 @Override 86 public void beforeFileTreeWalk() { 87 beforeFileTreeWalk(components); 88 } 89 90 /** 91 * Calls {@link #beforeFileTreeWalk()} on all of the specified nested conditions. 92 * 93 * @param nestedConditions the conditions to call {@link #beforeFileTreeWalk()} on 94 */ 95 public static void beforeFileTreeWalk(final PathCondition[] nestedConditions) { 96 for (final PathCondition condition : nestedConditions) { 97 condition.beforeFileTreeWalk(); 98 } 99 } 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 }