001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender.rolling;
018
019import java.util.Arrays;
020import java.util.concurrent.TimeUnit;
021
022import org.apache.logging.log4j.core.Core;
023import org.apache.logging.log4j.core.LifeCycle;
024import org.apache.logging.log4j.core.LifeCycle2;
025import org.apache.logging.log4j.core.LogEvent;
026import org.apache.logging.log4j.core.config.plugins.Plugin;
027import org.apache.logging.log4j.core.config.plugins.PluginElement;
028import org.apache.logging.log4j.core.config.plugins.PluginFactory;
029
030/**
031 * Triggering policy that wraps other triggering policies.
032 */
033@Plugin(name = "Policies", category = Core.CATEGORY_NAME, printObject = true)
034public final class CompositeTriggeringPolicy extends AbstractTriggeringPolicy {
035
036    private final TriggeringPolicy[] triggeringPolicies;
037
038    private CompositeTriggeringPolicy(final TriggeringPolicy... triggeringPolicies) {
039        this.triggeringPolicies = triggeringPolicies;
040    }
041
042    public TriggeringPolicy[] getTriggeringPolicies() {
043        return triggeringPolicies;
044    }
045
046    /**
047     * Initializes the policy.
048     * @param manager The RollingFileManager.
049     */
050    @Override
051    public void initialize(final RollingFileManager manager) {
052        for (final TriggeringPolicy triggeringPolicy : triggeringPolicies) {
053            triggeringPolicy.initialize(manager);
054        }
055    }
056
057    /**
058     * Determines if a rollover should occur.
059     * @param event A reference to the currently event.
060     * @return true if a rollover should occur, false otherwise.
061     */
062    @Override
063    public boolean isTriggeringEvent(final LogEvent event) {
064        for (final TriggeringPolicy triggeringPolicy : triggeringPolicies) {
065            if (triggeringPolicy.isTriggeringEvent(event)) {
066                return true;
067            }
068        }
069        return false;
070    }
071
072    /**
073     * Creates a CompositeTriggeringPolicy.
074     * @param triggeringPolicy The triggering policies.
075     * @return A CompositeTriggeringPolicy.
076     */
077    @PluginFactory
078    public static CompositeTriggeringPolicy createPolicy(
079                                                @PluginElement("Policies") final TriggeringPolicy... triggeringPolicy) {
080        return new CompositeTriggeringPolicy(triggeringPolicy);
081    }
082
083    @Override
084    public boolean stop(final long timeout, final TimeUnit timeUnit) {
085        setStopping();
086        boolean stopped = true;
087        for (final TriggeringPolicy triggeringPolicy : triggeringPolicies) {
088            if (triggeringPolicy instanceof LifeCycle2) {
089                stopped &= ((LifeCycle2) triggeringPolicy).stop(timeout, timeUnit);
090            } else if (triggeringPolicy instanceof LifeCycle) {
091                ((LifeCycle) triggeringPolicy).stop();
092                stopped &= true;
093            }
094        }
095        setStopped();
096        return stopped;
097    }
098
099    @Override
100    public String toString() {
101        return "CompositeTriggeringPolicy(policies=" + Arrays.toString(triggeringPolicies) + ")";
102    }
103
104}