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.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.apache.logging.log4j.Logger;
26 import org.apache.logging.log4j.core.config.plugins.Plugin;
27 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
28 import org.apache.logging.log4j.core.config.plugins.PluginElement;
29 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
30 import org.apache.logging.log4j.status.StatusLogger;
31
32
33
34
35 @Plugin(name = "IfAccumulatedFileCount", category = "Core", printObject = true)
36 public final class IfAccumulatedFileCount implements PathCondition {
37 private static final Logger LOGGER = StatusLogger.getLogger();
38 private final int threshold;
39 private int count;
40 private final PathCondition[] nestedConditions;
41
42 private IfAccumulatedFileCount(final int thresholdParam, final PathCondition[] nestedConditions) {
43 if (thresholdParam <= 0) {
44 throw new IllegalArgumentException("Count must be a positive integer but was " + thresholdParam);
45 }
46 this.threshold = thresholdParam;
47 this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions,
48 nestedConditions.length);
49 }
50
51 public int getThresholdCount() {
52 return threshold;
53 }
54
55 public List<PathCondition> getNestedConditions() {
56 return Collections.unmodifiableList(Arrays.asList(nestedConditions));
57 }
58
59
60
61
62
63
64
65 @Override
66 public boolean accept(final Path basePath, final Path relativePath, final BasicFileAttributes attrs) {
67 final boolean result = ++count > threshold;
68 final String match = result ? ">" : "<=";
69 final String accept = result ? "ACCEPTED" : "REJECTED";
70 LOGGER.trace("IfAccumulatedFileCount {}: {} count '{}' {} threshold '{}'", accept, relativePath, count, match,
71 threshold);
72 if (result) {
73 return IfAll.accept(nestedConditions, basePath, relativePath, attrs);
74 }
75 return result;
76 }
77
78
79
80
81
82
83 @Override
84 public void beforeFileTreeWalk() {
85 count = 0;
86 IfAll.beforeFileTreeWalk(nestedConditions);
87 }
88
89
90
91
92
93
94
95 @PluginFactory
96 public static IfAccumulatedFileCount createFileCountCondition(
97
98 @PluginAttribute(value = "exceeds", defaultInt = Integer.MAX_VALUE) final int threshold,
99 @PluginElement("PathConditions") final PathCondition... nestedConditions) {
100
101
102 if (threshold == Integer.MAX_VALUE) {
103 LOGGER.error("IfAccumulatedFileCount invalid or missing threshold value.");
104 }
105 return new IfAccumulatedFileCount(threshold, nestedConditions);
106 }
107
108 @Override
109 public String toString() {
110 final String nested = nestedConditions.length == 0 ? "" : " AND " + Arrays.toString(nestedConditions);
111 return "IfAccumulatedFileCount(exceeds=" + threshold + nested + ")";
112 }
113 }