FELIX-1472: better handling of configurations from the deployer and features service

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@803185 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/deployer/filemonitor/src/main/java/org/apache/felix/karaf/deployer/filemonitor/FileMonitor.java b/karaf/deployer/filemonitor/src/main/java/org/apache/felix/karaf/deployer/filemonitor/FileMonitor.java
index f12ac05..e3a18c8 100644
--- a/karaf/deployer/filemonitor/src/main/java/org/apache/felix/karaf/deployer/filemonitor/FileMonitor.java
+++ b/karaf/deployer/filemonitor/src/main/java/org/apache/felix/karaf/deployer/filemonitor/FileMonitor.java
@@ -76,7 +76,7 @@
     public final static String SCAN_INTERVAL = "org.apache.felix.karaf.filemonitor.scanInterval";
     public final static String PREFERENCE_KEY = "FileMonitor";
 
-    protected static final String ALIAS_KEY = "_alias_factory_pid";
+    protected static final String FILENAME = "org.apache.felix.karaf.filemonitor.filename";
 
     private static final Log LOGGER = LogFactory.getLog(FileMonitor.class);
 
@@ -447,10 +447,7 @@
                 String[] pid = parsePid(file);
                 Hashtable<Object, Object> hashtable = new Hashtable<Object, Object>();
                 hashtable.putAll(properties);
-                if (pid[1] != null) {
-                    hashtable.put(ALIAS_KEY, pid[1]);
-                }
-
+                hashtable.put(FILENAME, file.getName());
                 Configuration config = getConfiguration(pid[0], pid[1]);
                 if (config.getBundleLocation() != null) {
                     config.setBundleLocation(null);
@@ -484,18 +481,32 @@
         config.delete();
     }
 
-    protected Configuration getConfiguration(String pid, String factoryPid) throws IOException,
-        InvalidSyntaxException {
-        ConfigurationAdmin configurationAdmin = activator.getConfigurationAdmin();
-        if (factoryPid != null) {
-            Configuration[] configs = configurationAdmin.listConfigurations("(" + ALIAS_KEY + "=" + factoryPid + ")");
-            if (configs == null || configs.length == 0) {
+    protected Configuration getConfiguration(String pid, String factoryPid) throws IOException, InvalidSyntaxException {
+        Configuration oldConfiguration = findExistingConfiguration(pid, factoryPid);
+        if (oldConfiguration != null) {
+            return oldConfiguration;
+        } else {
+            ConfigurationAdmin configurationAdmin = activator.getConfigurationAdmin();
+            if (factoryPid != null) {
                 return configurationAdmin.createFactoryConfiguration(pid, null);
             } else {
-                return configs[0];
+                return configurationAdmin.getConfiguration(pid, null);
             }
-        } else {
-            return configurationAdmin.getConfiguration(pid, null);
+        }
+    }
+
+    protected Configuration findExistingConfiguration(String pid, String factoryPid) throws IOException, InvalidSyntaxException {
+        String suffix = factoryPid == null ? ".cfg" : "-" + factoryPid + ".cfg";
+        ConfigurationAdmin cm = activator.getConfigurationAdmin();
+        String filter = "(" + FILENAME + "=" + pid + suffix + ")";
+        Configuration[] configurations = cm.listConfigurations(filter);
+        if (configurations != null && configurations.length > 0)
+        {
+            return configurations[0];
+        }
+        else
+        {
+            return null;
         }
     }
 
diff --git a/karaf/features/core/src/main/java/org/apache/felix/karaf/features/internal/FeaturesServiceImpl.java b/karaf/features/core/src/main/java/org/apache/felix/karaf/features/internal/FeaturesServiceImpl.java
index a0d3dd5..2a288ac 100644
--- a/karaf/features/core/src/main/java/org/apache/felix/karaf/features/internal/FeaturesServiceImpl.java
+++ b/karaf/features/core/src/main/java/org/apache/felix/karaf/features/internal/FeaturesServiceImpl.java
@@ -66,7 +66,7 @@
  */
 public class FeaturesServiceImpl implements FeaturesService {
 
-    private static final String ALIAS_KEY = "_alias_factory_pid";
+    public static final String CONFIG_KEY = "org.apache.felix.karaf.features.configKey";
 
     private static final Logger LOGGER = LoggerFactory.getLogger(FeaturesServiceImpl.class);
 
@@ -197,9 +197,8 @@
         for (String config : f.getConfigurations().keySet()) {
             Dictionary<String,String> props = new Hashtable<String, String>(f.getConfigurations().get(config));
             String[] pid = parsePid(config);
-            if (pid[1] != null) {
-                props.put(ALIAS_KEY, pid[1]);
-            }
+            String key = (pid[1] == null ? pid[0] : pid[0] + "-" + pid[1]);
+            props.put(CONFIG_KEY, key);
             Configuration cfg = getConfiguration(configAdmin, pid[0], pid[1]);
             if (cfg.getBundleLocation() != null) {
                 cfg.setBundleLocation(null);
@@ -432,15 +431,30 @@
 
     protected Configuration getConfiguration(ConfigurationAdmin configurationAdmin,
                                              String pid, String factoryPid) throws IOException, InvalidSyntaxException {
-        if (factoryPid != null) {
-            Configuration[] configs = configurationAdmin.listConfigurations("(" + ALIAS_KEY + "=" + factoryPid + ")");
-            if (configs == null || configs.length == 0) {
+        Configuration oldConfiguration = findExistingConfiguration(configurationAdmin, pid, factoryPid);
+        if (oldConfiguration != null) {
+            return oldConfiguration;
+        } else {
+            if (factoryPid != null) {
                 return configurationAdmin.createFactoryConfiguration(pid, null);
             } else {
-                return configs[0];
+                return configurationAdmin.getConfiguration(pid, null);
             }
-        } else {
-            return configurationAdmin.getConfiguration(pid, null);
+        }
+    }
+
+    protected Configuration findExistingConfiguration(ConfigurationAdmin configurationAdmin,
+                                                      String pid, String factoryPid) throws IOException, InvalidSyntaxException {
+        String key = (factoryPid == null ? pid : pid + "-" + factoryPid);
+        String filter = "(" + CONFIG_KEY + "=" + key + ")";
+        Configuration[] configurations = configurationAdmin.listConfigurations(filter);
+        if (configurations != null && configurations.length > 0)
+        {
+            return configurations[0];
+        }
+        else
+        {
+            return null;
         }
     }