1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
26
27
28
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
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
104
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 }