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 java.beans.PropertyChangeEvent;
21  import java.beans.PropertyChangeListener;
22  import java.util.Map;
23  
24  import org.apache.log4j.rule.AbstractRule;
25  import org.apache.log4j.rule.Rule;
26  import org.apache.log4j.spi.LoggingEvent;
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   *
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    /* (non-Javadoc)
50     * @see org.apache.log4j.chainsaw.rule.Rule#evaluate(org.apache.log4j.spi.LoggingEvent)
51     */
52    public boolean evaluate(LoggingEvent e, Map matches) {
53      if (findRuleRequired) {
54        if (findRule == null) {
55          return false;
56        }
57        if (!findRule.evaluate(e, null)) {
58          return false;
59        }
60      }
61  
62      if (loggerRule != null && !loggerRule.evaluate(e, null)) {
63        return false;
64      }
65  
66      if (filterRule != null && !filterRule.evaluate(e, null)) {
67        return false;
68      }
69  
70      return true;
71    }
72  
73    public boolean isFindRuleRequired() {
74      return findRuleRequired;
75    }
76    
77    public void setFilterRule(Rule r) {
78      Rule oldFilterRule = this.filterRule;
79      this.filterRule = r;
80      firePropertyChange("filterRule", oldFilterRule, this.filterRule);
81    }
82  
83    public void setFindRule(Rule r) {
84      Rule oldFindRule = this.findRule;
85      this.findRule = r;
86      firePropertyChange("findRule", oldFindRule, this.findRule);
87    }
88  
89    public void setLoggerRule(Rule r) {
90      Rule oldLoggerRule = this.loggerRule;
91      this.loggerRule = r;
92      if(oldLoggerRule!=null){
93        oldLoggerRule.removePropertyChangeListener(ruleChangerNotifier);
94      }
95      this.loggerRule.addPropertyChangeListener(ruleChangerNotifier);
96      firePropertyChange("loggerRule", oldLoggerRule, this.loggerRule);
97    }
98  
99    /**
100    * Helper class that propagates internal Rules propertyChange events
101    * to external parties, since an internal rule changing really means
102    * this outter rule is going to change too.
103    */
104   private class RuleChangerNotifier implements PropertyChangeListener {
105     /* (non-Javadoc)
106      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
107      */
108     public void propertyChange(PropertyChangeEvent evt) {
109       RuleMediator.this.firePropertyChange(evt);
110     }
111   }
112 }