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  
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 }