From: <otm...@us...> - 2008-11-06 22:37:35
|
Revision: 5550 http://jython.svn.sourceforge.net/jython/?rev=5550&view=rev Author: otmarhumbel Date: 2008-11-06 22:37:32 +0000 (Thu, 06 Nov 2008) Log Message: ----------- do not use Runtime.exec(String) any more, it turned out to be dangerous on some platforms, use Runtime.exec(String[]) instead Modified Paths: -------------- trunk/installer/src/java/org/python/util/install/ChildProcess.java trunk/installer/src/java/org/python/util/install/StartScriptGenerator.java trunk/installer/test/java/org/python/util/install/ChildProcessTest.java trunk/installer/test/java/org/python/util/install/ChmodTest_Standalone.java Modified: trunk/installer/src/java/org/python/util/install/ChildProcess.java =================================================================== --- trunk/installer/src/java/org/python/util/install/ChildProcess.java 2008-11-06 16:53:15 UTC (rev 5549) +++ trunk/installer/src/java/org/python/util/install/ChildProcess.java 2008-11-06 22:37:32 UTC (rev 5550) @@ -93,14 +93,9 @@ private static final int NOT_SET_EXITVALUE = -9; /** - * The command to be executed - */ - private String _command = null; - - /** * The command as an array of strings */ - private String _commandArray[] = null; + private String _command[] = null; /** * The timeout (in milliseconds) @@ -143,82 +138,44 @@ public ChildProcess() {} /** - * Constructor taking the command as an argument - * - * @param command - * The command to be executed. - */ - public ChildProcess(String command) { - setCommand(command); - } - - /** * Constructor taking a command array as an argument * - * @param commandArray + * @param command * The command to be executed, every token as array element. */ - public ChildProcess(String commandArray[]) { - setCommandArray(commandArray); - } - - /** - * Constructor taking the command and the timeout as an argument - * - * @param command - * The command to be executed. - * @param timeout - * in milliseconds. Special value: <code>INFINITE_TIMEOUT</code> indicates no timeout - * at all. - */ - public ChildProcess(String command, long timeout) { + public ChildProcess(String command[]) { setCommand(command); - setTimeout(timeout); } /** * Constructor taking a command array and the timeout as an argument * - * @param commandArray + * @param command * The command to be executed, every token as array element. * @param timeout * in milliseconds. Special value: <code>INFINITE_TIMEOUT</code> indicates no timeout * at all. */ - public ChildProcess(String commandArray[], long timeout) { - setCommandArray(commandArray); + public ChildProcess(String command[], long timeout) { + setCommand(command); setTimeout(timeout); } /** - * Set the command to be executed + * Set the command array. This will override (but not overwrite) a previously set command */ - public void setCommand(String command) { + public void setCommand(String command[]) { _command = command; } /** - * Get the command to be executed + * Returns the command array */ - public String getCommand() { + public String[] getCommand() { return _command; } /** - * Set the command array. This will override (but not overwrite) a previously set command - */ - public void setCommandArray(String commandArray[]) { - _commandArray = commandArray; - } - - /** - * Returns the command array - */ - public String[] getCommandArray() { - return _commandArray; - } - - /** * Set the timeout (how long should the calling process wait for the child). * * @param timeout @@ -310,11 +267,7 @@ // determine start time _startTime = System.currentTimeMillis(); // start the process - if (getCommandArray() != null) { - _process = Runtime.getRuntime().exec(getCommandArray()); - } else { - _process = Runtime.getRuntime().exec(getCommand()); - } + _process = Runtime.getRuntime().exec(getCommand()); debugCommand(); // handle stdout and stderr StdoutMonitor stdoutMonitor = new StdoutMonitor(); @@ -386,11 +339,11 @@ */ private void debugCommand() { if (isDebug()) { - String[] commandArray = getCommandArray(); - if (commandArray != null) { + String[] command = getCommand(); + if (command != null) { System.out.print("[ChildProcess] command '"); - for (int i = 0; i < commandArray.length; i++) { - String commandPart = commandArray[i]; + for (int i = 0; i < command.length; i++) { + String commandPart = command[i]; if (i == 0) { System.out.print(commandPart); } else { @@ -398,9 +351,6 @@ } } System.out.println("' is now running..."); - } else { - System.out.println("[ChildProcess] command '" + getCommand() - + "' is now running..."); } } } Modified: trunk/installer/src/java/org/python/util/install/StartScriptGenerator.java =================================================================== --- trunk/installer/src/java/org/python/util/install/StartScriptGenerator.java 2008-11-06 16:53:15 UTC (rev 5549) +++ trunk/installer/src/java/org/python/util/install/StartScriptGenerator.java 2008-11-06 22:37:32 UTC (rev 5550) @@ -58,7 +58,7 @@ protected boolean hasUnixlikeShell() { int errorCode = 0; try { - String command = "sh -c env"; + String command[] = new String[] {"sh", "-c", "env"}; long timeout = 3000; ChildProcess childProcess = new ChildProcess(command, timeout); childProcess.setDebug(false); Modified: trunk/installer/test/java/org/python/util/install/ChildProcessTest.java =================================================================== --- trunk/installer/test/java/org/python/util/install/ChildProcessTest.java 2008-11-06 16:53:15 UTC (rev 5549) +++ trunk/installer/test/java/org/python/util/install/ChildProcessTest.java 2008-11-06 22:37:32 UTC (rev 5550) @@ -15,7 +15,7 @@ */ public void testDefault() { ChildProcess childProcess = new ChildProcess(); - String command = buildJavaCommand(CLASS_NAME); + String command[] = buildJavaCommand(CLASS_NAME); childProcess.setCommand(command); childProcess.setDebug(true); int exitValue = childProcess.run(); @@ -27,7 +27,7 @@ */ public void testTimeout() { ChildProcess childProcess = new ChildProcess(); - String command = buildJavaCommand(CLASS_NAME); + String command[] = buildJavaCommand(CLASS_NAME); childProcess.setCommand(command); childProcess.setDebug(true); childProcess.setTimeout(2000); // timeout to 2 seconds @@ -38,23 +38,11 @@ } /** - * test the child process with a command array - */ - public void testCommandArray() { - String command = buildJavaCommand(CLASS_NAME); - String commandArray[] = command.split(" "); - ChildProcess childProcess = new ChildProcess(commandArray); - childProcess.setDebug(true); - int exitValue = childProcess.run(); - assertEquals("Expected child process to end normally instead of " + exitValue, 0, exitValue); - } - - /** * test silent mode */ public void testSilent() throws IOException { ChildProcess childProcess = new ChildProcess(); - String command = "lwiklsl -siwK"; + String command[] = new String[] {"lwiklsl", "-siwK"}; childProcess.setCommand(command); childProcess.setDebug(false); childProcess.setSilent(true); @@ -81,12 +69,12 @@ // // private methods // - private String buildJavaCommand(String classAndArguments) { + private String[] buildJavaCommand(String classAndArguments) { String quote = ""; if (System.getProperty("os.name", "unknown").toLowerCase().indexOf("windows") >= 0) { quote = "\""; } String classpath = System.getProperty("java.class.path"); - return "java -classpath " + quote + classpath + quote + " " + classAndArguments; + return new String[] {"java", "-classpath", quote.concat(classpath).concat(quote), classAndArguments}; } } \ No newline at end of file Modified: trunk/installer/test/java/org/python/util/install/ChmodTest_Standalone.java =================================================================== --- trunk/installer/test/java/org/python/util/install/ChmodTest_Standalone.java 2008-11-06 16:53:15 UTC (rev 5549) +++ trunk/installer/test/java/org/python/util/install/ChmodTest_Standalone.java 2008-11-06 22:37:32 UTC (rev 5550) @@ -37,7 +37,8 @@ System.err.println(getPrefix() + "unable to create file " + path); System.exit(1); } else { - ChildProcess childProcess = new ChildProcess("chmod " + _mode + " " + path, 3000); + String command[] = new String[] {"chmod", _mode, path}; + ChildProcess childProcess = new ChildProcess(command, 3000); childProcess.setDebug(true); int exitValue = childProcess.run(); if (exitValue != 0) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |