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.appender.rolling.FileSize;
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 the accumulated file size threshold is exceeded during the file tree walk.
035 */
036@Plugin(name = "IfAccumulatedFileSize", category = "Core", printObject = true)
037public final class IfAccumulatedFileSize implements PathCondition {
038    private static final Logger LOGGER = StatusLogger.getLogger();
039    private final long thresholdBytes;
040    private long accumulatedSize;
041    private final PathCondition[] nestedConditions;
042
043    private IfAccumulatedFileSize(final long thresholdSize, final PathCondition[] nestedConditions) {
044        if (thresholdSize <= 0) {
045            throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdSize);
046        }
047        this.thresholdBytes = thresholdSize;
048        this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
049                nestedConditions.length);
050    }
051
052    public long getThresholdBytes() {
053        return thresholdBytes;
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        accumulatedSize += attrs.size();
069        final boolean result = accumulatedSize > thresholdBytes;
070        final String match = result ? ">" : "<=";
071        final String accept = result ? "ACCEPTED" : "REJECTED";
072        LOGGER.trace("IfAccumulatedFileSize {}: {} accumulated size '{}' {} thresholdBytes '{}'", accept, relativePath,
073                accumulatedSize, match, thresholdBytes);
074        if (result) {
075            return IfAll.accept(nestedConditions, basePath, relativePath, attrs);
076        }
077        return result;
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        accumulatedSize = 0;
088        IfAll.beforeFileTreeWalk(nestedConditions);
089    }
090
091    /**
092     * Create an IfAccumulatedFileSize condition.
093     * 
094     * @param threshold The threshold accumulated file size from which files will be deleted.
095     * @return An IfAccumulatedFileSize condition.
096     */
097    @PluginFactory
098    public static IfAccumulatedFileSize createFileSizeCondition( //
099            // @formatter:off
100            @PluginAttribute("exceeds") final String size,
101            @PluginElement("PathConditions") final PathCondition... nestedConditions) {
102            // @formatter:on
103
104        if (size == null) {
105            LOGGER.error("IfAccumulatedFileSize missing mandatory size threshold.");
106        }
107        final long threshold = size == null ? Long.MAX_VALUE : FileSize.parse(size, Long.MAX_VALUE);
108        return new IfAccumulatedFileSize(threshold, nestedConditions);
109    }
110
111    @Override
112    public String toString() {
113        final String nested = nestedConditions.length == 0 ? "" : " AND " + Arrays.toString(nestedConditions);
114        return "IfAccumulatedFileSize(exceeds=" + thresholdBytes + nested + ")";
115    }
116}