You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/09/23 18:10:30 UTC

[commons-exec] branch master updated (b669983 -> 7905376)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git


    from b669983  Replace TravisCI badge with GitHub Actions
     new cb33a0a  SpotBugs: Use the default platform encoding explicitly to write scripts
     new a415d74  SpotBugs: Use the default platform encoding explicitly to write scripts
     new 7905376  PMD: Wow

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../java/org/apache/commons/exec/CommandLine.java  | 108 +++++++++------------
 .../org/apache/commons/exec/PumpStreamHandler.java |   3 +-
 .../commons/exec/launcher/VmsCommandLauncher.java  |  41 +++-----
 3 files changed, 63 insertions(+), 89 deletions(-)


[commons-exec] 01/03: SpotBugs: Use the default platform encoding explicitly to write scripts

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git

commit cb33a0a07f78b75d05db65c014a7c40b75f46a19
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Sep 23 14:07:00 2022 -0400

    SpotBugs: Use the default platform encoding explicitly to write scripts
---
 .../java/org/apache/commons/exec/CommandLine.java  | 108 +++++++++------------
 1 file changed, 46 insertions(+), 62 deletions(-)

diff --git a/src/main/java/org/apache/commons/exec/CommandLine.java b/src/main/java/org/apache/commons/exec/CommandLine.java
index 9df00b1..13704dd 100644
--- a/src/main/java/org/apache/commons/exec/CommandLine.java
+++ b/src/main/java/org/apache/commons/exec/CommandLine.java
@@ -28,8 +28,7 @@ import java.util.Vector;
 import org.apache.commons.exec.util.StringUtils;
 
 /**
- * CommandLine objects help handling command lines specifying processes to
- * execute. The class can be used to a command line by an application.
+ * CommandLine objects help handling command lines specifying processes to execute. The class can be used to a command line by an application.
  *
  */
 public class CommandLine {
@@ -98,18 +97,18 @@ public class CommandLine {
      * @param executable the executable
      */
     public CommandLine(final String executable) {
-        this.isFile=false;
-        this.executable=toCleanExecutable(executable);
+        this.isFile = false;
+        this.executable = toCleanExecutable(executable);
     }
 
     /**
      * Create a command line without any arguments.
      *
-     * @param  executable the executable file
+     * @param executable the executable file
      */
     public CommandLine(final File executable) {
-        this.isFile=true;
-        this.executable=toCleanExecutable(executable.getAbsolutePath());
+        this.isFile = true;
+        this.executable = toCleanExecutable(executable.getAbsolutePath());
     }
 
     /**
@@ -117,14 +116,12 @@ public class CommandLine {
      *
      * @param other the instance to copy
      */
-    public CommandLine(final CommandLine other)
-    {
+    public CommandLine(final CommandLine other) {
         this.executable = other.getExecutable();
         this.isFile = other.isFile();
         this.arguments.addAll(other.arguments);
 
-        if (other.getSubstitutionMap() != null)
-        {
+        if (other.getSubstitutionMap() != null) {
             this.substitutionMap = new HashMap<>(other.getSubstitutionMap());
         }
     }
@@ -178,9 +175,8 @@ public class CommandLine {
     }
 
     /**
-     * Add multiple arguments. Handles parsing of quotes and whitespace.
-     * Please note that the parsing can have undesired side-effects therefore
-     * it is recommended to build the command line incrementally.
+     * Add multiple arguments. Handles parsing of quotes and whitespace. Please note that the parsing can have undesired side-effects therefore it is
+     * recommended to build the command line incrementally.
      *
      * @param addArguments An string containing multiple arguments.
      * @return The command line itself
@@ -190,9 +186,8 @@ public class CommandLine {
     }
 
     /**
-     * Add multiple arguments. Handles parsing of quotes and whitespace.
-     * Please note that the parsing can have undesired side-effects therefore
-     * it is recommended to build the command line incrementally.
+     * Add multiple arguments. Handles parsing of quotes and whitespace. Please note that the parsing can have undesired side-effects therefore it is
+     * recommended to build the command line incrementally.
      *
      * @param addArguments An string containing multiple arguments.
      * @param handleQuoting Add the argument with/without handling quoting
@@ -218,30 +213,28 @@ public class CommandLine {
         return this.addArgument(argument, true);
     }
 
-   /**
-    * Add a single argument.
-    *
-    * @param argument The argument to add
-    * @param handleQuoting Add the argument with/without handling quoting
-    * @return The command line itself
-    */
-   public CommandLine addArgument(final String argument, final boolean handleQuoting) {
-
-       if (argument == null)
-       {
-           return this;
-       }
-
-       // check if we can really quote the argument - if not throw an
-       // IllegalArgumentException
-       if (handleQuoting)
-       {
-           StringUtils.quoteArgument(argument);
-       }
-
-       arguments.add(new Argument(argument, handleQuoting));
-       return this;
-   }
+    /**
+     * Add a single argument.
+     *
+     * @param argument The argument to add
+     * @param handleQuoting Add the argument with/without handling quoting
+     * @return The command line itself
+     */
+    public CommandLine addArgument(final String argument, final boolean handleQuoting) {
+
+        if (argument == null) {
+            return this;
+        }
+
+        // check if we can really quote the argument - if not throw an
+        // IllegalArgumentException
+        if (handleQuoting) {
+            StringUtils.quoteArgument(argument);
+        }
+
+        arguments.add(new Argument(argument, handleQuoting));
+        return this;
+    }
 
     /**
      * Returns the expanded and quoted command line arguments.
@@ -254,7 +247,7 @@ public class CommandLine {
         String expandedArgument;
         final String[] result = new String[arguments.size()];
 
-        for (int i=0; i<result.length; i++) {
+        for (int i = 0; i < result.length; i++) {
             currArgument = arguments.get(i);
             expandedArgument = expandArgument(currArgument.getValue());
             result[i] = currArgument.isHandleQuoting() ? StringUtils.quoteArgument(expandedArgument) : expandedArgument;
@@ -271,8 +264,7 @@ public class CommandLine {
     }
 
     /**
-     * Set the substitutionMap to expand variables in the
-     * command line.
+     * Set the substitutionMap to expand variables in the command line.
      *
      * @param substitutionMap the map
      */
@@ -288,14 +280,13 @@ public class CommandLine {
     public String[] toStrings() {
         final String[] result = new String[arguments.size() + 1];
         result[0] = this.getExecutable();
-        System.arraycopy(getArguments(), 0, result, 1, result.length-1);
+        System.arraycopy(getArguments(), 0, result, 1, result.length - 1);
         return result;
     }
 
     /**
-     * Stringify operator returns the command line as a string.
-     * Parameters are correctly quoted when containing a space or
-     * left untouched if the are already quoted.
+     * Stringify operator returns the command line as a string. Parameters are correctly quoted when containing a space or left untouched if the are already
+     * quoted.
      *
      * @return the command line as single string
      */
@@ -320,10 +311,8 @@ public class CommandLine {
     /**
      * Crack a command line.
      *
-     * @param toProcess
-     *            the command line to process
-     * @return the command line broken into strings. An empty or null toProcess
-     *         parameter results in a zero sized array
+     * @param toProcess the command line to process
+     * @return the command line broken into strings. An empty or null toProcess parameter results in a zero sized array
      */
     private static String[] translateCommandline(final String toProcess) {
         if (toProcess == null || toProcess.isEmpty()) {
@@ -384,8 +373,7 @@ public class CommandLine {
         }
 
         if (state == inQuote || state == inDoubleQuote) {
-            throw new IllegalArgumentException("Unbalanced quotes in "
-                    + toProcess);
+            throw new IllegalArgumentException("Unbalanced quotes in " + toProcess);
         }
 
         final String[] args = new String[list.size()];
@@ -393,8 +381,7 @@ public class CommandLine {
     }
 
     /**
-     * Cleans the executable string. The argument is trimmed and '/' and '\\' are
-     * replaced with the platform specific file separator char
+     * Cleans the executable string. The argument is trimmed and '/' and '\\' are replaced with the platform specific file separator char
      *
      * @param dirtyExecutable the executable
      * @return the platform-specific executable string
@@ -417,19 +404,16 @@ public class CommandLine {
         private final String value;
         private final boolean handleQuoting;
 
-        private Argument(final String value, final boolean handleQuoting)
-        {
+        private Argument(final String value, final boolean handleQuoting) {
             this.value = value.trim();
             this.handleQuoting = handleQuoting;
         }
 
-        private String getValue()
-        {
+        private String getValue() {
             return value;
         }
 
-        private boolean isHandleQuoting()
-        {
+        private boolean isHandleQuoting() {
             return handleQuoting;
         }
     }


[commons-exec] 03/03: PMD: Wow

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git

commit 7905376655b4fc693afcc7b1c3582c55a3fee6ac
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Sep 23 14:10:26 2022 -0400

    PMD: Wow
---
 src/main/java/org/apache/commons/exec/PumpStreamHandler.java | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/commons/exec/PumpStreamHandler.java b/src/main/java/org/apache/commons/exec/PumpStreamHandler.java
index a75fdc5..e0281c1 100644
--- a/src/main/java/org/apache/commons/exec/PumpStreamHandler.java
+++ b/src/main/java/org/apache/commons/exec/PumpStreamHandler.java
@@ -256,8 +256,7 @@ public class PumpStreamHandler implements ExecuteStreamHandler {
      * @return the stream pumper thread
      */
     protected Thread createPump(final InputStream is, final OutputStream os) {
-        final boolean closeWhenExhausted = os instanceof PipedOutputStream ? true : false;
-        return createPump(is, os, closeWhenExhausted);
+        return createPump(is, os, os instanceof PipedOutputStream);
     }
 
     /**


[commons-exec] 02/03: SpotBugs: Use the default platform encoding explicitly to write scripts

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git

commit a415d7432cc2c68b02842fe762ecf6ccb83ff2fc
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Sep 23 14:07:05 2022 -0400

    SpotBugs: Use the default platform encoding explicitly to write scripts
---
 .../commons/exec/launcher/VmsCommandLauncher.java  | 41 +++++++++-------------
 1 file changed, 16 insertions(+), 25 deletions(-)

diff --git a/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java b/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java
index 6c6d437..d35bdc0 100644
--- a/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java
+++ b/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java
@@ -19,9 +19,11 @@
 package org.apache.commons.exec.launcher;
 
 import java.io.File;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -41,29 +43,17 @@ public class VmsCommandLauncher extends Java13CommandLauncher {
      * Launches the given command in a new process.
      */
     @Override
-    public Process exec(final CommandLine cmd, final Map<String, String> env)
-            throws IOException {
-        final CommandLine vmsCmd = new CommandLine(
-                createCommandFile(cmd, env).getPath()
-        );
-
-        return super.exec(vmsCmd, env);
+    public Process exec(final CommandLine cmd, final Map<String, String> env) throws IOException {
+        return super.exec(new CommandLine(createCommandFile(cmd, env).getPath()), env);
     }
 
     /**
-     * Launches the given command in a new process, in the given working
-     * directory. Note that under Java 1.3.1, 1.4.0 and 1.4.1 on VMS this method
-     * only works if {@code workingDir} is null or the logical
-     * JAVA$FORK_SUPPORT_CHDIR needs to be set to TRUE.
+     * Launches the given command in a new process, in the given working directory. Note that under Java 1.3.1, 1.4.0 and 1.4.1 on VMS this method only works if
+     * {@code workingDir} is null or the logical JAVA$FORK_SUPPORT_CHDIR needs to be set to TRUE.
      */
     @Override
-    public Process exec(final CommandLine cmd, final Map<String, String> env,
-            final File workingDir) throws IOException {
-        final CommandLine vmsCmd = new CommandLine(
-                createCommandFile(cmd, env).getPath()
-        );
-
-        return super.exec(vmsCmd, env, workingDir);
+    public Process exec(final CommandLine cmd, final Map<String, String> env, final File workingDir) throws IOException {
+        return super.exec(new CommandLine(createCommandFile(cmd, env).getPath()), env, workingDir);
     }
 
     /** @see org.apache.commons.exec.launcher.CommandLauncher#isFailure(int) */
@@ -79,9 +69,10 @@ public class VmsCommandLauncher extends Java13CommandLauncher {
      */
     private File createCommandFile(final CommandLine cmd, final Map<String, String> env)
             throws IOException {
-        final File script = File.createTempFile("EXEC", ".TMP");
+        final Path path = Files.createTempFile("EXEC", ".TMP");
+        final File script = path.toFile();
         script.deleteOnExit();
-        try (PrintWriter out = new PrintWriter(new FileWriter(script.getAbsolutePath(),true))) {
+        try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(path, Charset.defaultCharset()))) {
             // add the environment as global symbols for the DCL script
             if (env != null) {
                 final Set<Entry<String, String>> entries = env.entrySet();
@@ -102,7 +93,7 @@ public class VmsCommandLauncher extends Java13CommandLauncher {
                             }
                             sb.append(c);
                         }
-                        value=sb.toString();
+                        value = sb.toString();
                     }
                     out.print(value);
                     out.println('\"');
@@ -113,12 +104,12 @@ public class VmsCommandLauncher extends Java13CommandLauncher {
             if (cmd.isFile()) {// We assume it is it a script file
                 out.print("$ @");
                 // This is a bit crude, but seems to work
-                final String[] parts = StringUtils.split(command,"/");
+                final String[] parts = StringUtils.split(command, "/");
                 out.print(parts[0]); // device
                 out.print(":[");
                 out.print(parts[1]); // top level directory
-                final int lastPart = parts.length-1;
-                for (int i=2; i< lastPart; i++) {
+                final int lastPart = parts.length - 1;
+                for (int i = 2; i < lastPart; i++) {
                     out.print(".");
                     out.print(parts[i]);
                 }