2 // ========================================================================
3 // Copyright (c) 1995-2016 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util.thread;
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.concurrent.CopyOnWriteArrayList;
25 import org.eclipse.jetty.util.component.Destroyable;
26 import org.eclipse.jetty.util.component.LifeCycle;
27 import org.eclipse.jetty.util.log.Log;
28 import org.eclipse.jetty.util.log.Logger;
31 /* ------------------------------------------------------------ */
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
38 public class ShutdownThread extends Thread
40 private static final Logger LOG = Log.getLogger(ShutdownThread.class);
41 private static final ShutdownThread _thread = new ShutdownThread();
43 private boolean _hooked;
44 private final List<LifeCycle> _lifeCycles = new CopyOnWriteArrayList<LifeCycle>();
46 /* ------------------------------------------------------------ */
48 * Default constructor for the singleton
50 * Registers the instance as shutdown hook with the Java Runtime
52 private ShutdownThread()
56 /* ------------------------------------------------------------ */
57 private synchronized void hook()
62 Runtime.getRuntime().addShutdownHook(this);
68 LOG.info("shutdown already commenced");
72 /* ------------------------------------------------------------ */
73 private synchronized void unhook()
78 Runtime.getRuntime().removeShutdownHook(this);
83 LOG.debug("shutdown already commenced");
87 /* ------------------------------------------------------------ */
89 * Returns the instance of the singleton
91 * @return the singleton instance of the {@link ShutdownThread}
93 public static ShutdownThread getInstance()
98 /* ------------------------------------------------------------ */
99 public static synchronized void register(LifeCycle... lifeCycles)
101 _thread._lifeCycles.addAll(Arrays.asList(lifeCycles));
102 if (_thread._lifeCycles.size()>0)
106 /* ------------------------------------------------------------ */
107 public static synchronized void register(int index, LifeCycle... lifeCycles)
109 _thread._lifeCycles.addAll(index,Arrays.asList(lifeCycles));
110 if (_thread._lifeCycles.size()>0)
114 /* ------------------------------------------------------------ */
115 public static synchronized void deregister(LifeCycle lifeCycle)
117 _thread._lifeCycles.remove(lifeCycle);
118 if (_thread._lifeCycles.size()==0)
122 /* ------------------------------------------------------------ */
123 public static synchronized boolean isRegistered(LifeCycle lifeCycle)
125 return _thread._lifeCycles.contains(lifeCycle);
128 /* ------------------------------------------------------------ */
132 for (LifeCycle lifeCycle : _thread._lifeCycles)
136 if (lifeCycle.isStarted())
139 LOG.debug("Stopped {}",lifeCycle);
142 if (lifeCycle instanceof Destroyable)
144 ((Destroyable)lifeCycle).destroy();
145 LOG.debug("Destroyed {}",lifeCycle);