Improve the process runner 42/95042/2
authorKanagaraj M <mkr1481@gmail.com>
Thu, 5 Sep 2019 13:36:08 +0000 (19:06 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Thu, 5 Sep 2019 13:41:33 +0000 (19:11 +0530)
Issue-ID: CLI-166

Change-Id: I51fd485652ecccb7696e3023c527622d5e2ef966
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java
framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java

index e8b4389..d09dfa5 100644 (file)
@@ -342,7 +342,7 @@ public class OnapCommandExecutionStore {
                 //find results -type d -newermt '2019-02-11 10:00:00' ! -newermt '2019-02-11 15:10:00' -name "*__*__profile-list*"
                 //find 'results' -type d -newermt '2019-02-11T10:00:00.000' ! -newermt '2019-02-11T15:10:00.000' -name "*__*__profile*"
 
-                String searchString = "find '" + new File(getBasePath()).getAbsolutePath() + "' -type d ";
+                String searchString = "find " + new File(getBasePath()).getAbsolutePath() + " -type d ";
 
                 String startTime = search.get("startTime");
                 if (startTime != null) {
@@ -354,7 +354,7 @@ public class OnapCommandExecutionStore {
                     searchString += " ! -newermt " + endTime ;
                 }
 
-                searchString += " -name '";
+                searchString += " -name \"";
 
                 if(search.containsKey("execution-id")) {
                     searchString += search.get("execution-id");
@@ -366,7 +366,7 @@ public class OnapCommandExecutionStore {
 
                 for (String term: Arrays.asList(new String []{"product", "service", "command", "profile"})) {
                     searchString += "__";
-                    if (search.get(term) != null) {
+                    if (search.get(term) != null && !search.get(term).isEmpty()) {
                         searchString += search.get(term);
                     } else {
                         searchString += "*";
@@ -375,16 +375,18 @@ public class OnapCommandExecutionStore {
                 if (!searchString.endsWith("*"))
                     searchString += "*";
 
-                searchString += "'";
+                searchString += "\"";
 
                 ProcessRunner pr = new ProcessRunner(new String [] {searchString}, null, ".");
+                pr.setTimeout(10000);
                 pr.overrideToUnix();
                 pr.run();
                 if (pr.getExitCode() != 0) {
                     throw new OnapCommandExecutionFailed("System failed to search the executions with error " + pr.getError());
                 }
 
-                dirs = Arrays.asList(pr.getOutput().split("\\r?\\n"));
+                if (!pr.getOutput().trim().isEmpty())
+                    dirs = Arrays.asList(pr.getOutput().split("\\r?\\n"));
             }
 
             for (String dir: dirs) {
index d36a0d6..ff9d552 100644 (file)
 package org.onap.cli.fw.utils;
 
 import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
-import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -41,8 +41,8 @@ public class ProcessRunner {
     private String cwd = System.getProperty("user.home");
     private String []env = null;
     private int exitCode = -1;
-    private String output;
-    private String error;
+    private String output = "";
+    private String error = "";
     private long timeout = 0;
     private OutputStream stdout;
     private OutputStream stderr;
@@ -92,12 +92,6 @@ public class ProcessRunner {
     public void run() throws InterruptedException, IOException {
         Process p = null;
 
-        final StringWriter writerOutput = new StringWriter();
-        final StringWriter writerError = new StringWriter();
-
-        final OutputStream stdout = this.getStdout();
-        final OutputStream stderr = this.getStderr();
-
         if (this.cmd.length == 1) {
             p = Runtime.getRuntime().exec(this.shell + this.cmd[0], this.env, null);
         } else {
@@ -107,47 +101,70 @@ public class ProcessRunner {
             p = Runtime.getRuntime().exec(cmds, this.env, null);
         }
 
-        final Process p1 = p;
-        new Thread(new Runnable() {
+        boolean readOutput = false;
+        if (this.getStdout() == null) {
+            this.setStdout(new ByteArrayOutputStream());
+            readOutput = true;
+        }
+
+        boolean readError = false;
+        if (this.getStderr() == null) {
+            this.setStderr(new ByteArrayOutputStream());
+            readError = true;
+        }
+
+        final OutputStream stdout = this.getStdout();
+        final OutputStream stderr = this.getStderr();
+
+        final InputStream stdoutP = p.getInputStream();
+        final InputStream stderrP = p.getErrorStream();
+
+        Thread outThread = new Thread(new Runnable() {
             public void run() {
                 try {
-                    if (stdout != null) {
-                        IOUtils.copy(p1.getInputStream(), stdout);
-                    }
-                    else IOUtils.copy(p1.getInputStream(), writerOutput);
+                    IOUtils.copy(stdoutP, stdout);
                 } catch (IOException e) {
                 }
             }
-        }).start();
+        });
 
-        new Thread(new Runnable() {
+        Thread errThread = new Thread(new Runnable() {
             public void run() {
                 try {
-                    if (stderr != null) {
-                        IOUtils.copy(p1.getErrorStream(), stderr);
-                    }
-                    else IOUtils.copy(p1.getErrorStream(), writerError);
+                    IOUtils.copy(stderrP, stderr);
                 } catch (IOException e) {
                 }
             }
-        }).start();
+        });
+
+        outThread.start();
+        errThread.start();
 
         boolean completed = p.waitFor(this.getTimeout(), TimeUnit.MILLISECONDS);
+        outThread.join();
+        errThread.join();
+
         if (completed) {
             this.exitCode = p.exitValue();
         }
 
-        this.output = writerOutput.toString();
-        this.error = writerError.toString();
-        log.debug("CMD: " + Arrays.asList(this.cmd).toString() + "\nWORKING_DIR: " + this.cwd + "\nENV: " +
-        ((this.env == null) ? this.env : Arrays.asList(this.env).toString()) +
-                "\nOUTPUT: " + this.output + "\nERROR: " + this.error + "\nEXIT_CODE: " + this.exitCode);
+        if (readOutput)
+            this.output = new String(((ByteArrayOutputStream)this.getStdout()).toByteArray(), "UTF-8");
+
+        if (readError)
+            this.error = new String(((ByteArrayOutputStream)this.getStderr()).toByteArray(), "UTF-8");;
+
         p.destroy();
 
+        log.debug("CMD: " + Arrays.asList(this.cmd).toString() +
+                "\nWORKING_DIR: " + this.cwd +
+                "\nENV: " + ((this.env == null) ? this.env : Arrays.asList(this.env).toString()) +
+                "\nOUTPUT: " + this.output +
+                "\nERROR: " + this.error +
+                "\nEXIT_CODE: " + this.exitCode);
+
         if (!completed) {
             throw new RuntimeException("TIMEOUT:: cmd:" + Arrays.asList(this.cmd).toString());
-        } else {
-
         }
     }