I wanted to use the ProcessShellFactory, but I needed to manipulate the
current working directory and the environment -- which could be done
through the ProcessBuilder that gets constructed but was hidden in the
ProcessShellFactory interface. So, I added a couple of new methods to the
ProcessShellFactory API:
public void setEnv(Map<String, String> env){
this.env_settings = env;
}
public void setWorkingDir(File dir){
this.working_dir = dir;
}
And changed the implementation of ProcessShell.start to reflect these
changes:
public void start(Map<String,String> env) throws IOException {
String[] cmds = new String[command.length];
for (int i = 0; i < cmds.length; i++) {
if ("$USER".equals(command[i])) {
cmds[i] = env.get("USER");
} else {
cmds[i] = command[i];
}
}
ProcessBuilder builder = new ProcessBuilder(cmds);
if(working_dir != null){
builder.directory(working_dir);
}
if(env_settings != null){
builder.environment().putAll(env_settings);
}
if (env != null) {
try {
builder.environment().putAll(env);
} catch (Exception e) {
LOG.info("Could not set environment for
command", e);
}
}
LOG.info("Starting shell with command: '{}' and env: {}",
builder.command(), builder.environment());
try{
process = builder.start();
out = new
TtyFilterInputStream(process.getInputStream());
err = new
TtyFilterInputStream(process.getErrorStream());
in = new
TtyFilterOutputStream(process.getOutputStream(), err);
}
catch(Exception e){
System.out.println("Exception: " + e.getMessage());
}
}
It's a small change but it gave me just what I needed to do a simple
connection between an SshServer and Scala shell applications. So I though
I'd offer it as a suggested change Š
Jim Donahue
Adobe Systems