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