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 org.apache.log4j.spi.LoggingEvent;
21  import org.apache.log4j.spi.LoggingEventFieldResolver;
22  
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.Stack;
27  
28  
29  /***
30   * A Rule class implementing inequality evaluation.
31   * expects to be able to convert two values to longs.
32   * If a specific inequality evaluation class has been provided
33   * for the event field, the appropriate rule is returned.
34   * (For example, if the expression is Level &lt DEBUG,
35   * a LevelInequalityRule is returned).
36   *
37   * @author Scott Deboy (sdeboy@apache.org)
38   */
39  public class InequalityRule extends AbstractRule {
40      /***
41       * Serialization ID.
42       */
43    static final long serialVersionUID = -5592986598528885122L;
44      /***
45       * field RESOLVER.
46       */
47    private static final LoggingEventFieldResolver RESOLVER =
48            LoggingEventFieldResolver.getInstance();
49      /***
50       * Field name.
51       */
52    private final String field;
53      /***
54       * Comparison value.
55       */
56    private final String value;
57      /***
58       * Inequality symbol.
59       */
60    private final String inequalitySymbol;
61  
62      /***
63       * Create new instance.
64       * @param inequalitySymbol inequality symbol.
65       * @param field field
66       * @param value comparison value.
67       */
68    private InequalityRule(
69      final String inequalitySymbol,
70      final String field,
71      final String value) {
72      super();
73      this.inequalitySymbol = inequalitySymbol;
74      if (!RESOLVER.isField(field)) {
75          throw new IllegalArgumentException("Invalid " + inequalitySymbol
76                  + " rule - " + field + " is not a supported field");
77      }
78  
79      this.field = field;
80      this.value = value;
81    }
82  
83      /***
84       * Create new instance from top two elements on stack.
85       * @param inequalitySymbol inequality symbol.
86       * @param stack stack.
87       * @return rule.
88       */
89    public static Rule getRule(final String inequalitySymbol,
90                               final Stack stack) {
91        if (stack.size() < 2) {
92            throw new IllegalArgumentException("Invalid " + inequalitySymbol
93                    + " rule - expected two parameters but received "
94                    + stack.size());
95        } 
96  
97        String p2 = stack.pop().toString();
98        String p1 = stack.pop().toString();
99        return getRule(inequalitySymbol, p1, p2);
100   }
101 
102     /***
103      * Create new instance from top two elements on stack.
104      * @param inequalitySymbol inequality symbol.
105      * @param field field.
106      * @param value comparison value.
107      * @return rule.
108      */
109   public static Rule getRule(final String inequalitySymbol,
110                              final String field,
111                              final String value) {
112     if (field.equalsIgnoreCase(LoggingEventFieldResolver.LEVEL_FIELD)) {
113       //push the value back on the stack and
114         // allow the level-specific rule pop values
115       return LevelInequalityRule.getRule(inequalitySymbol, value);
116     } else if (
117             field.equalsIgnoreCase(LoggingEventFieldResolver.TIMESTAMP_FIELD)) {
118       return TimestampInequalityRule.getRule(inequalitySymbol, value);
119     } else {
120       return new InequalityRule(inequalitySymbol, field, value);
121     }
122   }
123 
124     /*** {@inheritDoc} */
125   public boolean evaluate(final LoggingEvent event, Map matches) {
126     long first = 0;
127       try {
128           first = new Long(RESOLVER.getValue(field, event).toString()).longValue();
129     } catch (NumberFormatException nfe) {
130       return false;
131     }
132 
133     long second = 0;
134 
135     try {
136       second = new Long(value).longValue();
137     } catch (NumberFormatException nfe) {
138       return false;
139     }
140 
141     boolean result = false;
142 
143     if ("<".equals(inequalitySymbol)) {
144       result = first < second;
145     } else if (">".equals(inequalitySymbol)) {
146       result = first > second;
147     } else if ("<=".equals(inequalitySymbol)) {
148       result = first <= second;
149     } else if (">=".equals(inequalitySymbol)) {
150       result = first >= second;
151     }
152     if (result && matches != null) {
153         Set entries = (Set) matches.get(field.toUpperCase());
154         if (entries == null) {
155             entries = new HashSet();
156             matches.put(field.toUpperCase(), entries);
157         }
158         entries.add(String.valueOf(first));
159     }
160     return result;
161   }
162 }