From fcc59dad0eb7907788fcdd48a3821bec5606e42a Mon Sep 17 00:00:00 2001 From: Felix Marx Date: Mon, 24 Nov 2025 11:46:55 +0100 Subject: [PATCH] FELIX-6812: Fix ConfigInstaller to use listConfigurations instead of getConfiguration Replace getConfiguration() calls with listConfigurations() in ConfigInstaller.configurationEvent() method to avoid unintentionally creating new configurations. The getConfiguration() method creates a configuration if it doesn't exist, which is not the desired behavior when simply checking for existing configurations. Changes: - Use listConfigurations("(service.pid=" + pid + ")") instead of getConfiguration(pid, "?") - Add proper null and length checks for configuration arrays - Update corresponding test mocks to reflect the corrected API usage This prevents unwanted configuration creation during configuration event handling. --- .../fileinstall/internal/ConfigInstaller.java | 12 +++++++++--- .../fileinstall/internal/ConfigInstallerTest.java | 15 +++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/ConfigInstaller.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/ConfigInstaller.java index cf0a4b539f..020ee0bb52 100644 --- a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/ConfigInstaller.java +++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/ConfigInstaller.java @@ -237,9 +237,15 @@ public void doConfigurationEvent(ConfigurationEvent configurationEvent) { try { - Configuration config = getConfigurationAdmin().getConfiguration( - configurationEvent.getPid(), - "?"); + Configuration[] configurations = getConfigurationAdmin().listConfigurations("(service.pid=" + configurationEvent.getPid() + ")"); + if (null == configurations) { + return; + } + if (configurations.length < 1) { + return; + } + Configuration config = configurations[0]; + Dictionary dict = config.getProperties(); String fileName = dict != null ? (String) dict.get( DirectoryWatcher.FILENAME ) : null; File file = fileName != null ? fromConfigKey(fileName) : null; diff --git a/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/ConfigInstallerTest.java b/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/ConfigInstallerTest.java index 35ef49f2b3..f423379041 100644 --- a/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/ConfigInstallerTest.java +++ b/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/ConfigInstallerTest.java @@ -245,10 +245,10 @@ public void testCreateConfigAndObserveCMDeleted() throws Exception EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject())) .andReturn(null) .anyTimes(); - EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())) + EasyMock.expect(mockConfigurationAdmin.listConfigurations("(felix.fileinstall.filename=file:" + file + ")")) .andReturn(null); - EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")) - .andReturn(mockConfiguration); + EasyMock.expect(mockConfigurationAdmin.listConfigurations("(service.pid=" + pid + ")")) + .andReturn(new Configuration[] { mockConfiguration }); ServiceReference sr = EasyMock.createMock(ServiceReference.class); EasyMock.expect(mockConfiguration.updateIfDifferent(EasyMock.capture(props))).andReturn(true); @@ -327,8 +327,8 @@ public void testUseExistingConfigWithFileinstallFilenameAndObserveCMDeleted() th EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject())) .andReturn(null) .anyTimes(); - EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())) - .andReturn(null); + EasyMock.expect(mockConfigurationAdmin.listConfigurations("(service.pid=" + pid + ")")) + .andReturn(new Configuration[] { mockConfiguration }); EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")) .andReturn(mockConfiguration); EasyMock.expect(mockConfiguration.getPid()) @@ -386,6 +386,9 @@ public void testDoConfigurationEventSavesUpdatedConfigurationWhenUsingCachingPer .andReturn(cachingPersistenceConfiguration) .anyTimes(); + EasyMock.expect(mockConfigurationAdmin.listConfigurations("(service.pid=" + pid + ")")) + .andReturn(new Configuration[] { cachingPersistenceConfiguration }); + EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, null)) .andReturn(cachingPersistenceConfiguration) .anyTimes(); @@ -393,7 +396,7 @@ public void testDoConfigurationEventSavesUpdatedConfigurationWhenUsingCachingPer final Configuration newConfiguration = EasyMock.createMock(Configuration.class); EasyMock.expect(newConfiguration.getAttributes()).andReturn(Collections.emptySet()).times(2); - EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())) + EasyMock.expect(mockConfigurationAdmin.listConfigurations("(felix.fileinstall.filename=file:" + file + ")")) .andReturn(new Configuration[] { newConfiguration }); EasyMock.expect(newConfiguration.getProperties())