You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProcessDestroyer.java 8.0 kB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.lang.reflect.Method;
  21. import java.util.HashSet;
  22. import java.util.Set;
  23. /**
  24. * Destroys all registered <code>Process</code>es when the VM exits.
  25. *
  26. * @since Ant 1.5
  27. */
  28. class ProcessDestroyer implements Runnable {
  29. private static final int THREAD_DIE_TIMEOUT = 20000;
  30. private final Set<Process> processes = new HashSet<>();
  31. // methods to register and unregister shutdown hooks
  32. private Method addShutdownHookMethod;
  33. private Method removeShutdownHookMethod;
  34. private ProcessDestroyerImpl destroyProcessThread = null;
  35. // whether or not this ProcessDestroyer has been registered as a
  36. // shutdown hook
  37. private boolean added = false;
  38. // whether or not this ProcessDestroyer is currently running as
  39. // shutdown hook
  40. private boolean running = false;
  41. private class ProcessDestroyerImpl extends Thread {
  42. private boolean shouldDestroy = true;
  43. public ProcessDestroyerImpl() {
  44. super("ProcessDestroyer Shutdown Hook");
  45. }
  46. @Override
  47. public void run() {
  48. if (shouldDestroy) {
  49. ProcessDestroyer.this.run();
  50. }
  51. }
  52. public void setShouldDestroy(boolean shouldDestroy) {
  53. this.shouldDestroy = shouldDestroy;
  54. }
  55. }
  56. /**
  57. * Constructs a <code>ProcessDestroyer</code> and obtains
  58. * <code>Runtime.addShutdownHook()</code> and
  59. * <code>Runtime.removeShutdownHook()</code> through reflection. The
  60. * ProcessDestroyer manages a list of processes to be destroyed when the
  61. * VM exits. If a process is added when the list is empty,
  62. * this <code>ProcessDestroyer</code> is registered as a shutdown hook. If
  63. * removing a process results in an empty list, the
  64. * <code>ProcessDestroyer</code> is removed as a shutdown hook.
  65. */
  66. ProcessDestroyer() {
  67. try {
  68. // check to see if the shutdown hook methods exists
  69. // (support pre-JDK 1.3 and Non-Sun VMs)
  70. addShutdownHookMethod =
  71. Runtime.class.getMethod("addShutdownHook", Thread.class);
  72. removeShutdownHookMethod =
  73. Runtime.class.getMethod("removeShutdownHook", Thread.class);
  74. // wait to add shutdown hook as needed
  75. } catch (NoSuchMethodException e) {
  76. // it just won't be added as a shutdown hook... :(
  77. } catch (Exception e) {
  78. e.printStackTrace(); //NOSONAR
  79. }
  80. }
  81. /**
  82. * Registers this <code>ProcessDestroyer</code> as a shutdown hook,
  83. * uses reflection to ensure pre-JDK 1.3 compatibility.
  84. */
  85. private void addShutdownHook() {
  86. if (addShutdownHookMethod != null && !running) {
  87. destroyProcessThread = new ProcessDestroyerImpl();
  88. try {
  89. addShutdownHookMethod.invoke(Runtime.getRuntime(), destroyProcessThread);
  90. added = true;
  91. } catch (IllegalAccessException e) {
  92. e.printStackTrace(); //NOSONAR
  93. } catch (InvocationTargetException e) {
  94. Throwable t = e.getTargetException();
  95. if (t != null && t.getClass() == IllegalStateException.class) {
  96. // shutdown already is in progress
  97. running = true;
  98. } else {
  99. e.printStackTrace(); //NOSONAR
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Removes this <code>ProcessDestroyer</code> as a shutdown hook,
  106. * uses reflection to ensure pre-JDK 1.3 compatibility
  107. */
  108. private void removeShutdownHook() {
  109. if (removeShutdownHookMethod != null && added && !running) {
  110. try {
  111. if (!Boolean.TRUE.equals(removeShutdownHookMethod
  112. .invoke(Runtime.getRuntime(), destroyProcessThread))) {
  113. System.err.println("Could not remove shutdown hook");
  114. }
  115. } catch (IllegalAccessException e) {
  116. e.printStackTrace(); //NOSONAR
  117. } catch (InvocationTargetException e) {
  118. Throwable t = e.getTargetException();
  119. if (t != null && t.getClass() == IllegalStateException.class) {
  120. // shutdown already is in progress
  121. running = true;
  122. } else {
  123. e.printStackTrace(); //NOSONAR
  124. }
  125. }
  126. // start the hook thread, a unstarted thread may not be
  127. // eligible for garbage collection
  128. // Cf.: http://developer.java.sun.com/developer/bugParade/bugs/4533087.html
  129. destroyProcessThread.setShouldDestroy(false);
  130. if (!destroyProcessThread.getThreadGroup().isDestroyed()) {
  131. // start() would throw IllegalThreadStateException from
  132. // ThreadGroup.add if it were destroyed
  133. destroyProcessThread.start();
  134. }
  135. // this should return quickly, since it basically is a NO-OP.
  136. try {
  137. destroyProcessThread.join(THREAD_DIE_TIMEOUT);
  138. } catch (InterruptedException ie) {
  139. // the thread didn't die in time
  140. // it should not kill any processes unexpectedly
  141. }
  142. destroyProcessThread = null;
  143. added = false;
  144. }
  145. }
  146. /**
  147. * Returns whether or not the ProcessDestroyer is registered as
  148. * as shutdown hook
  149. * @return true if this is currently added as shutdown hook
  150. */
  151. public boolean isAddedAsShutdownHook() {
  152. return added;
  153. }
  154. /**
  155. * Returns <code>true</code> if the specified <code>Process</code> was
  156. * successfully added to the list of processes to destroy upon VM exit.
  157. *
  158. * @param process the process to add
  159. * @return <code>true</code> if the specified <code>Process</code> was
  160. * successfully added
  161. */
  162. public boolean add(Process process) {
  163. synchronized (processes) {
  164. // if this list is empty, register the shutdown hook
  165. if (processes.isEmpty()) {
  166. addShutdownHook();
  167. }
  168. return processes.add(process);
  169. }
  170. }
  171. /**
  172. * Returns <code>true</code> if the specified <code>Process</code> was
  173. * successfully removed from the list of processes to destroy upon VM exit.
  174. *
  175. * @param process the process to remove
  176. * @return <code>true</code> if the specified <code>Process</code> was
  177. * successfully removed
  178. */
  179. public boolean remove(Process process) {
  180. synchronized (processes) {
  181. boolean processRemoved = processes.remove(process);
  182. if (processRemoved && processes.isEmpty()) {
  183. removeShutdownHook();
  184. }
  185. return processRemoved;
  186. }
  187. }
  188. /**
  189. * Invoked by the VM when it is exiting.
  190. */
  191. @Override
  192. public void run() {
  193. synchronized (processes) {
  194. running = true;
  195. processes.forEach(Process::destroy);
  196. }
  197. }
  198. }