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