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.action;
18  
19  import java.io.IOException;
20  
21  import org.apache.logging.log4j.Logger;
22  import org.apache.logging.log4j.status.StatusLogger;
23  
24  
25  /**
26   * Abstract base class for implementations of Action.
27   */
28  public abstract class AbstractAction 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 AbstractAction() {
47      }
48  
49      /**
50       * Perform action.
51       *
52       * @return true if successful.
53       * @throws IOException if IO error.
54       */
55      @Override
56      public abstract boolean execute() throws IOException;
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public synchronized void run() {
63          if (!interrupted) {
64              try {
65                  execute();
66              } catch (final IOException ex) {
67                  reportException(ex);
68              }
69  
70              complete = true;
71              interrupted = true;
72          }
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public synchronized void close() {
80          interrupted = true;
81      }
82  
83      /**
84       * Tests if the action is complete.
85       *
86       * @return true if action is complete.
87       */
88      @Override
89      public boolean isComplete() {
90          return complete;
91      }
92  
93      /**
94       * Capture exception.
95       *
96       * @param ex exception.
97       */
98      protected void reportException(final Exception ex) {
99      }
100 }