comparison src/org/eclipse/jetty/util/component/AbstractLifeCycle.java @ 847:5dfb10ec0ca5

remove LifeCycle.Listener
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 19 Sep 2016 16:11:15 -0600
parents 8e9db0bbf4f9
children 3fa54d9d19cd
comparison
equal deleted inserted replaced
846:490960236c58 847:5dfb10ec0ca5
28 * 28 *
29 * 29 *
30 */ 30 */
31 public abstract class AbstractLifeCycle implements LifeCycle 31 public abstract class AbstractLifeCycle implements LifeCycle
32 { 32 {
33 private static final Logger LOG = LoggerFactory.getLogger(AbstractLifeCycle.class); 33 private static final Logger LOG = LoggerFactory.getLogger(AbstractLifeCycle.class);
34 public static final String STOPPED="STOPPED"; 34 public static final String STOPPED="STOPPED";
35 public static final String FAILED="FAILED"; 35 public static final String FAILED="FAILED";
36 public static final String STARTING="STARTING"; 36 public static final String STARTING="STARTING";
37 public static final String STARTED="STARTED"; 37 public static final String STARTED="STARTED";
38 public static final String STOPPING="STOPPING"; 38 public static final String STOPPING="STOPPING";
39 public static final String RUNNING="RUNNING"; 39 public static final String RUNNING="RUNNING";
40 40
41 private final Object _lock = new Object(); 41 private final Object _lock = new Object();
42 private final int __FAILED = -1, __STOPPED = 0, __STARTING = 1, __STARTED = 2, __STOPPING = 3; 42 private final int __FAILED = -1, __STOPPED = 0, __STARTING = 1, __STARTED = 2, __STOPPING = 3;
43 private volatile int _state = __STOPPED; 43 private volatile int _state = __STOPPED;
44 44
45 protected final CopyOnWriteArrayList<LifeCycle.Listener> _listeners=new CopyOnWriteArrayList<LifeCycle.Listener>(); 45 protected void doStart() throws Exception
46 {
47 }
46 48
47 protected void doStart() throws Exception 49 protected void doStop() throws Exception
48 { 50 {
49 } 51 }
50 52
51 protected void doStop() throws Exception 53 public final void start() throws Exception
52 { 54 {
53 } 55 synchronized (_lock)
56 {
57 try
58 {
59 if (_state == __STARTED || _state == __STARTING)
60 return;
61 setStarting();
62 doStart();
63 setStarted();
64 }
65 catch (Exception e)
66 {
67 setFailed(e);
68 throw e;
69 }
70 catch (Error e)
71 {
72 setFailed(e);
73 throw e;
74 }
75 }
76 }
54 77
55 public final void start() throws Exception 78 public final void stop() throws Exception
56 { 79 {
57 synchronized (_lock) 80 synchronized (_lock)
58 { 81 {
59 try 82 try
60 { 83 {
61 if (_state == __STARTED || _state == __STARTING) 84 if (_state == __STOPPING || _state == __STOPPED)
62 return; 85 return;
63 setStarting(); 86 setStopping();
64 doStart(); 87 doStop();
65 setStarted(); 88 setStopped();
66 } 89 }
67 catch (Exception e) 90 catch (Exception e)
68 { 91 {
69 setFailed(e); 92 setFailed(e);
70 throw e; 93 throw e;
71 } 94 }
72 catch (Error e) 95 catch (Error e)
73 { 96 {
74 setFailed(e); 97 setFailed(e);
75 throw e; 98 throw e;
76 } 99 }
77 } 100 }
78 } 101 }
79 102
80 public final void stop() throws Exception 103 public boolean isRunning()
81 { 104 {
82 synchronized (_lock) 105 final int state = _state;
83 { 106
84 try 107 return state == __STARTED || state == __STARTING;
85 { 108 }
86 if (_state == __STOPPING || _state == __STOPPED)
87 return;
88 setStopping();
89 doStop();
90 setStopped();
91 }
92 catch (Exception e)
93 {
94 setFailed(e);
95 throw e;
96 }
97 catch (Error e)
98 {
99 setFailed(e);
100 throw e;
101 }
102 }
103 }
104 109
105 public boolean isRunning() 110 public boolean isStarted()
106 { 111 {
107 final int state = _state; 112 return _state == __STARTED;
108 113 }
109 return state == __STARTED || state == __STARTING;
110 }
111 114
112 public boolean isStarted() 115 public boolean isStarting()
113 { 116 {
114 return _state == __STARTED; 117 return _state == __STARTING;
115 } 118 }
116 119
117 public boolean isStarting() 120 public boolean isStopping()
118 { 121 {
119 return _state == __STARTING; 122 return _state == __STOPPING;
120 } 123 }
121 124
122 public boolean isStopping() 125 public boolean isStopped()
123 { 126 {
124 return _state == __STOPPING; 127 return _state == __STOPPED;
125 } 128 }
126 129
127 public boolean isStopped() 130 public boolean isFailed()
128 { 131 {
129 return _state == __STOPPED; 132 return _state == __FAILED;
130 } 133 }
131 134
132 public boolean isFailed() 135 public String getState()
133 { 136 {
134 return _state == __FAILED; 137 switch(_state)
135 } 138 {
139 case __FAILED: return FAILED;
140 case __STARTING: return STARTING;
141 case __STARTED: return STARTED;
142 case __STOPPING: return STOPPING;
143 case __STOPPED: return STOPPED;
144 }
145 return null;
146 }
147
148 public static String getState(LifeCycle lc)
149 {
150 if (lc.isStarting()) return STARTING;
151 if (lc.isStarted()) return STARTED;
152 if (lc.isStopping()) return STOPPING;
153 if (lc.isStopped()) return STOPPED;
154 return FAILED;
155 }
136 156
137 public void addLifeCycleListener(LifeCycle.Listener listener) 157 private void setStarted()
138 { 158 {
139 _listeners.add(listener); 159 _state = __STARTED;
140 } 160 LOG.debug(STARTED+" {}",this);
161 }
141 162
142 public void removeLifeCycleListener(LifeCycle.Listener listener) 163 private void setStarting()
143 { 164 {
144 _listeners.remove(listener); 165 LOG.debug("starting {}",this);
145 } 166 _state = __STARTING;
146 167 }
147 public String getState()
148 {
149 switch(_state)
150 {
151 case __FAILED: return FAILED;
152 case __STARTING: return STARTING;
153 case __STARTED: return STARTED;
154 case __STOPPING: return STOPPING;
155 case __STOPPED: return STOPPED;
156 }
157 return null;
158 }
159
160 public static String getState(LifeCycle lc)
161 {
162 if (lc.isStarting()) return STARTING;
163 if (lc.isStarted()) return STARTED;
164 if (lc.isStopping()) return STOPPING;
165 if (lc.isStopped()) return STOPPED;
166 return FAILED;
167 }
168 168
169 private void setStarted() 169 private void setStopping()
170 { 170 {
171 _state = __STARTED; 171 LOG.debug("stopping {}",this);
172 LOG.debug(STARTED+" {}",this); 172 _state = __STOPPING;
173 for (Listener listener : _listeners) 173 }
174 listener.lifeCycleStarted(this);
175 }
176 174
177 private void setStarting() 175 private void setStopped()
178 { 176 {
179 LOG.debug("starting {}",this); 177 _state = __STOPPED;
180 _state = __STARTING; 178 LOG.debug("{} {}",STOPPED,this);
181 for (Listener listener : _listeners) 179 }
182 listener.lifeCycleStarting(this);
183 }
184 180
185 private void setStopping() 181 private void setFailed(Throwable th)
186 { 182 {
187 LOG.debug("stopping {}",this); 183 _state = __FAILED;
188 _state = __STOPPING; 184 LOG.warn(FAILED+" " + this+": "+th,th);
189 for (Listener listener : _listeners) 185 }
190 listener.lifeCycleStopping(this);
191 }
192 186
193 private void setStopped()
194 {
195 _state = __STOPPED;
196 LOG.debug("{} {}",STOPPED,this);
197 for (Listener listener : _listeners)
198 listener.lifeCycleStopped(this);
199 }
200
201 private void setFailed(Throwable th)
202 {
203 _state = __FAILED;
204 LOG.warn(FAILED+" " + this+": "+th,th);
205 for (Listener listener : _listeners)
206 listener.lifeCycleFailure(this,th);
207 }
208
209 public static abstract class AbstractLifeCycleListener implements LifeCycle.Listener
210 {
211 public void lifeCycleFailure(LifeCycle event, Throwable cause) {}
212 public void lifeCycleStarted(LifeCycle event) {}
213 public void lifeCycleStarting(LifeCycle event) {}
214 public void lifeCycleStopped(LifeCycle event) {}
215 public void lifeCycleStopping(LifeCycle event) {}
216 }
217 } 187 }