--- /dev/null
+/*\r
+ * Copyright 2018 Huawei Technologies Co., Ltd.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.cli.fw.cmd.cmd;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+public class ProcessRunner {\r
+ \r
+ private String []cmd = null;\r
+ private static String shell = System.getProperty("os.name").toLowerCase().startsWith("windows") ? "cmd.exe /c " : "sh -c ";\r
+ private String cwd = System.getProperty("user.home");\r
+ private String []env = null;\r
+ private int exitCode = -1;\r
+ private String output;\r
+ private String error;\r
+ private Map<String, Object> results;\r
+\r
+ public ProcessRunner(String []cmd, String []env, String cwd) {\r
+ this.cmd = cmd;\r
+ \r
+ if (cwd != null && !cwd.isEmpty()) {\r
+ this.cwd = cwd;\r
+ }\r
+ \r
+ this.env = env;\r
+ }\r
+ \r
+ public ProcessRunner(String []cmd, String cwd) {\r
+ this(cmd, null, cwd);\r
+ }\r
+ \r
+ public ProcessRunner(String []cmd) {\r
+ this(cmd, null, null);\r
+ }\r
+ \r
+ public ProcessRunner(String cmd, String []env, String cwd) {\r
+ this(new String []{cmd}, env, cwd);\r
+ }\r
+ \r
+ public ProcessRunner(String cmd, String cwd) {\r
+ this(new String []{cmd}, null, cwd);\r
+ }\r
+ \r
+ public ProcessRunner(String cmd) {\r
+ this(new String []{cmd}, null, null);\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ public void run() throws InterruptedException, IOException {\r
+ Process p = null;\r
+ if (this.cmd.length == 1) {\r
+ p = Runtime.getRuntime().exec(this.shell + this.cmd[0], this.env, null);\r
+ } else {\r
+ List list = new ArrayList(Arrays.asList(this.shell.split(" ")));\r
+ list.addAll(Arrays.asList(this.cmd));\r
+ String []cmds = Arrays.copyOf(list.toArray(), list.size(), String[].class);\r
+ p = Runtime.getRuntime().exec(cmds, this.env, null); \r
+ }\r
+ \r
+ this.exitCode = p.waitFor();\r
+ this.output = this.streamToString(p.getInputStream());\r
+ this.error = this.streamToString(p.getErrorStream());\r
+ p.destroy();\r
+ }\r
+\r
+ public String streamToString(InputStream stream) throws IOException {\r
+ StringBuilder sb = new StringBuilder();\r
+ BufferedReader br = null;\r
+ try {\r
+ br = new BufferedReader(new InputStreamReader(stream));\r
+ String line = null;\r
+ while ((line = br.readLine()) != null) {\r
+ sb.append(line + System.getProperty("line.separator"));\r
+ }\r
+ } finally {\r
+ br.close();\r
+ }\r
+ return sb.toString();\r
+ }\r
+ \r
+ public int getExitCode() {\r
+ return this.exitCode;\r
+ }\r
+ \r
+ public String getOutput() {\r
+ return this.output;\r
+ }\r
+ \r
+ public String getError() {\r
+ return this.error;\r
+ }\r
+ \r
+ public static void main(String[] args) {\r
+ try {\r
+ ProcessRunner pr = new ProcessRunner("dir", null);\r
+ pr.run();\r
+ System.out.println(pr.getOutput());\r
+ System.out.println(pr.getError());\r
+ System.out.println(pr.getExitCode());\r
+ \r
+ pr = new ProcessRunner(new String [] {"dir", "c:"}, null);\r
+ pr.run();\r
+ System.out.println(pr.getOutput());\r
+ System.out.println(pr.getError());\r
+ System.out.println(pr.getExitCode());\r
+ \r
+ } catch (InterruptedException | IOException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+}
\ No newline at end of file