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      public LifeCycle.State getState() {
37          return this.state;
38      }
39  
40      public boolean isInitialized() {
41          return this.state == LifeCycle.State.INITIALIZED;
42      }
43  
44      @Override
45      public boolean isStarted() {
46          return this.state == LifeCycle.State.STARTED;
47      }
48  
49      public boolean isStarting() {
50          return this.state == LifeCycle.State.STARTING;
51      }
52  
53      @Override
54      public boolean isStopped() {
55          return this.state == LifeCycle.State.STOPPED;
56      }
57  
58      public boolean isStopping() {
59          return this.state == LifeCycle.State.STOPPING;
60      }
61  
62      protected void setStarted() {
63          this.setState(LifeCycle.State.STARTED);
64      }
65  
66      protected void setStarting() {
67          this.setState(LifeCycle.State.STARTING);
68      }
69  
70      protected void setState(final LifeCycle.State newState) {
71          this.state = newState;
72          // Need a better string than this.toString() for the message
73          // LOGGER.debug("{} {}", this.state, this);
74      }
75  
76      protected void setStopped() {
77          this.setState(LifeCycle.State.STOPPED);
78      }
79  
80      protected void setStopping() {
81          this.setState(LifeCycle.State.STOPPING);
82      }
83  
84      @Override
85      public void start() {
86          this.setStarted();
87      }
88  
89      @Override
90      public void stop() {
91          this.state = LifeCycle.State.STOPPED;
92      }
93  
94  }