bodewig     2004/05/27 00:06:27

  Modified:    src/main/org/apache/tools/ant/taskdefs ProcessDestroyer.java
  Log:
  Don't try to add or remove shutdown hooks while the hook is running.  Remove 
a processes.wait that won't ever return as far as I can tell.
  
  Revision  Changes    Path
  1.17      +35 -27    
ant/src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java
  
  Index: ProcessDestroyer.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- ProcessDestroyer.java     9 Mar 2004 16:48:06 -0000       1.16
  +++ ProcessDestroyer.java     27 May 2004 07:06:27 -0000      1.17
  @@ -38,6 +38,9 @@
       // whether or not this ProcessDestroyer has been registered as a
       // shutdown hook
       private boolean added = false;
  +    // whether or not this ProcessDestroyer is currently running as
  +    // shutdown hook
  +    private boolean running = false;
   
       private class ProcessDestroyerImpl extends Thread {
           private boolean shouldDestroy = true;
  @@ -89,7 +92,7 @@
        * uses reflection to ensure pre-JDK 1.3 compatibility.
        */
       private void addShutdownHook() {
  -        if (addShutdownHookMethod != null) {
  +        if (addShutdownHookMethod != null && !running) {
               destroyProcessThread = new ProcessDestroyerImpl();
               Object[] args = {destroyProcessThread};
               try {
  @@ -98,7 +101,13 @@
               } catch (IllegalAccessException e) {
                   e.printStackTrace();
               } catch (InvocationTargetException e) {
  -                e.printStackTrace();
  +                Throwable t = e.getCause();
  +                if (t != null && t.getClass() == 
IllegalStateException.class) {
  +                    // shutdown already is in progress
  +                    running = true;
  +                } else {
  +                    e.printStackTrace();
  +                }
               }
           }
       }
  @@ -108,7 +117,7 @@
        * uses reflection to ensure pre-JDK 1.3 compatibility
        */
       private void removeShutdownHook() {
  -        if (removeShutdownHookMethod != null && destroyProcessThread != 
null) {
  +        if (removeShutdownHookMethod != null && added && !running) {
               Object[] args = {destroyProcessThread};
               try {
                   Boolean removed =
  @@ -118,25 +127,31 @@
                   if (!removed.booleanValue()) {
                       System.err.println("Could not remove shutdown hook");
                   }
  -                // start the hook thread, a unstarted thread may not be
  -                // eligible for garbage collection
  -                // Cf.: 
http://developer.java.sun.com/developer/bugParade/bugs/4533087.html
  -                destroyProcessThread.setShouldDestroy(false);
  -                destroyProcessThread.start();
  -                // this should return quickly, since Process.destroy()
  -                try {
  -                    destroyProcessThread.join(20000);
  -                } catch (InterruptedException ie) {
  -                    // the thread didn't die in time
  -                    // it should not kill any processes unexpectedly
  -                }
  -                destroyProcessThread = null;
  -                added = false;
               } catch (IllegalAccessException e) {
                   e.printStackTrace();
               } catch (InvocationTargetException e) {
  -                e.printStackTrace();
  +                Throwable t = e.getCause();
  +                if (t != null && t.getClass() == 
IllegalStateException.class) {
  +                    // shutdown already is in progress
  +                    running = true;
  +                } else {
  +                    e.printStackTrace();
  +                }
               }
  +            // start the hook thread, a unstarted thread may not be
  +            // eligible for garbage collection
  +            // Cf.: 
http://developer.java.sun.com/developer/bugParade/bugs/4533087.html
  +            destroyProcessThread.setShouldDestroy(false);
  +            destroyProcessThread.start();
  +            // this should return quickly, since it basically is a NO-OP.
  +            try {
  +                destroyProcessThread.join(20000);
  +            } catch (InterruptedException ie) {
  +                // the thread didn't die in time
  +                // it should not kill any processes unexpectedly
  +            }
  +            destroyProcessThread = null;
  +            added = false;
           }
       }
   
  @@ -179,8 +194,7 @@
       public boolean remove(Process process) {
           synchronized (processes) {
               boolean processRemoved = processes.removeElement(process);
  -            if (processes.size() == 0) {
  -                processes.notifyAll();
  +            if (processRemoved && processes.size() == 0) {
                   removeShutdownHook();
               }
               return processRemoved;
  @@ -192,16 +206,10 @@
        */
       public void run() {
           synchronized (processes) {
  +            running = true;
               Enumeration e = processes.elements();
               while (e.hasMoreElements()) {
                   ((Process) e.nextElement()).destroy();
  -            }
  -
  -            try {
  -                // wait for all processes to finish
  -                processes.wait();
  -            } catch (InterruptedException interrupt) {
  -                // ignore
               }
           }
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to