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  
23  import org.apache.log4j.rule.AbstractRule;
24  import org.apache.log4j.rule.Rule;
25  import org.apache.log4j.spi.LoggingEvent;
26  
27  
28  /***
29   * A mediator class that implements the Rule interface, by combining several 
30   * optional rules used by Chainsaw's filtering GUI's into a single Rule.
31   *
32   * <p>This class is based upon the concept of Inclusion, Exclusion and Refinement.
33   * By default, this class accepts all events by returning true as part of the 
34   * Rule interface, unless the Inclusion/Exclusion/Refinement sub-rules have been
35   * configured.
36   *
37   * <p>The sub-rules are queried in this order: Inclusion, Refinement, Exclusion.  
38   * If any are null, that particular sub-rule is not queried.  If any of the 
39   * sub-rules returns false, this mediator returns false immediately, otherwise
40   * they are queried in that order to ensure the overall rule evaluates.
41   *
42   * <p>Setting the individual sub-rules propagates a PropertyChangeEvent as per 
43   * standard Java beans principles.
44   *
45   * @author Paul Smith <psmith@apache.org>
46   * @author Scott Deboy <sdeboy@apache.org>
47   */
48  public class RuleMediator extends AbstractRule implements Rule {
49    private Rule inclusionRule;
50    private Rule loggerRule;
51    private Rule refinementRule;
52    private Rule exclusionRule;
53    private final PropertyChangeListener ruleChangerNotifier =
54      new RuleChangerNotifier();
55  
56    /* (non-Javadoc)
57     * @see org.apache.log4j.chainsaw.rule.Rule#evaluate(org.apache.log4j.spi.LoggingEvent)
58     */
59    public boolean evaluate(LoggingEvent e) {
60      boolean accepts = true;
61  
62      if (inclusionRule != null) {
63        accepts = inclusionRule.evaluate(e);
64      }
65  
66      if (!accepts) {
67        return false;
68      }
69  
70      if (loggerRule != null) {
71        accepts = loggerRule.evaluate(e);
72      }
73  
74      if (!accepts) {
75        return false;
76      }
77  
78      if (refinementRule != null) {
79        accepts = refinementRule.evaluate(e);
80      }
81  
82      if (!accepts) {
83        return false;
84      }
85  
86      if (exclusionRule != null) {
87        accepts = exclusionRule.evaluate(e);
88      }
89  
90      return accepts;
91    }
92  
93    /***
94     * Sets the Inclusion rule to be used, and fires a PropertyChangeEvent to listeners
95     * @param r
96     */
97    public void setInclusionRule(Rule r) {
98      Rule oldRule = this.inclusionRule;
99      this.inclusionRule = r;
100     firePropertyChange("inclusionRule", oldRule, this.inclusionRule);
101   }
102 
103   /***
104    * Sets the Refinement rule to be used, and fires a PropertyChangeEvent to listeners
105    * @param r
106    */
107   public void setRefinementRule(Rule r) {
108     Rule oldRefinementRule = this.refinementRule;
109     this.refinementRule = r;
110     firePropertyChange(
111       "refinementRule", oldRefinementRule, this.refinementRule);
112   }
113 
114   public void setLoggerRule(Rule r) {
115     Rule oldLoggerRule = this.loggerRule;
116     this.loggerRule = r;
117     if(oldLoggerRule!=null){
118       oldLoggerRule.removePropertyChangeListener(ruleChangerNotifier);
119     }
120     this.loggerRule.addPropertyChangeListener(ruleChangerNotifier);
121     firePropertyChange("loggerRule", oldLoggerRule, this.loggerRule);
122   }
123 
124   /***
125    * Sets the Exclusion rule to be used, and fires a PropertyChangeEvent to listeners.
126    *
127    * @param r
128    */
129   public void setExclusionRule(Rule r) {
130     Rule oldExclusionRule = this.exclusionRule;
131     this.exclusionRule = r;
132     firePropertyChange("exclusionRule", oldExclusionRule, this.exclusionRule);
133   }
134 
135   /***
136    * @return exclusion rule
137    */
138   public final Rule getExclusionRule() {
139     return exclusionRule;
140   }
141 
142   /***
143    * @return inclusion rule
144    */
145   public final Rule getInclusionRule() {
146     return inclusionRule;
147   }
148 
149   /***
150    * @return logger rule
151    */
152   public final Rule getLoggerRule() {
153     return loggerRule;
154   }
155 
156   /***
157    * @return refinement rule
158    */
159   public final Rule getRefinementRule() {
160     return refinementRule;
161   }
162 
163   /***
164    * Helper class that propagates internal Rules propertyChange events
165    * to external parties, since an internal rule changing really means
166    * this outter rule is going to change too.
167    */
168   private class RuleChangerNotifier implements PropertyChangeListener {
169     /* (non-Javadoc)
170      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
171      */
172     public void propertyChange(PropertyChangeEvent evt) {
173       RuleMediator.this.firePropertyChange(evt);
174     }
175   }
176 }