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.Core;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginElement;
27  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
28  
29  /**
30   * Composite {@code PathCondition} that accepts objects that are accepted by <em>any</em> component conditions.
31   * Corresponds to logical "OR".
32   */
33  @Plugin(name = "IfAny", category = Core.CATEGORY_NAME, printObject = true)
34  public final class IfAny implements PathCondition {
35  
36      private final PathCondition[] components;
37  
38      private IfAny(final PathCondition... filters) {
39          this.components = Objects.requireNonNull(filters, "filters");
40      }
41  
42      public PathCondition[] getDeleteFilters() {
43          return components;
44      }
45  
46      /*
47       * (non-Javadoc)
48       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#accept(java.nio.file.Path, java.nio.file.Path, java.nio.file.attribute.BasicFileAttributes)
49       */
50      @Override
51      public boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs) {
52          for (final PathCondition component : components) {
53              if (component.accept(baseDir, relativePath, attrs)) {
54                  return true;
55              }
56          }
57          return false;
58      }
59  
60      /*
61       * (non-Javadoc)
62       * @see org.apache.logging.log4j.core.appender.rolling.action.PathCondition#beforeFileTreeWalk()
63       */
64      @Override
65      public void beforeFileTreeWalk() {
66          for (final PathCondition condition : components) {
67              condition.beforeFileTreeWalk();
68          }
69      }
70  
71      /**
72       * Create a Composite PathCondition: accepts if any of the nested conditions accepts.
73       *
74       * @param components The component conditions.
75       * @return A Composite PathCondition.
76       */
77      @PluginFactory
78      public static IfAny createOrCondition(
79              @PluginElement("PathConditions") final PathCondition... components) {
80          return new IfAny(components);
81      }
82  
83      @Override
84      public String toString() {
85          return "IfAny" + Arrays.toString(components);
86      }
87  }