1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.util.Enumeration ; 24 import java.util.Vector ; 25 26 31 class ProcessDestroyer implements Runnable { 32 33 private Vector processes = new Vector (); 34 private Method addShutdownHookMethod; 36 private Method removeShutdownHookMethod; 37 private ProcessDestroyerImpl destroyProcessThread = null; 38 39 private boolean added = false; 42 private boolean running = false; 45 46 private class ProcessDestroyerImpl extends Thread { 47 private boolean shouldDestroy = true; 48 49 public ProcessDestroyerImpl() { 50 super("ProcessDestroyer Shutdown Hook"); 51 } 52 public void run() { 53 if (shouldDestroy) { 54 ProcessDestroyer.this.run(); 55 } 56 } 57 58 public void setShouldDestroy(boolean shouldDestroy) { 59 this.shouldDestroy = shouldDestroy; 60 } 61 } 62 63 73 public ProcessDestroyer() { 74 try { 75 Class [] paramTypes = {Thread .class}; 78 addShutdownHookMethod = 79 Runtime .class.getMethod("addShutdownHook", paramTypes); 80 81 removeShutdownHookMethod = 82 Runtime .class.getMethod("removeShutdownHook", paramTypes); 83 } catch (NoSuchMethodException e) { 85 } catch (Exception e) { 87 e.printStackTrace(); 88 } 89 } 90 91 95 private void addShutdownHook() { 96 if (addShutdownHookMethod != null && !running) { 97 destroyProcessThread = new ProcessDestroyerImpl(); 98 Object [] args = {destroyProcessThread}; 99 try { 100 addShutdownHookMethod.invoke(Runtime.getRuntime(), args); 101 added = true; 102 } catch (IllegalAccessException e) { 103 e.printStackTrace(); 104 } catch (InvocationTargetException e) { 105 Throwable t = e.getTargetException(); 106 if (t != null && t.getClass() == IllegalStateException .class) { 107 running = true; 109 } else { 110 e.printStackTrace(); 111 } 112 } 113 } 114 } 115 116 120 private void removeShutdownHook() { 121 if (removeShutdownHookMethod != null && added && !running) { 122 Object [] args = {destroyProcessThread}; 123 try { 124 Boolean removed = 125 (Boolean ) removeShutdownHookMethod.invoke( 126 Runtime.getRuntime(), 127 args); 128 if (!removed.booleanValue()) { 129 System.err.println("Could not remove shutdown hook"); 130 } 131 } catch (IllegalAccessException e) { 132 e.printStackTrace(); 133 } catch (InvocationTargetException e) { 134 Throwable t = e.getTargetException(); 135 if (t != null && t.getClass() == IllegalStateException .class) { 136 running = true; 138 } else { 139 e.printStackTrace(); 140 } 141 } 142 destroyProcessThread.setShouldDestroy(false); 146 if (!destroyProcessThread.getThreadGroup().isDestroyed()) { 147 destroyProcessThread.start(); 150 } 151 try { 153 destroyProcessThread.join(20000); 154 } catch (InterruptedException ie) { 155 } 158 destroyProcessThread = null; 159 added = false; 160 } 161 } 162 163 168 public boolean isAddedAsShutdownHook() { 169 return added; 170 } 171 172 180 public boolean add(Process process) { 181 synchronized (processes) { 182 if (processes.size() == 0) { 184 addShutdownHook(); 185 } 186 processes.addElement(process); 187 return processes.contains(process); 188 } 189 } 190 191 199 public boolean remove(Process process) { 200 synchronized (processes) { 201 boolean processRemoved = processes.removeElement(process); 202 if (processRemoved && processes.size() == 0) { 203 removeShutdownHook(); 204 } 205 return processRemoved; 206 } 207 } 208 209 212 public void run() { 213 synchronized (processes) { 214 running = true; 215 Enumeration e = processes.elements(); 216 while (e.hasMoreElements()) { 217 ((Process ) e.nextElement()).destroy(); 218 } 219 } 220 } 221 } 222 | Popular Tags |