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.chainsaw;
19  
20  import org.apache.log4j.rule.AbstractRule;
21  import org.apache.log4j.rule.Rule;
22  import org.apache.log4j.spi.LoggingEvent;
23  
24  import java.beans.PropertyChangeEvent;
25  import java.beans.PropertyChangeListener;
26  import java.util.Map;
27  
28  
29  /**
30   * A mediator class that implements the Rule interface, by combining several
31   * optional rules used by Chainsaw's filtering GUI's into a single Rule.
32   * <p>
33   * <p>Setting the individual sub-rules propagates a PropertyChangeEvent as per
34   * standard Java beans principles.
35   *
36   * @author Paul Smith &lt;psmith@apache.org&gt;
37   * @author Scott Deboy &lt;sdeboy@apache.org&gt;
38   */
39  public class RuleMediator extends AbstractRule {
40      private Rule loggerRule;
41      private Rule filterRule;
42      private Rule findRule;
43      private final PropertyChangeListener ruleChangerNotifier = new RuleChangerNotifier();
44      private boolean findRuleRequired;
45  
46      public RuleMediator(boolean findRuleRequired) {
47          this.findRuleRequired = findRuleRequired;
48      }
49  
50      /* (non-Javadoc)
51       * @see org.apache.log4j.chainsaw.rule.Rule#evaluate(org.apache.log4j.spi.LoggingEvent)
52       */
53      public boolean evaluate(LoggingEvent e, Map matches) {
54          if (findRuleRequired) {
55              if (findRule == null) {
56                  return false;
57              }
58              if (!findRule.evaluate(e, null)) {
59                  return false;
60              }
61          }
62  
63          return (loggerRule == null || loggerRule.evaluate(e, null)) && (filterRule == null || filterRule.evaluate(e, null));
64  
65      }
66  
67      public boolean isFindRuleRequired() {
68          return findRuleRequired;
69      }
70  
71      public void setFilterRule(Rule r) {
72          Rule oldFilterRule = this.filterRule;
73          this.filterRule = r;
74          firePropertyChange("filterRule", oldFilterRule, this.filterRule);
75      }
76  
77      public void setFindRule(Rule r) {
78          Rule oldFindRule = this.findRule;
79          this.findRule = r;
80          firePropertyChange("findRule", oldFindRule, this.findRule);
81      }
82  
83      public void setLoggerRule(Rule r) {
84          Rule oldLoggerRule = this.loggerRule;
85          this.loggerRule = r;
86          if (oldLoggerRule != null) {
87              oldLoggerRule.removePropertyChangeListener(ruleChangerNotifier);
88          }
89          this.loggerRule.addPropertyChangeListener(ruleChangerNotifier);
90          firePropertyChange("loggerRule", oldLoggerRule, this.loggerRule);
91      }
92  
93      /**
94       * Helper class that propagates internal Rules propertyChange events
95       * to external parties, since an internal rule changing really means
96       * this outter rule is going to change too.
97       */
98      private class RuleChangerNotifier implements PropertyChangeListener {
99          /* (non-Javadoc)
100          * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
101          */
102         public void propertyChange(PropertyChangeEvent evt) {
103             RuleMediator.this.firePropertyChange(evt);
104         }
105     }
106 }