package com.cisco.oss.tools;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

/**
 * Created by Yair Ogen (yaogen) on 10/07/2017.
 */
@Component
@Slf4j
public class PacketDumperSelfDestruct {

    @Value("${LISTEN_PID}")
    private String listenPid;

    private static final int SLEEP = 30000;

    @Scheduled(fixedDelay = SLEEP)
    public void exitIfListPidIsDead() {

        try {
            if (!isStillAllive()) {
                System.exit(0);
            }
        } catch (Exception e) {
            log.warn("problem checking if listen pid: {} is still running. error: {}", listenPid, e.toString());
        }


    }

    public boolean isStillAllive() {
        String OS = System.getProperty("os.name").toLowerCase();
        String command = null;
        if (OS.indexOf("win") >= 0) {
            log.debug("Check alive Windows mode. Pid: [{}]", listenPid);
            command = "cmd /c tasklist /FI \"PID eq " + listenPid + "\"";
        } else if (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0) {
            log.debug("Check alive Linux/Unix mode. Pid: [{}]", listenPid);
            command = "ps -p " + listenPid;
        } else {
            log.warn("Unsuported OS: Check alive for Pid: [{}] return false", listenPid);
            return false;
        }
        return isProcessIdRunning(listenPid, command); // call generic implementation
    }

    private boolean isProcessIdRunning(String pid, String command) {
        log.debug("Command [{}]", command);
        try {
            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec(command);

            InputStreamReader isReader = new InputStreamReader(pr.getInputStream());
            BufferedReader bReader = new BufferedReader(isReader);
            String strLine = null;
            while ((strLine = bReader.readLine()) != null) {
                if (strLine.contains(" " + pid + " ")) {
                    return true;
                }
            }

            return false;
        } catch (Exception ex) {
            log.warn("Got exception using system command [{}].", command, ex);
            return true;
        }
    }


}

@@ -230,47 +230,8 @@
 t;%0A%0A
-import javax.annotation.PostConstruct;%0A
 impo
@@ -294,46 +294,8 @@
 der;
-%0Aimport java.util.concurrent.TimeUnit;
 %0A%0A/*
@@ -431,16 +431,19 @@
 STEN_PID
+:-1
 %7D%22)%0A    
@@ -466,16 +466,23 @@
 istenPid
+ = %22-1%22
 ;%0A%0A    p
@@ -596,16 +596,54 @@
 ead() %7B%0A
+        if (!listenPid.equals(%22-1%22)) %7B
 %0A       
@@ -635,38 +635,46 @@
 -1%22)) %7B%0A        
+    
 try %7B%0A
+    
             if (
@@ -709,16 +709,20 @@
         
+  
+  
 System.e
@@ -741,26 +741,33 @@
         
-%7D%0A
+    %7D%0A   
         
 %7D catch 
@@ -750,32 +750,33 @@
    %7D%0A           
+ 
 %7D catch (Excepti
@@ -779,24 +779,28 @@
 eption e) %7B%0A
+    
             
@@ -908,19 +908,31 @@
         
-%7D%0A%0A
+    %7D%0A        %7D
 %0A    %7D%0A%0A

Add default value. Add check if was configured.