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:org.opennms.web.rest.v1.MeasurementsRestServiceITCase.java

protected static String findRrdtool() {
    try {//from   w ww.java 2  s .c o  m
        @SuppressWarnings("unchecked")
        final Map<String, String> env = new HashMap<String, String>(EnvironmentUtils.getProcEnvironment());
        if (env.get("PATH") != null) {
            final String pathVar = env.get("PATH");
            if (!OS.isFamilyWindows()) {
                final List<String> paths = new ArrayList<>(Arrays.asList(pathVar.split(":")));
                paths.add("/usr/local/bin");
                paths.add("/usr/local/sbin");
                for (final String path : paths) {
                    final String tryme = path + File.separator + "rrdtool";
                    if (new File(tryme).exists()) {
                        return tryme;
                    }
                }
            }
        }
        return "rrdtool";
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.opennms.web.rest.v1.MeasurementsRestServiceITCase.java

protected static void assumeRrdtoolExists(final String libraryName) {
    final String libraryPath = System.getProperty("java.library.path", "");
    if (!libraryPath.contains(":/usr/local/lib")) {
        System.setProperty("java.library.path", libraryPath + ":/usr/local/lib");
    }//w w w . ja  v  a2  s . c  om
    boolean rrdtoolExists = false;
    try {
        final CommandLauncher cl = CommandLauncherFactory.createVMLauncher();
        final Process p = cl.exec(new CommandLine(findRrdtool()), EnvironmentUtils.getProcEnvironment());
        final int returnCode = p.waitFor();
        LOG.debug("Loading library from java.library.path={}", System.getProperty("java.library.path"));
        System.loadLibrary(libraryName);
        rrdtoolExists = returnCode == 0;
    } catch (final Exception e) {
        LOG.warn("Failed to run 'rrdtool' or libjrrd(2)? is missing.", e);
    }
    Assume.assumeTrue(rrdtoolExists);
}

From source file:org.wisdom.maven.node.NPM.java

private static Map<String, String> extendEnvironmentWithNodeInPath(NodeManager node) throws IOException {
    Map<String, String> env = EnvironmentUtils.getProcEnvironment();
    if (env.containsKey("PATH")) {
        String path = env.get("PATH");
        env.put("PATH", node.getNodeExecutable().getParent() + File.pathSeparator + path);
    } else {/*ww w.  j  a  v  a2  s .c  o  m*/
        env.put("PATH", node.getNodeExecutable().getParent());
    }
    return env;
}

From source file:sce.ProcessExecutor.java

public String executeProcess(String[] processParameters) throws JobExecutionException {
    try {//from   w w  w.jav a  2  s .  com
        //Command to be executed
        CommandLine command = new CommandLine(processParameters[0]);

        String[] params = new String[processParameters.length - 1];
        for (int i = 0; i < processParameters.length - 1; i++) {
            params[i] = processParameters[i + 1];
        }

        //Adding its arguments
        command.addArguments(params);

        //set timeout in seconds
        ExecuteWatchdog watchDog = new ExecuteWatchdog(
                this.timeout == 0 ? ExecuteWatchdog.INFINITE_TIMEOUT : this.timeout * 1000);
        this.watchdog = watchDog;

        //Result Handler for executing the process in a Asynch way
        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        //MyResultHandler resultHandler = new MyResultHandler();

        //Using Std out for the output/error stream
        //ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        //This is used to end the process when the JVM exits
        ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();

        //Our main command executor
        DefaultExecutor executor = new DefaultExecutor();

        //Setting the properties
        executor.setStreamHandler(new PumpStreamHandler(null, null));
        executor.setWatchdog(watchDog);
        //executor.setExitValue(1); // this has to be set if the java code contains System.exit(1) to avoid a FAILED status

        //Setting the working directory
        //Use of recursion along with the ls makes this a long running process
        //executor.setWorkingDirectory(new File("/home"));
        executor.setProcessDestroyer(processDestroyer);

        //if set, use the java environment variables when running the command
        if (!this.environment.equals("")) {
            Map<String, String> procEnv = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(procEnv, this.environment);
            //Executing the command
            executor.execute(command, procEnv, resultHandler);
        } else {
            //Executing the command
            executor.execute(command, resultHandler);
        }

        //The below section depends on your need
        //Anything after this will be executed only when the command completes the execution
        resultHandler.waitFor();

        /*int exitValue = resultHandler.getExitValue();
         System.out.println(exitValue);
         if (executor.isFailure(exitValue)) {
         System.out.println("Execution failed");
         } else {
         System.out.println("Execution Successful");
         }
         System.out.println(outputStream.toString());*/
        //return outputStream.toString();
        if (watchdog.killedProcess()) {
            throw new JobExecutionException("Job Interrupted", new InterruptedException());
        }
        if (executor.isFailure(resultHandler.getExitValue())) {
            ExecuteException ex = resultHandler.getException();
            throw new JobExecutionException(ex.getMessage(), ex);
        }
        return "1";
    } catch (ExecuteException ex) {
        throw new JobExecutionException(ex.getMessage(), ex);
    } catch (IOException | InterruptedException | JobExecutionException ex) {
        throw new JobExecutionException(ex.getMessage(), ex);
    }
}