comparison src/org/eclipse/jetty/util/component/AggregateLifeCycle.java @ 864:e21ca9878a10

simplify ThreadPool use
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 02 Oct 2016 16:17:38 -0600
parents 8e9db0bbf4f9
children 9b65e8064f90
comparison
equal deleted inserted replaced
863:88d3c8ff242a 864:e21ca9878a10
43 * the API must be used to explicitly set it as unmanaged. 43 * the API must be used to explicitly set it as unmanaged.
44 * <p> 44 * <p>
45 */ 45 */
46 public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable, Dumpable 46 public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable, Dumpable
47 { 47 {
48 private static final Logger LOG = LoggerFactory.getLogger(AggregateLifeCycle.class); 48 private static final Logger LOG = LoggerFactory.getLogger(AggregateLifeCycle.class);
49 private final List<Bean> _beans=new CopyOnWriteArrayList<Bean>(); 49 private final List<Bean> _beans=new CopyOnWriteArrayList<Bean>();
50 private boolean _started=false; 50 private boolean _started=false;
51 51
52 private class Bean 52 private class Bean
53 { 53 {
54 Bean(Object b) 54 Bean(Object b)
55 { 55 {
56 _bean=b; 56 _bean=b;
57 } 57 }
58 final Object _bean; 58 final Object _bean;
59 volatile boolean _managed=true; 59 volatile boolean _managed=true;
60 60
61 public String toString() 61 public String toString()
62 { 62 {
63 return "{"+_bean+","+_managed+"}"; 63 return "{"+_bean+","+_managed+"}";
64 } 64 }
65 } 65 }
66 66
67 /* ------------------------------------------------------------ */ 67 /* ------------------------------------------------------------ */
68 /** 68 /**
69 * Start the managed lifecycle beans in the order they were added. 69 * Start the managed lifecycle beans in the order they were added.
70 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() 70 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart()
71 */ 71 */
72 @Override 72 @Override
73 protected void doStart() throws Exception 73 protected void doStart() throws Exception
74 { 74 {
75 for (Bean b:_beans) 75 for (Bean b:_beans)
76 { 76 {
77 if (b._managed && b._bean instanceof LifeCycle) 77 if (b._managed && b._bean instanceof LifeCycle)
78 { 78 {
79 LifeCycle l=(LifeCycle)b._bean; 79 LifeCycle l=(LifeCycle)b._bean;
80 if (!l.isRunning()) 80 if (!l.isRunning())
81 l.start(); 81 l.start();
82 } 82 }
83 } 83 }
84 // indicate that we are started, so that addBean will start other beans added. 84 // indicate that we are started, so that addBean will start other beans added.
85 _started=true; 85 _started=true;
86 super.doStart(); 86 super.doStart();
87 } 87 }
88 88
89 /* ------------------------------------------------------------ */ 89 /* ------------------------------------------------------------ */
90 /** 90 /**
91 * Stop the joined lifecycle beans in the reverse order they were added. 91 * Stop the joined lifecycle beans in the reverse order they were added.
92 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() 92 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart()
93 */ 93 */
94 @Override 94 @Override
95 protected void doStop() throws Exception 95 protected void doStop() throws Exception
96 { 96 {
97 _started=false; 97 _started=false;
98 super.doStop(); 98 super.doStop();
99 List<Bean> reverse = new ArrayList<Bean>(_beans); 99 List<Bean> reverse = new ArrayList<Bean>(_beans);
100 Collections.reverse(reverse); 100 Collections.reverse(reverse);
101 for (Bean b:reverse) 101 for (Bean b:reverse)
102 { 102 {
103 if (b._managed && b._bean instanceof LifeCycle) 103 if (b._managed && b._bean instanceof LifeCycle)
104 { 104 {
105 LifeCycle l=(LifeCycle)b._bean; 105 LifeCycle l=(LifeCycle)b._bean;
106 if (l.isRunning()) 106 if (l.isRunning())
107 l.stop(); 107 l.stop();
108 } 108 }
109 } 109 }
110 } 110 }
111 111
112 112
113 /* ------------------------------------------------------------ */ 113 /* ------------------------------------------------------------ */
114 /** 114 /**
115 * Destroy the joined Destroyable beans in the reverse order they were added. 115 * Destroy the joined Destroyable beans in the reverse order they were added.
116 * @see org.eclipse.jetty.util.component.Destroyable#destroy() 116 * @see org.eclipse.jetty.util.component.Destroyable#destroy()
117 */ 117 */
118 public void destroy() 118 public void destroy()
119 { 119 {
120 List<Bean> reverse = new ArrayList<Bean>(_beans); 120 List<Bean> reverse = new ArrayList<Bean>(_beans);
121 Collections.reverse(reverse); 121 Collections.reverse(reverse);
122 for (Bean b:reverse) 122 for (Bean b:reverse)
123 { 123 {
124 if (b._bean instanceof Destroyable && b._managed) 124 if (b._bean instanceof Destroyable && b._managed)
125 { 125 {
126 Destroyable d=(Destroyable)b._bean; 126 Destroyable d=(Destroyable)b._bean;
127 d.destroy(); 127 d.destroy();
128 } 128 }
129 } 129 }
130 _beans.clear(); 130 _beans.clear();
131 } 131 }
132 132
133 133
134 /* ------------------------------------------------------------ */ 134 /* ------------------------------------------------------------ */
135 /** Is the bean contained in the aggregate. 135 /** Is the bean contained in the aggregate.
136 * @param bean 136 * @param bean
137 * @return True if the aggregate contains the bean 137 * @return True if the aggregate contains the bean
138 */ 138 */
139 public boolean contains(Object bean) 139 public boolean contains(Object bean)
140 { 140 {
141 for (Bean b:_beans) 141 for (Bean b:_beans)
142 if (b._bean==bean) 142 if (b._bean==bean)
143 return true; 143 return true;
144 return false; 144 return false;
145 } 145 }
146 146
147 /* ------------------------------------------------------------ */ 147 /* ------------------------------------------------------------ */
148 /** Is the bean joined to the aggregate. 148 /** Is the bean joined to the aggregate.
149 * @param bean 149 * @param bean
150 * @return True if the aggregate contains the bean and it is joined 150 * @return True if the aggregate contains the bean and it is joined
151 */ 151 */
152 public boolean isManaged(Object bean) 152 public boolean isManaged(Object bean)
153 { 153 {
154 for (Bean b:_beans) 154 for (Bean b:_beans)
155 if (b._bean==bean) 155 if (b._bean==bean)
156 return b._managed; 156 return b._managed;
157 return false; 157 return false;
158 } 158 }
159 159
160 /* ------------------------------------------------------------ */ 160 /* ------------------------------------------------------------ */
161 /** 161 /**
162 * Add an associated bean. 162 * Add an associated bean.
163 * If the bean is a {@link LifeCycle}, then it will be managed if it is not 163 * If the bean is a {@link LifeCycle}, then it will be managed if it is not
164 * already started and umanaged if it is already started. The {@link #addBean(Object, boolean)} 164 * already started and umanaged if it is already started. The {@link #addBean(Object, boolean)}
165 * method should be used if this is not correct, or the {@link #manage(Object)} and {@link #unmanage(Object)} 165 * method should be used if this is not correct, or the {@link #manage(Object)} and {@link #unmanage(Object)}
166 * methods may be used after an add to change the status. 166 * methods may be used after an add to change the status.
167 * @param o the bean object to add 167 * @param o the bean object to add
168 * @return true if the bean was added or false if it has already been added. 168 * @return true if the bean was added or false if it has already been added.
169 */ 169 */
170 public boolean addBean(Object o) 170 public boolean addBean(Object o)
171 { 171 {
172 // beans are joined unless they are started lifecycles 172 // beans are joined unless they are started lifecycles
173 return addBean(o,!((o instanceof LifeCycle)&&((LifeCycle)o).isStarted())); 173 return addBean(o,!((o instanceof LifeCycle)&&((LifeCycle)o).isStarted()));
174 } 174 }
175 175
176 /* ------------------------------------------------------------ */ 176 /* ------------------------------------------------------------ */
177 /** Add an associated lifecycle. 177 /** Add an associated lifecycle.
178 * @param o The lifecycle to add 178 * @param o The lifecycle to add
179 * @param managed True if the LifeCycle is to be joined, otherwise it will be disjoint. 179 * @param managed True if the LifeCycle is to be joined, otherwise it will be disjoint.
180 * @return true if bean was added, false if already present. 180 * @return true if bean was added, false if already present.
181 */ 181 */
182 public boolean addBean(Object o, boolean managed) 182 public boolean addBean(Object o, boolean managed)
183 { 183 {
184 if (contains(o)) 184 if (contains(o))
185 return false; 185 return false;
186 186
187 Bean b = new Bean(o); 187 Bean b = new Bean(o);
188 b._managed=managed; 188 b._managed=managed;
189 _beans.add(b); 189 _beans.add(b);
190 190
191 if (o instanceof LifeCycle) 191 if (o instanceof LifeCycle)
192 { 192 {
193 LifeCycle l=(LifeCycle)o; 193 LifeCycle l=(LifeCycle)o;
194 194
195 // Start the bean if we are started 195 // Start the bean if we are started
196 if (managed && _started) 196 if (managed && _started)
197 { 197 {
198 try 198 try
199 { 199 {
200 l.start(); 200 l.start();
201 } 201 }
202 catch(Exception e) 202 catch(Exception e)
203 { 203 {
204 throw new RuntimeException (e); 204 throw new RuntimeException (e);
205 } 205 }
206 } 206 }
207 } 207 }
208 return true; 208 return true;
209 } 209 }
210 210
211 /* ------------------------------------------------------------ */ 211 /* ------------------------------------------------------------ */
212 /** 212 /** Get dependent beans
213 * Manage a bean by this aggregate, so that it is started/stopped/destroyed with the 213 * @return List of beans.
214 * aggregate lifecycle. 214 */
215 * @param bean The bean to manage (must already have been added). 215 public Collection<Object> getBeans()
216 */ 216 {
217 public void manage(Object bean) 217 return getBeans(Object.class);
218 { 218 }
219 for (Bean b :_beans) 219
220 { 220 /* ------------------------------------------------------------ */
221 if (b._bean==bean) 221 /** Get dependent beans of a specific class
222 { 222 * @see #addBean(Object)
223 b._managed=true; 223 * @param clazz
224 return; 224 * @return List of beans.
225 } 225 */
226 } 226 public <T> List<T> getBeans(Class<T> clazz)
227 throw new IllegalArgumentException(); 227 {
228 } 228 ArrayList<T> beans = new ArrayList<T>();
229 229 for (Bean b:_beans)
230 /* ------------------------------------------------------------ */ 230 {
231 /** 231 if (clazz.isInstance(b._bean))
232 * Unmanage a bean by this aggregate, so that it is not started/stopped/destroyed with the 232 beans.add((T)(b._bean));
233 * aggregate lifecycle. 233 }
234 * @param bean The bean to manage (must already have been added). 234 return beans;
235 */ 235 }
236 public void unmanage(Object bean) 236
237 { 237
238 for (Bean b :_beans) 238 /* ------------------------------------------------------------ */
239 { 239 /** Get dependent beans of a specific class.
240 if (b._bean==bean) 240 * If more than one bean of the type exist, the first is returned.
241 { 241 * @see #addBean(Object)
242 b._managed=false; 242 * @param clazz
243 return; 243 * @return bean or null
244 } 244 */
245 } 245 public <T> T getBean(Class<T> clazz)
246 throw new IllegalArgumentException(); 246 {
247 } 247 for (Bean b:_beans)
248 248 {
249 /* ------------------------------------------------------------ */ 249 if (clazz.isInstance(b._bean))
250 /** Get dependent beans 250 return (T)b._bean;
251 * @return List of beans. 251 }
252 */ 252
253 public Collection<Object> getBeans() 253 return null;
254 { 254 }
255 return getBeans(Object.class); 255
256 } 256 /* ------------------------------------------------------------ */
257 257 /**
258 /* ------------------------------------------------------------ */ 258 * Remove all associated bean.
259 /** Get dependent beans of a specific class 259 */
260 * @see #addBean(Object) 260 public void removeBeans ()
261 * @param clazz 261 {
262 * @return List of beans. 262 _beans.clear();
263 */ 263 }
264 public <T> List<T> getBeans(Class<T> clazz) 264
265 { 265 /* ------------------------------------------------------------ */
266 ArrayList<T> beans = new ArrayList<T>(); 266 /**
267 for (Bean b:_beans) 267 * Remove an associated bean.
268 { 268 */
269 if (clazz.isInstance(b._bean)) 269 public boolean removeBean (Object o)
270 beans.add((T)(b._bean)); 270 {
271 } 271 Iterator<Bean> i = _beans.iterator();
272 return beans; 272 while(i.hasNext())
273 } 273 {
274 274 Bean b=i.next();
275 275 if (b._bean==o)
276 /* ------------------------------------------------------------ */ 276 {
277 /** Get dependent beans of a specific class. 277 _beans.remove(b);
278 * If more than one bean of the type exist, the first is returned. 278 return true;
279 * @see #addBean(Object) 279 }
280 * @param clazz 280 }
281 * @return bean or null 281 return false;
282 */ 282 }
283 public <T> T getBean(Class<T> clazz) 283
284 { 284 /* ------------------------------------------------------------ */
285 for (Bean b:_beans) 285 public void dumpStdErr()
286 { 286 {
287 if (clazz.isInstance(b._bean)) 287 try
288 return (T)b._bean; 288 {
289 } 289 dump(System.err,"");
290 290 }
291 return null; 291 catch (IOException e)
292 } 292 {
293 293 LOG.warn("",e);
294 /* ------------------------------------------------------------ */ 294 }
295 /** 295 }
296 * Remove all associated bean. 296
297 */ 297 /* ------------------------------------------------------------ */
298 public void removeBeans () 298 public String dump()
299 { 299 {
300 _beans.clear(); 300 return dump(this);
301 } 301 }
302 302
303 /* ------------------------------------------------------------ */ 303 /* ------------------------------------------------------------ */
304 /** 304 public static String dump(Dumpable dumpable)
305 * Remove an associated bean. 305 {
306 */ 306 StringBuilder b = new StringBuilder();
307 public boolean removeBean (Object o) 307 try
308 { 308 {
309 Iterator<Bean> i = _beans.iterator(); 309 dumpable.dump(b,"");
310 while(i.hasNext()) 310 }
311 { 311 catch (IOException e)
312 Bean b=i.next(); 312 {
313 if (b._bean==o) 313 LOG.warn("",e);
314 { 314 }
315 _beans.remove(b); 315 return b.toString();
316 return true; 316 }
317 } 317
318 } 318 /* ------------------------------------------------------------ */
319 return false; 319 public void dump(Appendable out) throws IOException
320 } 320 {
321 321 dump(out,"");
322 /* ------------------------------------------------------------ */ 322 }
323 public void dumpStdErr() 323
324 { 324 /* ------------------------------------------------------------ */
325 try 325 protected void dumpThis(Appendable out) throws IOException
326 { 326 {
327 dump(System.err,""); 327 out.append(String.valueOf(this)).append(" - ").append(getState()).append("\n");
328 } 328 }
329 catch (IOException e) 329
330 { 330 /* ------------------------------------------------------------ */
331 LOG.warn("",e); 331 public static void dumpObject(Appendable out,Object o) throws IOException
332 } 332 {
333 } 333 try
334 334 {
335 /* ------------------------------------------------------------ */ 335 if (o instanceof LifeCycle)
336 public String dump() 336 out.append(String.valueOf(o)).append(" - ").append((AbstractLifeCycle.getState((LifeCycle)o))).append("\n");
337 { 337 else
338 return dump(this); 338 out.append(String.valueOf(o)).append("\n");
339 } 339 }
340 340 catch(Throwable th)
341 /* ------------------------------------------------------------ */ 341 {
342 public static String dump(Dumpable dumpable) 342 out.append(" => ").append(th.toString()).append('\n');
343 { 343 }
344 StringBuilder b = new StringBuilder(); 344 }
345 try 345
346 { 346 /* ------------------------------------------------------------ */
347 dumpable.dump(b,""); 347 public void dump(Appendable out,String indent) throws IOException
348 } 348 {
349 catch (IOException e) 349 dumpThis(out);
350 { 350 int size=_beans.size();
351 LOG.warn("",e); 351 if (size==0)
352 } 352 return;
353 return b.toString(); 353 int i=0;
354 } 354 for (Bean b : _beans)
355 355 {
356 /* ------------------------------------------------------------ */ 356 i++;
357 public void dump(Appendable out) throws IOException 357
358 { 358 out.append(indent).append(" +- ");
359 dump(out,""); 359 if (b._managed)
360 } 360 {
361 361 if (b._bean instanceof Dumpable)
362 /* ------------------------------------------------------------ */ 362 ((Dumpable)b._bean).dump(out,indent+(i==size?" ":" | "));
363 protected void dumpThis(Appendable out) throws IOException 363 else
364 { 364 dumpObject(out,b._bean);
365 out.append(String.valueOf(this)).append(" - ").append(getState()).append("\n"); 365 }
366 } 366 else
367 367 dumpObject(out,b._bean);
368 /* ------------------------------------------------------------ */ 368 }
369 public static void dumpObject(Appendable out,Object o) throws IOException 369
370 { 370 if (i!=size)
371 try 371 out.append(indent).append(" |\n");
372 { 372 }
373 if (o instanceof LifeCycle) 373
374 out.append(String.valueOf(o)).append(" - ").append((AbstractLifeCycle.getState((LifeCycle)o))).append("\n"); 374 /* ------------------------------------------------------------ */
375 else 375 public static void dump(Appendable out,String indent,Collection<?>... collections) throws IOException
376 out.append(String.valueOf(o)).append("\n"); 376 {
377 } 377 if (collections.length==0)
378 catch(Throwable th) 378 return;
379 { 379 int size=0;
380 out.append(" => ").append(th.toString()).append('\n'); 380 for (Collection<?> c : collections)
381 } 381 size+=c.size();
382 } 382 if (size==0)
383 383 return;
384 /* ------------------------------------------------------------ */ 384
385 public void dump(Appendable out,String indent) throws IOException 385 int i=0;
386 { 386 for (Collection<?> c : collections)
387 dumpThis(out); 387 {
388 int size=_beans.size(); 388 for (Object o : c)
389 if (size==0) 389 {
390 return; 390 i++;
391 int i=0; 391 out.append(indent).append(" +- ");
392 for (Bean b : _beans) 392
393 { 393 if (o instanceof Dumpable)
394 i++; 394 ((Dumpable)o).dump(out,indent+(i==size?" ":" | "));
395 395 else
396 out.append(indent).append(" +- "); 396 dumpObject(out,o);
397 if (b._managed) 397 }
398 { 398
399 if (b._bean instanceof Dumpable) 399 if (i!=size)
400 ((Dumpable)b._bean).dump(out,indent+(i==size?" ":" | ")); 400 out.append(indent).append(" |\n");
401 else 401 }
402 dumpObject(out,b._bean); 402 }
403 }
404 else
405 dumpObject(out,b._bean);
406 }
407
408 if (i!=size)
409 out.append(indent).append(" |\n");
410 }
411
412 /* ------------------------------------------------------------ */
413 public static void dump(Appendable out,String indent,Collection<?>... collections) throws IOException
414 {
415 if (collections.length==0)
416 return;
417 int size=0;
418 for (Collection<?> c : collections)
419 size+=c.size();
420 if (size==0)
421 return;
422
423 int i=0;
424 for (Collection<?> c : collections)
425 {
426 for (Object o : c)
427 {
428 i++;
429 out.append(indent).append(" +- ");
430
431 if (o instanceof Dumpable)
432 ((Dumpable)o).dump(out,indent+(i==size?" ":" | "));
433 else
434 dumpObject(out,o);
435 }
436
437 if (i!=size)
438 out.append(indent).append(" |\n");
439 }
440 }
441 } 403 }