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.action;
018    
019    import java.io.IOException;
020    import java.util.Arrays;
021    import java.util.List;
022    
023    
024    /**
025     * A group of Actions to be executed in sequence.
026     */
027    public class CompositeAction extends AbstractAction {
028        /**
029         * Actions to perform.
030         */
031        private final Action[] actions;
032    
033        /**
034         * Stop on error.
035         */
036        private final boolean stopOnError;
037    
038        /**
039         * Construct a new composite action.
040         *
041         * @param actions     list of actions, may not be null.
042         * @param stopOnError if true, stop on the first false return value or exception.
043         */
044        public CompositeAction(final List<Action> actions,
045                               final boolean stopOnError) {
046            this.actions = new Action[actions.size()];
047            actions.toArray(this.actions);
048            this.stopOnError = stopOnError;
049        }
050    
051        /**
052         * {@inheritDoc}
053         */
054        @Override
055        public void run() {
056            try {
057                execute();
058            } catch (final IOException ex) {
059                LOGGER.warn("Exception during file rollover.", ex);
060            }
061        }
062    
063        /**
064         * Execute sequence of actions.
065         *
066         * @return true if all actions were successful.
067         * @throws IOException on IO error.
068         */
069        @Override
070        public boolean execute() throws IOException {
071            if (stopOnError) {
072                for (final Action action : actions) {
073                    if (!action.execute()) {
074                        return false;
075                    }
076                }
077    
078                return true;
079            }
080            boolean status = true;
081            IOException exception = null;
082    
083            for (final Action action : actions) {
084                try {
085                    status &= action.execute();
086                } catch (final IOException ex) {
087                    status = false;
088    
089                    if (exception == null) {
090                        exception = ex;
091                    }
092                }
093            }
094    
095            if (exception != null) {
096                throw exception;
097            }
098    
099            return status;
100        }
101        
102        @Override
103        public String toString() {
104            return CompositeAction.class.getSimpleName() + Arrays.toString(actions);
105        }
106    }