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.util.concurrent.Future;
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 LifeCycle2 {
31  
32      public static final int DEFAULT_STOP_TIMEOUT = 0;
33      public static final TimeUnit DEFAULT_STOP_TIMEUNIT = TimeUnit.MILLISECONDS;
34  
35      /**
36       * Allow subclasses access to the status logger without creating another instance.
37       */
38      protected static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
39  
40      private volatile LifeCycle.State state = LifeCycle.State.INITIALIZED;
41  
42      protected boolean equalsImpl(final Object obj) {
43          if (this == obj) {
44              return true;
45          }
46          if (obj == null) {
47              return false;
48          }
49          if (getClass() != obj.getClass()) {
50              return false;
51          }
52          final LifeCycle other = (LifeCycle) obj;
53          if (state != other.getState()) {
54              return false;
55          }
56          return true;
57      }
58  
59      @Override
60      public LifeCycle.State getState() {
61          return this.state;
62      }
63  
64      protected int hashCodeImpl() {
65          final int prime = 31;
66          int result = 1;
67          result = prime * result + ((state == null) ? 0 : state.hashCode());
68          return result;
69      }
70  
71      public boolean isInitialized() {
72          return this.state == LifeCycle.State.INITIALIZED;
73      }
74  
75      @Override
76      public boolean isStarted() {
77          return this.state == LifeCycle.State.STARTED;
78      }
79  
80      public boolean isStarting() {
81          return this.state == LifeCycle.State.STARTING;
82      }
83  
84      @Override
85      public boolean isStopped() {
86          return this.state == LifeCycle.State.STOPPED;
87      }
88  
89      public boolean isStopping() {
90          return this.state == LifeCycle.State.STOPPING;
91      }
92  
93      protected void setStarted() {
94          this.setState(LifeCycle.State.STARTED);
95      }
96  
97      protected void setStarting() {
98          this.setState(LifeCycle.State.STARTING);
99      }
100 
101     protected void setState(final LifeCycle.State newState) {
102         this.state = newState;
103         // Need a better string than this.toString() for the message
104         // LOGGER.trace("{} {}", this.state, this);
105     }
106 
107     protected void setStopped() {
108         this.setState(LifeCycle.State.STOPPED);
109     }
110 
111     protected void setStopping() {
112         this.setState(LifeCycle.State.STOPPING);
113     }
114 
115     @Override
116     public void initialize() {
117         this.state = State.INITIALIZED;
118     }
119 
120     @Override
121     public void start() {
122         this.setStarted();
123     }
124 
125     @Override
126     public void stop() {
127         stop(DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT);
128     }
129 
130     protected boolean stop(final Future<?> future) {
131         boolean stopped = true;
132         if (future != null) {
133             if (future.isCancelled() || future.isDone()) {
134                 return true;
135             }
136             stopped = future.cancel(true);
137         }
138         return stopped;
139     }
140 
141     @Override
142     public boolean stop(final long timeout, final TimeUnit timeUnit) {
143         this.state = LifeCycle.State.STOPPED;
144         return true;
145     }
146 
147 }