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
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.Stack;
28
29
30 /***
31 * A Rule class implementing a logical 'and'.
32 *
33 * @author Scott Deboy (sdeboy@apache.org)
34 */
35 public class AndRule extends AbstractRule {
36 /***
37 * First rule.
38 */
39 private final Rule firstRule;
40 /***
41 * Second rule.
42 */
43 private final Rule secondRule;
44 /***
45 * Serialization id.
46 */
47 static final long serialVersionUID = -8233444426923854651L;
48
49 /***
50 * Create new instance.
51 * @param first first rule.
52 * @param second second rule.
53 */
54 private AndRule(final Rule first, final Rule second) {
55 super();
56 this.firstRule = first;
57 this.secondRule = second;
58 }
59
60 /***
61 * Create rule from top two elements of stack.
62 * @param stack stack of rules.
63 * @return Rule that evaluates true only if both rules are true.
64 */
65 public static Rule getRule(final Stack stack) {
66 if (stack.size() < 2) {
67 throw new IllegalArgumentException(
68 "Invalid AND rule - expected two rules but received "
69 + stack.size());
70 }
71 Object o2 = stack.pop();
72 Object o1 = stack.pop();
73 if ((o2 instanceof Rule) && (o1 instanceof Rule)) {
74 Rule p2 = (Rule) o2;
75 Rule p1 = (Rule) o1;
76 return new AndRule(p1, p2);
77 }
78 throw new IllegalArgumentException("Invalid AND rule: " + o2 + "..." + o1);
79 }
80
81 /***
82 * Get rule.
83 * @param firstParam first rule.
84 * @param secondParam second rule.
85 * @return Rule that evaluates true only if both rules are true.
86 */
87 public static Rule getRule(final Rule firstParam, final Rule secondParam) {
88 return new AndRule(firstParam, secondParam);
89 }
90
91 /***
92 * {@inheritDoc}
93 */
94 public boolean evaluate(final LoggingEvent event, Map matches) {
95 if (matches == null) {
96 return firstRule.evaluate(event, null) && secondRule.evaluate(event, null);
97 }
98 Map tempMatches1 = new HashMap();
99 Map tempMatches2 = new HashMap();
100 boolean result = firstRule.evaluate(event, tempMatches1) && secondRule.evaluate(event, tempMatches2);
101 if (result) {
102 for (Iterator iter = tempMatches1.entrySet().iterator();iter.hasNext();) {
103 Map.Entry entry = (Map.Entry)iter.next();
104 Object key = entry.getKey();
105 Set value = (Set)entry.getValue();
106 Set mainSet = (Set) matches.get(key);
107 if (mainSet == null) {
108 mainSet = new HashSet();
109 matches.put(key, mainSet);
110 }
111 mainSet.addAll(value);
112 }
113 for (Iterator iter = tempMatches2.entrySet().iterator();iter.hasNext();) {
114 Map.Entry entry = (Map.Entry)iter.next();
115 Object key = entry.getKey();
116 Set value = (Set)entry.getValue();
117 Set mainSet = (Set) matches.get(key);
118 if (mainSet == null) {
119 mainSet = new HashSet();
120 matches.put(key, mainSet);
121 }
122 mainSet.addAll(value);
123 }
124 }
125 return result;
126 }
127 }