1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 which returns the result of
28 * performing equals against two strings.
29 *
30 * @author Scott Deboy (sdeboy@apache.org)
31 */
32 public class EqualsRule extends AbstractRule {
33 /***
34 * Serialization ID.
35 */
36 static final long serialVersionUID = 1712851553477517245L;
37 /***
38 * Resolver.
39 */
40 private static final LoggingEventFieldResolver RESOLVER =
41 LoggingEventFieldResolver.getInstance();
42 /***
43 * Value.
44 */
45 private final String value;
46 /***
47 * Field.
48 */
49 private final String field;
50
51 /***
52 * Create new instance.
53 * @param field field
54 * @param value value
55 */
56 private EqualsRule(final String field, final String value) {
57 super();
58 if (!RESOLVER.isField(field)) {
59 throw new IllegalArgumentException(
60 "Invalid EQUALS rule - " + field + " is not a supported field");
61 }
62
63 this.field = field;
64 this.value = value;
65 }
66
67 /***
68 * Create new instance from top two elements of stack.
69 * @param stack stack
70 * @return new instance
71 */
72 public static Rule getRule(final Stack stack) {
73 if (stack.size() < 2) {
74 throw new IllegalArgumentException(
75 "Invalid EQUALS rule - expected two parameters but received "
76 + stack.size());
77 }
78
79 String p2 = stack.pop().toString();
80 String p1 = stack.pop().toString();
81
82 return getRule(p1, p2);
83 }
84
85 /***
86 * Create new instance.
87 * @param p1 field, special treatment for level and timestamp.
88 * @param p2 value
89 * @return new instance
90 */
91 public static Rule getRule(final String p1, final String p2) {
92 if (p1.equalsIgnoreCase(LoggingEventFieldResolver.LEVEL_FIELD)) {
93 return LevelEqualsRule.getRule(p2);
94 } else if (p1.equalsIgnoreCase(LoggingEventFieldResolver.TIMESTAMP_FIELD)) {
95 return TimestampEqualsRule.getRule(p2);
96 } else {
97 return new EqualsRule(p1, p2);
98 }
99 }
100
101 /*** {@inheritDoc} */
102 public boolean evaluate(final LoggingEvent event) {
103 Object p2 = RESOLVER.getValue(field, event);
104
105 return ((p2 != null) && p2.toString().equals(value));
106 }
107 }