Java Code Examples for org.apache.commons.exec.environment.EnvironmentUtils#getProcEnvironment()

The following examples show how to use org.apache.commons.exec.environment.EnvironmentUtils#getProcEnvironment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardInterpreterLauncher.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public Map<String, String> buildEnvFromProperties(InterpreterLaunchContext context) throws IOException {
  Map<String, String> env = EnvironmentUtils.getProcEnvironment();
  for (Map.Entry entry : context.getProperties().entrySet()) {
    String key = (String) entry.getKey();
    String value = (String) entry.getValue();
    if (RemoteInterpreterUtils.isEnvString(key) && !StringUtils.isBlank(value)) {
      env.put(key, value);
    }
  }
  env.put("INTERPRETER_GROUP_ID", context.getInterpreterGroupId());
  return env;
}
 
Example 2
Source File: PythonInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
protected Map<String, String> setupPythonEnv() throws IOException {
  Map<String, String> env = EnvironmentUtils.getProcEnvironment();
  appendToPythonPath(env, pythonWorkDir.getAbsolutePath());
  if (useBuiltinPy4j) {
    appendToPythonPath(env, pythonWorkDir.getAbsolutePath() + "/py4j-src-0.10.7.zip");
  }
  LOGGER.info("PYTHONPATH: " + env.get("PYTHONPATH"));
  return env;
}
 
Example 3
Source File: NetworkUtils.java    From elasticsearch with Apache License 2.0 5 votes vote down vote up
public static Map<String, String> getEnvironment() {
    Map<String, String> env = Collections.emptyMap();
    try {
        env = EnvironmentUtils.getProcEnvironment();
    } catch (IOException e) {
        LOG.error("Unable to get environmental variables", e);
    }
    return env;
}
 
Example 4
Source File: NetworkUtils.java    From logstash with Apache License 2.0 5 votes vote down vote up
public Map<String, String> getEnvironment() {
    Map<String, String> env = Collections.emptyMap();
    try {
        env = EnvironmentUtils.getProcEnvironment();
    } catch (IOException e) {
        LOG.error("Unable to get environmental variables", e);
    }
    return env;
}
 
Example 5
Source File: ProcessUtil.java    From elasticsearch-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Create an environment by merging the current environment and the supplied one.
 * If the supplied environment is null, null is returned.
 * @param environment the environment properties to append
 * @return an execution environment
 */
public static Map<String, String> createEnvironment(Map<String, String> environment)
{
    Map<String, String> result = null;
    
    try
    {
        result = EnvironmentUtils.getProcEnvironment();
    }
    catch (IOException ex)
    {
        throw new ElasticsearchSetupException(
                "Cannot get the current process environment", ex);
    }
    
    // Clean up the base environment:

    // The elasticsearch start and plugin scripts print warnings if these environment variables
    // are set, so lets unset them.
    result.remove("JAVA_TOOL_OPTIONS");
    result.remove("JAVA_OPTS");

    // The following environment variables may interfere with the elasticsearch instance.
    result.remove("ES_HOME");
    result.remove("ES_JAVA_OPTS");
    result.remove("ES_PATH_CONF");
    result.remove("ES_TMPDIR");

    // Now that we have a clean environment, set the provided variables on it.
    if (environment != null)
    {
        result.putAll(environment);
    }

    return result;
}
 
Example 6
Source File: NPM.java    From wisdom with Apache License 2.0 5 votes vote down vote up
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 {
        env.put("PATH", node.getNodeExecutable().getParent());
    }
    return env;
}
 
Example 7
Source File: SolrCLI.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected Map<String,Object> startSolr(File solrHomeDir,
                                       boolean cloudMode,
                                       CommandLine cli,
                                       int port,
                                       String zkHost,
                                       int maxWaitSecs)
    throws Exception
{

  String extraArgs = readExtraArgs(cli.getArgs());

  String host = cli.getOptionValue('h');
  String memory = cli.getOptionValue('m');

  String hostArg = (host != null && !"localhost".equals(host)) ? " -h "+host : "";
  String zkHostArg = (zkHost != null) ? " -z "+zkHost : "";
  String memArg = (memory != null) ? " -m "+memory : "";
  String cloudModeArg = cloudMode ? "-cloud " : "";
  String forceArg = cli.hasOption("force") ? " -force" : "";

  String addlOpts = cli.getOptionValue('a');
  String addlOptsArg = (addlOpts != null) ? " -a \""+addlOpts+"\"" : "";

  File cwd = new File(System.getProperty("user.dir"));
  File binDir = (new File(script)).getParentFile();

  boolean isWindows = (OS.isFamilyDOS() || OS.isFamilyWin9x() || OS.isFamilyWindows());
  String callScript = (!isWindows && cwd.equals(binDir.getParentFile())) ? "bin/solr" : script;

  String cwdPath = cwd.getAbsolutePath();
  String solrHome = solrHomeDir.getAbsolutePath();

  // don't display a huge path for solr home if it is relative to the cwd
  if (!isWindows && cwdPath.length() > 1 && solrHome.startsWith(cwdPath))
    solrHome = solrHome.substring(cwdPath.length()+1);

  String startCmd =
      String.format(Locale.ROOT, "\"%s\" start %s -p %d -s \"%s\" %s %s %s %s %s %s",
          callScript, cloudModeArg, port, solrHome, hostArg, zkHostArg, memArg, forceArg, extraArgs, addlOptsArg);
  startCmd = startCmd.replaceAll("\\s+", " ").trim(); // for pretty printing

  echo("\nStarting up Solr on port " + port + " using command:");
  echo(startCmd + "\n");

  String solrUrl =
      String.format(Locale.ROOT, "%s://%s:%d/solr", urlScheme, (host != null ? host : "localhost"), port);

  Map<String,Object> nodeStatus = checkPortConflict(solrUrl, solrHomeDir, port, cli);
  if (nodeStatus != null)
    return nodeStatus; // the server they are trying to start is already running

  int code = 0;
  if (isWindows) {
    // On Windows, the execution doesn't return, so we have to execute async
    // and when calling the script, it seems to be inheriting the environment that launched this app
    // so we have to prune out env vars that may cause issues
    Map<String,String> startEnv = new HashMap<>();
    Map<String,String> procEnv = EnvironmentUtils.getProcEnvironment();
    if (procEnv != null) {
      for (Map.Entry<String, String> entry : procEnv.entrySet()) {
        String envVar = entry.getKey();
        String envVarVal = entry.getValue();
        if (envVarVal != null && !"EXAMPLE".equals(envVar) && !envVar.startsWith("SOLR_")) {
          startEnv.put(envVar, envVarVal);
        }
      }
    }
    DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
    executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd), startEnv, handler);

    // wait for execution.
    try {
      handler.waitFor(3000);
    } catch (InterruptedException ie) {
      // safe to ignore ...
      Thread.interrupted();
    }
    if (handler.hasResult() && handler.getExitValue() != 0) {
      throw new Exception("Failed to start Solr using command: "+startCmd+" Exception : "+handler.getException());
    }
  } else {
    try {
      code = executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd));
    } catch(ExecuteException e){
      throw new Exception("Failed to start Solr using command: "+startCmd+" Exception : "+ e);
    }
  }
  if (code != 0)
    throw new Exception("Failed to start Solr using command: "+startCmd);

  return getNodeStatus(solrUrl, maxWaitSecs);
}
 
Example 8
Source File: ShellExecutorHelperTest.java    From CodeCheckerEclipsePlugin with Eclipse Public License 1.0 4 votes vote down vote up
@Before
public void setupExecutors() throws IOException {
    emptyEnvExecutor = new ShellExecutorHelper(new HashMap<String, String>());
    defaultEnvExecutor = new ShellExecutorHelper(EnvironmentUtils.getProcEnvironment());
}
 
Example 9
Source File: JupyterKernelInterpreter.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
protected Map<String, String> setupKernelEnv() throws IOException {
  return EnvironmentUtils.getProcEnvironment();
}