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.io.ObjectStreamException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.concurrent.CopyOnWriteArrayList;
21
22 import org.eclipse.jetty.util.component.AbstractLifeCycle;
23 import org.eclipse.jetty.util.component.LifeCycle;
24 import org.eclipse.jetty.util.log.Log;
25
26
27 /* ------------------------------------------------------------ */
28 /**
29 * ShutdownThread is a shutdown hook thread implemented as
30 * singleton that maintains a list of lifecycle instances
31 * that are registered with it and provides ability to stop
32 * these lifecycles upon shutdown of the Java Virtual Machine
33 */
34 public class ShutdownThread extends Thread
35 {
36 private static final ShutdownThread _thread = new ShutdownThread();
37
38 private boolean _hooked;
39 private final List<LifeCycle> _lifeCycles = new CopyOnWriteArrayList<LifeCycle>();
40
41 /* ------------------------------------------------------------ */
42 /**
43 * Default constructor for the singleton
44 *
45 * Registers the instance as shutdown hook with the Java Runtime
46 */
47 private ShutdownThread()
48 {
49 }
50
51 /* ------------------------------------------------------------ */
52 private synchronized void hook()
53 {
54 try
55 {
56 if (!_hooked)
57 Runtime.getRuntime().addShutdownHook(this);
58 _hooked=true;
59 }
60 catch(Exception e)
61 {
62 Log.ignore(e);
63 Log.info("shutdown already commenced");
64 }
65 }
66
67 /* ------------------------------------------------------------ */
68 private synchronized void unhook()
69 {
70 try
71 {
72 _hooked=false;
73 Runtime.getRuntime().removeShutdownHook(this);
74 }
75 catch(Exception e)
76 {
77 Log.ignore(e);
78 Log.info("shutdown already commenced");
79 }
80 }
81
82 /* ------------------------------------------------------------ */
83 /**
84 * Returns the instance of the singleton
85 *
86 * @return the singleton instance of the {@link ShutdownThread}
87 */
88 public static ShutdownThread getInstance()
89 {
90 return _thread;
91 }
92
93 /* ------------------------------------------------------------ */
94 public static synchronized void register(LifeCycle... lifeCycles)
95 {
96 _thread._lifeCycles.addAll(Arrays.asList(lifeCycles));
97 if (_thread._lifeCycles.size()>0)
98 _thread.hook();
99 }
100
101 /* ------------------------------------------------------------ */
102 public static synchronized void register(int index, LifeCycle... lifeCycles)
103 {
104 _thread._lifeCycles.addAll(index,Arrays.asList(lifeCycles));
105 if (_thread._lifeCycles.size()>0)
106 _thread.hook();
107 }
108
109 /* ------------------------------------------------------------ */
110 public static synchronized void deregister(LifeCycle lifeCycle)
111 {
112 _thread._lifeCycles.remove(lifeCycle);
113 if (_thread._lifeCycles.size()==0)
114 _thread.unhook();
115 }
116
117 /* ------------------------------------------------------------ */
118 public void run()
119 {
120 for (LifeCycle lifeCycle : _thread._lifeCycles)
121 {
122 try
123 {
124 lifeCycle.stop();
125 Log.debug("Stopped " + lifeCycle);
126 }
127 catch (Exception ex)
128 {
129 Log.debug(ex);
130 }
131 }
132 }
133 }