FELIX-4389 Add support for jaas.ranking to order LoginModuleFactory services.
   (Thanks Tobias Bocanegra for providing the patch)
   - For backwards compatibility support for service.ranking is retained
       if the jaas.ranking property is missing
   - Also consolidated the service property and configuration constants

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1558854 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/jaas/src/main/java/org/apache/felix/jaas/LoginModuleFactory.java b/jaas/src/main/java/org/apache/felix/jaas/LoginModuleFactory.java
index c36d297..a44ab4c 100644
--- a/jaas/src/main/java/org/apache/felix/jaas/LoginModuleFactory.java
+++ b/jaas/src/main/java/org/apache/felix/jaas/LoginModuleFactory.java
@@ -48,6 +48,13 @@
     String JAAS_REALM_NAME = "jaas.realmName";
 
     /**
+     * Property name specifying the ranking (i.e. sort order) of the configured login module entries. The entries
+     * are sorted in a descending order (i.e. higher value ranked configurations come first)
+     * @since 1.0.1 (bundle version 0.0.2)
+     */
+    String JAAS_RANKING = "jaas.ranking";
+
+    /**
      * Creates the LoginModule instance
      * @return loginModule instance
      */
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 143964d..ec63d49 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
@@ -19,16 +19,19 @@
 
 package org.apache.felix.jaas.internal;
 
+import static org.apache.felix.jaas.internal.Util.trimToNull;
+
 import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.Map;
-import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
 import org.apache.felix.jaas.LoginModuleFactory;
 import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.PropertyOption;
 import org.apache.felix.scr.annotations.PropertyUnbounded;
@@ -40,14 +43,22 @@
 import org.osgi.service.cm.ManagedServiceFactory;
 import org.osgi.service.log.LogService;
 
-import static org.apache.felix.jaas.internal.Util.trimToNull;
-
 @Component(label = "%jaas.name",
         description = "%jaas.description",
         metatype = true,
         ds = false,
         name = JaasConfigFactory.SERVICE_PID,
         configurationFactory = true)
+@Properties({
+    @Property(name = LoginModuleFactory.JAAS_CONTROL_FLAG, value = "required", options = {
+        @PropertyOption(name = "required", value = "%jaas.flag.required"),
+        @PropertyOption(name = "requisite", value = "%jaas.flag.requisite"),
+        @PropertyOption(name = "sufficient", value = "%jaas.flag.sufficient"),
+        @PropertyOption(name = "optional", value = "%jaas.flag.optional")
+    }),
+    @Property(name = LoginModuleFactory.JAAS_RANKING, intValue = 0),
+    @Property(name = LoginModuleFactory.JAAS_REALM_NAME)
+})
 public class JaasConfigFactory implements ManagedServiceFactory
 {
 
@@ -56,22 +67,9 @@
     @Property
     static final String JAAS_CLASS_NAME = "jaas.classname";
 
-    @Property(value = "required", options = {
-            @PropertyOption(name = "required", value = "%jaas.flag.required"),
-            @PropertyOption(name = "requisite", value = "%jaas.flag.requisite"),
-            @PropertyOption(name = "sufficient", value = "%jaas.flag.sufficient"),
-            @PropertyOption(name = "optional", value = "%jaas.flag.optional") })
-    static final String JAAS_CONTROL_FLAG = "jaas.controlFlag";
-
-    @Property(intValue = 0)
-    static final String JAAS_RANKING = "jaas.ranking";
-
     @Property(unbounded = PropertyUnbounded.ARRAY)
     static final String JAAS_OPTIONS = "jaas.options";
 
-    @Property
-    static final String JAAS_REALM_NAME = "jaas.realmName";
-
     private final Logger log;
 
     private final LoginModuleCreator factory;
@@ -86,9 +84,9 @@
         this.factory = factory;
         this.log = log;
 
-        Properties props = new Properties();
-        props.setProperty(Constants.SERVICE_VENDOR, "Apache Software Foundation");
-        props.setProperty(Constants.SERVICE_PID, SERVICE_PID);
+        Hashtable<String, String> props = new Hashtable<String, String>();
+        props.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
+        props.put(Constants.SERVICE_PID, SERVICE_PID);
         context.registerService(ManagedServiceFactory.class.getName(), this, props);
     }
 
@@ -103,13 +101,13 @@
     public void updated(String pid, Dictionary config) throws ConfigurationException
     {
         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 flag = trimToNull(PropertiesUtil.toString(config.get(LoginModuleFactory.JAAS_CONTROL_FLAG), "required"));
+        int ranking = PropertiesUtil.toInteger(config.get(LoginModuleFactory.JAAS_RANKING), 0);
 
         //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));
+        String realmName = trimToNull(PropertiesUtil.toString(config.get(LoginModuleFactory.JAAS_REALM_NAME), null));
 
         if (className == null)
         {
@@ -129,8 +127,7 @@
         LoginModuleProvider lmf = new ConfigLoginModuleProvider(realmName, className,
             combinedOptions, ControlFlag.from(flag).flag(), ranking, factory);
 
-        ServiceRegistration reg = context.registerService(
-            LoginModuleFactory.class.getName(), lmf, new Properties());
+        ServiceRegistration reg = context.registerService(LoginModuleFactory.class.getName(), lmf, null);
         ServiceRegistration oldReg = registrations.put(pid, reg);
 
         //Remove earlier registration if any
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 a16ed46..97a3e4d 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
@@ -48,7 +48,13 @@
     }
 
     public void configure() {
-        ranking = PropertiesUtil.toInteger(serviceReference.getProperty(Constants.SERVICE_RANKING), 0);
+        // FELIX-4389: Support jaas.ranking (in addition to service.ranking
+        // to help ordering LoginModuleFactory instances
+        Object ranking = serviceReference.getProperty(LoginModuleFactory.JAAS_RANKING);
+        if (ranking == null) {
+            ranking = serviceReference.getProperty(Constants.SERVICE_RANKING);
+        }
+        ranking = PropertiesUtil.toInteger(ranking, 0);
         flag = ControlFlag.from((String) serviceReference.getProperty(LoginModuleFactory.JAAS_CONTROL_FLAG)).flag();
         realmName = (String) serviceReference.getProperty(LoginModuleFactory.JAAS_REALM_NAME);
     }
diff --git a/jaas/src/main/java/org/apache/felix/jaas/package-info.java b/jaas/src/main/java/org/apache/felix/jaas/package-info.java
index 40828e5..1ac66fc 100644
--- a/jaas/src/main/java/org/apache/felix/jaas/package-info.java
+++ b/jaas/src/main/java/org/apache/felix/jaas/package-info.java
@@ -22,7 +22,7 @@
  *
  * @version 1.0
  */
-@Version("1.0")
+@Version("1.0.1")
 @Export(optional = "provide:=true")
 package org.apache.felix.jaas;