View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core;
18  
19  import java.io.Serializable;
20  import java.util.concurrent.TimeUnit;
21  
22  import org.apache.logging.log4j.status.StatusLogger;
23  
24  /**
25   * A life cycle to be extended.
26   * <p>
27   * Wraps a {@link LifeCycle.State}.
28   * </p>
29   */
30  public class AbstractLifeCycle implements LifeCycle, Serializable {
31  
32      /** @since 2.3.2 */
33      public static final int DEFAULT_STOP_TIMEOUT = 0;
34      
35      /** @since 2.3.2 */
36      public static final TimeUnit DEFAULT_STOP_TIMEUNIT = TimeUnit.MILLISECONDS;
37  
38      /**
39       * Allow subclasses access to the status logger without creating another instance.
40       */
41      protected static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
42  
43      private static final long serialVersionUID = 1L;
44  
45      private volatile LifeCycle.State state = LifeCycle.State.INITIALIZED;
46  
47      protected boolean equalsImpl(final Object obj) {
48          if (this == obj) {
49              return true;
50          }
51          if (obj == null) {
52              return false;
53          }
54          if (getClass() != obj.getClass()) {
55              return false;
56          }
57          final LifeCycle other = (LifeCycle) obj;
58          if (state != other.getState()) {
59              return false;
60          }
61          return true;
62      }
63  
64      @Override
65      public LifeCycle.State getState() {
66          return this.state;
67      }
68  
69      protected int hashCodeImpl() {
70          final int prime = 31;
71          int result = 1;
72          result = prime * result + ((state == null) ? 0 : state.hashCode());
73          return result;
74      }
75  
76      public boolean isInitialized() {
77          return this.state == LifeCycle.State.INITIALIZED;
78      }
79  
80      @Override
81      public boolean isStarted() {
82          return this.state == LifeCycle.State.STARTED;
83      }
84  
85      public boolean isStarting() {
86          return this.state == LifeCycle.State.STARTING;
87      }
88  
89      @Override
90      public boolean isStopped() {
91          return this.state == LifeCycle.State.STOPPED;
92      }
93  
94      public boolean isStopping() {
95          return this.state == LifeCycle.State.STOPPING;
96      }
97  
98      protected void setStarted() {
99          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 }