From 6595d1eb30fec6428b9de625dacc521d32fbfb4e Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Wed, 10 May 2017 09:56:41 +0200 Subject: [PATCH] Completed demo. --- .../util/cmdexec/demo/CmdExecDemo.java | 85 +++++++++++++++---- .../devinsy/util/cmdexec/CmdExecSandbox.java | 2 +- 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/src/fr/devinsy/util/cmdexec/demo/CmdExecDemo.java b/src/fr/devinsy/util/cmdexec/demo/CmdExecDemo.java index 1415820..814c4fd 100644 --- a/src/fr/devinsy/util/cmdexec/demo/CmdExecDemo.java +++ b/src/fr/devinsy/util/cmdexec/demo/CmdExecDemo.java @@ -18,7 +18,18 @@ */ package fr.devinsy.util.cmdexec.demo; -import fr.devinsy.util.strings.StringList; +import java.io.File; +import java.io.IOException; + +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Level; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.util.cmdexec.CmdExec; +import fr.devinsy.util.cmdexec.CmdExecException; +import fr.devinsy.util.cmdexec.CmdExecUtils; +import fr.devinsy.util.cmdexec.StreamGobbler; /** * The Class Demo. @@ -30,27 +41,71 @@ public class CmdExecDemo * * @param args * the arguments + * @throws CmdExecException + * @throws IOException */ - public static void main(final String[] args) + public static void main(final String[] args) throws CmdExecException, IOException { - // #1 + // + BasicConfigurator.configure(); + org.apache.log4j.Logger.getRootLogger().setLevel(Level.ERROR); + Logger logger = LoggerFactory.getLogger(CmdExecDemo.class); + + // + File tmpFile = null; + try { - StringList strings = new StringList(); + tmpFile = File.createTempFile("cmdexec", ".tmp"); - strings.appendln("========== DEMO #1;"); - strings.appendln(); + // #0 + { + System.out.println("========== DEMO #0;"); - System.out.println(strings.toString()); + System.out.println("Launch…"); + + String result = CmdExecUtils.run("/bin/bash", "-c", "echo '10\n5\n7\n1' > " + tmpFile.getAbsolutePath()); + + System.out.println("result=[" + result + "]"); + } + + // #1 + { + System.out.println("========== DEMO #1;"); + + System.out.println("Launch…"); + + String[] command = { "/bin/cat", tmpFile.getAbsolutePath() }; + + CmdExec cmdexec = new CmdExec(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER); + System.out.println("command=[" + cmdexec.toString() + "]"); + System.out.println("exitVal=[" + cmdexec.getExitValue() + "]"); + System.out.println("out=[" + cmdexec.getOutStream() + "]"); + System.out.println("err=[" + cmdexec.getErrStream() + "]"); + } + + // #2 + { + System.out.println("========== DEMO #2;"); + + System.out.println("Launch…"); + + // String command = "/bin/sort -r /etc/passwd"; + String[] command = { "/usr/bin/sort", "-n", "-r", tmpFile.getAbsolutePath() }; + + CmdExec cmdexec = new CmdExec(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER); + System.out.println("command=[" + cmdexec.toString() + "]"); + System.out.println("exitVal=[" + cmdexec.getExitValue() + "]"); + System.out.println("out=[" + cmdexec.getOutStream() + "]"); + System.out.println("err=[" + cmdexec.getErrStream() + "]"); + } } - - // #2 + finally { - StringList strings = new StringList(); - - strings.appendln("========== DEMO #2;"); - strings.appendln(); - - System.out.println(strings.toString()); + if (tmpFile != null) + { + boolean status = tmpFile.delete(); + logger.debug("file delete status={}", status); + } } } } diff --git a/test/fr/devinsy/util/cmdexec/CmdExecSandbox.java b/test/fr/devinsy/util/cmdexec/CmdExecSandbox.java index fd431f9..8ac7923 100644 --- a/test/fr/devinsy/util/cmdexec/CmdExecSandbox.java +++ b/test/fr/devinsy/util/cmdexec/CmdExecSandbox.java @@ -100,7 +100,7 @@ class CmdExecSandbox { try { - System.out.println("Launch…s"); + System.out.println("Launch…"); // String command = "/bin/sort -r /etc/passwd"; String[] command = { "/usr/bin/sort", "-r", "/etc/passwd" };