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.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   * PathCondition that accepts paths after some count threshold is exceeded during the file tree walk.
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       * (non-Javadoc)
61       * 
62       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
63       * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
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       * (non-Javadoc)
80       * 
81       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
82       */
83      @Override
84      public void beforeFileTreeWalk() {
85          count = 0;
86          IfAll.beforeFileTreeWalk(nestedConditions);
87      }
88  
89      /**
90       * Create an IfAccumulatedFileCount condition.
91       * 
92       * @param threshold The threshold count from which files will be deleted.
93       * @return An IfAccumulatedFileCount condition.
94       */
95      @PluginFactory
96      public static IfAccumulatedFileCount createFileCountCondition( //
97              // @formatter:off
98              @PluginAttribute(value = "exceeds", defaultInt = Integer.MAX_VALUE) final int threshold,
99              @PluginElement("PathConditions") final PathCondition... nestedConditions) {
100             // @formatter:on
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 }