001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: import java.lang.reflect.Method;
012: import java.lang.reflect.InvocationTargetException;
013:
014: /**
015: * This is instantiated statically by ProxoolFacade. It will automatically
016: * close down all the connections when the JVM stops.
017: * @version $Revision: 1.13 $, $Date: 2006/11/02 10:00:34 $
018: * @author bill
019: * @author $Author: billhorsman $ (current maintainer)
020: * @since Proxool 0.7
021: */
022: class ShutdownHook implements Runnable {
023:
024: private static final Log LOG = LogFactory
025: .getLog(ShutdownHook.class);
026:
027: private static boolean registered;
028:
029: protected static void init() {
030: if (!registered) {
031: registered = true;
032: new ShutdownHook();
033: }
034: }
035:
036: protected static void remove(Thread t) {
037: Runtime runtime = Runtime.getRuntime();
038: try {
039: final Method removeShutdownHookMethod = Runtime.class
040: .getMethod("removeShutdownHook",
041: new Class[] { Thread.class });
042: removeShutdownHookMethod
043: .invoke(runtime, new Object[] { t });
044: if (LOG.isDebugEnabled()) {
045: LOG.debug("Removed shutdownHook");
046: }
047: } catch (NoSuchMethodException e) {
048: LOG
049: .warn("Proxool will have to be shutdown manually with ProxoolFacade.shutdown() because this version of the JDK does not support Runtime.getRuntime().addShutdownHook()");
050: } catch (SecurityException e) {
051: LOG.error("Problem removing shutdownHook", e);
052: } catch (IllegalAccessException e) {
053: LOG.error("Problem removing shutdownHook", e);
054: } catch (InvocationTargetException e) {
055: // Use getTargetException() because getCause() is only supported in JDK 1.4 and later
056: Throwable cause = ((InvocationTargetException) e)
057: .getTargetException();
058: if (cause instanceof IllegalStateException) {
059: // This is probably because a shutdown is in progress. We can
060: // safely ignore that.
061: } else {
062: LOG.error("Problem removing shutdownHook", e);
063: }
064: }
065: }
066:
067: /**
068: * Registers this ShutdownHook with Runtime
069: */
070: private ShutdownHook() {
071: Thread t = new Thread(this );
072: t.setName("ShutdownHook");
073: Runtime runtime = Runtime.getRuntime();
074: try {
075: Method addShutdownHookMethod = Runtime.class.getMethod(
076: "addShutdownHook", new Class[] { Thread.class });
077: addShutdownHookMethod.invoke(runtime, new Object[] { t });
078: ProxoolFacade.setShutdownHook(t);
079: if (LOG.isDebugEnabled()) {
080: LOG.debug("Registered shutdownHook");
081: }
082: } catch (NoSuchMethodException e) {
083: LOG
084: .warn("Proxool will have to be shutdown manually with ProxoolFacade.shutdown() because this version of the JDK does not support Runtime.getRuntime().addShutdownHook()");
085: } catch (SecurityException e) {
086: LOG.error("Problem registering shutdownHook", e);
087: } catch (IllegalAccessException e) {
088: LOG.error("Problem registering shutdownHook", e);
089: } catch (InvocationTargetException e) {
090: LOG.error("Problem registering shutdownHook", e);
091: }
092: }
093:
094: /**
095: * Remove all connection pools without delay. Only runs if the
096: * shutdown hook is {@link org.logicalcobwebs.proxool.ProxoolFacade#isShutdownHookEnabled() enabled}.
097: * @see ProxoolFacade#removeAllConnectionPools
098: */
099: public void run() {
100: if (ProxoolFacade.isShutdownHookEnabled()) {
101: LOG.debug("Running ShutdownHook");
102: Thread.currentThread().setName("Shutdown Hook");
103: ProxoolFacade.shutdown(0);
104: } else {
105: LOG
106: .debug("Skipping ShutdownHook because it's been disabled");
107: }
108: }
109:
110: }
111:
112: /*
113: Revision history:
114: $Log: ShutdownHook.java,v $
115: Revision 1.13 2006/11/02 10:00:34 billhorsman
116: Added ProxoolFacade.disableShutdownHook.
117:
118: Revision 1.12 2006/01/18 14:40:02 billhorsman
119: Unbundled Jakarta's Commons Logging.
120:
121: Revision 1.11 2003/12/16 09:09:32 billhorsman
122: Switched from getCause() to getTargetException() so that we can trap the IllegalStateException in all JDKs.
123:
124: Revision 1.10 2003/11/16 18:19:14 chr32
125: Started calling to Exception.getCause() via refletion to maintain compilability with < jdk 1.4 compilers.
126:
127: Revision 1.9 2003/10/27 12:32:06 billhorsman
128: Fixed typos and silently ignore IllegalStateException during shutdownHook removal (it's probably because
129: the JVM is shutting down).
130:
131: Revision 1.8 2003/09/07 22:05:15 billhorsman
132: Now uses reflection to add ShutdownHook to Runtime so that it is JDK independent. Using JDK1.2
133: will disable the shutdownHook and simply log a warning message that Proxool must be shutdown
134: explicitly.
135:
136: Revision 1.7 2003/03/03 17:07:58 billhorsman
137: name thread
138:
139: Revision 1.6 2003/03/03 11:11:58 billhorsman
140: fixed licence
141:
142: Revision 1.5 2003/02/26 11:20:59 billhorsman
143: removed debug
144:
145: Revision 1.4 2003/02/10 15:13:57 billhorsman
146: fixed deprecated call
147:
148: Revision 1.3 2003/02/06 17:41:05 billhorsman
149: now uses imported logging
150:
151: Revision 1.2 2003/02/04 17:19:11 billhorsman
152: ShutdownHook now initialises
153:
154: Revision 1.1 2003/02/04 15:04:17 billhorsman
155: New ShutdownHook
156:
157: */
|