Merge branch 'master' into traff/zip_helpers
[idea/community.git] / platform / lang-impl / src / com / intellij / execution / configuration / EnvironmentVariablesData.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.execution.configuration;
17
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;
23
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26
27 /**
28  * Holds environment variables configuration:
29  * <ul>
30  * <li>list of user-defined environment variables</li>
31  * <li>boolean flag - whether to pass system environment</li>
32  * </ul>
33  * Instances of this class are immutable objects, so it can be safely passed across threads.
34  */
35 public class EnvironmentVariablesData {
36
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;
43
44   private final ImmutableMap<String, String> myEnvs;
45   private final boolean myPassParentEnvs;
46
47   private EnvironmentVariablesData(@NotNull Map<String, String> envs, boolean passParentEnvs) {
48     myEnvs = ImmutableMap.copyOf(envs);
49     myPassParentEnvs = passParentEnvs;
50   }
51
52   /**
53    * @return immutable Map instance containing user-defined environment variables (iteration order is reliable user-specified)
54    */
55   @NotNull
56   public Map<String, String> getEnvs() {
57     return myEnvs;
58   }
59
60   public boolean isPassParentEnvs() {
61     return myPassParentEnvs;
62   }
63
64   @Override
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);
70   }
71
72   @Override
73   public int hashCode() {
74     int result = myEnvs.hashCode();
75     result = 31 * result + (myPassParentEnvs ? 1 : 0);
76     return result;
77   }
78
79   @NotNull
80   public static EnvironmentVariablesData readExternal(@NotNull Element element) {
81     Element envsElement = element.getChild(ENVS);
82     if (envsElement == null) {
83       return DEFAULT;
84     }
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) {
92         if (envs.isEmpty()) {
93           envs = ContainerUtil.newLinkedHashMap();
94         }
95         envs.put(envName, envValue);
96       }
97     }
98     return create(envs, passParentEnvs);
99   }
100
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());
107     }
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);
113     }
114     parent.addContent(envsElement);
115   }
116
117   /**
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
121    */
122   @NotNull
123   public static EnvironmentVariablesData create(@NotNull Map<String, String> envs, boolean passParentEnvs) {
124     if (passParentEnvs && envs.isEmpty()) {
125       return DEFAULT;
126     }
127     return new EnvironmentVariablesData(envs, passParentEnvs);
128   }
129 }