Example usage for org.apache.commons.exec.environment EnvironmentUtils getProcEnvironment

List of usage examples for org.apache.commons.exec.environment EnvironmentUtils getProcEnvironment

Introduction

In this page you can find the example usage for org.apache.commons.exec.environment EnvironmentUtils getProcEnvironment.

Prototype

public static Map<String, String> getProcEnvironment() throws IOException 

Source Link

Document

Find the list of environment variables for this process.

Usage

From source file:com.intuit.cto.selfservice.service.ManagedProcessBuilder.java

@SuppressWarnings("unchecked")
protected Map<String, String> initialEnvironment() throws ManagedProcessException {
    try {//from   w w  w  .java2  s . c om
        return EnvironmentUtils.getProcEnvironment();
    } catch (IOException e) {
        throw new ManagedProcessException("Retrieving default environment variables failed", e);
    }
}

From source file:io.selendroid.io.ShellCommand.java

public static void execAsync(String display, CommandLine commandline) throws ShellCommandException {
    log.info("executing async command: " + commandline);
    DefaultExecutor exec = new DefaultExecutor();

    ExecuteResultHandler handler = new DefaultExecuteResultHandler();
    PumpStreamHandler streamHandler = new PumpStreamHandler(new PritingLogOutputStream());
    exec.setStreamHandler(streamHandler);
    try {/*from ww  w .java  2s.c om*/
        if (display == null || display.isEmpty()) {
            exec.execute(commandline, handler);
        } else {
            Map env = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:" + display);

            exec.execute(commandline, env, handler);
        }
    } catch (Exception e) {
        throw new ShellCommandException("An error occured while executing shell command: " + commandline, e);
    }
}

From source file:com.blackducksoftware.tools.scmconnector.core.CommandLineExecutor.java

@Override
public int executeCommand(Logger log, CommandLine command, File targetDir, String promptResponse)
        throws Exception {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);/*from w  w w.j a v  a 2s. c o m*/

    PumpStreamHandler psh = new PumpStreamHandler(new ExecLogHangler(log, Level.INFO));
    executor.setStreamHandler(psh);

    providePromptResponse(psh, promptResponse);

    executor.setWorkingDirectory(targetDir);

    // This log msg would reveal password
    // log.info("Command: " + command.toString() + " executed in: "
    // + targetDir.toString());

    int exitStatus = 1;
    try {
        exitStatus = executor.execute(command, EnvironmentUtils.getProcEnvironment());
    } catch (Exception e) {
        log.error("Failure executing Command: " + e.getMessage());
    }
    return exitStatus;
}

From source file:io.selendroid.standalone.io.ShellCommand.java

public static void execAsync(String display, CommandLine commandline) throws ShellCommandException {
    log.info("executing async command: " + commandline);
    DefaultExecutor exec = new DefaultExecutor();

    ExecuteResultHandler handler = new DefaultExecuteResultHandler();
    PumpStreamHandler streamHandler = new PumpStreamHandler(new PrintingLogOutputStream());
    exec.setStreamHandler(streamHandler);
    try {/*from   ww w  . j  ava2  s .  c o  m*/
        if (display == null || display.isEmpty()) {
            exec.execute(commandline, handler);
        } else {
            Map env = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:" + display);

            exec.execute(commandline, env, handler);
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error executing command: " + commandline, e);
        throw new ShellCommandException("Error executing shell command: " + commandline, e);
    }
}

From source file:com.zxy.commons.exec.CmdExecutor.java

/**
 * /*from ww w  .j  a  va 2 s  .com*/
 * 
 * @param workHome workHome
 * @param command command
 * @return ???
 * @throws InterruptedException InterruptedException
 * @throws IOException IOException
 */
public static ExecutorResult exec(String workHome, String command) throws InterruptedException, IOException {
    CommandLine cmdLine = CommandLine.parse(PRE_CMD + command);
    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(new File(workHome));

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
    executor.setStreamHandler(streamHandler);

    int code = executor.execute(cmdLine, EnvironmentUtils.getProcEnvironment());
    String successMsg = outputStream.toString(ENCODING);
    String errorMsg = errorStream.toString(ENCODING);
    return new ExecutorResult(code, successMsg, errorMsg);
}

From source file:ch.vorburger.exec.ManagedProcessBuilder.java

protected Map<String, String> initialEnvironment() throws ManagedProcessException {
    try {/*from www.j  a v  a 2s .  co m*/
        return EnvironmentUtils.getProcEnvironment();
    } catch (IOException e) {
        throw new ManagedProcessException("Retrieving default environment variables failed", e);
    }
}

From source file:com.github.peterjanes.node.NpmTestMojo.java

/**
 *
 * @throws MojoExecutionException if anything unexpected happens.
 *///  w  w  w .  ja  va 2  s.  co m
public void execute() throws MojoExecutionException {
    if (!workingDirectory.exists()) {
        workingDirectory.mkdirs();
    }

    CommandLine commandLine = new CommandLine(executable);
    Executor exec = new DefaultExecutor();
    exec.setWorkingDirectory(workingDirectory);

    Map env = new HashMap();
    try {
        Map systemEnvVars = EnvironmentUtils.getProcEnvironment();
        env.putAll(systemEnvVars);
    } catch (IOException e) {
        getLog().error("Could not assign default system enviroment variables.", e);
    }
    env.put("NODE_PATH", new File(workingDirectory, "node_modules").getAbsolutePath());
    env.put("XUNIT_FILE", outputFile.getAbsolutePath());

    List commandArguments = new ArrayList();
    commandArguments.add("test");

    String[] args = new String[commandArguments.size()];
    for (int i = 0; i < commandArguments.size(); i++) {
        args[i] = (String) commandArguments.get(i);
    }

    commandLine.addArguments(args, false);

    OutputStream stdout = System.out;
    OutputStream stderr = System.err;

    try {
        outputFile.getParentFile().mkdirs();
        getLog().debug("Executing command line " + commandLine + " in directory "
                + workingDirectory.getAbsolutePath());
        exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in));

        int resultCode = exec.execute(commandLine, env);

        if (0 != resultCode) {
            throw new MojoExecutionException(
                    "Result of " + commandLine + " execution is: '" + resultCode + "'.");
        }
    } catch (ExecuteException e) {
        throw new MojoExecutionException(EXECUTION_FAILED, e);

    } catch (IOException e) {
        throw new MojoExecutionException(EXECUTION_FAILED, e);
    }
}

From source file:io.seqware.pipeline.whitestar.WhiteStarTest.java

protected static void createAndRunWorkflow(Path settingsFile, boolean metadata) throws Exception, IOException {
    // create a helloworld
    Path tempDir = Files.createTempDirectory("tempTestingDirectory");
    PluginRunner it = new PluginRunner();
    String SEQWARE_VERSION = it.getClass().getPackage().getImplementationVersion();
    Assert.assertTrue("unable to detect seqware version", SEQWARE_VERSION != null);
    Log.info("SeqWare version detected as: " + SEQWARE_VERSION);
    String archetype = "java-workflow";
    String workflow = "seqware-archetype-" + archetype;
    String workflowName = workflow.replace("-", "");
    // generate and install archetypes to local maven repo
    String command = "mvn archetype:generate -DarchetypeCatalog=local -Dpackage=com.seqware.github -DgroupId=com.github.seqware -DarchetypeArtifactId="
            + workflow + " -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=com.github.seqware -DartifactId="
            + workflow + " -Dworkflow-name=" + workflowName + " -B -Dgoals=install";
    String genOutput = ITUtility.runArbitraryCommand(command, 0, tempDir.toFile());
    Log.info(genOutput);//from  w  w w. j  av  a  2  s  . c  o m
    // install the workflows to the database and record their information
    File workflowDir = new File(tempDir.toFile(), workflow);
    File targetDir = new File(workflowDir, "target");
    final String workflow_name = "Workflow_Bundle_" + workflowName + "_1.0-SNAPSHOT_SeqWare_" + SEQWARE_VERSION;
    File bundleDir = new File(targetDir, workflow_name);

    Map environment = EnvironmentUtils.getProcEnvironment();
    environment.put("SEQWARE_SETTINGS", settingsFile.toAbsolutePath().toString());

    // save system environment variables
    Map<String, String> env = System.getenv();
    try {
        // override for launching
        Utility.set(environment);
        List<String> cmd = new ArrayList<>();
        cmd.add("bundle");
        cmd.add("launch");
        cmd.add("--dir");
        cmd.add(bundleDir.getAbsolutePath());
        if (!metadata) {
            cmd.add("--no-metadata");
        }
        Main.main(cmd.toArray(new String[cmd.size()]));
    } finally {
        Utility.set(env);
    }

}

From source file:hu.bme.mit.trainbenchmark.benchmark.fourstore.driver.UnixUtils.java

public static void exec(final String command, final Map<String, String> environmentVariables,
        final OutputStream outputStream) throws IOException, ExecuteException {
    final Map<?, ?> executionEnvironment = EnvironmentUtils.getProcEnvironment();
    for (final Entry<String, String> environmentVariable : environmentVariables.entrySet()) {
        final String keyAndValue = environmentVariable.getKey() + "=" + environmentVariable.getValue();
        EnvironmentUtils.addVariableToEnvironment(executionEnvironment, keyAndValue);
    }/*from  ww w. java2s.  c  o m*/

    final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);

    final CommandLine commandLine = new CommandLine("/bin/bash");
    commandLine.addArguments(new String[] { "-c", command }, false);

    final DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(streamHandler);
    executor.execute(commandLine, executionEnvironment);
}

From source file:com.zxy.commons.exec.CmdExecutor.java

/**
 * //www.j  ava 2  s  .  c om
 * 
 * @param workHome workHome
 * @param command command
 * @throws InterruptedException InterruptedException
 * @throws IOException IOException
 */
public static void execAsyc(String workHome, String command) throws InterruptedException, IOException {
    CommandLine cmdLine = CommandLine.parse(PRE_CMD + command);
    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(new File(workHome));

    executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
        @Override
        protected void processLine(String line, int level) {
            LOGGER.debug(line);
        }
    }, new LogOutputStream() {
        @Override
        protected void processLine(String line, int level) {
            LOGGER.debug(line);
        }
    }));

    ExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    executor.execute(cmdLine, EnvironmentUtils.getProcEnvironment(), resultHandler);
    // resultHandler.waitFor();
}