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  
23  /***
24   * Abstract base class for implementations of Action.
25   *
26   * @author Curt Arnold
27   */
28  public abstract class ActionBase implements Action {
29    /***
30     * Is action complete.
31     */
32    private boolean complete = false;
33  
34    /***
35     * Is action interrupted.
36     */
37    private boolean interrupted = false;
38  
39    /***
40     * Constructor.
41     */
42    protected ActionBase() {
43    }
44  
45    /***
46     * Perform action.
47     *
48     * @throws IOException if IO error.
49     * @return true if successful.
50     */
51    public abstract boolean execute() throws IOException;
52  
53    /***
54     * {@inheritDoc}
55     */
56    public synchronized void run() {
57      if (!interrupted) {
58        try {
59          execute();
60        } catch (IOException ex) {
61          reportException(ex);
62        }
63  
64        complete = true;
65        interrupted = true;
66      }
67    }
68  
69    /***
70     * {@inheritDoc}
71     */
72    public synchronized void close() {
73      interrupted = true;
74    }
75  
76    /***
77     * Tests if the action is complete.
78     * @return true if action is complete.
79     */
80    public boolean isComplete() {
81      return complete;
82    }
83  
84    /***
85     * Capture exception.
86     *
87     * @param ex exception.
88     */
89    protected void reportException(final Exception ex) {
90    }
91  }