View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
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 }