Skip to content

Commit 724920d

Browse files
authored
Fix override of numeric value via env variable or system property (#2251)
1 parent 3355ca0 commit 724920d

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

config/core/src/main/java/org/apache/karaf/config/core/impl/KarafConfigurationPlugin.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ public void modifyConfiguration(ServiceReference<?> reference, Dictionary<String
4848
properties.put(key, values);
4949
} else {
5050
value = InterpolationHelper.substVars(value, null,null, convertDictionaryToMap(properties));
51-
try {
52-
int intValue = Integer.parseInt(value);
53-
properties.put(key, intValue);
54-
} catch (NumberFormatException e) {
51+
if (properties.get(key) != null && (properties.get(key) instanceof Number)) {
52+
properties.put(key, Integer.parseInt(value));
53+
} else {
5554
properties.put(key, value);
5655
}
5756
}
@@ -66,14 +65,12 @@ public void modifyConfiguration(ServiceReference<?> reference, Dictionary<String
6665
properties.put(key, values);
6766
} else {
6867
value = InterpolationHelper.substVars(value, null,null, convertDictionaryToMap(properties));
69-
try {
70-
int intValue = Integer.parseInt(value);
71-
properties.put(key, intValue);
72-
} catch (NumberFormatException e) {
68+
if (properties.get(key) != null && (properties.get(key) instanceof Number)) {
69+
properties.put(key, Integer.parseInt(value));
70+
} else {
7371
properties.put(key, value);
7472
}
7573
}
76-
7774
}
7875
}
7976
}

config/core/src/test/java/org/apache/karaf/config/core/impl/KarafConfigurationPluginTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ public class KarafConfigurationPluginTest {
2828
@Test
2929
public void testSystemProperty() throws Exception {
3030
System.setProperty("org.apache.karaf.shell.sshPort", "8102");
31+
System.setProperty("org.apache.karaf.shell.sshPorts", "[8102,8103]");
3132
KarafConfigurationPlugin plugin = new KarafConfigurationPlugin();
3233
Dictionary<String, Object> properties = new Hashtable<>();
3334
properties.put(Constants.SERVICE_PID, "org.apache.karaf.shell");
3435
properties.put("foo", "bar");
3536
properties.put("sshPort", 8101);
37+
properties.put("sshPorts", new String[] { "8102" });
3638
plugin.modifyConfiguration(null, properties);
3739

3840
Assert.assertEquals(8102, properties.get("sshPort"));
41+
Assert.assertEquals(2, ((String[])properties.get("sshPorts")).length);
42+
Assert.assertEquals("8102", ((String[])properties.get("sshPorts"))[0]);
43+
Assert.assertEquals("8103", ((String[])properties.get("sshPorts"))[1]);
3944
Assert.assertEquals("bar", properties.get("foo"));
4045
}
4146

0 commit comments

Comments
 (0)