FELIX-3983 - Use inlined Sling commons PropertiesUtil for reading config values

Replaced copied code with direct usage of PropertiesUtil and ManifestParser. Also inlined the classes

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1459169 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/jaas/pom.xml b/jaas/pom.xml
index 9cbc129..befb351 100644
--- a/jaas/pom.xml
+++ b/jaas/pom.xml
@@ -104,6 +104,11 @@
                         <_removeheaders>
                             Embed-Dependency,Private-Package,Include-Resource
                         </_removeheaders>
+                        <Embed-Dependency>
+                          org.apache.sling.commons.osgi;inline=
+                            org/apache/sling/commons/osgi/PropertiesUtil*.class|
+                            org/apache/sling/commons/osgi/ManifestHeader*.class
+                        </Embed-Dependency>
                     </instructions>
                 </configuration>
             </plugin>
@@ -199,6 +204,11 @@
             <artifactId>servlet-api</artifactId>
             <version>2.3</version>
         </dependency>
+        <dependency>
+          <groupId>org.apache.sling</groupId>
+          <artifactId>org.apache.sling.commons.osgi</artifactId>
+          <version>2.2.0</version>
+        </dependency>
 
         <!-- testing -->
         <dependency>
diff --git a/jaas/src/main/java/org/apache/felix/jaas/internal/BundleLoginModuleCreator.java b/jaas/src/main/java/org/apache/felix/jaas/internal/BundleLoginModuleCreator.java
index 4926049..35b4ab3 100644
--- a/jaas/src/main/java/org/apache/felix/jaas/internal/BundleLoginModuleCreator.java
+++ b/jaas/src/main/java/org/apache/felix/jaas/internal/BundleLoginModuleCreator.java
@@ -20,12 +20,14 @@
 package org.apache.felix.jaas.internal;
 
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.security.auth.spi.LoginModule;
 
+import org.apache.sling.commons.osgi.ManifestHeader;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
@@ -125,7 +127,7 @@
 
     private Set<String> registerBundle(Bundle bundle)
     {
-        Set<String> classNames = Util.parseHeader((String) bundle.getHeaders().get(
+        Set<String> classNames = parseHeader((String) bundle.getHeaders().get(
             JAAS_MODULE_CLASS));
         for (String className : classNames)
         {
@@ -209,4 +211,16 @@
             return bundle;
         }
     }
+
+    private static Set<String> parseHeader(String header)
+    {
+        Set<String> values = new HashSet<String>();
+        ManifestHeader mh = ManifestHeader.parse(header);
+        for(ManifestHeader.Entry e : mh.getEntries())
+        {
+            values.add(e.getValue());
+        }
+        return values;
+    }
+
 }
diff --git a/jaas/src/main/java/org/apache/felix/jaas/internal/ConfigSpiOsgi.java b/jaas/src/main/java/org/apache/felix/jaas/internal/ConfigSpiOsgi.java
index 25a294f..671342b 100644
--- a/jaas/src/main/java/org/apache/felix/jaas/internal/ConfigSpiOsgi.java
+++ b/jaas/src/main/java/org/apache/felix/jaas/internal/ConfigSpiOsgi.java
@@ -37,6 +37,7 @@
 import org.apache.felix.scr.annotations.ConfigurationPolicy;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.PropertyOption;
+import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
@@ -261,8 +262,8 @@
         {
             return;
         }
-        String newDefaultRealmName = Util.toString(
-            properties.get(JAAS_DEFAULT_REALM_NAME), DEFAULT_REALM_NAME);
+        String newDefaultRealmName = PropertiesUtil.toString(
+                properties.get(JAAS_DEFAULT_REALM_NAME), DEFAULT_REALM_NAME);
 
         if (!newDefaultRealmName.equals(defaultRealmName))
         {
@@ -270,7 +271,7 @@
             recreateConfigs();
         }
 
-        String newProviderName = Util.toString(properties.get(JAAS_CONFIG_PROVIDER_NAME),
+        String newProviderName = PropertiesUtil.toString(properties.get(JAAS_CONFIG_PROVIDER_NAME),
             DEFAULT_CONFIG_PROVIDER_NAME);
 
         deregisterProvider(jaasConfigProviderName);
@@ -282,7 +283,7 @@
 
     private void manageGlobalConfiguration(Dictionary props)
     {
-        String configPolicy = Util.toString(props.get(JAAS_CONFIG_POLICY),
+        String configPolicy = PropertiesUtil.toString(props.get(JAAS_CONFIG_POLICY),
                 GlobalConfigurationPolicy.DEFAULT.name());
         configPolicy = Util.trimToNull(configPolicy);
 
diff --git a/jaas/src/main/java/org/apache/felix/jaas/internal/JaasConfigFactory.java b/jaas/src/main/java/org/apache/felix/jaas/internal/JaasConfigFactory.java
index 31bf805..6235afa 100644
--- a/jaas/src/main/java/org/apache/felix/jaas/internal/JaasConfigFactory.java
+++ b/jaas/src/main/java/org/apache/felix/jaas/internal/JaasConfigFactory.java
@@ -29,6 +29,7 @@
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.PropertyOption;
 import org.apache.felix.scr.annotations.PropertyUnbounded;
+import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
@@ -96,13 +97,14 @@
     @Override
     public void updated(String pid, Dictionary config) throws ConfigurationException
     {
-        String className = trimToNull(Util.toString(config.get(JAAS_CLASS_NAME), null));
-        String flag = trimToNull(Util.toString(config.get(JAAS_CONTROL_FLAG), "required"));
-        int ranking = Util.toInteger(config.get(JAAS_RANKING), 0);
+        String className = trimToNull(PropertiesUtil.toString(config.get(JAAS_CLASS_NAME), null));
+        String flag = trimToNull(PropertiesUtil.toString(config.get(JAAS_CONTROL_FLAG), "required"));
+        int ranking = PropertiesUtil.toInteger(config.get(JAAS_RANKING), 0);
 
-        String[] props = Util.toStringArray(config.get(JAAS_OPTIONS), new String[0]);
-        Map options = toMap(props);
-        String realmName = trimToNull(Util.toString(config.get(JAAS_REALM_NAME), null));
+        //TODO support system property substitution e.g. ${user.home}
+        //in property values
+        Map options = PropertiesUtil.toMap(config.get(JAAS_OPTIONS), new String[0]);
+        String realmName = trimToNull(PropertiesUtil.toString(config.get(JAAS_REALM_NAME), null));
 
         if (className == null)
         {
@@ -138,28 +140,6 @@
     }
 
     //~----------------------------------- Utility Methods
-
-    private static Map<String, Object> toMap(String[] props)
-    {
-        //TODO support system property substitution e.g. ${user.home}
-        //in property values
-        Map<String, Object> result = new HashMap<String, Object>();
-        for (String kv : props)
-        {
-            int indexOfEqual = kv.indexOf('=');
-            if (indexOfEqual > 0)
-            {
-                String key = trimToNull(kv.substring(0, indexOfEqual));
-                String value = trimToNull(kv.substring(indexOfEqual + 1));
-                if (key != null && value != null)
-                {
-                    result.put(key, value);
-                }
-            }
-        }
-        return result;
-    }
-
     @SuppressWarnings("unchecked")
     private static Map convertToMap(Dictionary config)
     {
diff --git a/jaas/src/main/java/org/apache/felix/jaas/internal/LoginModuleCreator.java b/jaas/src/main/java/org/apache/felix/jaas/internal/LoginModuleCreator.java
index d5db0a4..ab83566 100644
--- a/jaas/src/main/java/org/apache/felix/jaas/internal/LoginModuleCreator.java
+++ b/jaas/src/main/java/org/apache/felix/jaas/internal/LoginModuleCreator.java
@@ -22,11 +22,6 @@
 import javax.security.auth.spi.LoginModule;
 
 
-/**
- * User: chetanm
- * Date: 7/9/12
- * Time: 10:17 PM
- */
 public interface LoginModuleCreator
 {
 
diff --git a/jaas/src/main/java/org/apache/felix/jaas/internal/OsgiLoginModuleProvider.java b/jaas/src/main/java/org/apache/felix/jaas/internal/OsgiLoginModuleProvider.java
index 57e29c2..4e71fa5 100644
--- a/jaas/src/main/java/org/apache/felix/jaas/internal/OsgiLoginModuleProvider.java
+++ b/jaas/src/main/java/org/apache/felix/jaas/internal/OsgiLoginModuleProvider.java
@@ -27,6 +27,7 @@
 import javax.security.auth.spi.LoginModule;
 
 import org.apache.felix.jaas.LoginModuleFactory;
+import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 
@@ -41,7 +42,7 @@
     public OsgiLoginModuleProvider(ServiceReference sr, LoginModuleFactory delegate)
     {
         this.delegate = delegate;
-        this.ranking = Util.toInteger(sr.getProperty(Constants.SERVICE_RANKING), 0);
+        this.ranking = PropertiesUtil.toInteger(sr.getProperty(Constants.SERVICE_RANKING), 0);
         this.flag = ControlFlag.from(
             (String) sr.getProperty(LoginModuleFactory.JAAS_CONTROL_FLAG)).flag();
         this.realmName = (String) sr.getProperty(LoginModuleFactory.JAAS_REALM_NAME);
diff --git a/jaas/src/main/java/org/apache/felix/jaas/internal/Util.java b/jaas/src/main/java/org/apache/felix/jaas/internal/Util.java
index 515891f..433fedd 100644
--- a/jaas/src/main/java/org/apache/felix/jaas/internal/Util.java
+++ b/jaas/src/main/java/org/apache/felix/jaas/internal/Util.java
@@ -19,38 +19,8 @@
 
 package org.apache.felix.jaas.internal;
 
-import java.util.*;
-
-
 final class Util
 {
-
-    public static Set<String> parseHeader(String header)
-    {
-        //Could have used Sling commons ManifestHeader.parse
-        //but our requirement are simple
-
-        header = trimToNull(header);
-        if (header == null)
-        {
-            return new HashSet<String>();
-        }
-
-        String[] splits = header.split(",");
-        Set<String> values = new HashSet<String>();
-        for (String s : splits)
-        {
-            s = trimToNull(s);
-            if (s != null)
-            {
-                values.add(s);
-            }
-        }
-        return values;
-    }
-
-    //Instead of adding dependency on commons StringUtil we copy the used method below
-
     public static String trimToNull(String str)
     {
         String ts = trim(str);
@@ -66,145 +36,4 @@
     {
         return str == null || str.length() == 0;
     }
-
-    //----------------Methods taken from org.apache.sling.commons.osgi.PropertiesUtil
-
-    //These are required to safely access properties from ConfigurationAdmin
-
-    /**
-     * Returns the parameter as a string or the
-     * <code>defaultValue</code> if the parameter is <code>null</code>.
-     * @param propValue the property value or <code>null</code>
-     * @param defaultValue the default string value
-     */
-    public static String toString(Object propValue, String defaultValue)
-    {
-        propValue = toObject(propValue);
-        return (propValue != null) ? propValue.toString() : defaultValue;
-    }
-
-    /**
-     * Returns the parameter as an integer or the
-     * <code>defaultValue</code> if the parameter is <code>null</code> or if
-     * the parameter is not an <code>Integer</code> and cannot be converted to
-     * an <code>Integer</code> from the parameter's string value.
-     * @param propValue the property value or <code>null</code>
-     * @param defaultValue the default integer value
-     */
-    public static int toInteger(Object propValue, int defaultValue)
-    {
-        propValue = toObject(propValue);
-        if (propValue instanceof Integer)
-        {
-            return (Integer) propValue;
-        }
-        else if (propValue != null)
-        {
-            try
-            {
-                return Integer.valueOf(String.valueOf(propValue));
-            }
-            catch (NumberFormatException nfe)
-            {
-                // don't care, fall through to default value
-            }
-        }
-
-        return defaultValue;
-    }
-
-    /**
-     * Returns the parameter as a single value. If the
-     * parameter is neither an array nor a <code>java.util.Collection</code> the
-     * parameter is returned unmodified. If the parameter is a non-empty array,
-     * the first array element is returned. If the property is a non-empty
-     * <code>java.util.Collection</code>, the first collection element is returned.
-     * Otherwise <code>null</code> is returned.
-     * @param propValue the parameter to convert.
-     */
-    private static Object toObject(Object propValue)
-    {
-        if (propValue == null)
-        {
-            return null;
-        }
-        else if (propValue.getClass().isArray())
-        {
-            Object[] prop = (Object[]) propValue;
-            return prop.length > 0 ? prop[0] : null;
-        }
-        else if (propValue instanceof Collection<?>)
-        {
-            Collection<?> prop = (Collection<?>) propValue;
-            return prop.isEmpty() ? null : prop.iterator().next();
-        }
-        else
-        {
-            return propValue;
-        }
-    }
-
-    /**
-     * Returns the parameter as an array of Strings. If
-     * the parameter is a scalar value its string value is returned as a single
-     * element array. If the parameter is an array, the elements are converted to
-     * String objects and returned as an array. If the parameter is a collection, the
-     * collection elements are converted to String objects and returned as an array.
-     * Otherwise (if the property is <code>null</code>) a provided default value is
-     * returned.
-     * @param propValue The object to convert.
-     * @param defaultArray The default array to return.
-     */
-    public static String[] toStringArray(Object propValue, String[] defaultArray)
-    {
-        if (propValue == null)
-        {
-            // no value at all
-            return defaultArray;
-
-        }
-        else if (propValue instanceof String)
-        {
-            // single string
-            return new String[] { (String) propValue };
-
-        }
-        else if (propValue instanceof String[])
-        {
-            // String[]
-            return (String[]) propValue;
-
-        }
-        else if (propValue.getClass().isArray())
-        {
-            // other array
-            Object[] valueArray = (Object[]) propValue;
-            List<String> values = new ArrayList<String>(valueArray.length);
-            for (Object value : valueArray)
-            {
-                if (value != null)
-                {
-                    values.add(value.toString());
-                }
-            }
-            return values.toArray(new String[values.size()]);
-
-        }
-        else if (propValue instanceof Collection<?>)
-        {
-            // collection
-            Collection<?> valueCollection = (Collection<?>) propValue;
-            List<String> valueList = new ArrayList<String>(valueCollection.size());
-            for (Object value : valueCollection)
-            {
-                if (value != null)
-                {
-                    valueList.add(value.toString());
-                }
-            }
-            return valueList.toArray(new String[valueList.size()]);
-        }
-
-        return defaultArray;
-    }
 }