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;
020
021import org.apache.logging.log4j.core.LogEvent;
022import org.apache.logging.log4j.core.config.plugins.Plugin;
023import org.apache.logging.log4j.core.config.plugins.PluginElement;
024import org.apache.logging.log4j.core.config.plugins.PluginFactory;
025
026/**
027 * Triggering policy that wraps other policies.
028 */
029@Plugin(name = "Policies", category = "Core", printObject = true)
030public final class CompositeTriggeringPolicy implements TriggeringPolicy {
031
032    private final TriggeringPolicy[] policies;
033
034    private CompositeTriggeringPolicy(final TriggeringPolicy... policies) {
035        this.policies = policies;
036    }
037
038    public TriggeringPolicy[] getTriggeringPolicies() {
039        return policies;
040    }
041
042    /**
043     * Initializes the policy.
044     * @param manager The RollingFileManager.
045     */
046    @Override
047    public void initialize(final RollingFileManager manager) {
048        for (final TriggeringPolicy policy : policies) {
049            policy.initialize(manager);
050        }
051    }
052
053    /**
054     * Determines if a rollover should occur.
055     * @param event A reference to the currently event.
056     * @return true if a rollover should occur, false otherwise.
057     */
058    @Override
059    public boolean isTriggeringEvent(final LogEvent event) {
060        for (final TriggeringPolicy policy : policies) {
061            if (policy.isTriggeringEvent(event)) {
062                return true;
063            }
064        }
065        return false;
066    }
067
068    /**
069     * Create a CompositeTriggeringPolicy.
070     * @param policies The triggering policies.
071     * @return A CompositeTriggeringPolicy.
072     */
073    @PluginFactory
074    public static CompositeTriggeringPolicy createPolicy(
075                                                @PluginElement("Policies") final TriggeringPolicy... policies) {
076        return new CompositeTriggeringPolicy(policies);
077    }
078
079    @Override
080    public String toString() {
081        return "CompositeTriggeringPolicy(policies=" + Arrays.toString(policies) + ")";
082    }
083
084}