IDEA-118292 (process idea.properties in declaration order)

This commit is contained in:
Roman Shevchenko
2013-12-18 15:11:44 +04:00
parent acc25544be
commit 5ade729260
3 changed files with 38 additions and 8 deletions
@@ -320,19 +320,19 @@ public class PathManager {
if (propFile != null) {
try {
InputStream fis = new BufferedInputStream(new FileInputStream(propFile));
Reader fis = new BufferedReader(new FileReader(propFile));
try {
final PropertyResourceBundle bundle = new PropertyResourceBundle(fis);
final Enumeration keys = bundle.getKeys();
String home = (String)bundle.handleGetObject("idea.home");
Map<String, String> properties = FileUtil.loadProperties(fis);
String home = properties.get("idea.home");
if (home != null && ourHomePath == null) {
ourHomePath = getAbsolutePath(substituteVars(home));
}
final Properties sysProperties = System.getProperties();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
Properties sysProperties = System.getProperties();
for (String key : properties.keySet()) {
if (sysProperties.getProperty(key, null) == null) { // load the property from the property file only if it is not defined yet
final String value = substituteVars(bundle.getString(key));
String value = substituteVars(properties.get(key));
sysProperties.setProperty(key, value);
}
}
@@ -1438,4 +1438,23 @@ public class FileUtil extends FileUtilRt {
return true;
}
/**
* Like {@link Properties#load(java.io.Reader)}, but preserves the order of key/value pairs.
*/
@NotNull
public static Map<String, String> loadProperties(@NotNull Reader reader) throws IOException {
final Map<String, String> map = ContainerUtil.newLinkedHashMap();
new Properties() {
@Override
public synchronized Object put(Object key, Object value) {
map.put(String.valueOf(key), String.valueOf(value));
//noinspection UseOfPropertiesAsHashtable
return super.put(key, value);
}
}.load(reader);
return map;
}
}
@@ -19,12 +19,16 @@ import com.intellij.openapi.util.SystemInfo;
import com.intellij.util.ArrayUtil;
import com.intellij.util.PairProcessor;
import com.intellij.util.ThreeState;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.Convertor;
import org.junit.Test;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
@@ -145,4 +149,11 @@ public class FileUtilLightTest {
assertFalse(FileUtil.startsWith("c:/idea2", "c:/idea"));
assertFalse(FileUtil.startsWith("c:/idea_branches/i18n", "c:/idea"));
}
@Test
public void testLoadProperties() throws IOException {
String data = "key2=value2\nkey1=value1\nkey3=value3";
Map<String, String> map = FileUtil.loadProperties(new StringReader(data));
assertEquals(ContainerUtil.newArrayList("key2", "key1", "key3"), ContainerUtil.newArrayList(map.keySet()));
}
}