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     */
017    package org.apache.logging.log4j.core.appender.rolling.helper;
018    
019    import org.apache.logging.log4j.Logger;
020    import org.apache.logging.log4j.status.StatusLogger;
021    
022    import java.io.IOException;
023    
024    
025    /**
026     * Abstract base class for implementations of Action.
027     */
028    public 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        public abstract boolean execute() throws IOException;
056    
057        /**
058         * {@inheritDoc}
059         */
060        public synchronized void run() {
061            if (!interrupted) {
062                try {
063                    execute();
064                } catch (final IOException ex) {
065                    reportException(ex);
066                }
067    
068                complete = true;
069                interrupted = true;
070            }
071        }
072    
073        /**
074         * {@inheritDoc}
075         */
076        public synchronized void close() {
077            interrupted = true;
078        }
079    
080        /**
081         * Tests if the action is complete.
082         *
083         * @return true if action is complete.
084         */
085        public boolean isComplete() {
086            return complete;
087        }
088    
089        /**
090         * Capture exception.
091         *
092         * @param ex exception.
093         */
094        protected void reportException(final Exception ex) {
095        }
096    }