1 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 ; 12 import java.lang.reflect.InvocationTargetException ; 13 14 22 class ShutdownHook implements Runnable { 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 t) { 36 Runtime runtime = Runtime.getRuntime(); 37 try { 38 final Method removeShutdownHookMethod = Runtime .class.getMethod("removeShutdownHook", new Class [] {Thread .class}); 39 removeShutdownHookMethod.invoke(runtime, new Object [] {t}); 40 if (LOG.isDebugEnabled()) { 41 LOG.debug("Removed shutdownHook"); 42 } 43 } catch (NoSuchMethodException 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 e) { 46 LOG.error("Problem removing shutdownHook", e); 47 } catch (IllegalAccessException e) { 48 LOG.error("Problem removing shutdownHook", e); 49 } catch (InvocationTargetException e) { 50 Throwable cause = ((InvocationTargetException ) e).getTargetException(); 52 if (cause instanceof IllegalStateException ) { 53 } else { 56 LOG.error("Problem removing shutdownHook", e); 57 } 58 } 59 } 60 61 64 private ShutdownHook() { 65 Thread t = new Thread (this); 66 t.setName("ShutdownHook"); 67 Runtime runtime = Runtime.getRuntime(); 68 try { 69 Method addShutdownHookMethod = Runtime .class.getMethod("addShutdownHook", new Class [] {Thread .class}); 70 addShutdownHookMethod.invoke(runtime, new Object [] {t}); 71 ProxoolFacade.setShutdownHook(t); 72 if (LOG.isDebugEnabled()) { 73 LOG.debug("Registered shutdownHook"); 74 } 75 } catch (NoSuchMethodException 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 e) { 78 LOG.error("Problem registering shutdownHook", e); 79 } catch (IllegalAccessException e) { 80 LOG.error("Problem registering shutdownHook", e); 81 } catch (InvocationTargetException e) { 82 LOG.error("Problem registering shutdownHook", e); 83 } 84 } 85 86 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 | Popular Tags |