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", category = "Core", printObject = true)
028    public final class CompositeTriggeringPolicy implements TriggeringPolicy {
029    
030        private final TriggeringPolicy[] policies;
031    
032        private CompositeTriggeringPolicy(final TriggeringPolicy... policies) {
033            this.policies = policies;
034        }
035    
036        /**
037         * Initializes the policy.
038         * @param manager The RollingFileManager.
039         */
040        @Override
041        public void initialize(final RollingFileManager manager) {
042            for (final TriggeringPolicy policy : policies) {
043                policy.initialize(manager);
044            }
045        }
046    
047        /**
048         * Determines if a rollover should occur.
049         * @param event A reference to the currently event.
050         * @return true if a rollover should occur, false otherwise.
051         */
052        @Override
053        public boolean isTriggeringEvent(final LogEvent event) {
054            for (final TriggeringPolicy policy : policies) {
055                if (policy.isTriggeringEvent(event)) {
056                    return true;
057                }
058            }
059            return false;
060        }
061    
062        @Override
063        public String toString() {
064            final StringBuilder sb = new StringBuilder("CompositeTriggeringPolicy{");
065            boolean first = true;
066            for (final TriggeringPolicy policy : policies) {
067                if (!first) {
068                    sb.append(", ");
069                }
070                sb.append(policy.toString());
071                first = false;
072            }
073            sb.append('}');
074            return sb.toString();
075        }
076    
077        /**
078         * Create a CompositeTriggeringPolicy.
079         * @param policies The triggering policies.
080         * @return A CompositeTriggeringPolicy.
081         */
082        @PluginFactory
083        public static CompositeTriggeringPolicy createPolicy(
084                                                    @PluginElement("Policies") final TriggeringPolicy... policies) {
085            return new CompositeTriggeringPolicy(policies);
086        }
087    }