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;
018    
019    import org.apache.logging.log4j.status.StatusLogger;
020    
021    /**
022     * A life cycle to be extended.
023     * <p>
024     * Wraps a {@link LifeCycle.State}.
025     * </p>
026     */
027    public class AbstractLifeCycle implements LifeCycle {
028    
029        /**
030         * Allow subclasses access to the status logger without creating another instance.
031         */
032        protected static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
033    
034        private volatile LifeCycle.State state = LifeCycle.State.INITIALIZED;
035    
036        public LifeCycle.State getState() {
037            return this.state;
038        }
039    
040        public boolean isInitialized() {
041            return this.state == LifeCycle.State.INITIALIZED;
042        }
043    
044        @Override
045        public boolean isStarted() {
046            return this.state == LifeCycle.State.STARTED;
047        }
048    
049        public boolean isStarting() {
050            return this.state == LifeCycle.State.STARTING;
051        }
052    
053        @Override
054        public boolean isStopped() {
055            return this.state == LifeCycle.State.STOPPED;
056        }
057    
058        public boolean isStopping() {
059            return this.state == LifeCycle.State.STOPPING;
060        }
061    
062        protected void setStarted() {
063            this.setState(LifeCycle.State.STARTED);
064        }
065    
066        protected void setStarting() {
067            this.setState(LifeCycle.State.STARTING);
068        }
069    
070        protected void setState(final LifeCycle.State newState) {
071            this.state = newState;
072            // Need a better string than this.toString() for the message
073            // LOGGER.debug("{} {}", this.state, this);
074        }
075    
076        protected void setStopped() {
077            this.setState(LifeCycle.State.STOPPED);
078        }
079    
080        protected void setStopping() {
081            this.setState(LifeCycle.State.STOPPING);
082        }
083    
084        @Override
085        public void start() {
086            this.setStarted();
087        }
088    
089        @Override
090        public void stop() {
091            this.state = LifeCycle.State.STOPPED;
092        }
093    
094    }