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