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.Collections;
023import java.util.List;
024
025import org.apache.logging.log4j.Logger;
026import org.apache.logging.log4j.core.Core;
027import org.apache.logging.log4j.core.config.plugins.Plugin;
028import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
029import org.apache.logging.log4j.core.config.plugins.PluginElement;
030import org.apache.logging.log4j.core.config.plugins.PluginFactory;
031import org.apache.logging.log4j.status.StatusLogger;
032
033/**
034 * PathCondition that accepts paths after some count threshold is exceeded during the file tree walk.
035 */
036@Plugin(name = "IfAccumulatedFileCount", category = Core.CATEGORY_NAME, printObject = true)
037public final class IfAccumulatedFileCount implements PathCondition {
038    private static final Logger LOGGER = StatusLogger.getLogger();
039    private final int threshold;
040    private int count;
041    private final PathCondition[] nestedConditions;
042
043    private IfAccumulatedFileCount(final int thresholdParam, final PathCondition[] nestedConditions) {
044        if (thresholdParam <= 0) {
045            throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdParam);
046        }
047        this.threshold = thresholdParam;
048        this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
049                nestedConditions.length);
050    }
051
052    public int getThresholdCount() {
053        return threshold;
054    }
055
056    public List<PathCondition> getNestedConditions() {
057        return Collections.unmodifiableList(Arrays.asList(nestedConditions));
058    }
059
060    /*
061     * (non-Javadoc)
062     *
063     * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
064     * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
065     */
066    @Override
067    public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
068        final boolean result = ++count > threshold;
069        final String match = result ? ">" : "<=";
070        final String accept = result ? "ACCEPTED" : "REJECTED";
071        LOGGER.trace("IfAccumulatedFileCount {}: {} count '{}' {} threshold '{}'", accept, relativePath, count, match,
072                threshold);
073        if (result) {
074            return IfAll.accept(nestedConditions, basePath, relativePath, attrs);
075        }
076        return result;
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        count = 0;
087        IfAll.beforeFileTreeWalk(nestedConditions);
088    }
089
090    /**
091     * Create an IfAccumulatedFileCount condition.
092     *
093     * @param threshold The threshold count from which files will be deleted.
094     * @return An IfAccumulatedFileCount condition.
095     */
096    @PluginFactory
097    public static IfAccumulatedFileCount createFileCountCondition(
098            // @formatter:off
099            @PluginAttribute(value = "exceeds", defaultInt = Integer.MAX_VALUE) final int threshold,
100            @PluginElement("PathConditions") final PathCondition... nestedConditions) {
101            // @formatter:on
102
103        if (threshold == Integer.MAX_VALUE) {
104            LOGGER.error("IfAccumulatedFileCount invalid or missing threshold value.");
105        }
106        return new IfAccumulatedFileCount(threshold, nestedConditions);
107    }
108
109    @Override
110    public String toString() {
111        final String nested = nestedConditions.length == 0 ? "" : " AND " + Arrays.toString(nestedConditions);
112        return "IfAccumulatedFileCount(exceeds=" + threshold + nested + ")";
113    }
114}