1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.util;
22
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Vector;
26
27 import org.apache.log4j.Logger;
28
29 /***
30 * TODO Add docs
31 *
32 * Created on 09.12.2004
33 * Contributing Author(s):
34 *
35 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
36 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
37 *
38 * @author vlads
39 * @version $Revision: 1.4 $ ($Author: vlads $) $Date: 2004/12/09 22:58:18 $
40 */
41 public class ShutdownHook extends Thread {
42
43 protected static final Logger log = Logger.getLogger(ShutdownHook.class);
44
45 public static ShutdownHook instance;
46
47 private List listenerList;
48 private static boolean shutDownInProgress;
49
50 private ShutdownHook() {
51 listenerList = new Vector();
52 Runtime.getRuntime().addShutdownHook(this);
53 }
54
55 public static synchronized void addListener(ShutdownListener listener) {
56 if (shutDownInProgress) {
57 return;
58 }
59
60 if (instance == null) {
61 try {
62 instance = new ShutdownHook();
63 log.info("ShutdownHook registered");
64 } catch (SecurityException e) {
65 log.info("manager denies RuntimePermission(shutdownHooks)");
66 return;
67 }
68 }
69
70 if (!instance.listenerList.contains(listener)) {
71 instance.listenerList.add(listener);
72 }
73 }
74
75 public static synchronized void removeListener(ShutdownListener listener) {
76 if (shutDownInProgress) {
77 return;
78 }
79 instance.listenerList.remove(listener);
80 }
81
82 public void run() {
83 Thread.currentThread().setName("Jour-Shutdown");
84 log.debug("System shutdown In Progress");
85 try {
86 shutDownInProgress = true;
87 shutdownNow();
88 } catch (Exception e) {
89 log.error("shutdown error", e);
90 }
91 log.info("Jour - System shutdown completed");
92 }
93
94 private void shutdownNow() {
95 for (Iterator i = listenerList.iterator(); i.hasNext(); ) {
96 ShutdownListener listener = (ShutdownListener) i.next();
97 listener.shutdown();
98 }
99 }
100
101 }