1 // ========================================================================
2 // Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14 package org.eclipse.jetty.util.thread;
15
16 import java.util.Arrays;
17 import java.util.List;
18 import java.util.concurrent.CopyOnWriteArrayList;
19
20 import org.eclipse.jetty.util.component.LifeCycle;
21 import org.eclipse.jetty.util.log.Log;
22
23
24 /* ------------------------------------------------------------ */
25 /**
26 * ShutdownThread is a shutdown hook thread implemented as
27 * singleton that maintains a list of lifecycle instances
28 * that are registered with it and provides ability to stop
29 * these lifecycles upon shutdown of the Java Virtual Machine
30 */
31 public class ShutdownThread extends Thread
32 {
33 private static final ShutdownThread _thread = new ShutdownThread();
34
35 private boolean _hooked;
36 private final List<LifeCycle> _lifeCycles = new CopyOnWriteArrayList<LifeCycle>();
37
38 /* ------------------------------------------------------------ */
39 /**
40 * Default constructor for the singleton
41 *
42 * Registers the instance as shutdown hook with the Java Runtime
43 */
44 private ShutdownThread()
45 {
46 }
47
48 /* ------------------------------------------------------------ */
49 private synchronized void hook()
50 {
51 try
52 {
53 if (!_hooked)
54 Runtime.getRuntime().addShutdownHook(this);
55 _hooked=true;
56 }
57 catch(Exception e)
58 {
59 Log.ignore(e);
60 Log.info("shutdown already commenced");
61 }
62 }
63
64 /* ------------------------------------------------------------ */
65 private synchronized void unhook()
66 {
67 try
68 {
69 _hooked=false;
70 Runtime.getRuntime().removeShutdownHook(this);
71 }
72 catch(Exception e)
73 {
74 Log.ignore(e);
75 Log.info("shutdown already commenced");
76 }
77 }
78
79 /* ------------------------------------------------------------ */
80 /**
81 * Returns the instance of the singleton
82 *
83 * @return the singleton instance of the {@link ShutdownThread}
84 */
85 public static ShutdownThread getInstance()
86 {
87 return _thread;
88 }
89
90 /* ------------------------------------------------------------ */
91 public static synchronized void register(LifeCycle... lifeCycles)
92 {
93 _thread._lifeCycles.addAll(Arrays.asList(lifeCycles));
94 if (_thread._lifeCycles.size()>0)
95 _thread.hook();
96 }
97
98 /* ------------------------------------------------------------ */
99 public static synchronized void register(int index, LifeCycle... lifeCycles)
100 {
101 _thread._lifeCycles.addAll(index,Arrays.asList(lifeCycles));
102 if (_thread._lifeCycles.size()>0)
103 _thread.hook();
104 }
105
106 /* ------------------------------------------------------------ */
107 public static synchronized void deregister(LifeCycle lifeCycle)
108 {
109 _thread._lifeCycles.remove(lifeCycle);
110 if (_thread._lifeCycles.size()==0)
111 _thread.unhook();
112 }
113
114 /* ------------------------------------------------------------ */
115 public void run()
116 {
117 for (LifeCycle lifeCycle : _thread._lifeCycles)
118 {
119 try
120 {
121 lifeCycle.stop();
122 Log.debug("Stopped " + lifeCycle);
123 }
124 catch (Exception ex)
125 {
126 Log.debug(ex);
127 }
128 }
129 }
130 }