@Override protected Process process(Object input) throws Exception { Validate.executionNotNull(command, "Command must not be null"); Validate.executionNotNull(command.getProgramName(), "Command program name must not be null"); ProcessBuilder builder = new ProcessBuilder(command.getFullCommand()); builder.directory(workingDirectory); builder.environment().putAll(environment); builder.redirectErrorStream(redirectErrorStream); return builder.start(); } }
/** * Adds a map of key, value environment variables to the default process environment * * @param envVariables * environment variables. Value might be null. * * @throws IllegalArgumentException */ public CommandTool addEnvironment(Map<? extends CharSequence, ? extends CharSequence> envVariables) throws IllegalArgumentException { Validate.notNull(envVariables, "Environment variables must not be null"); for (Map.Entry<? extends CharSequence, ? extends CharSequence> entry : envVariables.entrySet()) { Validate.notNull(entry.getKey(), "Environment variable name must not be null nor empty"); CharSequence value = entry.getValue(); environment.put(entry.getKey().toString(), value != null ? value.toString() : null); } return this; }
/** * Sets executable to be executed. This can either a an absolute path or executed on path of underlying file system * * @param programName * program name * * @throws IllegalArgumentException * If program name is null or empty */ public CommandTool programName(CharSequence programName) throws IllegalArgumentException { Validate.notNullOrEmpty(programName, "Program name must not be empty nor null"); this.commandBuilder = new CommandBuilder(programName); return this; }
@Override protected ProcessResult process(Object input) throws Exception { Validate.executionNotNull(commandBuilder, "Command must not be null");
/** * Adds a sequence of key, value environment variables to the default process environment * * @param envVariables * environment variables. Value might be null. * * @throws IllegalArgumentException * If values do not form complete pairs or if key is null */ public CommandTool addEnvironment(CharSequence... envVariables) throws IllegalArgumentException { if (envVariables.length % 2 == 1) { throw new IllegalArgumentException("Environment variables must be a sequence of key, value pairs."); } for (int i = 0; i < (envVariables.length / 2); i += 2) { CharSequence key = envVariables[i]; CharSequence value = envVariables[i + 1]; Validate.notNull(key, "Environment variable name must not be null nor empty"); environment.put(key.toString(), value != null ? value.toString() : null); } return this; }