package eu.stratuslab.storage.disk.utils;

import java.io.IOException;

import org.restlet.data.Status;
import org.restlet.resource.ResourceException;

import eu.stratuslab.storage.disk.main.PersistentDiskApplication;

public class ProcessUtils {

    public static void execute(ProcessBuilder pb, String errorMsg) {
        int returnCode = 1;
        Process process;

        try {
            process = pb.start();

            boolean blocked = true;
            while (blocked) {
                process.waitFor();
                blocked = false;
            }

            returnCode = process.exitValue();
        } catch (IOException e) {
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
                    "An error occurred while executing command: "
                            + PersistentDiskApplication.join(pb.command(), " ")
                            + ".\n" + errorMsg + ".");
        } catch (InterruptedException consumed) {
            // Just continue with the loop.
        }

        if (returnCode != 0) {
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
                    "An error occurred while executing command: "
                            + PersistentDiskApplication.join(pb.command(), " ")
                            + ".\n" + errorMsg + ".\nReturn code was: "
                            + String.valueOf(returnCode));
        }
    }

}

@@ -475,24 +475,50 @@
 (blocked) %7B%0A
+                try %7B%0A    
             
@@ -556,16 +556,20 @@
         
+    
 blocked 
@@ -577,16 +577,137 @@
  false;%0A
+                %7D catch (InterruptedException consumed) %7B%0A                    // just continue to wait%0A                %7D%0A
         
@@ -1077,102 +1077,8 @@
    %7D
- catch (InterruptedException consumed) %7B%0A            // Just continue with the loop.%0A        %7D
 %0A%0A  

correct logic when dealing with interruptions