summaryrefslogtreecommitdiff
path: root/platform/util/src/com/intellij/util/EnvironmentUtil.java
blob: 1d7016460f1e94a991a6ce16adb288ed583e7791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.util;

import com.intellij.execution.process.UnixProcessManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.AtomicNotNullLazyValue;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.concurrency.FixedFuture;
import com.intellij.util.text.CaseInsensitiveStringHashingStrategy;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

import java.io.File;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class EnvironmentUtil {
  private static final Logger LOG = Logger.getInstance("#com.intellij.util.EnvironmentUtil");

  private static final int SHELL_ENV_READING_TIMEOUT = 10000;

  private static final Future<Map<String, String>> ourEnvGetter;
  static {
    if (SystemInfo.isMac && "unlocked".equals(System.getProperty("__idea.mac.env.lock")) && Registry.is("idea.fix.mac.env")) {
      ExecutorService executor = Executors.newSingleThreadExecutor();
      ourEnvGetter = executor.submit(new Callable<Map<String, String>>() {
        @Override
        public Map<String, String> call() throws Exception {
          try {
            return getShellEnv();
          }
          catch (Throwable t) {
            LOG.warn("can't get shell environment", t);
            return System.getenv();
          }
        }
      });
      executor.shutdown();
    }
    else {
      ourEnvGetter = new FixedFuture<Map<String, String>>(System.getenv());
    }
  }

  private static final NotNullLazyValue<Map<String, String>> ourEnvironment = new AtomicNotNullLazyValue<Map<String, String>>() {
    @NotNull
    @Override
    protected Map<String, String> compute() {
      try {
        return ourEnvGetter.get();
      }
      catch (Exception e) {
        LOG.warn(e);
        return System.getenv();
      }
    }
  };

  private static final NotNullLazyValue<Map<String, String>> ourEnvironmentOsSpecific = new AtomicNotNullLazyValue<Map<String, String>>() {
    @NotNull
    @Override
    protected Map<String, String> compute() {
      Map<String, String> env = ourEnvironment.getValue();
      if (SystemInfo.isWindows) {
        env = Collections.unmodifiableMap(new THashMap<String, String>(env, CaseInsensitiveStringHashingStrategy.INSTANCE));
      }
      return env;
    }
  };

  private EnvironmentUtil() { }

  public static boolean isEnvironmentReady() {
    return ourEnvGetter.isDone();
  }

  /**
   * Returns the process environment.
   * On Mac OS X a shell (Terminal.app) environment is returned (unless disabled by a system property).
   *
   * @return unmodifiable map of the process environment.
   */
  @NotNull
  public static Map<String, String> getEnvironmentMap() {
    return ourEnvironment.getValue();
  }

  /**
   * Returns value for the passed environment variable name.
   * The passed environment variable name is handled in a case-sensitive or case-insensitive manner depending on OS.<p>
   * For example, on Windows <code>getValue("Path")</code> will return the same result as <code>getValue("PATH")</code>.
   *
   * @param name environment variable name
   * @return value of the environment variable or null if no such variable found
   */
  @Nullable
  public static String getValue(@NotNull String name) {
    return ourEnvironmentOsSpecific.getValue().get(name);
  }

  public static String[] getEnvironment() {
    return flattenEnvironment(getEnvironmentMap());
  }

  public static String[] flattenEnvironment(Map<String, String> environment) {
    String[] array = new String[environment.size()];
    int i = 0;
    for (Map.Entry<String, String> entry : environment.entrySet()) {
      array[i++] = entry.getKey() + "=" + entry.getValue();
    }
    return array;
  }

  private static Map<String, String> getShellEnv() throws Exception {
    String shell = System.getenv("SHELL");
    if (shell == null || !new File(shell).canExecute()) {
      throw new Exception("shell:" + shell);
    }

    File reader = FileUtil.findFirstThatExist(
      PathManager.getBinPath() + "/printenv.py",
      PathManager.getHomePath() + "/community/bin/mac/printenv.py",
      PathManager.getHomePath() + "/bin/mac/printenv.py"
    );
    if (reader == null) {
      throw new Exception("bin:" + PathManager.getBinPath());
    }

    File envFile = FileUtil.createTempFile("intellij-shell-env", null, false);
    try {
      String[] command = {shell, "-l", "-i", "-c", ("'" + reader.getAbsolutePath() + "' '" + envFile.getAbsolutePath() + "'")};
      LOG.info("loading shell env: " + StringUtil.join(command, " "));

      Process process = Runtime.getRuntime().exec(command);
      ProcessKiller processKiller = new ProcessKiller(process);
      processKiller.killAfter(SHELL_ENV_READING_TIMEOUT);
      int rv = process.waitFor();
      processKiller.stopWaiting();

      String lines = FileUtil.loadFile(envFile);
      if (rv != 0 || lines.isEmpty()) {
        throw new Exception("rv:" + rv + " text:" + lines.length());
      }
      return parseEnv(lines);
    }
    finally {
      FileUtil.delete(envFile);
    }
  }

  private static Map<String, String> parseEnv(String text) throws Exception {
    Set<String> toIgnore = new HashSet<String>(Arrays.asList("_", "PWD", "SHLVL"));
    Map<String, String> env = System.getenv();
    Map<String, String> newEnv = new HashMap<String, String>();

    String[] lines = text.split("\0");
    for (String line : lines) {
      int pos = line.indexOf('=');
      if (pos <= 0) {
        throw new Exception("malformed:" + line);
      }
      String name = line.substring(0, pos);
      if (!toIgnore.contains(name)) {
        newEnv.put(name, line.substring(pos + 1));
      }
      else if (env.containsKey(name)) {
        newEnv.put(name, env.get(name));
      }
    }

    LOG.info("shell environment loaded (" + newEnv.size() + " vars)");
    return Collections.unmodifiableMap(newEnv);
  }


  private static class ProcessKiller {
    private final Process myProcess;
    private final Object myWaiter = new Object();

    public ProcessKiller(Process process) {
      myProcess = process;
    }

    public void killAfter(long timeout) {
      final long stop = System.currentTimeMillis() + timeout;
      new Thread() {
        @Override
        public void run() {
          synchronized (myWaiter) {
            while (System.currentTimeMillis() < stop) {
              try {
                myProcess.exitValue();
                break;
              }
              catch (IllegalThreadStateException ignore) { }

              try {
                myWaiter.wait(100);
              }
              catch (InterruptedException ignore) { }
            }
          }

          try {
            myProcess.exitValue();
          }
          catch (IllegalThreadStateException e) {
            UnixProcessManager.sendSigKillToProcessTree(myProcess);
            LOG.warn("timed out");
          }
        }
      }.start();
    }

    public void stopWaiting() {
      synchronized (myWaiter) {
        myWaiter.notifyAll();
      }
    }
  }

  /** @deprecated use {@link #getEnvironmentMap()} (to remove in IDEA 14) */
  @SuppressWarnings({"UnusedDeclaration", "SpellCheckingInspection"})
  public static Map<String, String> getEnviromentProperties() {
    return getEnvironmentMap();
  }

  /** @deprecated use {@link #getEnvironmentMap()} (to remove in IDEA 14) */
  @SuppressWarnings({"UnusedDeclaration", "SpellCheckingInspection"})
  public static Map<String, String> getEnvironmentProperties() {
    return getEnvironmentMap();
  }

  @TestOnly
  static Map<String, String> testLoader() {
    try {
      return getShellEnv();
    }
    catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  @TestOnly
  static Map<String, String> testParser(@NotNull String lines) {
    try {
      return parseEnv(lines);
    }
    catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}