View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender.rolling.action;
18  
19  import java.nio.file.Path;
20  import java.nio.file.attribute.BasicFileAttributes;
21  import java.nio.file.attribute.FileTime;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Objects;
26  
27  import org.apache.logging.log4j.Logger;
28  import org.apache.logging.log4j.core.config.plugins.Plugin;
29  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
30  import org.apache.logging.log4j.core.config.plugins.PluginElement;
31  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
32  import org.apache.logging.log4j.core.util.Clock;
33  import org.apache.logging.log4j.core.util.ClockFactory;
34  import org.apache.logging.log4j.status.StatusLogger;
35  
36  /**
37   * PathCondition that accepts paths that are older than the specified duration.
38   */
39  @Plugin(name = "IfLastModified", category = "Core", printObject = true)
40  public final class IfLastModified implements PathCondition {
41      private static final Logger LOGGER = StatusLogger.getLogger();
42      private static final Clock CLOCK = ClockFactory.getClock();
43  
44      private final Duration age;
45      private final PathCondition[] nestedConditions;
46  
47      private IfLastModified(final Duration age, final PathCondition[] nestedConditions) {
48          this.age = Objects.requireNonNull(age, "age");
49          this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
50                  nestedConditions.length);
51      }
52  
53      public Duration getAge() {
54          return age;
55      }
56  
57      public List<PathCondition> getNestedConditions() {
58          return Collections.unmodifiableList(Arrays.asList(nestedConditions));
59      }
60  
61      /*
62       * (non-Javadoc)
63       * 
64       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
65       * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
66       */
67      @Override
68      public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
69          final FileTime fileTime = attrs.lastModifiedTime();
70          final long millis = fileTime.toMillis();
71          final long ageMillis = CLOCK.currentTimeMillis() - millis;
72          final boolean result = ageMillis >= age.toMillis();
73          final String match = result ? ">=" : "<";
74          final String accept = result ? "ACCEPTED" : "REJECTED";
75          LOGGER.trace("IfLastModified {}: {} ageMillis '{}' {} '{}'", accept, relativePath, ageMillis, match, age);
76          if (result) {
77              return IfAll.accept(nestedConditions, basePath, relativePath, attrs);
78          }
79          return result;
80      }
81  
82      /*
83       * (non-Javadoc)
84       * 
85       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
86       */
87      @Override
88      public void beforeFileTreeWalk() {
89          IfAll.beforeFileTreeWalk(nestedConditions);
90      }
91  
92      /**
93       * Create an IfLastModified condition.
94       * 
95       * @param age The path age that is accepted by this condition. Must be a valid Duration.
96       * @param nestedConditions nested conditions to evaluate if this condition accepts a path
97       * @return An IfLastModified condition.
98       */
99      @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 }