2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.execution.configuration;
18 import com.google.common.collect.ImmutableMap;
19 import com.intellij.util.containers.ContainerUtil;
20 import org.jdom.Element;
21 import org.jetbrains.annotations.NonNls;
22 import org.jetbrains.annotations.NotNull;
24 import java.util.LinkedHashMap;
28 * Holds environment variables configuration:
30 * <li>list of user-defined environment variables</li>
31 * <li>boolean flag - whether to pass system environment</li>
33 * Instances of this class are immutable objects, so it can be safely passed across threads.
35 public class EnvironmentVariablesData {
37 public static final EnvironmentVariablesData DEFAULT = new EnvironmentVariablesData(ImmutableMap.<String, String>of(), true);
38 @NonNls private static final String ENVS = "envs";
39 @NonNls private static final String PASS_PARENT_ENVS = "pass-parent-envs";
40 @NonNls private static final String ENV = EnvironmentVariablesComponent.ENV;
41 @NonNls private static final String NAME = EnvironmentVariablesComponent.NAME;
42 @NonNls private static final String VALUE = EnvironmentVariablesComponent.VALUE;
44 private final ImmutableMap<String, String> myEnvs;
45 private final boolean myPassParentEnvs;
47 private EnvironmentVariablesData(@NotNull Map<String, String> envs, boolean passParentEnvs) {
48 myEnvs = ImmutableMap.copyOf(envs);
49 myPassParentEnvs = passParentEnvs;
53 * @return immutable Map instance containing user-defined environment variables (iteration order is reliable user-specified)
56 public Map<String, String> getEnvs() {
60 public boolean isPassParentEnvs() {
61 return myPassParentEnvs;
65 public boolean equals(Object o) {
66 if (this == o) return true;
67 if (o == null || getClass() != o.getClass()) return false;
68 EnvironmentVariablesData data = (EnvironmentVariablesData)o;
69 return myPassParentEnvs == data.myPassParentEnvs && myEnvs.equals(data.myEnvs);
73 public int hashCode() {
74 int result = myEnvs.hashCode();
75 result = 31 * result + (myPassParentEnvs ? 1 : 0);
80 public static EnvironmentVariablesData readExternal(@NotNull Element element) {
81 Element envsElement = element.getChild(ENVS);
82 if (envsElement == null) {
85 Map<String, String> envs = ImmutableMap.of();
86 String passParentEnvsStr = envsElement.getAttributeValue(PASS_PARENT_ENVS);
87 boolean passParentEnvs = passParentEnvsStr == null || Boolean.parseBoolean(passParentEnvsStr);
88 for (Element envElement : envsElement.getChildren(ENV)) {
89 String envName = envElement.getAttributeValue(NAME);
90 String envValue = envElement.getAttributeValue(VALUE);
91 if (envName != null && envValue != null) {
93 envs = ContainerUtil.newLinkedHashMap();
95 envs.put(envName, envValue);
98 return create(envs, passParentEnvs);
101 public void writeExternal(@NotNull Element parent) {
102 Element envsElement = new Element(ENVS);
103 if (!myPassParentEnvs) {
104 // Avoid writing pass-parent-envs="true" to minimize changes in xml comparing it to xml written by
105 // com.intellij.execution.configuration.EnvironmentVariablesComponent.writeExternal
106 envsElement.setAttribute(PASS_PARENT_ENVS, Boolean.FALSE.toString());
108 for (Map.Entry<String, String> entry : myEnvs.entrySet()) {
109 Element envElement = new Element(ENV);
110 envElement.setAttribute(NAME, entry.getKey());
111 envElement.setAttribute(VALUE, entry.getValue());
112 envsElement.addContent(envElement);
114 parent.addContent(envsElement);
118 * @param envs Map instance containing user-defined environment variables
119 * (iteration order should be reliable user-specified, like {@link LinkedHashMap} or {@link ImmutableMap})
120 * @param passParentEnvs true if system environment should be passed
123 public static EnvironmentVariablesData create(@NotNull Map<String, String> envs, boolean passParentEnvs) {
124 if (passParentEnvs && envs.isEmpty()) {
127 return new EnvironmentVariablesData(envs, passParentEnvs);