1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.rolling.helper;
19
20 import java.io.IOException;
21
22 import java.util.List;
23 import org.apache.log4j.helpers.LogLog;
24
25
26 /***
27 * A group of Actions to be executed in sequence.
28 *
29 * @author Curt Arnold
30 *
31 */
32 public class CompositeAction extends ActionBase {
33 /***
34 * Actions to perform.
35 */
36 private final Action[] actions;
37
38 /***
39 * Stop on error.
40 */
41 private final boolean stopOnError;
42
43 /***
44 * Construct a new composite action.
45 * @param actions list of actions, may not be null.
46 * @param stopOnError if true, stop on the first false return value or exception.
47 */
48 public CompositeAction(final List actions,
49 final boolean stopOnError) {
50 this.actions = new Action[actions.size()];
51 actions.toArray(this.actions);
52 this.stopOnError = stopOnError;
53 }
54
55 /***
56 * {@inheritDoc}
57 */
58 public void run() {
59 try {
60 execute();
61 } catch (IOException ex) {
62 LogLog.warn("Exception during file rollover.", ex);
63 }
64 }
65
66 /***
67 * Execute sequence of actions.
68 * @return true if all actions were successful.
69 * @throws IOException on IO error.
70 */
71 public boolean execute() throws IOException {
72 if (stopOnError) {
73 for (int i = 0; i < actions.length; i++) {
74 if (!actions[i].execute()) {
75 return false;
76 }
77 }
78
79 return true;
80 } else {
81 boolean status = true;
82 IOException exception = null;
83
84 for (int i = 0; i < actions.length; i++) {
85 try {
86 status &= actions[i].execute();
87 } catch (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 }