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.rolling;
19  
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Properties;
23  import java.util.Set;
24  
25  import org.apache.log4j.Appender;
26  import org.apache.log4j.spi.LoggingEvent;
27  import org.apache.log4j.spi.OptionHandler;
28  import org.apache.log4j.xml.UnrecognizedElementHandler;
29  import org.w3c.dom.Element;
30  
31  
32  /***
33   * CompositeTriggeringPolicy determines if rolling should be triggered
34   * by evaluating the current event against a set of triggering policies.
35   * 
36   * TriggeringPolicy results are OR'd together - if any of the triggering policies report rolling should occur,  
37   * a rolling event will be triggered.
38   *
39   */
40  public final class CompositeTriggeringPolicy implements TriggeringPolicy, UnrecognizedElementHandler {
41    Set triggeringPolicies = new HashSet();
42  
43    public CompositeTriggeringPolicy() {
44    }
45  
46    public boolean isTriggeringEvent(final Appender appender, final LoggingEvent event, final String file, final long fileLength) {
47      boolean isTriggered = false;
48      for (Iterator iter = triggeringPolicies.iterator();iter.hasNext();) {
49          boolean result = ((TriggeringPolicy)iter.next()).isTriggeringEvent(appender, event, file, fileLength);
50          isTriggered = isTriggered || result;
51      }
52      return isTriggered;
53    }
54  
55    /***
56     * Add triggering policy
57     * 
58     * @param policy
59     */
60    public void addTriggeringPolicy(final TriggeringPolicy policy) {
61      triggeringPolicies.add(policy);
62    }
63  
64    public void activateOptions() {
65      for (Iterator iter = triggeringPolicies.iterator();iter.hasNext();) {
66        ((TriggeringPolicy)iter.next()).activateOptions();
67      }
68    }
69  
70    public boolean parseUnrecognizedElement(final Element element, final Properties props) throws Exception {
71      final String nodeName = element.getNodeName();
72      if ("triggeringPolicy".equals(nodeName)) {
73        OptionHandler policy = org.apache.log4j.extras.DOMConfigurator.parseElement(element, props, TriggeringPolicy.class);
74        if (policy instanceof TriggeringPolicy) {
75          policy.activateOptions();
76          addTriggeringPolicy((TriggeringPolicy)policy);
77        }
78        return true;
79      }
80      return false;
81    }
82  }