FELIX-6560 : Interpolation of embedded arrays does not work master
authorCarsten Ziegeler <cziegeler@apache.org>
Tue, 9 Aug 2022 09:47:05 +0000 (11:47 +0200)
committerCarsten Ziegeler <cziegeler@apache.org>
Tue, 9 Aug 2022 09:47:05 +0000 (11:47 +0200)
configadmin-plugins/interpolation/src/main/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPlugin.java
configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolationConfigurationPluginTest.java
configadmin-plugins/interpolation/src/test/java/org/apache/felix/configadmin/plugin/interpolation/InterpolatorTest.java

index f2e7b673729282e702699f039b5e5cb27d0057f2..30435f407a2c126d640e5054dcd431218a5c7fb2 100644 (file)
@@ -20,6 +20,7 @@ import static java.util.stream.Collectors.toList;
 
 import java.io.File;
 import java.io.IOException;
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.Array;
 import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.util.ArrayList;
@@ -136,19 +137,30 @@ class InterpolationConfigurationPlugin implements ConfigurationPlugin {
                 }
             } else if (val instanceof String[]) {
                 String[] array = (String[]) val;
                 }
             } else if (val instanceof String[]) {
                 String[] array = (String[]) val;
-                String[] newArray = null;
+                List<String> newArray = null;
                 for (int i = 0; i < array.length; i++) {
                     Object newVal = getNewValue(key, array[i], pid, properties);
                     if (newVal != null && !newVal.equals(array[i])) {
                         if (newArray == null) {
                 for (int i = 0; i < array.length; i++) {
                     Object newVal = getNewValue(key, array[i], pid, properties);
                     if (newVal != null && !newVal.equals(array[i])) {
                         if (newArray == null) {
-                            newArray = new String[array.length];
-                            System.arraycopy(array, 0, newArray, 0, array.length);
+                            newArray = new ArrayList<>();
+                            for(int m=0;m<i;m++) {
+                                newArray.add(array[m]);
+                            }
                         }
                         }
-                        newArray[i] = newVal.toString();
+                        if ( newVal.getClass().isArray() ) {
+                            for(int m=0;m<Array.getLength(newVal);m++ ) {
+                                newArray.add(Array.get(newVal, m).toString());
+                            }
+                        } else {
+                            newArray.add(newVal.toString());
+                        }
+                    } else if ( newArray != null ) {
+                        newArray.add(array[i]);
                     }
                 }
                 if (newArray != null) {
                     }
                 }
                 if (newArray != null) {
-                    properties.put(key, newArray);
+                    final String[] update = newArray.toArray(new String[newArray.size()]);
+                    properties.put(key, update);
                     getLog().info("Replaced value of configuration property '{}' for PID {}", key, pid);
                 }
             }
                     getLog().info("Replaced value of configuration property '{}' for PID {}", key, pid);
                 }
             }
index 4fdeab038d9addad511b75c4cb55f1954d73390b..1e996647c3935f57a8f4c3ac0d860d186e75d411 100644 (file)
@@ -306,4 +306,18 @@ public class InterpolationConfigurationPluginTest {
         obj = plugin.convertType("String", "a,b", ",");
         assertEquals("a,b", obj);
     }
         obj = plugin.convertType("String", "a,b", ",");
         assertEquals("a,b", obj);
     }
+
+    @Test
+    public void testModifyConfigurationWithArrayInArray() throws Exception {
+        BundleContext bc = Mockito.mock(BundleContext.class);
+        Mockito.when(bc.getProperty("foo")).thenReturn("2000,3000");
+        InterpolationConfigurationPlugin plugin = new InterpolationConfigurationPlugin(bc::getProperty, null, null);
+
+        Dictionary<String, Object> dict = new Hashtable<>();
+        dict.put("array", new String[] {"1000", "$[prop:foo;type=String[];delimiter=,]", "4000"});
+        plugin.modifyConfiguration(null, dict);
+
+        assertEquals(1, dict.size());
+        assertArrayEquals(new String[] {"1000", "2000", "3000", "4000"}, (String[])dict.get("array"));
+    }
 }
 }
index 74c892c973e14736e8539d520366aa7cc3b4217d..05132a91674912e9d1f66247b48371a4156b7b57 100755 (executable)
@@ -18,7 +18,6 @@ package org.apache.felix.configadmin.plugin.interpolation;
 
 import static org.junit.Assert.assertEquals;
 
 
 import static org.junit.Assert.assertEquals;
 
-import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.felix.configadmin.plugin.interpolation.Interpolator.Provider;
 import java.util.Map;
 
 import org.apache.felix.configadmin.plugin.interpolation.Interpolator.Provider;