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  
18  package org.apache.log4j.rule;
19  
20  import java.util.HashSet;
21  import java.util.Map;
22  import java.util.Set;
23  import java.util.Stack;
24  
25  import org.apache.log4j.spi.LoggingEvent;
26  import org.apache.log4j.spi.LoggingEventFieldResolver;
27  
28  
29  /***
30   * A Rule class implementing not equals against two strings.
31   *
32   * @author Scott Deboy (sdeboy@apache.org)
33   */
34  public class NotEqualsRule extends AbstractRule {
35      /***
36       * Serialization ID.
37       */
38    static final long serialVersionUID = -1135478467213793211L;
39      /***
40       * Resolver.
41       */
42    private static final LoggingEventFieldResolver RESOLVER =
43      LoggingEventFieldResolver.getInstance();
44      /***
45       * Field.
46       */
47    private final String field;
48      /***
49       * Value.
50       */
51    private final String value;
52  
53      /***
54       * Create new instance.
55       * @param field field
56       * @param value value
57       */
58    private NotEqualsRule(final String field, final String value) {
59      super();
60      if (!RESOLVER.isField(field)) {
61        throw new IllegalArgumentException(
62          "Invalid NOT EQUALS rule - " + field + " is not a supported field");
63      }
64  
65      this.field = field;
66      this.value = value;
67    }
68  
69      /***
70       * Get new instance.
71       * @param field field
72       * @param value value
73       * @return new instance.
74       */
75    public static Rule getRule(final String field, final String value) {
76      if (field.equalsIgnoreCase(LoggingEventFieldResolver.LEVEL_FIELD)) {
77          return NotLevelEqualsRule.getRule(value);
78      } else {
79          return new NotEqualsRule(field, value);
80      }
81    }
82  
83      /***
84       * Get new instance from top two elements of stack.
85       * @param stack stack.
86       * @return new instance.
87       */
88    public static Rule getRule(final Stack stack) {
89      if (stack.size() < 2) {
90        throw new IllegalArgumentException(
91          "Invalid NOT EQUALS rule - expected two parameters but received "
92          + stack.size());
93      }
94  
95      String p2 = stack.pop().toString();
96      String p1 = stack.pop().toString();
97  
98      if (p1.equalsIgnoreCase(LoggingEventFieldResolver.LEVEL_FIELD)) {
99          return NotLevelEqualsRule.getRule(p2);
100     } else {
101         return new NotEqualsRule(p1, p2);
102     }
103   }
104 
105     /*** {@inheritDoc} */
106   public boolean evaluate(final LoggingEvent event, Map matches) {
107     Object p2 = RESOLVER.getValue(field, event);
108 
109     boolean result = (p2 != null) && !(p2.toString().equals(value));
110     if (result && matches != null) {
111         //not equals - add the text that isn't equal (p2)
112         Set entries = (Set) matches.get(field.toUpperCase());
113         if (entries == null) {
114             entries = new HashSet();
115             matches.put(field.toUpperCase(), entries);
116         }
117         entries.add(value);
118     }
119     return result;
120   }
121 }