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.log4j.builders.filter;
18  
19  import org.apache.log4j.bridge.FilterWrapper;
20  import org.apache.log4j.builders.AbstractBuilder;
21  import org.apache.log4j.builders.BooleanHolder;
22  import org.apache.log4j.builders.Holder;
23  import org.apache.log4j.config.PropertiesConfiguration;
24  import org.apache.log4j.spi.Filter;
25  import org.apache.log4j.xml.XmlConfiguration;
26  import org.apache.logging.log4j.Logger;
27  import org.apache.logging.log4j.core.config.plugins.Plugin;
28  import org.apache.logging.log4j.core.filter.StringMatchFilter;
29  import org.apache.logging.log4j.status.StatusLogger;
30  import org.w3c.dom.Element;
31  
32  import java.util.Properties;
33  
34  import static org.apache.log4j.builders.BuilderManager.CATEGORY;
35  import static org.apache.log4j.xml.XmlConfiguration.*;
36  
37  /**
38   * Build a String match filter.
39   */
40  @Plugin(name = "org.apache.log4j.varia.StringMatchFilter", category = CATEGORY)
41  public class StringMatchFilterBuilder extends AbstractBuilder implements FilterBuilder {
42  
43      private static final Logger LOGGER = StatusLogger.getLogger();
44      private static final String STRING_TO_MATCH = "StringToMatch";
45      private static final String ACCEPT_ON_MATCH = "AcceptOnMatch";
46  
47      @Override
48      public Filter parseFilter(Element filterElement, XmlConfiguration config) {
49          final Holder<Boolean> acceptOnMatch = new BooleanHolder();
50          final Holder<String> text = new Holder<>();
51          forEachElement(filterElement.getElementsByTagName("param"), (currentElement) -> {
52              if (currentElement.getTagName().equals("param")) {
53                  switch (currentElement.getAttribute(NAME_ATTR)) {
54                      case STRING_TO_MATCH:
55                          text.set(currentElement.getAttribute(VALUE_ATTR));
56                          break;
57                      case ACCEPT_ON_MATCH:
58                          acceptOnMatch.set(Boolean.parseBoolean(currentElement.getAttribute(VALUE_ATTR)));
59                          break;
60  
61                  }
62              }
63          });
64          return createFilter(text.get(), acceptOnMatch.get());
65      }
66  
67      @Override
68      public Filter parseFilter(PropertiesConfiguration config) {
69          String text = getProperty(STRING_TO_MATCH);
70          boolean acceptOnMatch = getBooleanProperty(ACCEPT_ON_MATCH);
71          return createFilter(text, acceptOnMatch);
72      }
73  
74      private Filter createFilter(String text, boolean acceptOnMatch) {
75          if (text == null) {
76              LOGGER.warn("No text provided for StringMatchFilter");
77              return null;
78          }
79          org.apache.logging.log4j.core.Filter.Result onMatch = acceptOnMatch
80                  ? org.apache.logging.log4j.core.Filter.Result.ACCEPT
81                  : org.apache.logging.log4j.core.Filter.Result.DENY;
82          return new FilterWrapper(StringMatchFilter.newBuilder()
83                  .setMatchString(text)
84                  .setOnMatch(onMatch)
85                  .setOnMismatch(org.apache.logging.log4j.core.Filter.Result.NEUTRAL)
86                  .build());
87      }
88  }