SLING-8523 : Duplicate OSGI configs after upgrade
Upgrade resulting in duplicate factory configs for cases where alais is null
diff --git a/src/main/java/org/apache/sling/installer/factories/configuration/impl/ConfigUpdateHandler.java b/src/main/java/org/apache/sling/installer/factories/configuration/impl/ConfigUpdateHandler.java
index b011329..9544cb6 100644
--- a/src/main/java/org/apache/sling/installer/factories/configuration/impl/ConfigUpdateHandler.java
+++ b/src/main/java/org/apache/sling/installer/factories/configuration/impl/ConfigUpdateHandler.java
@@ -77,7 +77,8 @@
     private void update(final UpdatableResourceGroup group) {
         if ( this.activator.isActive() ) {
             // check if the group handles configurations and has an alias (aka factory config)
-            if ( InstallableResource.TYPE_CONFIG.equals(group.getResourceType()) && group.getAlias() != null ) {
+            if ( InstallableResource.TYPE_CONFIG.equals(group.getResourceType()) && ( group.getAlias() != null || group.getId().contains("-") )) {
+                this.logger.debug("Configuration going under updation is : {} with alias : {}", group.getId(), group.getAlias());
                 this.updateFactoryConfig(group);
             }
         }
@@ -85,19 +86,48 @@
 
     protected String[] getFactoryPidAndPid(final String alias, final String oldId) {
         int pos = 0;
-        while ( alias.charAt(pos) == oldId.charAt(pos) ) {
-            pos++;
-        }
-        while (alias.charAt(pos - 1) != '.') {
-            pos--;
-        }
+        String factoryPid;
+        String pid;
+        if(alias != null) {
 
-        final String factoryPid = alias.substring(0, pos - 1);
-        final String pid = oldId.substring(factoryPid.length() + 1);
+            while (alias.charAt(pos) == oldId.charAt(pos)) {
+                pos++;
+            }
+            while (alias.charAt(pos - 1) != '.') {
+                pos--;
+            }
+
+            factoryPid = alias.substring(0, pos - 1);
+            pid = oldId.substring(factoryPid.length() + 1);
+        } else {
+            // extract factory id for these cases where alias is not available and factoryId and pid need to be separated from the old id string itself
+            //format assumption ::: "factory_pid.factory_pid.pid"
+            // split pid with lastIndexOf('.') then remove the duplicate factory_pid part from the remaining string using the middle dot split index
+
+            String factoryIdString = oldId.substring(0,oldId.lastIndexOf('.')+1); // keep it +1 to have last dot intact so that we always have even dots in the string
+            factoryPid = oldId.substring(0,getMiddleDotSplitIndex(factoryIdString,'.'));
+            pid = oldId.substring(oldId.lastIndexOf('.')+1,oldId.length());
+
+        }
 
         return new String[] { factoryPid, pid };
     }
 
+    private int getMiddleDotSplitIndex(final String strId, char dot){
+
+        int dotCount = 0;
+        int[] dotIndexArray = new int[strId.length()];
+
+        for (int i=0;i<strId.length();i++)
+
+            if (strId.charAt(i)==dot) {
+                dotCount++;
+                dotIndexArray[dotCount] = i;
+            }
+
+        return dotIndexArray[dotCount/2]; // get the middle dot index
+    }
+
     private void updateFactoryConfig(final UpdatableResourceGroup group) {
         final String alias = group.getAlias();
         final String oldId = group.getId();