comparison src/org/eclipse/jetty/util/thread/ShutdownThread.java @ 802:3428c60d7cfc

replace jetty jars with source
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 07 Sep 2016 21:15:48 -0600
parents
children 8e9db0bbf4f9
comparison
equal deleted inserted replaced
801:6a21393191c1 802:3428c60d7cfc
1 //
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
19 package org.eclipse.jetty.util.thread;
20
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.concurrent.CopyOnWriteArrayList;
24
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;
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 */
38 public 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 }