View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache license, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License. You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the license for the specific language governing permissions and
    15   * limitations under the license.
    16   */
    17  package org.apache.logging.log4j.core.appender.rolling.action;
    18
    19  import java.io.IOException;
    20  import java.util.Arrays;
    21  import java.util.List;
    22
    23
    24  /**
    25   * A group of Actions to be executed in sequence.
    26   */
    27  public class CompositeAction extends AbstractAction {
    28      /**
    29       * Actions to perform.
    30       */
    31      private final Action[] actions;
    32
    33      /**
    34       * Stop on error.
    35       */
    36      private final boolean stopOnError;
    37
    38      /**
    39       * Construct a new composite action.
    40       *
    41       * @param actions     list of actions, may not be null.
    42       * @param stopOnError if true, stop on the first false return value or exception.
    43       */
    44      public CompositeAction(final List<Action> actions,
    45                             final boolean stopOnError) {
    46          this.actions = new Action[actions.size()];
    47          actions.toArray(this.actions);
    48          this.stopOnError = stopOnError;
    49      }
    50
    51      /**
    52       * {@inheritDoc}
    53       */
    54      @Override
    55      public void run() {
    56          try {
    57              execute();
    58          } catch (final IOException ex) {
    59              LOGGER.warn("Exception during file rollover.", ex);
    60          }
    61      }
    62
    63      /**
    64       * Execute sequence of actions.
    65       *
    66       * @return true if all actions were successful.
    67       * @throws IOException on IO error.
    68       */
    69      @Override
    70      public boolean execute() throws IOException {
    71          if (stopOnError) {
    72              for (final Action action : actions) {
    73                  if (!action.execute()) {
    74                      return false;
    75                  }
    76              }
    77
    78              return true;
    79          }
    80          boolean status = true;
    81          IOException exception = null;
    82
    83          for (final Action action : actions) {
    84              try {
    85                  status &= action.execute();
    86              } catch (final IOException ex) {
    87                  status = false;
    88
    89                  if (exception == null) {
    90                      exception = ex;
    91                  }
    92              }
    93          }
    94
    95          if (exception != null) {
    96              throw exception;
    97          }
    98
    99          return status;
    100     }
    101
    102     @Override
    103     public String toString() {
    104         return CompositeAction.class.getSimpleName() + Arrays.toString(actions);
    105     }
    106 }