View Javadoc

1   /*
2    * Copyright 1999,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.log4j.rolling.helper;
18  
19  import java.io.IOException;
20  
21  
22  /***
23   * Abstract base class for implementations of Action.
24   *
25   * @author Curt Arnold
26   */
27  public abstract class ActionBase implements Action {
28    /***
29     * Is action complete.
30     */
31    private boolean complete = false;
32  
33    /***
34     * Is action interrupted.
35     */
36    private boolean interrupted = false;
37  
38    /***
39     * Constructor.
40     */
41    protected ActionBase() {
42    }
43  
44    /***
45     * Perform action.
46     *
47     * @throws IOException if IO error.
48     * @return true if successful.
49     */
50    public abstract boolean execute() throws IOException;
51  
52    /***
53     * {@inheritDoc}
54     */
55    public synchronized void run() {
56      if (!interrupted) {
57        try {
58          execute();
59        } catch (IOException ex) {
60          reportException(ex);
61        }
62  
63        complete = true;
64        interrupted = true;
65      }
66    }
67  
68    /***
69     * {@inheritDoc}
70     */
71    public synchronized void close() {
72      interrupted = true;
73    }
74  
75    /***
76     * Tests if the action is complete.
77     * @return true if action is complete.
78     */
79    public boolean isComplete() {
80      return complete;
81    }
82  
83    /***
84     * Capture exception.
85     *
86     * @param ex exception.
87     */
88    protected void reportException(final Exception ex) {
89    }
90  }