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