TAMAYA-297: Removed OSGIConfigMapper to reduce complexity. Added restore 
functionality for backups.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/2281cb3b
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/2281cb3b
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/2281cb3b

Branch: refs/heads/master
Commit: 2281cb3b60ea8fbf1740a20b6ab706ab2e0a757f
Parents: dab1da7
Author: anatole <[email protected]>
Authored: Sun Sep 24 21:41:55 2017 +0200
Committer: anatole <[email protected]>
Committed: Sun Sep 24 21:41:55 2017 +0200

----------------------------------------------------------------------
 .../org/apache/tamaya/osgi/ConfigChanger.java   | 163 +++++++++++--------
 .../apache/tamaya/osgi/TamayaConfigPlugin.java  | 140 +++++++++++-----
 .../tamaya/osgi/commands/ConfigCommands.java    |  90 +++++++---
 3 files changed, 259 insertions(+), 134 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2281cb3b/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigChanger.java
----------------------------------------------------------------------
diff --git 
a/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigChanger.java 
b/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigChanger.java
index f0ecdfb..71fe18b 100644
--- a/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigChanger.java
+++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/ConfigChanger.java
@@ -18,13 +18,22 @@
  */
 package org.apache.tamaya.osgi;
 
+import org.apache.tamaya.ConfigurationProvider;
+import org.apache.tamaya.functions.ConfigurationFunctions;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
 
-import java.util.*;
+import java.io.IOException;
+import java.util.Date;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Objects;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -34,7 +43,8 @@ import java.util.logging.Logger;
 final class ConfigChanger {
 
     private static final Logger LOG = 
Logger.getLogger(TamayaConfigPlugin.class.getName());
-    private static final OSGIConfigMapper DEFAULT_CONFIG_MAPPER = new 
DefaultOSGIConfigMapper();
+    private static final String TAMAYA_OPERATION_MODE = "tamaya-operationmode";
+    private static final String TAMAYA_ROOT = "tamaya-root";
 
     private BundleContext context;
     private ConfigurationAdmin cm;
@@ -53,55 +63,82 @@ final class ConfigChanger {
         return cm;
     }
 
-    public void configure(String pid, Bundle bundle, OperationMode 
defaultOpMode) {
-        OperationMode opMode = Objects.requireNonNull(defaultOpMode);
-        if(bundle!=null) {
-            String opVal = bundle.getHeaders().get("Tamaya-OperationMode");
-            if (opVal != null) {
-                opMode = OperationMode.valueOf(opVal.toUpperCase());
-            }
-        }
-        LOG.finest("Evaluating Tamaya Config for PID: " + pid);
-        if(bundle!=null) {
-            ConfigHistory.configuring(pid, "bundle=" + 
bundle.getSymbolicName() + ", bundle-id=" + bundle.getBundleId() + ", 
operationMode=" + opMode);
-        }else{
-            ConfigHistory.configuring(pid, "trigger=Tamaya, operationMode=" + 
opMode);
-        }
-        org.apache.tamaya.Configuration tamayaConfig = 
configMapper().getConfiguration(pid);
-        if (tamayaConfig == null) {
-            LOG.finest("No Tamaya configuration for PID: " + pid);
-            return;
-        }
+    public Dictionary<String, Object> configure(String pid, Bundle bundle, 
OperationMode operationMode, boolean opModeExplicit, boolean dryRun) {
         try {
+            String root = '[' + pid + ']';
             // TODO Check for Bundle.getLocation() usage here...
             Configuration osgiConfig = cm.getConfiguration(pid, 
bundle!=null?bundle.getLocation():null);
+            OperationMode opMode = Objects.requireNonNull(operationMode);
+            // Check manifest config
+            if(bundle!=null) {
+                if(!opModeExplicit) {
+                    String opVal = 
bundle.getHeaders().get(TAMAYA_OPERATION_MODE);
+                    if (opVal != null) {
+                        opMode = OperationMode.valueOf(opVal.toUpperCase());
+                    }
+                }
+                String customRoot = bundle.getHeaders().get(TAMAYA_ROOT);
+                if(customRoot!=null){
+                    root = customRoot;
+                }
+            }
+            // Check for dynamic OSGI overrides
             if(osgiConfig!=null){
-                Dictionary<String, Object> dictionary = 
osgiConfig.getProperties();
-                if(dictionary==null){
-                    dictionary = new Hashtable<>();
-                }else{
-                    if(!InitialState.contains(pid)){
-                        InitialState.set(pid, dictionary);
+                Dictionary<String,Object> props = osgiConfig.getProperties();
+                if(props!=null){
+                    if(!opModeExplicit) {
+                        String opVal = (String) 
props.get(TAMAYA_OPERATION_MODE);
+                        if (opVal != null) {
+                            opMode = 
OperationMode.valueOf(opVal.toUpperCase());
+                        }
                     }
+                    String customRoot = (String)props.get(TAMAYA_ROOT);
+                    if(customRoot!=null){
+                        root = customRoot;
+                    }
+                }else{
+                    props = new Hashtable<>();
                 }
-                modifyConfiguration(pid, tamayaConfig, dictionary, opMode);
-                if(!dictionary.isEmpty()) {
-                    osgiConfig.update(dictionary);
-                    LOG.info("Updated configuration for PID: " + pid + ": " + 
dictionary);
+                if(!dryRun && !Backups.contains(pid)){
+                    Backups.set(pid, props);
                 }
+                LOG.finest("Evaluating Tamaya Config for PID: " + pid);
+                org.apache.tamaya.Configuration tamayaConfig = 
getTamayaConfiguration(root);
+                if (tamayaConfig == null) {
+                    LOG.finest("No Tamaya configuration for root: " + root);
+                }else {
+                    if(dryRun){
+                        modifyConfiguration(pid, tamayaConfig, props, opMode);
+                    }else {
+                        try {
+                            if (bundle != null) {
+                                ConfigHistory.configuring(pid, "bundle=" + 
bundle.getSymbolicName() + ", opMode=" + opMode);
+                            } else {
+                                ConfigHistory.configuring(pid, 
"trigger=Tamaya, opMode=" + opMode);
+                            }
+                            modifyConfiguration(pid, tamayaConfig, props, 
opMode);
+                            if (!props.isEmpty()) {
+                                osgiConfig.update(props);
+                                LOG.info("Updated configuration for PID: " + 
pid + ": " + props);
+                                ConfigHistory.configured(pid, "SUCCESS");
+                            }
+                        }catch(Exception e){
+                            LOG.log(Level.WARNING, "Failed to update 
configuration for PID: " + pid, e);
+                            ConfigHistory.configured(pid, "FAILED: " + e);
+                        }
+                    }
+                }
+                return props;
             }
-            ConfigHistory.configured(pid, "SUCCESS");
+            return null;
         } catch (Exception e) {
             LOG.log(Level.WARNING, "Failed to initialize configuration for 
PID: " + pid, e);
-            ConfigHistory.configured(pid, "FAILED: " + e);
+            return null;
         }
-
     }
 
     public void modifyConfiguration(String pid, 
org.apache.tamaya.Configuration config, Dictionary<String, Object> dictionary, 
OperationMode opMode) {
         LOG.info(() -> "Updating configuration for PID: " + pid + "...");
-        dictionary.put("tamaya.opMode", opMode.toString());
-        ConfigHistory.propertySet(pid, "tamaya.opMode", opMode.toString(), 
null);
         dictionary.put("tamaya.modified.at", new Date().toString());
         ConfigHistory.propertySet(pid, "tamaya.modified.at", 
dictionary.get("tamaya.modified.at"), null);
 
@@ -114,27 +151,30 @@ final class ConfigChanger {
         }
         for (Map.Entry<String, Object> dictEntry : dictionaryMap.entrySet()) {
             Object configuredValue = config.getOrDefault(dictEntry.getKey(), 
dictEntry.getValue().getClass(), null);
+            if (configuredValue != null) {
+                if(configuredValue.equals(dictEntry.getValue())){
+                    continue;
+                }
+            }
             switch (opMode) {
                 case EXTEND:
                     break;
                 case OVERRIDE:
-                    if (configuredValue != null) {
-                        LOG.info(() -> "Setting key " + dictEntry.getKey() + " 
to " + configuredValue);
-                        ConfigHistory.propertySet(pid,dictEntry.getKey(), 
configuredValue, dictEntry.getValue());
-                        dictionary.put(dictEntry.getKey(), configuredValue);
-                    }
+                    LOG.info(() -> "Setting key " + dictEntry.getKey() + " to 
" + configuredValue);
+                    ConfigHistory.propertySet(pid,dictEntry.getKey(), 
configuredValue, dictEntry.getValue());
+                    dictionary.put(dictEntry.getKey(), configuredValue);
                     break;
                 case UPDATE_ONLY:
-                    if (configuredValue != null) {
-                        LOG.info(() -> "Setting key " + dictEntry.getKey() + " 
to " + configuredValue);
-                        ConfigHistory.propertySet(pid,dictEntry.getKey(), 
configuredValue, dictEntry.getValue());
-                        dictionary.put(dictEntry.getKey(), configuredValue);
-
-                    }
+                    LOG.info(() -> "Setting key " + dictEntry.getKey() + " to 
" + configuredValue);
+                    ConfigHistory.propertySet(pid,dictEntry.getKey(), 
configuredValue, dictEntry.getValue());
+                    dictionary.put(dictEntry.getKey(), configuredValue);
             }
         }
         for (Map.Entry<String, String> configEntry : 
config.getProperties().entrySet()) {
             Object dictValue = dictionary.get(configEntry.getKey());
+            if(dictValue.equals(configEntry.getValue())){
+                continue;
+            }
             switch (opMode) {
                 case EXTEND:
                     if(dictValue==null){
@@ -159,26 +199,19 @@ final class ConfigChanger {
         }
     }
 
-    /**
-     * Loads the configuration toor mapper using the OSGIConfigRootMapper OSGI 
service resolving mechanism. If no
-     * such service is available it loads the default mapper.
-     * @return the mapper to be used, bever null.
-     */
-    private OSGIConfigMapper configMapper() {
-        OSGIConfigMapper mapper = null;
-        if(context!=null) {
-            ServiceReference<OSGIConfigMapper> ref = 
context.getServiceReference(OSGIConfigMapper.class);
-            if (ref != null) {
-                mapper = context.getService(ref);
-            }
+    public org.apache.tamaya.Configuration getTamayaConfiguration(String root) 
{
+        if (root != null) {
+            return ConfigurationProvider.getConfiguration()
+                    .with(ConfigurationFunctions.section(root, true));
         }
-        if(mapper==null){
-            return DEFAULT_CONFIG_MAPPER;
-        }
-        return mapper;
+        return null;
     }
 
-    public org.apache.tamaya.Configuration getTamayaConfiguration(String pid) {
-        return configMapper().getConfiguration(pid);
+    public void restoreBackup(String pid, Dictionary<String, Object> 
config)throws IOException{
+        Configuration osgiConfig = cm.getConfiguration(pid);
+        if(osgiConfig!=null){
+            config.put(TamayaConfigPlugin.TAMAYA_ENABLED, "false");
+            osgiConfig.update(Objects.requireNonNull(config));
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2281cb3b/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java
----------------------------------------------------------------------
diff --git 
a/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java 
b/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java
index a4ed082..e1aa4a5 100644
--- a/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java
+++ b/osgi/common/src/main/java/org/apache/tamaya/osgi/TamayaConfigPlugin.java
@@ -24,6 +24,7 @@ import org.osgi.service.cm.ConfigurationAdmin;
 
 import java.io.IOException;
 import java.util.Dictionary;
+import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Objects;
 import java.util.logging.Level;
@@ -37,12 +38,10 @@ public class TamayaConfigPlugin implements BundleListener, 
ServiceListener{
     static final String COMPONENTID = "TamayaConfigPlugin";
     /** the logger. */
     private static final Logger LOG = 
Logger.getLogger(TamayaConfigPlugin.class.getName());
-    private static final String TAMAYA_DISABLED = "tamaya.disabled";
-    private static final String TAMAYA_AUTO_UPDATE_ENABLED = 
"tamaya.autoUpdateEnabled";
-    public static final String TAMAYA_DISABLED_KEY = "Tamaya-Disabled";
-    public static final String TAMAYA_ENABLED_KEY = "Tamaya-Enabled";
-    public static final String TAMAYA_PID_KEY = "Tamaya-PID";
-    private boolean disabled = false;
+    public static final String TAMAYA_ENABLED = "tamaya-enabled";
+    public static final String TAMAYA_AUTO_UPDATE_ENABLED = 
"tamaya.autoUpdateEnabled";
+    public static final String TAMAYA_ROOT_KEY = "tamaya-root";
+    private boolean enabledByDefault = false;
     private OperationMode defaultOpMode = OperationMode.OVERRIDE;
 
     private ConfigChanger configChanger;
@@ -68,7 +67,7 @@ public class TamayaConfigPlugin implements BundleListener, 
ServiceListener{
      */
     TamayaConfigPlugin(BundleContext context) {
         configChanger = new ConfigChanger(context);
-        InitialState.restore(this);
+        Backups.restore(this);
         ConfigHistory.restore(this);
         initDefaultEnabled();
         initAutoUpdateEnabled();
@@ -81,13 +80,13 @@ public class TamayaConfigPlugin implements BundleListener, 
ServiceListener{
         setConfigValue(TAMAYA_AUTO_UPDATE_ENABLED, enabled);
     }
 
-    public void setDefaultDisabled(boolean disabled){
-        this.disabled = disabled;
-        setConfigValue(TAMAYA_DISABLED, disabled);
+    public void setTamayaEnabledByDefault(boolean enabledByDefault){
+        this.enabledByDefault = enabledByDefault;
+        setConfigValue(TAMAYA_ENABLED, enabledByDefault);
     }
 
-    public boolean isDefaultDisabled(){
-        return disabled;
+    public boolean isTamayaEnabledByDefault(){
+        return enabledByDefault;
     }
 
     public OperationMode getDefaultOperationMode(){
@@ -131,23 +130,36 @@ public class TamayaConfigPlugin implements 
BundleListener, ServiceListener{
             LOG.finest("No service pid for: " + event.getServiceReference());
             return;
         }
-        configChanger.configure(pid, event.getServiceReference().getBundle(), 
defaultOpMode);
-        InitialState.save(this);
+        configChanger.configure(pid, event.getServiceReference().getBundle(), 
defaultOpMode, false, false);
+        Backups.save(this);
         ConfigHistory.save(this);
     }
 
-    public void updateConfig(String pid) {
-        LOG.fine("Updating getConfig for pid...: " + pid);
-        configChanger.configure(pid, null, defaultOpMode);
-        InitialState.save(this);
-        ConfigHistory.save(this);
+    public Dictionary<String,Object> updateConfig(String pid) {
+        return updateConfig(pid, defaultOpMode, false, false);
+    }
+
+    public Dictionary<String,Object> updateConfig(String pid, boolean dryRun) {
+        return updateConfig(pid, defaultOpMode, false, dryRun);
+    }
+
+    public Dictionary<String,Object> updateConfig(String pid, OperationMode 
opMode, boolean explicitMode, boolean dryRun) {
+        if(dryRun){
+            return configChanger.configure(pid, null, opMode, explicitMode, 
true);
+        }else {
+            LOG.fine("Updating getConfig for pid...: " + pid);
+            Dictionary<String,Object> result = configChanger.configure(pid, 
null, opMode, explicitMode, false);
+            Backups.save(this);
+            ConfigHistory.save(this);
+            return result;
+        }
     }
 
     private void configureBundle(Bundle bundle) {
         if(!isBundleEnabled(bundle)){
             return;
         }
-        String tamayaPid = bundle.getHeaders().get(TAMAYA_PID_KEY);
+        String tamayaPid = bundle.getHeaders().get(TAMAYA_ROOT_KEY);
         String pid = tamayaPid!=null?tamayaPid:bundle.getSymbolicName();
         if(pid==null){
             pid = bundle.getLocation();
@@ -156,54 +168,51 @@ public class TamayaConfigPlugin implements 
BundleListener, ServiceListener{
             LOG.finest(() -> "No PID/location for bundle " + 
bundle.getSymbolicName() + '('+bundle.getBundleId()+')');
             return;
         }
-        configChanger.configure(pid, bundle, defaultOpMode);
-        InitialState.save(this);
+        configChanger.configure(pid, bundle, defaultOpMode, false, false);
+        Backups.save(this);
         ConfigHistory.save(this);
     }
 
-
     public boolean isBundleEnabled(Bundle bundle){
         // Optional MANIFEST entries
-        String enabledTamaya = bundle.getHeaders().get(TAMAYA_ENABLED_KEY);
-        String disabledTamaya = bundle.getHeaders().get(TAMAYA_DISABLED_KEY);
-
-        if(Boolean.parseBoolean(disabledTamaya)){
-            LOG.finest("Bundle is disabled for Tamaya: " + 
bundle.getSymbolicName());
+        String bundleEnabledVal = bundle.getHeaders().get(TAMAYA_ENABLED);
+        if(bundleEnabledVal==null && !enabledByDefault){
+            LOG.finest("tamaya.enabled=false: not configuring bundle: " + 
bundle.getSymbolicName());
             return false;
         }
-        if(enabledTamaya != null && !Boolean.parseBoolean(enabledTamaya)){
-            LOG.finest("Bundle is disabled for Tamaya: " + 
bundle.getSymbolicName());
+        if(bundleEnabledVal != null && 
!Boolean.parseBoolean(bundleEnabledVal)){
+            LOG.finest("Bundle is explcitly disabled for Tamaya: " + 
bundle.getSymbolicName());
             return false;
         }
-        if(disabled){
-            LOG.finest("tamaya.disabled=false: not configuring bundle: " + 
bundle.getSymbolicName());
-            return false;
+        if(bundleEnabledVal != null && Boolean.parseBoolean(bundleEnabledVal)){
+            LOG.finest("Bundle is explicitly enabled for Tamaya: " + 
bundle.getSymbolicName());
+            return true;
         }
         return true;
     }
 
     private void initAutoUpdateEnabled() {
-        String enabledVal = (String)getConfigValue(TAMAYA_AUTO_UPDATE_ENABLED);
+        Object enabledVal = getConfigValue(TAMAYA_AUTO_UPDATE_ENABLED);
         if(enabledVal!=null){
-            this.autoUpdateEnabled = Boolean.parseBoolean(enabledVal);
+            this.autoUpdateEnabled = 
Boolean.parseBoolean(enabledVal.toString());
         }
         if(this.autoUpdateEnabled) {
             LOG.info("Tamaya Automatic Config Updating is enabled.");
         }else{
-            LOG.info("Tamaya Automatic Config Updating is disabled.");
+            LOG.info("Tamaya Automatic Config Updating is enabledByDefault.");
         }
     }
 
     private void initDefaultEnabled() {
-        String disabledVal = (String)getConfigValue(TAMAYA_DISABLED);
-        if(disabledVal==null){
-            disabledVal = System.getProperty(TAMAYA_DISABLED);
+        Object disabledVal = getConfigValue(TAMAYA_ENABLED);
+        if(disabledVal==null && System.getProperty(TAMAYA_ENABLED)!=null){
+            disabledVal = 
Boolean.parseBoolean(System.getProperty(TAMAYA_ENABLED));
         }
         if(disabledVal!=null){
-            this.disabled = Boolean.parseBoolean(disabledVal);
+            this.enabledByDefault = 
Boolean.parseBoolean(disabledVal.toString());
         }
-        if(this.disabled) {
-            LOG.info("Tamaya Config is disabled by default. Add Tamaya-Enabled 
to your bundle manifests to enable it.");
+        if(this.enabledByDefault) {
+            LOG.info("Tamaya Config is enabledByDefault by default. Add 
Tamaya-Enabled to your bundle manifests to enable it.");
         }else{
             LOG.info("Tamaya Config is enabled by default. Add Tamaya-Disabled 
to your bundle manifests to disable it.");
         }
@@ -262,11 +271,52 @@ public class TamayaConfigPlugin implements 
BundleListener, ServiceListener{
     }
 
 
-    public org.apache.tamaya.Configuration getTamayaConfiguration(String pid) {
-        return configChanger.getTamayaConfiguration(pid);
+    public org.apache.tamaya.Configuration getTamayaConfiguration(String root) 
{
+        return configChanger.getTamayaConfiguration(root);
     }
 
     public boolean isAutoUpdateEnabled() {
-        return autoUpdateEnabled;
+        return this.autoUpdateEnabled;
+    }
+
+    public boolean restoreBackup(String pid)throws IOException{
+        Dictionary<String,Object> config = (Dictionary<String,Object>) 
Backups.get(pid);
+        if(config==null){
+            return false;
+        }
+        this.configChanger.restoreBackup(pid, config);
+        return true;
     }
+
+    public Dictionary<String, Object> getOSGIConfiguration(String pid, String 
section) {
+        try {
+            Configuration config = 
configChanger.getConfigurationAdmin().getConfiguration(pid);
+            Dictionary<String, Object> props = null;
+            if (config == null
+                    || config.getProperties() == null) {
+                return null;
+            }
+            props = config.getProperties();
+            if(section!=null){
+                return filter(props, section);
+            }
+            return props;
+        } catch (IOException e) {
+            LOG.log(Level.WARNING, "Error reading OSGI config for PID: " + 
pid, e);
+            return null;
+        }
+    }
+
+    private Dictionary<String, Object> filter(Dictionary<String, Object> 
props, String section) {
+        Hashtable<String, Object> result = new Hashtable<>();
+        Enumeration<String> keys = props.keys();
+        while(keys.hasMoreElements()){
+            String key = keys.nextElement();
+            if(key.startsWith(section)){
+                result.put(key, props.get(key));
+            }
+        }
+        return result;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/2281cb3b/osgi/common/src/main/java/org/apache/tamaya/osgi/commands/ConfigCommands.java
----------------------------------------------------------------------
diff --git 
a/osgi/common/src/main/java/org/apache/tamaya/osgi/commands/ConfigCommands.java 
b/osgi/common/src/main/java/org/apache/tamaya/osgi/commands/ConfigCommands.java
index ae06a18..23ba263 100644
--- 
a/osgi/common/src/main/java/org/apache/tamaya/osgi/commands/ConfigCommands.java
+++ 
b/osgi/common/src/main/java/org/apache/tamaya/osgi/commands/ConfigCommands.java
@@ -29,8 +29,7 @@ import org.apache.tamaya.spi.PropertyValue;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
 
 /**
  * Utility class implementing the available configuration related commands.
@@ -44,35 +43,70 @@ public final class ConfigCommands {
         Configuration config = ConfigurationProvider.getConfiguration();
         return config.toString() + "\n\n"
                 + StringUtil.format("Default OperationMode:", 30) + 
configPlugin.getDefaultOperationMode() + '\n'
-                + StringUtil.format("Default Disabled: ", 30) + 
configPlugin.isDefaultDisabled();
+                + StringUtil.format("Default Disabled: ", 30) + 
configPlugin.isTamayaEnabledByDefault();
     }
 
-    public static String readConfig(String section) {
+    public static String readTamayaConfig(String section, String filter) {
         Configuration config = ConfigurationProvider.getConfiguration();
         if(section!=null){
-            return config
-                    .with(ConfigurationFunctions.section(section))
-                    .query(ConfigurationFunctions.textInfo());
+            config = config
+                    .with(ConfigurationFunctions.section(section, true));
+        }
+        if(filter!=null){
+            config = config.with(ConfigurationFunctions.section(filter, 
false));
         }
         return config.query(ConfigurationFunctions.textInfo());
     }
 
-    public static String readConfig(TamayaConfigPlugin configPlugin, String 
pid, String section) {
-        Configuration config = null;
-        if(pid!=null){
-            config = configPlugin.getTamayaConfiguration(pid);
-            if(config==null){
-                return "No Tamaya Config found for PID: " + pid;
-            }
-        }else {
-            config = ConfigurationProvider.getConfiguration();
+    public static String readTamayaConfig4PID(String pid, String filter) {
+        return readTamayaConfig("["+pid+"]", filter);
+    }
+
+    public static String applyTamayaConfiguration(TamayaConfigPlugin 
configPlugin, String pid, String operationMode, boolean dryRun){
+        Dictionary<String,Object> config = null;
+        if(operationMode!=null){
+            config = configPlugin.updateConfig(pid, 
OperationMode.valueOf(operationMode), true, dryRun);
+            return  "Full configuration\n" +
+                    "------------------\n" +
+                    "PID           : " + pid + "\n" +
+                    "OperationMode : "+ operationMode + "\n" +
+                    "Applied       : " + !dryRun + "\n" +
+                    printOSGIConfig(pid, config);
+        }else{
+            config = configPlugin.updateConfig(pid, dryRun);
+            return  "Full configuration\n" +
+                    "------------------\n" +
+                    "PID           : " + pid + "\n" +
+                    "OperationMode : "+ configPlugin.getDefaultOperationMode() 
+ "\n" +
+                    "Applied       : " + !dryRun + "\n" +
+                    printOSGIConfig(pid, config);
         }
-        if(section!=null){
-            return config
-                    .with(ConfigurationFunctions.section(section))
-                    .query(ConfigurationFunctions.textInfo());
+    }
+
+    public static String readOSGIConfiguration(TamayaConfigPlugin 
configPlugin, String pid, String section) {
+        Dictionary<String,Object> config = 
configPlugin.getOSGIConfiguration(pid, section);
+        return printOSGIConfig(pid, config);
+    }
+
+    private static String printOSGIConfig(String pid, 
Dictionary<String,Object> config){
+        if(config.isEmpty()){
+            return "No Config present for PID: " + pid;
         }
-        return config.query(ConfigurationFunctions.textInfo());
+        StringBuilder b = new StringBuilder();
+        b.append("OSGI Configuration for PID: " + pid);
+        b.append("------------------------------------------------");
+        TreeMap<String,String> result = new TreeMap<>();
+        Enumeration<String> keys = config.keys();
+        while(keys.hasMoreElements()){
+            String key = keys.nextElement();
+            result.put(key, String.valueOf(config.get(key)));
+        }
+        for(Map.Entry<String,String> en:result.entrySet()){
+            b.append(StringUtil.format(en.getKey(), 40));
+            b.append(StringUtil.format(en.getValue(), 40));
+            b.append('\n');
+        }
+        return b.toString();
     }
 
     public static String getDefaultOpPolicy(TamayaConfigPlugin configPlugin) 
throws IOException {
@@ -183,13 +217,21 @@ public final class ConfigCommands {
         return sw.toString();
     }
 
-    public static String setDefaultDisabled(TamayaConfigPlugin configPlugin, 
boolean disabled) throws IOException {
-        configPlugin.setDefaultDisabled(disabled);
-        return disabled?"Tamaya is disabled by default.":"Tamaya is enabled by 
default.";
+    public static String setDefaultEnabled(TamayaConfigPlugin configPlugin, 
boolean enabled) throws IOException {
+        configPlugin.setTamayaEnabledByDefault(enabled);
+        return "tamaya.enabled="+enabled;
+    }
+
+    public static String getDefaultEnabled(TamayaConfigPlugin configPlugin) {
+        return String.valueOf(configPlugin.isTamayaEnabledByDefault());
     }
 
     public static String setAutoUpdateEnabled(TamayaConfigPlugin configPlugin, 
boolean enabled) {
         configPlugin.setAutoUpdateEnabled(enabled);
         return "tamaya.autoUpdate="+enabled;
     }
+
+    public static String getAutoUpdateEnabled(TamayaConfigPlugin configPlugin) 
{
+        return String.valueOf(configPlugin.isAutoUpdateEnabled());
+    }
 }
\ No newline at end of file

Reply via email to