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.nio.file.attribute.FileTime;
022import java.util.Arrays;
023import java.util.Collections;
024import java.util.List;
025import java.util.Objects;
026
027import org.apache.logging.log4j.Logger;
028import org.apache.logging.log4j.core.Core;
029import org.apache.logging.log4j.core.config.plugins.Plugin;
030import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
031import org.apache.logging.log4j.core.config.plugins.PluginElement;
032import org.apache.logging.log4j.core.config.plugins.PluginFactory;
033import org.apache.logging.log4j.core.util.Clock;
034import org.apache.logging.log4j.core.util.ClockFactory;
035import org.apache.logging.log4j.status.StatusLogger;
036
037/**
038 * PathCondition that accepts paths that are older than the specified duration.
039 */
040@Plugin(name = "IfLastModified", category = Core.CATEGORY_NAME, printObject = true)
041public final class IfLastModified implements PathCondition {
042    private static final Logger LOGGER = StatusLogger.getLogger();
043    private static final Clock CLOCK = ClockFactory.getClock();
044
045    private final Duration age;
046    private final PathCondition[] nestedConditions;
047
048    private IfLastModified(final Duration age, final PathCondition[] nestedConditions) {
049        this.age = Objects.requireNonNull(age, "age");
050        this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
051                nestedConditions.length);
052    }
053
054    public Duration getAge() {
055        return age;
056    }
057
058    public List<PathCondition> getNestedConditions() {
059        return Collections.unmodifiableList(Arrays.asList(nestedConditions));
060    }
061
062    /*
063     * (non-Javadoc)
064     *
065     * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
066     * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
067     */
068    @Override
069    public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
070        final FileTime fileTime = attrs.lastModifiedTime();
071        final long millis = fileTime.toMillis();
072        final long ageMillis = CLOCK.currentTimeMillis() - millis;
073        final boolean result = ageMillis >= age.toMillis();
074        final String match = result ? ">=" : "<";
075        final String accept = result ? "ACCEPTED" : "REJECTED";
076        LOGGER.trace("IfLastModified {}: {} ageMillis '{}' {} '{}'", accept, relativePath, ageMillis, match, age);
077        if (result) {
078            return IfAll.accept(nestedConditions, basePath, relativePath, attrs);
079        }
080        return result;
081    }
082
083    /*
084     * (non-Javadoc)
085     *
086     * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
087     */
088    @Override
089    public void beforeFileTreeWalk() {
090        IfAll.beforeFileTreeWalk(nestedConditions);
091    }
092
093    /**
094     * Create an IfLastModified condition.
095     *
096     * @param age The path age that is accepted by this condition. Must be a valid Duration.
097     * @param nestedConditions nested conditions to evaluate if this condition accepts a path
098     * @return An IfLastModified condition.
099     */
100    @PluginFactory
101    public static IfLastModified createAgeCondition(
102            // @formatter:off
103            @PluginAttribute("age") final Duration age,
104            @PluginElement("PathConditions") final PathCondition... nestedConditions) {
105            // @formatter:on
106        return new IfLastModified(age, nestedConditions);
107    }
108
109    @Override
110    public String toString() {
111        final String nested = nestedConditions.length == 0 ? "" : " AND " + Arrays.toString(nestedConditions);
112        return "IfLastModified(age=" + age + nested + ")";
113    }
114}