    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 java.io.Serializable;
    020
    021    import org.apache.logging.log4j.status.StatusLogger;
    022
    023    /**
    024     * A life cycle to be extended.
    025     * <p>
    026     * Wraps a {@link LifeCycle.State}.
    027     * </p>
    028     */
    029    public class AbstractLifeCycle implements LifeCycle, Serializable {
    030
    031        /**
    032         * Allow subclasses access to the status logger without creating another instance.
    033         */
    034        protected static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
    035
    036        private static final long serialVersionUID = 1L;
    037
    038        private volatile LifeCycle.State state = LifeCycle.State.INITIALIZED;
    039
    040        protected boolean equalsImpl(final Object obj) {
    041            if (this == obj) {
    042                return true;
    043            }
    044            if (obj == null) {
    045                return false;
    046            }
    047            if (getClass() != obj.getClass()) {
    048                return false;
    049            }
    050            final LifeCycle other = (LifeCycle) obj;
    051            if (state != other.getState()) {
    052                return false;
    053            }
    054            return true;
    055        }
    056
    057        @Override
    058        public LifeCycle.State getState() {
    059            return this.state;
    060        }
    061
    062        protected int hashCodeImpl() {
    063            final int prime = 31;
    064            int result = 1;
    065            result = prime * result + ((state == null) ? 0 : state.hashCode());
    066            return result;
    067        }
    068
    069        public boolean isInitialized() {
    070            return this.state == LifeCycle.State.INITIALIZED;
    071        }
    072
    073        @Override
    074        public boolean isStarted() {
    075            return this.state == LifeCycle.State.STARTED;
    076        }
    077
    078        public boolean isStarting() {
    079            return this.state == LifeCycle.State.STARTING;
    080        }
    081
    082        @Override
    083        public boolean isStopped() {
    084            return this.state == LifeCycle.State.STOPPED;
    085        }
    086
    087        public boolean isStopping() {
    088            return this.state == LifeCycle.State.STOPPING;
    089        }
    090
    091        protected void setStarted() {
    092            this.setState(LifeCycle.State.STARTED);
    093        }
    094
    095        protected void setStarting() {
    096            this.setState(LifeCycle.State.STARTING);
    097        }
    098
    099        protected void setState(final LifeCycle.State newState) {
    100            this.state = newState;
    101            // Need a better string than this.toString() for the message
    102            // LOGGER.debug("{} {}", this.state, this);
    103        }
    104
    105        protected void setStopped() {
    106            this.setState(LifeCycle.State.STOPPED);
    107        }
    108
    109        protected void setStopping() {
    110            this.setState(LifeCycle.State.STOPPING);
    111        }
    112
    113        @Override
    114        public void start() {
    115            this.setStarted();
    116        }
    117
    118        @Override
    119        public void stop() {
    120            this.state = LifeCycle.State.STOPPED;
    121        }
    122
    123    }