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