/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.security.auth.spi;

import java.io.IOException;
import java.util.Properties;
import java.util.Map;
import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;

/**
 * A subclass of UsersRolesLoginModule that uses a singleton instance and
 * memory based users/roles Properties maps to manage user/password and
 * user/role mappings. These maps need to be specified via the login module
 * options.
 * 
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1 $
 */
public class MemoryUsersRolesLoginModule extends UsersRolesLoginModule
{
   private Properties users;
   private Properties roles;

   /**
    * Override the UsersRolesLoginModule initialize to look for a users
    * and roles options specifying the
    * 
    * @param subject
    * @param callbackHandler
    * @param sharedState
    * @param options
    */ 
   public void initialize(Subject subject, CallbackHandler callbackHandler,
      Map sharedState, Map options)
   {
      // First extract the users/roles Properties from the options
      this.users = (Properties) options.get("users");
      this.roles = (Properties) options.get("roles");
      // Now initialize the superclass which will invoke createUsers/createRoles
      super.initialize(subject, callbackHandler, sharedState, options);
   }

   /**
    * Provide the users map obtained during initialize
    * @return the users login module option value
    */ 
   protected Properties createUsers(Map options)
   {
      return users;
   }

   /**
    * Provide the users map obtained during initialize
    * @return the users login module option value
    */ 
   protected Properties createRoles(Map options) throws IOException
   {
      return roles;
   }
}
