package io.vertx.it.plugin;

import io.vertx.it.plugin.win.WindowsDestroyer;
import org.apache.commons.exec.OS;
import org.apache.commons.exec.ProcessDestroyer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by clement on 25/06/2015.
 */
class Destroyer implements ProcessDestroyer {

  public static final Destroyer INSTANCE = new Destroyer();

  private List<Process> processes = new ArrayList<>();

  private Destroyer() {
    // Avoid direct instantiation.
  }

  public void killThemAll() {
    System.out.println(processes.size() + " processes need to be destroyed");
    List<Process> copy = new ArrayList<>(processes);
    for (Process p : copy) {
      if (OS.isFamilyWindows()) {
        // Well windows...
        // Destroying the process won't kill children process,
        // so we need another dirty way
        killWindowsProcessAndChildren(p);
      } else {
        p.destroyForcibly();
      }
      processes.remove(p);
      System.out.println("Process destroyed");
    }
  }

  private void killWindowsProcessAndChildren(Process p) {
    WindowsDestroyer.destroy(p);
  }

  /**
   * Returns <code>true</code> if the specified
   * {@link Process} was
   * successfully added to the list of processes to be destroy.
   *
   * @param process the process to add
   * @return <code>true</code> if the specified
   * {@link Process} was
   * successfully added
   */
  @Override
  public boolean add(Process process) {
    return processes.add(process);
  }

  /**
   * Returns <code>true</code> if the specified
   * {@link Process} was
   * successfully removed from the list of processes to be destroy.
   *
   * @param process the process to remove
   * @return <code>true</code> if the specified
   * {@link Process} was
   * successfully removed
   */
  @Override
  public boolean remove(Process process) {
    return processes.remove(process);
  }

  /**
   * Returns the number of registered processes.
   *
   * @return the number of register process
   */
  @Override
  public int size() {
    return processes.size();
  }

}

@@ -489,16 +489,29 @@
  public 
+synchronized 
 void kil
@@ -519,24 +519,77 @@
 ThemAll() %7B%0A
+    if (processes.size() %3C= 0) %7B%0A      return;%0A    %7D%0A
     System.o
@@ -1478,32 +1478,45 @@
 erride%0A  public 
+synchronized 
 boolean add(Proc
@@ -1890,16 +1890,29 @@
  public 
+synchronized 
 boolean 
@@ -2111,16 +2111,29 @@
  public 
+synchronized 
 int size

Fix synchronization