jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject [jmeter] branch master updated: Restart does not work when parameters contain spaces
Date Sun, 29 Aug 2021 15:24:24 GMT
This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new b9cbab8  Restart does not work when parameters contain spaces
b9cbab8 is described below

commit b9cbab843b13f1d4c61e93ce4b87bff3ef995b66
Author: Felix Schumacher <felix.schumacher@internetallee.de>
AuthorDate: Sun Aug 29 17:19:00 2021 +0200

    Restart does not work when parameters contain spaces
    
    Try to re-combine the parameters to regard spaces in file names
    
    Java command line has no knowledge of the 'real' parameters and
    we have to do a bit of guessing to re-assemble the parameters with
    spaces and the drop the spaces, that should split the parameters.
    
    So we guess, that each parameter starts with a dash (-) and
    everything else are values, that should be stitched together.
    
    Bugzilla Id: 65522
---
 .../java/org/apache/jmeter/gui/action/Restart.java | 70 +++++++++++++++++-----
 xdocs/changes.xml                                  |  1 +
 2 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/src/core/src/main/java/org/apache/jmeter/gui/action/Restart.java b/src/core/src/main/java/org/apache/jmeter/gui/action/Restart.java
index d99bf72..2b49b69 100644
--- a/src/core/src/main/java/org/apache/jmeter/gui/action/Restart.java
+++ b/src/core/src/main/java/org/apache/jmeter/gui/action/Restart.java
@@ -22,6 +22,7 @@ import java.awt.event.KeyEvent;
 import java.io.File;
 import java.io.IOException;
 import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -100,6 +101,7 @@ public class Restart extends AbstractActionWithNoRunningTest implements
MenuCrea
      */
     public static void restartApplication(Runnable runBeforeRestart) {
         String javaCommand = System.getProperty(SUN_JAVA_COMMAND);
+        List<String> processArgs = new ArrayList<>();
         if(StringUtils.isEmpty(javaCommand)) {
             JOptionPane.showMessageDialog(GuiPackage.getInstance().getMainFrame(),
                     JMeterUtils.getResString("restart_error")+":\n This command is only supported
on Open JDK or Oracle JDK" ,  //$NON-NLS-1$  //$NON-NLS-2$
@@ -107,46 +109,43 @@ public class Restart extends AbstractActionWithNoRunningTest implements
MenuCrea
             return;
         }
         // java binary
-        String java = System.getProperty("java.home") + "/bin/java";
+        processArgs.add(System.getProperty("java.home") + "/bin/java");
         // vm arguments
         List<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
-        StringBuilder vmArgsOneLine = new StringBuilder();
         for (String arg : vmArguments) {
             // if it's the agent argument : we ignore it otherwise the
             // address of the old application and the new one will be in
             // conflict
             if (!arg.contains("-agentlib")) {
-                vmArgsOneLine.append(arg);
-                vmArgsOneLine.append(" ");
+                processArgs.add(arg);
             }
         }
-        // init the command to execute, add the vm args
-        final StringBuilder cmd = new StringBuilder(java + " " + vmArgsOneLine);
 
         // program main and program arguments
         String[] mainCommand = javaCommand.split(" ");
         // program main is a jar
         if (mainCommand[0].endsWith(".jar")) {
             // if it's a jar, add -jar mainJar
-            cmd.append("-jar " + new File(mainCommand[0]).getPath());
+            processArgs.add("-jar");
+            processArgs.add(new File(mainCommand[0]).getPath());
         } else {
             // else it's a .class, add the classpath and mainClass
-            cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
+            processArgs.add("-cp");
+            processArgs.add(System.getProperty("java.class.path"));
+            processArgs.add(mainCommand[0]);
         }
         // finally add program arguments
-        for (int i = 1; i < mainCommand.length; i++) {
-            cmd.append(" ");
-            cmd.append(mainCommand[i]);
-        }
+        processRemainingArgs(processArgs, mainCommand);
+        log.debug("Restart with {} from [{}]", processArgs, javaCommand);
         // execute the command in a shutdown hook, to be sure that all the
         // resources have been disposed before restarting the application
         Runtime.getRuntime().addShutdownHook(new Thread() {
             @Override
             public void run() {
                 try {
-                    Runtime.getRuntime().exec(cmd.toString());
+                    new ProcessBuilder(processArgs).start();
                 } catch (IOException e) {
-                    log.error("Error calling restart command {}", cmd.toString(), e);
+                    log.error("Error calling restart command {}", processArgs, e);
                 }
             }
         });
@@ -160,6 +159,49 @@ public class Restart extends AbstractActionWithNoRunningTest implements
MenuCrea
     }
 
     /**
+     * Try to re-combine the parameters to regard spaces in file names
+     * <p>
+     * Java command line has no knowledge of the 'real' parameters and
+     * we have to do a bit of guessing to re-assemble the parameters with
+     * spaces and the drop the spaces, that should split the parameters.
+     * <p>
+     * So we guess, that each parameter starts with a dash ({@code -}) and
+     * everything else are values, that should be stitched together.
+     *
+     * @param processArgs arguments to be given to ProcessBuilder
+     * @param mainCommand original command line split at spaces
+     */
+    private static void processRemainingArgs(List<String> processArgs, String[] mainCommand)
{
+        boolean paramValue = false;
+        StringBuilder partialParamValue = new StringBuilder();
+        for (int i = 1; i < mainCommand.length; i++) {
+            String currentPart = mainCommand[i];
+            if (paramValue) {
+                if (currentPart.startsWith("-")) {
+                    paramValue = false;
+                    processArgs.add(partialParamValue.toString());
+                    partialParamValue.setLength(0);
+                    processArgs.add(currentPart);
+                } else {
+                    partialParamValue.append(" ");
+                    partialParamValue.append(currentPart);
+                }
+            } else {
+                if (currentPart.startsWith("-")) {
+                    processArgs.add(currentPart);
+                } else {
+                    paramValue = true;
+                    partialParamValue.setLength(0);
+                    partialParamValue.append(currentPart);
+                }
+            }
+        }
+        if (paramValue) {
+            processArgs.add(partialParamValue.toString());
+        }
+    }
+
+    /**
      * @see Command#getActionNames()
      */
     @Override
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index 7b0c7a1..415b972 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -241,6 +241,7 @@ Summary
   <li>Silence warnings of missing font Arial on startup under linux</li>
   <li><bug>65300</bug>IllegalAccessError when opening file dialog with
Java 16</li>
   <li><bug>65336</bug>Blank labels when different elements had the same
name</li>
+  <li><bug>65522</bug>Restart doesn't work, when parameters cotain spaces</li>
 </ul>
 
  <!--  =================== Thanks =================== -->

Mime
View raw message