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