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