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 org.apache.logging.log4j.status.StatusLogger;
20  
21  /**
22   * A life cycle to be extended.
23   * <p>
24   * Wraps a {@link LifeCycle.State}.
25   * </p>
26   */
27  public class AbstractLifeCycle implements LifeCycle {
28  
29      /**
30       * Allow subclasses access to the status logger without creating another instance.
31       */
32      protected static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
33  
34      private volatile LifeCycle.State state = LifeCycle.State.INITIALIZED;
35  
36      protected boolean equalsImpl(final Object obj) {
37          if (this == obj) {
38              return true;
39          }
40          if (obj == null) {
41              return false;
42          }
43          if (getClass() != obj.getClass()) {
44              return false;
45          }
46          final LifeCycle other = (LifeCycle) obj;
47          if (state != other.getState()) {
48              return false;
49          }
50          return true;
51      }
52  
53      @Override
54      public LifeCycle.State getState() {
55          return this.state;
56      }
57  
58      protected int hashCodeImpl() {
59          final int prime = 31;
60          int result = 1;
61          result = prime * result + ((state == null) ? 0 : state.hashCode());
62          return result;
63      }
64  
65      public boolean isInitialized() {
66          return this.state == LifeCycle.State.INITIALIZED;
67      }
68  
69      @Override
70      public boolean isStarted() {
71          return this.state == LifeCycle.State.STARTED;
72      }
73  
74      public boolean isStarting() {
75          return this.state == LifeCycle.State.STARTING;
76      }
77  
78      @Override
79      public boolean isStopped() {
80          return this.state == LifeCycle.State.STOPPED;
81      }
82  
83      public boolean isStopping() {
84          return this.state == LifeCycle.State.STOPPING;
85      }
86  
87      protected void setStarted() {
88          this.setState(LifeCycle.State.STARTED);
89      }
90  
91      protected void setStarting() {
92          this.setState(LifeCycle.State.STARTING);
93      }
94  
95      protected void setState(final LifeCycle.State newState) {
96          this.state = newState;
97          // Need a better string than this.toString() for the message
98          // LOGGER.trace("{} {}", this.state, this);
99      }
100 
101     protected void setStopped() {
102         this.setState(LifeCycle.State.STOPPED);
103     }
104 
105     protected void setStopping() {
106         this.setState(LifeCycle.State.STOPPING);
107     }
108 
109     @Override
110     public void initialize() {
111         this.state = State.INITIALIZED;
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 }