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        @Override
041        public boolean equals(final Object obj) {
042            if (this == obj) {
043                return true;
044            }
045            if (obj == null) {
046                return false;
047            }
048            if (getClass() != obj.getClass()) {
049                return false;
050            }
051            final AbstractLifeCycle other = (AbstractLifeCycle) obj;
052            if (state != other.state) {
053                return false;
054            }
055            return true;
056        }
057    
058        public LifeCycle.State getState() {
059            return this.state;
060        }
061    
062        @Override
063        public int hashCode() {
064            final int prime = 31;
065            int result = 1;
066            result = prime * result + ((state == null) ? 0 : state.hashCode());
067            return result;
068        }
069    
070        public boolean isInitialized() {
071            return this.state == LifeCycle.State.INITIALIZED;
072        }
073    
074        @Override
075        public boolean isStarted() {
076            return this.state == LifeCycle.State.STARTED;
077        }
078    
079        public boolean isStarting() {
080            return this.state == LifeCycle.State.STARTING;
081        }
082    
083        @Override
084        public boolean isStopped() {
085            return this.state == LifeCycle.State.STOPPED;
086        }
087    
088        public boolean isStopping() {
089            return this.state == LifeCycle.State.STOPPING;
090        }
091    
092        protected void setStarted() {
093            this.setState(LifeCycle.State.STARTED);
094        }
095    
096        protected void setStarting() {
097            this.setState(LifeCycle.State.STARTING);
098        }
099    
100        protected void setState(final LifeCycle.State newState) {
101            this.state = newState;
102            // Need a better string than this.toString() for the message
103            // LOGGER.debug("{} {}", this.state, this);
104        }
105    
106        protected void setStopped() {
107            this.setState(LifeCycle.State.STOPPED);
108        }
109    
110        protected void setStopping() {
111            this.setState(LifeCycle.State.STOPPING);
112        }
113    
114        @Override
115        public void start() {
116            this.setStarted();
117        }
118    
119        @Override
120        public void stop() {
121            this.state = LifeCycle.State.STOPPED;
122        }
123    
124    }