1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.Core;
29 import org.apache.logging.log4j.core.config.plugins.Plugin;
30 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
31 import org.apache.logging.log4j.core.config.plugins.PluginElement;
32 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
33 import org.apache.logging.log4j.core.util.Clock;
34 import org.apache.logging.log4j.core.util.ClockFactory;
35 import org.apache.logging.log4j.status.StatusLogger;
36
37
38
39
40 @Plugin(name = "IfLastModified", category = Core.CATEGORY_NAME, printObject = true)
41 public final class IfLastModified implements PathCondition {
42 private static final Logger LOGGER = StatusLogger.getLogger();
43 private static final Clock CLOCK = ClockFactory.getClock();
44
45 private final Duration age;
46 private final PathCondition[] nestedConditions;
47
48 private IfLastModified(final Duration age, final PathCondition[] nestedConditions) {
49 this.age = Objects.requireNonNull(age, "age");
50 this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
51 nestedConditions.length);
52 }
53
54 public Duration getAge() {
55 return age;
56 }
57
58 public List<PathCondition> getNestedConditions() {
59 return Collections.unmodifiableList(Arrays.asList(nestedConditions));
60 }
61
62
63
64
65
66
67
68 @Override
69 public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
70 final FileTime fileTime = attrs.lastModifiedTime();
71 final long millis = fileTime.toMillis();
72 final long ageMillis = CLOCK.currentTimeMillis() - millis;
73 final boolean result = ageMillis >= age.toMillis();
74 final String match = result ? ">=" : "<";
75 final String accept = result ? "ACCEPTED" : "REJECTED";
76 LOGGER.trace("IfLastModified {}: {} ageMillis '{}' {} '{}'", accept, relativePath, ageMillis, match, age);
77 if (result) {
78 return IfAll.accept(nestedConditions, basePath, relativePath, attrs);
79 }
80 return result;
81 }
82
83
84
85
86
87
88 @Override
89 public void beforeFileTreeWalk() {
90 IfAll.beforeFileTreeWalk(nestedConditions);
91 }
92
93
94
95
96
97
98
99
100 @PluginFactory
101 public static IfLastModified createAgeCondition(
102
103 @PluginAttribute("age") final Duration age,
104 @PluginElement("PathConditions") final PathCondition... nestedConditions) {
105
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 }