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  package org.apache.logging.log4j.core.appender.rolling.helper;
18  
19  import org.apache.logging.log4j.Logger;
20  import org.apache.logging.log4j.status.StatusLogger;
21  
22  import java.io.IOException;
23  
24  
25  /**
26   * Abstract base class for implementations of Action.
27   */
28  public abstract class ActionBase implements Action {
29      /**
30       * Allow subclasses access to the status logger without creating another instance.
31       */
32      protected static final Logger LOGGER = StatusLogger.getLogger();
33      /**
34       * Is action complete.
35       */
36      private boolean complete = false;
37  
38      /**
39       * Is action interrupted.
40       */
41      private boolean interrupted = false;
42  
43      /**
44       * Constructor.
45       */
46      protected ActionBase() {
47      }
48  
49      /**
50       * Perform action.
51       *
52       * @return true if successful.
53       * @throws IOException if IO error.
54       */
55      public abstract boolean execute() throws IOException;
56  
57      /**
58       * {@inheritDoc}
59       */
60      public synchronized void run() {
61          if (!interrupted) {
62              try {
63                  execute();
64              } catch (IOException ex) {
65                  reportException(ex);
66              }
67  
68              complete = true;
69              interrupted = true;
70          }
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      public synchronized void close() {
77          interrupted = true;
78      }
79  
80      /**
81       * Tests if the action is complete.
82       *
83       * @return true if action is complete.
84       */
85      public boolean isComplete() {
86          return complete;
87      }
88  
89      /**
90       * Capture exception.
91       *
92       * @param ex exception.
93       */
94      protected void reportException(final Exception ex) {
95      }
96  }