felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1802546 - in /felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl: Configurator.java model/State.java
Date Fri, 21 Jul 2017 06:24:45 GMT
Author: cziegeler
Date: Fri Jul 21 06:24:45 2017
New Revision: 1802546

URL: http://svn.apache.org/viewvc?rev=1802546&view=rev
Log:
Improve retry mechanism

Modified:
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/State.java

Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java?rev=1802546&r1=1802545&r2=1802546&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
Fri Jul 21 06:24:45 2017
@@ -108,8 +108,9 @@ public class Configurator {
 
                         @Override
                         public void run() {
-                            processAddBundle(bundle);
-                            process();
+                            if ( processAddBundle(bundle) ) {
+                                process();
+                            }
                         }
                     });
                 }
@@ -131,9 +132,9 @@ public class Configurator {
                         @Override
                         public void run() {
                             try {
-                                processRemoveBundle(bundle.getBundleId());
-                                process();
-                                Configurator.this.state.removeConfigAdminBundleId(bundle.getBundleId());
+                                if ( processRemoveBundle(bundle.getBundleId()) ) {
+                                    process();
+                                }
                             } catch ( final IllegalStateException ise) {
                                 SystemLogger.error("Error processing bundle " + getBundleIdentity(bundle),
ise);
                             }
@@ -291,22 +292,22 @@ public class Configurator {
         this.tracker.open();
     }
 
-    public void processAddBundle(final Bundle bundle) {
+    public boolean processAddBundle(final Bundle bundle) {
+        final long bundleId = bundle.getBundleId();
+        final long bundleLastModified = bundle.getLastModified();
+
+        final Long lastModified = state.getLastModified(bundleId);
+        if ( lastModified != null && lastModified.longValue() == bundleLastModified
) {
+            // no changes, nothing to do
+            return false;
+        }
+
+        BundleState config = null;
         try {
-            final long bundleId = bundle.getBundleId();
-            final long bundleLastModified = bundle.getLastModified();
-            final Long lastModified = state.getLastModified(bundleId);
-            if ( lastModified != null && lastModified.longValue() == bundleLastModified
) {
-                // no changes, nothing to do
-                return;
-            }
-            if ( lastModified != null ) {
-                processRemoveBundle(bundleId);
-            }
             final Set<String> paths = Util.isConfigurerBundle(bundle, this.bundleContext.getBundle().getBundleId());
             if ( paths != null ) {
                 final JSONUtil.Report report = new JSONUtil.Report();
-                final BundleState config = JSONUtil.readConfigurationsFromBundle(new BinUtil.ResourceProvider()
{
+                config = JSONUtil.readConfigurationsFromBundle(new BinUtil.ResourceProvider()
{
 
                     @Override
                     public String getIdentifier() {
@@ -334,22 +335,33 @@ public class Configurator {
                 for(final String e : report.errors) {
                     SystemLogger.error(e);
                 }
-                for(final String pid : config.getPids()) {
-                    state.addAll(pid, config.getConfigurations(pid));
-                }
             }
-            state.setLastModified(bundleId, bundleLastModified);
         } catch ( final IllegalStateException ise) {
             SystemLogger.error("Error processing bundle " + getBundleIdentity(bundle), ise);
         }
+        if ( lastModified != null ) {
+            processRemoveBundle(bundleId);
+        }
+        if ( config != null ) {
+            for(final String pid : config.getPids()) {
+                state.addAll(pid, config.getConfigurations(pid));
+            }
+            state.setLastModified(bundleId, bundleLastModified);
+            return true;
+        }
+        return lastModified != null;
     }
 
-    public void processRemoveBundle(final long bundleId) {
-        state.removeLastModified(bundleId);
-        for(final String pid : state.getPids()) {
-            final ConfigList configList = state.getConfigurations(pid);
-            configList.uninstall(bundleId);
+    public boolean processRemoveBundle(final long bundleId) {
+        if ( state.getLastModified(bundleId) != null ) {
+            state.removeLastModified(bundleId);
+            for(final String pid : state.getPids()) {
+                final ConfigList configList = state.getConfigurations(pid);
+                configList.uninstall(bundleId);
+            }
+            return true;
         }
+        return false;
     }
 
     /**
@@ -370,16 +382,20 @@ public class Configurator {
             coordination = CoordinatorUtil.getCoordination(localCoordinator);
         }
 
+        boolean retry = false;
         try {
             for(final String pid : state.getPids()) {
                 final ConfigList configList = state.getConfigurations(pid);
 
                 if ( configList.hasChanges() ) {
-                    process(configList);
-                    try {
-                        State.writeState(this.bundleContext.getDataFile(State.FILE_NAME),
state);
-                    } catch ( final IOException ioe) {
-                        SystemLogger.error("Unable to persist state to " + State.FILE_NAME,
ioe);
+                    if ( process(configList) ) {
+                        try {
+                            State.writeState(this.bundleContext.getDataFile(State.FILE_NAME),
state);
+                        } catch ( final IOException ioe) {
+                            SystemLogger.error("Unable to persist state to " + State.FILE_NAME,
ioe);
+                        }
+                    } else {
+                        retry = true;
                     }
                 }
             }
@@ -389,6 +405,23 @@ public class Configurator {
                 CoordinatorUtil.endCoordination(coordination);
             }
         }
+        if ( !retry ) {
+            // check whether there is a stale config admin bundle id
+            boolean changed = false;
+            for(final Long bundleId : this.state.getBundleIdsUsingConfigAdmin()) {
+                if ( this.state.getLastModified(bundleId) == null ) {
+                    this.state.removeConfigAdminBundleId(bundleId);
+                    changed = true;
+                }
+            }
+            if ( changed ) {
+                try {
+                    State.writeState(this.bundleContext.getDataFile(State.FILE_NAME), state);
+                } catch ( final IOException ioe) {
+                    SystemLogger.error("Unable to persist state to " + State.FILE_NAME, ioe);
+                }
+            }
+        }
     }
 
     /**

Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/State.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/State.java?rev=1802546&r1=1802545&r2=1802546&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/State.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/State.java
Fri Jul 21 06:24:45 2017
@@ -26,6 +26,7 @@ import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
@@ -170,4 +171,8 @@ public class State extends AbstractState
         return "State [bundlesLastModified=" + bundlesLastModified +
                 ", initialHashes=" + initialHashes + "]";
     }
+
+    public Set<Long> getBundleIdsUsingConfigAdmin() {
+        return new HashSet<>(this.bundlesConfigAdminBundleId.keySet());
+    }
 }



Mime
View raw message