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 a not null (and not empty string) check.
28 *
29 * @author Scott Deboy (sdeboy@apache.org)
30 */
31 public class ExistsRule extends AbstractRule {
32 /***
33 * Serialization id.
34 */
35 static final long serialVersionUID = -5386265224649967464L;
36 /***
37 * field resolver.
38 */
39 private static final LoggingEventFieldResolver RESOLVER =
40 LoggingEventFieldResolver.getInstance();
41 /***
42 * field name.
43 */
44 private final String field;
45
46 /***
47 * Create new instance.
48 * @param fld field name.
49 */
50 private ExistsRule(final String fld) {
51 super();
52 if (!RESOLVER.isField(fld)) {
53 throw new IllegalArgumentException(
54 "Invalid EXISTS rule - " + fld + " is not a supported field");
55 }
56
57 this.field = fld;
58 }
59
60 /***
61 * Get an instance of ExistsRule.
62 * @param field field.
63 * @return instance of ExistsRule.
64 */
65 public static Rule getRule(final String field) {
66 return new ExistsRule(field);
67 }
68
69 /***
70 * Create an instance of ExistsRule using the
71 * top name on the stack.
72 * @param stack stack
73 * @return instance of ExistsRule.
74 */
75 public static Rule getRule(final Stack stack) {
76 if (stack.size() < 1) {
77 throw new IllegalArgumentException(
78 "Invalid EXISTS rule - expected one parameter but received "
79 + stack.size());
80 }
81
82 return new ExistsRule(stack.pop().toString());
83 }
84
85 /***
86 * {@inheritDoc}
87 */
88 public boolean evaluate(final LoggingEvent event) {
89 Object p2 = RESOLVER.getValue(field, event);
90
91 return (!((p2 == null) || ((p2 != null) && p2.toString().equals(""))));
92 }
93 }