UrlLauncher:

- Use arrays for exec
- Randomize temp file name
- Require quotes around args containing spaces in routerconsole.browser property
- Add debug logging
- Add chromium-browser to the default list
- Parse and use full command line from Windows registry
- Replace %1 with url in registry line and routerconsole.browser property
ShellCommand:
- Switch to i2p logging
This commit is contained in:
zzz
2018-12-12 20:12:07 +00:00
parent 51bf23a34c
commit 9738db7254
4 changed files with 197 additions and 58 deletions

View File

@@ -18,6 +18,8 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import net.i2p.I2PAppContext;
/**
* Passes a command to the OS shell for execution and manages the input and
* output.
@@ -28,7 +30,6 @@ import java.util.Arrays;
*/
public class ShellCommand {
private static final boolean DEBUG = false;
private static final boolean CONSUME_OUTPUT = true;
private static final boolean NO_CONSUME_OUTPUT = false;
@@ -358,7 +359,8 @@ public class ShellCommand {
private boolean executeSAWT(Object shellCommand, int seconds) {
String name = null;
long begin = 0;
if (DEBUG) {
Log log = I2PAppContext.getGlobalContext().logManager().getLog(ShellCommand.class);
if (log.shouldDebug()) {
if (shellCommand instanceof String) {
name = (String) shellCommand;
} else if (shellCommand instanceof String[]) {
@@ -374,16 +376,16 @@ public class ShellCommand {
if (seconds > 0) {
commandThread.join(seconds * 1000);
if (commandThread.isAlive()) {
if (DEBUG)
System.out.println("ShellCommand gave up waiting for \"" + name + "\" after " + seconds + " seconds");
if (log.shouldDebug())
log.debug("ShellCommand gave up waiting for \"" + name + "\" after " + seconds + " seconds");
return true;
}
}
} catch (InterruptedException e) {
// Wake up, time to die.
}
if (DEBUG)
System.out.println("ShellCommand returning " + result.commandSuccessful + " for \"" + name + "\" after " + (System.currentTimeMillis() - begin) + " ms");
if (log.shouldDebug())
log.debug("ShellCommand returning " + result.commandSuccessful + " for \"" + name + "\" after " + (System.currentTimeMillis() - begin) + " ms");
return result.commandSuccessful;
}
@@ -426,18 +428,19 @@ public class ShellCommand {
private boolean execute(Object shellCommand, boolean consumeOutput, boolean waitForExitStatus) {
Process process;
String name = null; // for debugging only
Log log = I2PAppContext.getGlobalContext().logManager().getLog(ShellCommand.class);
try {
// easy way so we don't have to copy this whole method
if (shellCommand instanceof String) {
name = (String) shellCommand;
if (DEBUG)
System.out.println("ShellCommand exec \"" + name + "\" consume? " + consumeOutput + " wait? " + waitForExitStatus);
if (log.shouldDebug())
log.debug("ShellCommand exec \"" + name + "\" consume? " + consumeOutput + " wait? " + waitForExitStatus);
process = Runtime.getRuntime().exec(name);
} else if (shellCommand instanceof String[]) {
String[] arr = (String[]) shellCommand;
if (DEBUG) {
if (log.shouldDebug()) {
name = Arrays.toString(arr);
System.out.println("ShellCommand exec \"" + name + "\" consume? " + consumeOutput + " wait? " + waitForExitStatus);
log.debug("ShellCommand exec \"" + name + "\" consume? " + consumeOutput + " wait? " + waitForExitStatus);
}
process = Runtime.getRuntime().exec(arr);
} else {
@@ -461,14 +464,13 @@ public class ShellCommand {
processStdoutReader.start();
}
if (waitForExitStatus) {
if (DEBUG)
System.out.println("ShellCommand waiting for \"" + name + '\"');
if (log.shouldDebug())
log.debug("ShellCommand waiting for \"" + name + '\"');
try {
process.waitFor();
} catch (InterruptedException e) {
if (DEBUG) {
System.out.println("ShellCommand exception waiting for \"" + name + '\"');
e.printStackTrace();
if (log.shouldWarn()) {
log.warn("ShellCommand exception waiting for \"" + name + '"', e);
}
if (!consumeOutput)
killStreams();
@@ -478,16 +480,15 @@ public class ShellCommand {
if (!consumeOutput)
killStreams();
if (DEBUG)
System.out.println("ShellCommand exit value is " + process.exitValue() + " for \"" + name + '\"');
if (log.shouldDebug())
log.debug("ShellCommand exit value is " + process.exitValue() + " for \"" + name + '\"');
if (process.exitValue() > 0)
return false;
}
} catch (IOException e) {
// probably IOException, file not found from exec()
if (DEBUG) {
System.out.println("ShellCommand execute exception for \"" + name + '\"');
e.printStackTrace();
if (log.shouldWarn()) {
log.warn("ShellCommand execute exception for \"" + name + '"', e);
}
return false;
}