FELIX-2709 Support some configuration from bundle context properties:
  - felix.webconsole.manager.root -- root URL of the console
  - felix.webconsole.realm -- HTTP Basic realm
  - felix.webconsole.username -- HTTP Basic user name
  - felix.webconsole.password -- HTTP Basic password
  - such framework property configuration will also be assumed
    as default configuration in the generated Metatype descriptor

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1220651 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java
index 07dbb7f..2c74992 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationListener2.java
@@ -22,6 +22,7 @@
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Locale;
+import java.util.Map;
 import java.util.ResourceBundle;
 import java.util.TreeMap;
 
@@ -113,13 +114,14 @@
         ObjectClassDefinition xocd = null;
         final Locale localeObj = Util.parseLocaleString(locale);
         final ResourceBundle rb = osgiManager.resourceBundleManager.getResourceBundle(osgiManager.getBundleContext().getBundle(), localeObj);
+        final Map<String, String> defaultConfig = osgiManager.getDefaultConfiguration();
 
         // simple configuration properties
         final ArrayList<AttributeDefinition> adList = new ArrayList<AttributeDefinition>();
         for (int i = 0; i < CONF_PROPS.length; i++)
         {
             final String key = CONF_PROPS[i++];
-            final String defaultValue = CONF_PROPS[i];
+            final String defaultValue = ConfigurationUtil.getProperty( defaultConfig, key, CONF_PROPS[i] );
             final String name = getString(rb, "metadata." + key + ".name", key); //$NON-NLS-1$ //$NON-NLS-2$
             final String descr = getString(rb, "metadata." + key + ".description", key); //$NON-NLS-1$ //$NON-NLS-2$
             adList.add( new AttributeDefinitionImpl(key, name, descr, defaultValue) );
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationUtil.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationUtil.java
index 7d743ff..edf6567 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationUtil.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/ConfigurationUtil.java
@@ -17,10 +17,12 @@
 package org.apache.felix.webconsole.internal.servlet;

 

 import java.util.Collection;

-import java.util.Dictionary;

 import java.util.Iterator;

+import java.util.Map;

 import java.util.StringTokenizer;

 

+import org.osgi.framework.BundleContext;

+

 /**

  * A helper class to get configuration properties.

  */

@@ -32,6 +34,24 @@
         // prevent instantiation

     }

 

+

+    /**

+     * Returns the named property from the framework. If the property does

+     * not exist, the default value <code>def</code> is returned.

+     *

+     * @param context The BundleContext providing framework properties

+     * @param name The name of the property to return

+     * @param def The default value if the named property does not exist

+     * @return The value of the named property or <code>def</code>

+     *         if the property does not exist

+     */

+    public static final String getProperty( BundleContext context, String name, String def )

+    {

+        String value = context.getProperty( name );

+        return ( value == null ) ? def : value;

+    }

+

+

     /**

      * Returns the named property from the configuration. If the property does

      * not exist, the default value <code>def</code> is returned.

@@ -42,7 +62,7 @@
      * @return The value of the named property as a string or <code>def</code>

      *         if the property does not exist

      */

-    public static final String getProperty(Dictionary<String, ?> config, String name, String def)

+    public static final String getProperty(Map<String, ?> config, String name, String def)

     {

         Object value = config.get(name);

         if (value instanceof String)

@@ -68,7 +88,7 @@
      * @return The value of the named property as a string or <code>def</code>

      *         if the property does not exist

      */

-    public static final int getProperty(Dictionary<String, ?> config, String name, int def)

+    public static final int getProperty(Map<String, ?> config, String name, int def)

     {

         Object value = config.get(name);

         if (value instanceof Number)

@@ -100,7 +120,7 @@
      * @param name The name of the property to return

      * @return the property value as string array - no matter if originally it was other kind of array, collection or comma-separated string. Returns <code>null</code> if the property is not set.

      */

-    public static final String[] getStringArrayProperty(Dictionary<String, ?> config, String name)

+    public static final String[] getStringArrayProperty(Map<String, ?> config, String name)

     {

         Object value = config.get(name);

         if (value == null)

diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
index b778a72..ef4fdeb 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/servlet/OsgiManager.java
@@ -30,6 +30,7 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.ResourceBundle;
 import java.util.Set;
 
@@ -109,6 +110,14 @@
      */
     private static final String COOKIE_LOCALE = "felix.webconsole.locale"; //$NON-NLS-1$
 
+    private final String FRAMEWORK_PROP_MANAGER_ROOT = "felix.webconsole.manager.root"; //$NON-NLS-1$
+
+    private static final String FRAMEWORK_PROP_REALM = "felix.webconsole.realm"; //$NON-NLS-1$
+
+    private static final String FRAMEWORK_PROP_USER_NAME = "felix.webconsole.username"; //$NON-NLS-1$
+
+    private static final String FRAMEWORK_PROP_PASSWORD = "felix.webconsole.password"; //$NON-NLS-1$
+
     static final String PROP_MANAGER_ROOT = "manager.root"; //$NON-NLS-1$
 
     static final String PROP_DEFAULT_RENDER = "default.render"; //$NON-NLS-1$
@@ -190,7 +199,11 @@
     // true if the resources have been registered with the HttpService
     private boolean httpResourcesRegistered;
 
-    private Dictionary<String, ?> configuration;
+    // default configuration from framework properties
+    private HashMap<String, String> defaultConfiguration;
+
+    // configuration from Configuration Admin
+    private HashMap<String, ?> configuration;
 
     // See https://issues.apache.org/jira/browse/FELIX-2267
     private Locale configuredLocale;
@@ -279,6 +292,17 @@
             WebConsoleSecurityProvider.class.getName(), null);
         securityProviderTracker.open();
 
+        // load the default configuration from the framework
+        this.defaultConfiguration = new HashMap<String, String>();
+        this.defaultConfiguration.put( PROP_MANAGER_ROOT,
+            ConfigurationUtil.getProperty( bundleContext, FRAMEWORK_PROP_MANAGER_ROOT, DEFAULT_MANAGER_ROOT ) );
+        this.defaultConfiguration.put( PROP_REALM,
+            ConfigurationUtil.getProperty( bundleContext, FRAMEWORK_PROP_REALM, DEFAULT_REALM ) );
+        this.defaultConfiguration.put( PROP_USER_NAME,
+            ConfigurationUtil.getProperty( bundleContext, FRAMEWORK_PROP_USER_NAME, DEFAULT_USER_NAME ) );
+        this.defaultConfiguration.put( PROP_PASSWORD,
+            ConfigurationUtil.getProperty( bundleContext, FRAMEWORK_PROP_PASSWORD, DEFAULT_PASSWORD ) );
+
         // configure and start listening for configuration
         updateConfiguration(null);
 
@@ -701,7 +725,7 @@
             return;
         }
 
-        Dictionary<String, ?> config = getConfiguration();
+        Map<String, ?> config = getConfiguration();
 
         // get authentication details
         String realm = ConfigurationUtil.getProperty(config, PROP_REALM, DEFAULT_REALM);
@@ -776,16 +800,29 @@
         }
     }
 
-    private Dictionary<String, ?> getConfiguration()
+    private Map<String, ?> getConfiguration()
     {
         return configuration;
     }
 
-    synchronized void updateConfiguration(Dictionary<String, ?> config)
+
+    Map<String, String> getDefaultConfiguration()
     {
-        if (config == null)
+        return defaultConfiguration;
+    }
+
+
+    synchronized void updateConfiguration(Dictionary<String, ?> osgiConfig)
+    {
+        HashMap<String, Object> config = new HashMap<String, Object>(this.defaultConfiguration);
+
+        if ( osgiConfig != null )
         {
-            config = new Hashtable<String, Object>();
+            for ( Enumeration<String> keys = osgiConfig.keys(); keys.hasMoreElements(); )
+            {
+                final String key = keys.nextElement();
+                config.put( key, osgiConfig.get( key ) );
+            }
         }
 
         configuration = config;
@@ -886,13 +923,13 @@
         return enabledPlugins != null && !enabledPlugins.contains(pluginClass);
     }
 
-    private Dictionary<String, String> toStringConfig(Dictionary<String, ?> config)
+
+    private Dictionary<String, String> toStringConfig( Map<String, ?> config )
     {
         Dictionary<String, String> stringConfig = new Hashtable<String, String>();
-        for (Enumeration<String> ke = config.keys(); ke.hasMoreElements();)
+        for ( Entry<String, ?> entry : config.entrySet() )
         {
-            String key = ke.nextElement();
-            stringConfig.put(key, String.valueOf(config.get(key)));
+            stringConfig.put( entry.getKey(), String.valueOf( entry.getValue() ) );
         }
         return stringConfig;
     }