From 25363d85348328874f875997186afaa0d3cd4899 Mon Sep 17 00:00:00 2001 From: Kanagaraj M Date: Thu, 5 Sep 2019 19:06:08 +0530 Subject: Improve the process runner Issue-ID: CLI-166 Change-Id: I51fd485652ecccb7696e3023c527622d5e2ef966 Signed-off-by: Kanagaraj Manickam k00365106 --- .../cli/fw/store/OnapCommandExecutionStore.java | 12 ++-- .../java/org/onap/cli/fw/utils/ProcessRunner.java | 75 +++++++++++++--------- 2 files changed, 53 insertions(+), 34 deletions(-) (limited to 'framework/src/main/java/org/onap') diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java index e8b43891..d09dfa50 100644 --- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java +++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandExecutionStore.java @@ -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) { diff --git a/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java b/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java index d36a0d6c..ff9d5520 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/ProcessRunner.java @@ -17,12 +17,12 @@ 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 { - } } -- cgit 1.2.3-korg