KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > ShutdownHook


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.lang.reflect.Method JavaDoc;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13
14 /**
15  * This is instantiated statically by ProxoolFacade. It will automatically
16  * close down all the connections when the JVM stops.
17  * @version $Revision: 1.13 $, $Date: 2006/11/02 10:00:34 $
18  * @author bill
19  * @author $Author: billhorsman $ (current maintainer)
20  * @since Proxool 0.7
21  */

22 class ShutdownHook implements Runnable JavaDoc {
23
24     private static final Log LOG = LogFactory.getLog(ShutdownHook.class);
25
26     private static boolean registered;
27
28     protected static void init() {
29         if (!registered) {
30             registered = true;
31             new ShutdownHook();
32         }
33     }
34
35     protected static void remove(Thread JavaDoc t) {
36         Runtime JavaDoc runtime = Runtime.getRuntime();
37         try {
38             final Method JavaDoc removeShutdownHookMethod = Runtime JavaDoc.class.getMethod("removeShutdownHook", new Class JavaDoc[] {Thread JavaDoc.class});
39             removeShutdownHookMethod.invoke(runtime, new Object JavaDoc[] {t});
40             if (LOG.isDebugEnabled()) {
41                 LOG.debug("Removed shutdownHook");
42             }
43         } catch (NoSuchMethodException JavaDoc e) {
44             LOG.warn("Proxool will have to be shutdown manually with ProxoolFacade.shutdown() because this version of the JDK does not support Runtime.getRuntime().addShutdownHook()");
45         } catch (SecurityException JavaDoc e) {
46             LOG.error("Problem removing shutdownHook", e);
47         } catch (IllegalAccessException JavaDoc e) {
48             LOG.error("Problem removing shutdownHook", e);
49         } catch (InvocationTargetException JavaDoc e) {
50             // Use getTargetException() because getCause() is only supported in JDK 1.4 and later
51
Throwable JavaDoc cause = ((InvocationTargetException JavaDoc) e).getTargetException();
52             if (cause instanceof IllegalStateException JavaDoc) {
53                 // This is probably because a shutdown is in progress. We can
54
// safely ignore that.
55
} else {
56                 LOG.error("Problem removing shutdownHook", e);
57             }
58         }
59     }
60
61     /**
62      * Registers this ShutdownHook with Runtime
63      */

64     private ShutdownHook() {
65         Thread JavaDoc t = new Thread JavaDoc(this);
66         t.setName("ShutdownHook");
67         Runtime JavaDoc runtime = Runtime.getRuntime();
68         try {
69             Method JavaDoc addShutdownHookMethod = Runtime JavaDoc.class.getMethod("addShutdownHook", new Class JavaDoc[] {Thread JavaDoc.class});
70             addShutdownHookMethod.invoke(runtime, new Object JavaDoc[] {t});
71             ProxoolFacade.setShutdownHook(t);
72             if (LOG.isDebugEnabled()) {
73                 LOG.debug("Registered shutdownHook");
74             }
75         } catch (NoSuchMethodException JavaDoc e) {
76             LOG.warn("Proxool will have to be shutdown manually with ProxoolFacade.shutdown() because this version of the JDK does not support Runtime.getRuntime().addShutdownHook()");
77         } catch (SecurityException JavaDoc e) {
78             LOG.error("Problem registering shutdownHook", e);
79         } catch (IllegalAccessException JavaDoc e) {
80             LOG.error("Problem registering shutdownHook", e);
81         } catch (InvocationTargetException JavaDoc e) {
82             LOG.error("Problem registering shutdownHook", e);
83         }
84     }
85
86     /**
87      * Remove all connection pools without delay. Only runs if the
88      * shutdown hook is {@link org.logicalcobwebs.proxool.ProxoolFacade#isShutdownHookEnabled() enabled}.
89      * @see ProxoolFacade#removeAllConnectionPools
90      */

91     public void run() {
92         if (ProxoolFacade.isShutdownHookEnabled()) {
93             LOG.debug("Running ShutdownHook");
94             Thread.currentThread().setName("Shutdown Hook");
95             ProxoolFacade.shutdown(0);
96         } else {
97             LOG.debug("Skipping ShutdownHook because it's been disabled");
98         }
99     }
100
101 }
102
103
104 /*
105  Revision history:
106  $Log: ShutdownHook.java,v $
107  Revision 1.13 2006/11/02 10:00:34 billhorsman
108  Added ProxoolFacade.disableShutdownHook.
109
110  Revision 1.12 2006/01/18 14:40:02 billhorsman
111  Unbundled Jakarta's Commons Logging.
112
113  Revision 1.11 2003/12/16 09:09:32 billhorsman
114  Switched from getCause() to getTargetException() so that we can trap the IllegalStateException in all JDKs.
115
116  Revision 1.10 2003/11/16 18:19:14 chr32
117  Started calling to Exception.getCause() via refletion to maintain compilability with < jdk 1.4 compilers.
118
119  Revision 1.9 2003/10/27 12:32:06 billhorsman
120  Fixed typos and silently ignore IllegalStateException during shutdownHook removal (it's probably because
121  the JVM is shutting down).
122
123  Revision 1.8 2003/09/07 22:05:15 billhorsman
124  Now uses reflection to add ShutdownHook to Runtime so that it is JDK independent. Using JDK1.2
125  will disable the shutdownHook and simply log a warning message that Proxool must be shutdown
126  explicitly.
127
128  Revision 1.7 2003/03/03 17:07:58 billhorsman
129  name thread
130
131  Revision 1.6 2003/03/03 11:11:58 billhorsman
132  fixed licence
133
134  Revision 1.5 2003/02/26 11:20:59 billhorsman
135  removed debug
136
137  Revision 1.4 2003/02/10 15:13:57 billhorsman
138  fixed deprecated call
139
140  Revision 1.3 2003/02/06 17:41:05 billhorsman
141  now uses imported logging
142
143  Revision 1.2 2003/02/04 17:19:11 billhorsman
144  ShutdownHook now initialises
145
146  Revision 1.1 2003/02/04 15:04:17 billhorsman
147  New ShutdownHook
148
149  */
Popular Tags