001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender.rolling.action;
018
019import java.io.IOException;
020
021import org.apache.logging.log4j.Logger;
022import org.apache.logging.log4j.status.StatusLogger;
023
024
025/**
026 * Abstract base class for implementations of Action.
027 */
028public abstract class AbstractAction implements Action {
029    /**
030     * Allow subclasses access to the status logger without creating another instance.
031     */
032    protected static final Logger LOGGER = StatusLogger.getLogger();
033    /**
034     * Is action complete.
035     */
036    private boolean complete = false;
037
038    /**
039     * Is action interrupted.
040     */
041    private boolean interrupted = false;
042
043    /**
044     * Constructor.
045     */
046    protected AbstractAction() {
047    }
048
049    /**
050     * Perform action.
051     *
052     * @return true if successful.
053     * @throws IOException if IO error.
054     */
055    @Override
056    public abstract boolean execute() throws IOException;
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public synchronized void run() {
063        if (!interrupted) {
064            try {
065                execute();
066            } catch (final IOException ex) {
067                reportException(ex);
068            }
069
070            complete = true;
071            interrupted = true;
072        }
073    }
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public synchronized void close() {
080        interrupted = true;
081    }
082
083    /**
084     * Tests if the action is complete.
085     *
086     * @return true if action is complete.
087     */
088    @Override
089    public boolean isComplete() {
090        return complete;
091    }
092
093    /**
094     * Capture exception.
095     *
096     * @param ex exception.
097     */
098    protected void reportException(final Exception ex) {
099    }
100}