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