summaryrefslogtreecommitdiffstats
path: root/profiles/command/src
diff options
context:
space:
mode:
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>2018-08-13 14:32:41 +0530
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>2018-08-13 15:10:16 +0530
commit7e5f6a0f9c1e7d8ee52d96fc4b1c28ccb65168f2 (patch)
tree6a4abeb97e3cb9897d94b919257363342eb62b9c /profiles/command/src
parent7aae8a875703270ece78d7cc0d3d2674294b1b8d (diff)
Command profile - process runner
Issue-ID: VNFSDK-300 CLI-123 Change-Id: Icb217ecb43d81a7426a7a7874b9f8408b11ab450 Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'profiles/command/src')
-rw-r--r--profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/ProcessRunner.java133
1 files changed, 133 insertions, 0 deletions
diff --git a/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/ProcessRunner.java b/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/ProcessRunner.java
new file mode 100644
index 00000000..5bd3f463
--- /dev/null
+++ b/profiles/command/src/main/java/org/onap/cli/fw/cmd/cmd/ProcessRunner.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2018 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.cli.fw.cmd.cmd;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+public class ProcessRunner {
+
+ private String []cmd = null;
+ private static String shell = System.getProperty("os.name").toLowerCase().startsWith("windows") ? "cmd.exe /c " : "sh -c ";
+ private String cwd = System.getProperty("user.home");
+ private String []env = null;
+ private int exitCode = -1;
+ private String output;
+ private String error;
+ private Map<String, Object> results;
+
+ public ProcessRunner(String []cmd, String []env, String cwd) {
+ this.cmd = cmd;
+
+ if (cwd != null && !cwd.isEmpty()) {
+ this.cwd = cwd;
+ }
+
+ this.env = env;
+ }
+
+ public ProcessRunner(String []cmd, String cwd) {
+ this(cmd, null, cwd);
+ }
+
+ public ProcessRunner(String []cmd) {
+ this(cmd, null, null);
+ }
+
+ public ProcessRunner(String cmd, String []env, String cwd) {
+ this(new String []{cmd}, env, cwd);
+ }
+
+ public ProcessRunner(String cmd, String cwd) {
+ this(new String []{cmd}, null, cwd);
+ }
+
+ public ProcessRunner(String cmd) {
+ this(new String []{cmd}, null, null);
+ }
+
+ @SuppressWarnings("unchecked")
+ public void run() throws InterruptedException, IOException {
+ Process p = null;
+ if (this.cmd.length == 1) {
+ p = Runtime.getRuntime().exec(this.shell + this.cmd[0], this.env, null);
+ } else {
+ List list = new ArrayList(Arrays.asList(this.shell.split(" ")));
+ list.addAll(Arrays.asList(this.cmd));
+ String []cmds = Arrays.copyOf(list.toArray(), list.size(), String[].class);
+ p = Runtime.getRuntime().exec(cmds, this.env, null);
+ }
+
+ this.exitCode = p.waitFor();
+ this.output = this.streamToString(p.getInputStream());
+ this.error = this.streamToString(p.getErrorStream());
+ p.destroy();
+ }
+
+ public String streamToString(InputStream stream) throws IOException {
+ StringBuilder sb = new StringBuilder();
+ BufferedReader br = null;
+ try {
+ br = new BufferedReader(new InputStreamReader(stream));
+ String line = null;
+ while ((line = br.readLine()) != null) {
+ sb.append(line + System.getProperty("line.separator"));
+ }
+ } finally {
+ br.close();
+ }
+ return sb.toString();
+ }
+
+ public int getExitCode() {
+ return this.exitCode;
+ }
+
+ public String getOutput() {
+ return this.output;
+ }
+
+ public String getError() {
+ return this.error;
+ }
+
+ public static void main(String[] args) {
+ try {
+ ProcessRunner pr = new ProcessRunner("dir", null);
+ pr.run();
+ System.out.println(pr.getOutput());
+ System.out.println(pr.getError());
+ System.out.println(pr.getExitCode());
+
+ pr = new ProcessRunner(new String [] {"dir", "c:"}, null);
+ pr.run();
+ System.out.println(pr.getOutput());
+ System.out.println(pr.getError());
+ System.out.println(pr.getExitCode());
+
+ } catch (InterruptedException | IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+} \ No newline at end of file