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 /**
30 * Actions to perform.
31 */
32 private final Action[] actions;
33
34 /**
35 * Stop on error.
36 */
37 private final boolean stopOnError;
38
39 /**
40 * Construct a new composite action.
41 *
42 * @param actions list of actions, may not be null.
43 * @param stopOnError if true, stop on the first false return value or exception.
44 */
45 public CompositeAction(final List<Action> actions,
46 final boolean stopOnError) {
47 this.actions = new Action[actions.size()];
48 actions.toArray(this.actions);
49 this.stopOnError = stopOnError;
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 @Override
56 public void run() {
57 try {
58 execute();
59 } catch (final IOException ex) {
60 LOGGER.warn("Exception during file rollover.", ex);
61 }
62 }
63
64 /**
65 * Execute sequence of actions.
66 *
67 * @return true if all actions were successful.
68 * @throws IOException on IO error.
69 */
70 @Override
71 public boolean execute() throws IOException {
72 if (stopOnError) {
73 for (final Action action : actions) {
74 if (!action.execute()) {
75 return false;
76 }
77 }
78
79 return true;
80 }
81 boolean status = true;
82 IOException exception = null;
83
84 for (final Action action : actions) {
85 try {
86 status &= action.execute();
87 } catch (final IOException ex) {
88 status = false;
89
90 if (exception == null) {
91 exception = ex;
92 }
93 }
94 }
95
96 if (exception != null) {
97 throw exception;
98 }
99
100 return status;
101 }
102
103 @Override
104 public String toString() {
105 return CompositeAction.class.getSimpleName() + Arrays.toString(actions);
106 }
107
108 public Action[] getActions() {
109 return actions;
110 }
111
112 public boolean isStopOnError() {
113 return stopOnError;
114 }
115 }