Author: jbonofre
Date: Wed Sep 21 19:35:38 2011
New Revision: 1173805

URL: http://svn.apache.org/viewvc?rev=1173805&view=rev
Log:
[KARAF-786] Add support of cfg file writing in the Config MBean

Modified:
    karaf/trunk/management/mbeans/config/pom.xml
    
karaf/trunk/management/mbeans/config/src/main/java/org/apache/karaf/management/mbeans/config/ConfigMBean.java
    
karaf/trunk/management/mbeans/config/src/main/java/org/apache/karaf/management/mbeans/config/internal/ConfigMBeanImpl.java
    
karaf/trunk/management/mbeans/config/src/main/resources/OSGI-INF/blueprint/config-management.xml

Modified: karaf/trunk/management/mbeans/config/pom.xml
URL: 
http://svn.apache.org/viewvc/karaf/trunk/management/mbeans/config/pom.xml?rev=1173805&r1=1173804&r2=1173805&view=diff
==============================================================================
--- karaf/trunk/management/mbeans/config/pom.xml (original)
+++ karaf/trunk/management/mbeans/config/pom.xml Wed Sep 21 19:35:38 2011
@@ -46,6 +46,10 @@
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.utils</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

Modified: 
karaf/trunk/management/mbeans/config/src/main/java/org/apache/karaf/management/mbeans/config/ConfigMBean.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/management/mbeans/config/src/main/java/org/apache/karaf/management/mbeans/config/ConfigMBean.java?rev=1173805&r1=1173804&r2=1173805&view=diff
==============================================================================
--- 
karaf/trunk/management/mbeans/config/src/main/java/org/apache/karaf/management/mbeans/config/ConfigMBean.java
 (original)
+++ 
karaf/trunk/management/mbeans/config/src/main/java/org/apache/karaf/management/mbeans/config/ConfigMBean.java
 Wed Sep 21 19:35:38 2011
@@ -52,9 +52,10 @@ public interface ConfigMBean {
      *
      * @param pid the configuration PID.
      * @param key the property key.
+     * @param bypassStorage doesn't flush the change on the storage config 
file.
      * @throws Exception
      */
-    void propdel(String pid, String key) throws Exception;
+    void propdel(String pid, String key, boolean bypassStorage) throws 
Exception;
 
     /**
      * Append (or add) a value for the given configuration key.
@@ -62,9 +63,10 @@ public interface ConfigMBean {
      * @param pid the configuration PID.
      * @param key the property key.
      * @param value the value to append to the current property value.
+     * @param bypassStorage doesn't flush the change on the storage config 
file.
      * @throws Exception
      */
-    void propappend(String pid, String key, String value) throws Exception;
+    void propappend(String pid, String key, String value, boolean 
bypassStorage) throws Exception;
 
     /**
      * Set a configuration property.
@@ -72,8 +74,9 @@ public interface ConfigMBean {
      * @param pid the configuration PID.
      * @param key the property key.
      * @param value the property value.
+     * @param bypassStorage doesn't flush the change on the storage config 
file.
      * @throws Exception
      */
-    void propset(String pid, String key, String value) throws Exception;
+    void propset(String pid, String key, String value, boolean bypassStorage) 
throws Exception;
 
 }

Modified: 
karaf/trunk/management/mbeans/config/src/main/java/org/apache/karaf/management/mbeans/config/internal/ConfigMBeanImpl.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/management/mbeans/config/src/main/java/org/apache/karaf/management/mbeans/config/internal/ConfigMBeanImpl.java?rev=1173805&r1=1173804&r2=1173805&view=diff
==============================================================================
--- 
karaf/trunk/management/mbeans/config/src/main/java/org/apache/karaf/management/mbeans/config/internal/ConfigMBeanImpl.java
 (original)
+++ 
karaf/trunk/management/mbeans/config/src/main/java/org/apache/karaf/management/mbeans/config/internal/ConfigMBeanImpl.java
 Wed Sep 21 19:35:38 2011
@@ -13,20 +13,31 @@
  */
 package org.apache.karaf.management.mbeans.config.internal;
 
+import org.apache.felix.utils.properties.Properties;
 import org.apache.karaf.management.mbeans.config.ConfigMBean;
+import org.osgi.framework.Constants;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 
 import javax.management.NotCompliantMBeanException;
 import javax.management.StandardMBean;
-import java.util.*;
+import java.io.File;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * Implementation of the ConfigMBean.
  */
 public class ConfigMBeanImpl extends StandardMBean implements ConfigMBean {
 
+    private final String FELIX_FILEINSTALL_FILENAME = 
"felix.fileinstall.filename";
+
     private ConfigurationAdmin configurationAdmin;
+    private File storage;
 
     public ConfigurationAdmin getConfigurationAdmin() {
         return this.configurationAdmin;
@@ -36,6 +47,14 @@ public class ConfigMBeanImpl extends Sta
         this.configurationAdmin = configurationAdmin;
     }
 
+    public File getStorage() {
+        return this.storage;
+    }
+
+    public void setStorage(File storage) {
+        this.storage = storage;
+    }
+
     public ConfigMBeanImpl() throws NotCompliantMBeanException {
         super(ConfigMBean.class);
     }
@@ -72,16 +91,17 @@ public class ConfigMBeanImpl extends Sta
         return propertiesMap;
     }
 
-    public void propdel(String pid, String key) throws Exception {
+    public void propdel(String pid, String key, boolean bypassStorage) throws 
Exception {
         Configuration configuration = configurationAdmin.getConfiguration(pid);
         if (configuration == null) {
             throw new IllegalArgumentException("Configuration PID " + pid + " 
doesn't exist");
         }
         Dictionary dictionary = configuration.getProperties();
         dictionary.remove(key);
+        store(pid, dictionary, bypassStorage);
     }
 
-    public void propappend(String pid, String key, String value) throws 
Exception {
+    public void propappend(String pid, String key, String value, boolean 
bypassStorage) throws Exception {
         Configuration configuration = configurationAdmin.getConfiguration(pid);
         if (configuration == null) {
             throw new IllegalArgumentException("Configuration PID " + pid + " 
doesn't exist");
@@ -95,15 +115,74 @@ public class ConfigMBeanImpl extends Sta
         } else {
             throw new IllegalStateException("Current value is not a String");
         }
+        store(pid, dictionary, bypassStorage);
     }
 
-    public void propset(String pid, String key, String value) throws Exception 
{
+    public void propset(String pid, String key, String value, boolean 
bypassStorage) throws Exception {
         Configuration configuration = configurationAdmin.getConfiguration(pid);
         if (configuration == null) {
             throw new IllegalArgumentException("Configuration PID " + pid + " 
doesn't exist");
         }
         Dictionary dictionary = configuration.getProperties();
         dictionary.put(key, value);
+        store(pid, dictionary, bypassStorage);
+    }
+
+    /**
+     * Store/flush a configuration PID into the configuration file.
+     *
+     * @param pid        the configuration PID.
+     * @param properties the configuration properties.
+     * @throws Exception
+     */
+    private void store(String pid, Dictionary properties, boolean 
bypassStorage) throws Exception {
+        if (!bypassStorage && storage != null) {
+            File storageFile = new File(storage, pid + ".cfg");
+            Configuration configuration = 
configurationAdmin.getConfiguration(pid, null);
+            if (configuration != null && configuration.getProperties() != 
null) {
+                Object val = 
configuration.getProperties().get(FELIX_FILEINSTALL_FILENAME);
+                if (val instanceof String) {
+                    if (((String) val).startsWith("file:")) {
+                        val = ((String) val).substring("file:".length());
+                    }
+                    storageFile = new File((String) val);
+                }
+            }
+            Properties p = new Properties(storageFile);
+            for (Enumeration keys = properties.keys(); keys.hasMoreElements(); 
) {
+                Object key = keys.nextElement();
+                if (!Constants.SERVICE_PID.equals(key)
+                        && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key)
+                        && !FELIX_FILEINSTALL_FILENAME.equals(key)) {
+                    p.put((String) key, (String) properties.get(key));
+                }
+            }
+            storage.mkdirs();
+            p.save();
+        } else {
+            Configuration cfg = configurationAdmin.getConfiguration(pid, null);
+            if (cfg.getProperties() == null) {
+                String[] pids = parsePid(pid);
+                if (pids[1] != null) {
+                    cfg = 
configurationAdmin.createFactoryConfiguration(pids[0], null);
+                }
+            }
+            if (cfg.getBundleLocation() != null) {
+                cfg.setBundleLocation(null);
+            }
+            cfg.update(properties);
+        }
+    }
+
+    private String[] parsePid(String pid) {
+        int n = pid.indexOf('-');
+        if (n > 0) {
+            String factoryPid = pid.substring(n + 1);
+            pid = pid.substring(0, n);
+            return new String[] { pid, factoryPid };
+        } else {
+            return new String[] { pid, null };
+        }
     }
 
 }

Modified: 
karaf/trunk/management/mbeans/config/src/main/resources/OSGI-INF/blueprint/config-management.xml
URL: 
http://svn.apache.org/viewvc/karaf/trunk/management/mbeans/config/src/main/resources/OSGI-INF/blueprint/config-management.xml?rev=1173805&r1=1173804&r2=1173805&view=diff
==============================================================================
--- 
karaf/trunk/management/mbeans/config/src/main/resources/OSGI-INF/blueprint/config-management.xml
 (original)
+++ 
karaf/trunk/management/mbeans/config/src/main/resources/OSGI-INF/blueprint/config-management.xml
 Wed Sep 21 19:35:38 2011
@@ -12,7 +12,10 @@
    See the License for the specific language governing permissions and
    limitations under the License.
 -->
-<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0";>
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0";
+           
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0";
+           
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0";
+           default-activation="lazy">
 
     <!-- Reference to the MBean server -->
     <reference id="mbeanServer" interface="javax.management.MBeanServer">
@@ -25,6 +28,7 @@
     <!-- Config MBean -->
     <bean id="configMBean" 
class="org.apache.karaf.management.mbeans.config.internal.ConfigMBeanImpl">
         <property name="configurationAdmin" ref="configurationAdmin"/>
+        <property name="storage" value="${storage}"/>
     </bean>
 
     <!-- Register MBean in the MBeanServer -->
@@ -36,4 +40,12 @@
         </property>
     </bean>
 
+    <ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]" />
+
+    <cm:property-placeholder persistent-id="org.apache.karaf.shell.config">
+        <cm:default-properties>
+            <cm:property name="storage" value="$[karaf.base]/etc/"/>
+        </cm:default-properties>
+    </cm:property-placeholder>
+
 </blueprint>
\ No newline at end of file


Reply via email to