FELIX-1718: karaf shell config:update command should persist / override configurations from the etc/ folder

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@826734 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/shell/config/src/main/java/org/apache/felix/karaf/shell/config/UpdateCommand.java b/karaf/shell/config/src/main/java/org/apache/felix/karaf/shell/config/UpdateCommand.java
index f32c053..9da60a2 100644
--- a/karaf/shell/config/src/main/java/org/apache/felix/karaf/shell/config/UpdateCommand.java
+++ b/karaf/shell/config/src/main/java/org/apache/felix/karaf/shell/config/UpdateCommand.java
@@ -16,8 +16,13 @@
  */
 package org.apache.felix.karaf.shell.config;
 
+import java.io.File;
+import java.io.FileOutputStream;
 import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Properties;
 
+import org.apache.felix.gogo.commands.Option;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 import org.apache.felix.gogo.commands.Command;
@@ -25,10 +30,41 @@
 @Command(scope = "config", name = "update", description = "Save and propagate changes from the configuration being edited.")
 public class UpdateCommand extends ConfigCommandSupport {
 
+    @Option(name = "-b", aliases = { "--bypass-storage" }, multiValued = false, required = false, description = "Do not store the configuration in a properties file, but feed it directly to ConfigAdmin")
+    private boolean bypassStorage;
+
+    private File storage;
+
+    public File getStorage() {
+        return storage;
+    }
+
+    public void setStorage(File storage) {
+        this.storage = storage;
+    }
+
     protected void doExecute(ConfigurationAdmin admin) throws Exception {
         Dictionary props = getEditedProps();
         if (props == null) {
             System.err.println("No configuration is being edited. Run the edit command first");
+        } else if (!bypassStorage && storage != null) {
+            Properties p = new Properties();
+            for (Enumeration keys = props.keys(); keys.hasMoreElements();) {
+                Object key = keys.nextElement();
+                if (!"service.pid".equals(key) && !"felix.fileinstall.filename".equals(key)) {
+                    p.put(key, props.get(key));
+                }
+            }
+            storage.mkdirs();
+            String pid = (String) this.session.get(PROPERTY_CONFIG_PID);
+            FileOutputStream os = new FileOutputStream(new File(storage, pid + ".cfg"));
+            try {
+                p.store(os, null);
+            } finally {
+                os.close();
+            }
+            this.session.put(PROPERTY_CONFIG_PID, null);
+            this.session.put(PROPERTY_CONFIG_PROPS, null);
         } else {
             String pid = (String) this.session.get(PROPERTY_CONFIG_PID);
             Configuration cfg = admin.getConfiguration(pid, null);
diff --git a/karaf/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/karaf/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 1c04063..e04df53 100644
--- a/karaf/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/karaf/shell/config/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -18,6 +18,8 @@
 
 -->
 <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:cm="http://geronimo.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
+           xmlns:ext="http://geronimo.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
            default-activation="lazy">
 
     <command-bundle xmlns="http://felix.apache.org/karaf/xmlns/shell/v1.0.0">
@@ -59,7 +61,9 @@
             </completers>
         </command>
         <command name="config/update">
-            <action class="org.apache.felix.karaf.shell.config.UpdateCommand"/>
+            <action class="org.apache.felix.karaf.shell.config.UpdateCommand">
+                <property name="storage" value="${storage}" />
+            </action>
         </command>
     </command-bundle>
 
@@ -72,5 +76,12 @@
 
     <reference id="configAdmin" interface="org.osgi.service.cm.ConfigurationAdmin"  />
 
+    <ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]" />
+
+    <cm:property-placeholder persistent-id="org.apache.felix.karaf.shell.config">
+        <cm:default-properties>
+            <cm:property name="storage" value="$[karaf.base]/etc/"/>
+        </cm:default-properties>
+    </cm:property-placeholder>
 
 </blueprint>