You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ro...@apache.org on 2021/05/09 13:59:19 UTC

[felix-dev] 02/02: [FELIX-6412] Use Configuration.updateIfDifferent

This is an automated email from the ASF dual-hosted git repository.

rotty3000 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git

commit 98d3533818194ce73c0dc8f8e512e46065444b95
Author: Raymond Augé <ro...@apache.org>
AuthorDate: Sun May 9 09:56:40 2021 -0400

    [FELIX-6412] Use Configuration.updateIfDifferent
    
    Signed-off-by: Raymond Augé <ro...@apache.org>
---
 .../fileinstall/internal/ConfigInstaller.java      | 31 +++++++++++++++++++++-
 .../fileinstall/internal/ConfigInstallerTest.java  | 14 +++++-----
 2 files changed, 37 insertions(+), 8 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 9b76f8a..3e11c29 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
@@ -73,6 +73,7 @@ public class ConfigInstaller implements ArtifactInstaller, ConfigurationListener
     private final Method addAttributesMethod;
     private final Method getAttributesMethod;
     private final Method removeAttributesMethod;
+    private final Method updateIfDifferentMethod;
     private final Object READ_ONLY_ATTRIBUTE_ARRAY;
     private final boolean osWin;
     private ServiceRegistration<?> registration;
@@ -86,6 +87,7 @@ public class ConfigInstaller implements ArtifactInstaller, ConfigurationListener
         Method aaMethod = null;
         Method gaMethod = null;
         Method raMethod = null;
+        Method uidMethod = null;
 
         if (this.configAdmin != null) {
             for (Method method : Configuration.class.getDeclaredMethods())
@@ -102,12 +104,17 @@ public class ConfigInstaller implements ArtifactInstaller, ConfigurationListener
                 {
                     raMethod = method;
                 }
+                else if ("updateIfDifferent".equals(method.getName()))
+                {
+                    uidMethod = method;
+                }
             }
         }
 
         this.addAttributesMethod = aaMethod;
         this.getAttributesMethod = gaMethod;
         this.removeAttributesMethod = raMethod;
+        this.updateIfDifferentMethod = uidMethod;
 
         Object roAttributesArray = null;
         if (this.addAttributesMethod != null)
@@ -393,7 +400,7 @@ public class ConfigInstaller implements ArtifactInstaller, ConfigurationListener
                         + (pid[1] == null ? "" : "-" + pid[1])
                         + "} from " + f.getName(), null);
             }
-            config.update(ht);
+            update0(config, ht);
             return true;
         }
         else
@@ -565,6 +572,28 @@ public class ConfigInstaller implements ArtifactInstaller, ConfigurationListener
         return false;
     }
 
+    void update0(final Configuration config, final Hashtable<String, Object> ht) throws IOException {
+        if (updateIfDifferentMethod != null) {
+            try {
+                updateIfDifferentMethod.invoke(config, ht);
+            }
+            catch (InvocationTargetException ite) {
+                Throwable targetException = ite.getTargetException();
+                if (targetException instanceof IOException) {
+                    throw (IOException)targetException;
+                }
+
+                throw new RuntimeException(targetException);
+            }
+            catch (Throwable t) {
+                throw new RuntimeException(t);
+            }
+        }
+        else {
+            config.update(ht);
+        }
+    }
+
     private boolean canWrite(File f) throws IOException {
         if (osWin) {
             return f.canWrite();
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 6b6a12c..3eb872d 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
@@ -101,7 +101,7 @@ public class ConfigInstallerTest extends TestCase {
         EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject()))
                 .andReturn(null)
                 .anyTimes();
-        mockConfiguration.update(EasyMock.capture(props));
+        EasyMock.expect(mockConfiguration.updateIfDifferent(EasyMock.capture(props))).andReturn(true);
         EasyMock.expectLastCall();
         EasyMock.replay(mockConfiguration, mockConfigurationAdmin, mockBundleContext, mockBundle);
 
@@ -136,7 +136,7 @@ public class ConfigInstallerTest extends TestCase {
         EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject()))
                 .andReturn(null)
                 .anyTimes();
-        mockConfiguration.update(EasyMock.capture(props));
+        EasyMock.expect(mockConfiguration.updateIfDifferent(EasyMock.capture(props))).andReturn(true);
         EasyMock.expectLastCall();
         EasyMock.replay(mockConfiguration, mockConfigurationAdmin, mockBundleContext, mockBundle);
 
@@ -248,7 +248,7 @@ public class ConfigInstallerTest extends TestCase {
                 .andReturn(mockConfiguration);
 
         ServiceReference<ConfigurationAdmin> sr = EasyMock.createMock(ServiceReference.class);
-        mockConfiguration.update(EasyMock.capture(props));
+        EasyMock.expect(mockConfiguration.updateIfDifferent(EasyMock.capture(props))).andReturn(true);
         EasyMock.expectLastCall();
 
         EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?"))
@@ -400,9 +400,9 @@ public class ConfigInstallerTest extends TestCase {
                 .andReturn(pid);
 
         final Capture<Dictionary<String, Object>> props = new Capture<>();
-        newConfiguration.update(EasyMock.capture(props));
+        EasyMock.expect(newConfiguration.updateIfDifferent(EasyMock.capture(props))).andReturn(true);
 
-        cachingPersistenceConfiguration.update(EasyMock.capture(props));
+        EasyMock.expect(cachingPersistenceConfiguration.updateIfDifferent(EasyMock.capture(props)));
         EasyMock.expectLastCall()
                 .andAnswer(new IAnswer<Boolean>() {
                     @Override
@@ -450,7 +450,7 @@ public class ConfigInstallerTest extends TestCase {
                 buffer.append("<Dictionary check: testkey present?>");
             }
         } );
-        mockConfiguration.update(new Hashtable<String, Object>());
+        EasyMock.expect(mockConfiguration.updateIfDifferent(new Hashtable<String, Object>())).andReturn(true);
         EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject()))
                         .andReturn(null);
         EasyMock.expect(mockConfigurationAdmin.getConfiguration("firstcfg", "?"))
@@ -578,7 +578,7 @@ public class ConfigInstallerTest extends TestCase {
                 .andReturn(pid).times(2);
 
         final Capture<Dictionary<String, Object>> props = new Capture<>();
-        cachingPersistenceConfiguration.update(EasyMock.capture(props));
+        EasyMock.expect(cachingPersistenceConfiguration.updateIfDifferent(EasyMock.capture(props)));
         EasyMock.expectLastCall()
                 .andAnswer(new IAnswer<Boolean>() {
                     @Override