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 implementing case-insensitive
28 * partial-text matches against two strings.
29 *
30 * @author Scott Deboy (sdeboy@apache.org)
31 */
32 public class PartialTextMatchRule extends AbstractRule {
33 /***
34 * Serialization ID.
35 */
36 static final long serialVersionUID = 6963284773637727558L;
37 /***
38 * Resolver.
39 */
40 private static final LoggingEventFieldResolver RESOLVER =
41 LoggingEventFieldResolver.getInstance();
42 /***
43 * Field.
44 */
45 private final String field;
46 /***
47 * Value.
48 */
49 private final String value;
50
51 /***
52 * Create new instance.
53 * @param field field
54 * @param value value
55 */
56 private PartialTextMatchRule(final String field, final String value) {
57 super();
58 if (!RESOLVER.isField(field)) {
59 throw new IllegalArgumentException(
60 "Invalid partial text rule - " + field + " is not a supported field");
61 }
62
63 this.field = field;
64 this.value = value;
65 }
66
67 /***
68 * Create new instance.
69 * @param field field
70 * @param value value
71 * @return new instance
72 */
73 public static Rule getRule(final String field, final String value) {
74 return new PartialTextMatchRule(field, value);
75 }
76
77 /***
78 * Create new instance from top two elements of stack.
79 * @param stack stack
80 * @return new instance
81 */
82 public static Rule getRule(final Stack stack) {
83 if (stack.size() < 2) {
84 throw new IllegalArgumentException(
85 "invalid partial text rule - expected two parameters but received "
86 + stack.size());
87 }
88
89 String p2 = stack.pop().toString();
90 String p1 = stack.pop().toString();
91
92 return new PartialTextMatchRule(p1, p2);
93 }
94
95 /*** {@inheritDoc} */
96 public boolean evaluate(final LoggingEvent event) {
97 Object p2 = RESOLVER.getValue(field, event);
98
99 return ((p2 != null) && (value != null)
100 && (p2.toString().toLowerCase().indexOf(value.toLowerCase()) > -1));
101 }
102 }