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.Objects;
23  
24  import org.apache.logging.log4j.core.config.plugins.Plugin;
25  import org.apache.logging.log4j.core.config.plugins.PluginElement;
26  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
27  
28  /**
29   * Composite {@code PathCondition} that only accepts objects that are accepted by <em>all</em> component conditions.
30   * Corresponds to logical "AND".
31   */
32  @Plugin(name = "IfAll", category = "Core", printObject = true)
33  public final class IfAll implements PathCondition {
34  
35      private final PathCondition[] components;
36  
37      private IfAll(final PathCondition... filters) {
38          this.components = Objects.requireNonNull(filters, "filters");
39      }
40  
41      public PathCondition[] getDeleteFilters() {
42          return components;
43      }
44  
45      /*
46       * (non-Javadoc)
47       * 
48       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path,
49       * java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
50       */
51      @Override
52      public boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs) {
53          if (components == null || components.length == 0) {
54              return false; // unconditional delete not supported
55          }
56          return accept(components, baseDir, relativePath, attrs);
57      }
58  
59      /**
60       * Returns {@code true} if all the specified conditions accept the specified path, {@code false} otherwise.
61       * 
62       * @param list the array of conditions to evaluate
63       * @param baseDir the directory from where to start scanning for deletion candidate files
64       * @param relativePath the candidate for deletion. This path is relative to the baseDir.
65       * @param attrs attributes of the candidate path
66       * @return {@code true} if all the specified conditions accept the specified path, {@code false} otherwise
67       * @throws NullPointerException if any of the parameters is {@code null}
68       */
69      public static boolean accept(final PathCondition[] list, final Path baseDir, final Path relativePath,
70              final BasicFileAttributes attrs) {
71          for (final PathCondition component : list) {
72              if (!component.accept(baseDir, relativePath, attrs)) {
73                  return false;
74              }
75          }
76          return true;
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
83       */
84      @Override
85      public void beforeFileTreeWalk() {
86          beforeFileTreeWalk(components);
87      }
88  
89      /**
90       * Calls {@link #beforeFileTreeWalk()} on all of the specified nested conditions.
91       * 
92       * @param nestedConditions the conditions to call {@link #beforeFileTreeWalk()} on
93       */
94      public static void beforeFileTreeWalk(final PathCondition[] nestedConditions) {
95          for (final PathCondition condition : nestedConditions) {
96              condition.beforeFileTreeWalk();
97          }
98      }
99  
100     /**
101      * Create a Composite PathCondition whose components all need to accept before this condition accepts.
102      * 
103      * @param components The component filters.
104      * @return A Composite PathCondition.
105      */
106     @PluginFactory
107     public static IfAll createAndCondition( //
108             @PluginElement("PathConditions") final PathCondition... components) {
109         return new IfAll(components);
110     }
111 
112     @Override
113     public String toString() {
114         return "IfAll" + Arrays.toString(components);
115     }
116 }