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

package org.jboss.security.auth.login;

import java.util.HashMap;
import javax.security.auth.login.AppConfigurationEntry;
import javax.xml.namespace.QName;

import org.jboss.xb.binding.GenericValueContainer;

/**
 * A container for creating AppConfigurationEntry during jbxb parse.
 * 
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.1 $
 */
public class AppConfigurationEntryHolder
   implements GenericValueContainer
{
   String code;
   AppConfigurationEntry.LoginModuleControlFlag controlFlag;
   HashMap options = new HashMap();

   // GenericValueContainer should have default ctor
   public AppConfigurationEntryHolder()
   {
   }

   AppConfigurationEntryHolder(String code, String flag)
   {
      this.code = code;
      controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUIRED;
      if (flag != null)
      {
         // Lower case is what is used by the jdk1.4.1 implementation
         flag = flag.toLowerCase();
         if (AppConfigurationEntry.LoginModuleControlFlag.REQUIRED.toString().indexOf(flag) > 0)
            controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUIRED;
         else if (AppConfigurationEntry.LoginModuleControlFlag.REQUISITE.toString().indexOf(flag) > 0)
            controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUISITE;
         else if (AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT.toString().indexOf(flag) > 0)
            controlFlag = AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT;
         else if (AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL.toString().indexOf(flag) > 0)
            controlFlag = AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL;
      }
   }

   public AppConfigurationEntry getEntry()
   {
      AppConfigurationEntry entry = new AppConfigurationEntry(code, controlFlag, options);
      return entry;
   }

   public void addOption(ModuleOption option)
   {
      options.put(option.name, option.value);
   }

   // GenericValueContainer impl

   public void addChild(QName name, Object value)
   {
      if("code".equals(name.getLocalPart()))
      {
         this.code = (String)value;
      }
      else if("flag".equals(name.getLocalPart()))
      {
         // Lower case is what is used by the jdk1.4.1 implementation
         String flag = ((String)value).toLowerCase();
         if (AppConfigurationEntry.LoginModuleControlFlag.REQUIRED.toString().indexOf(flag) > 0)
            controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUIRED;
         else if (AppConfigurationEntry.LoginModuleControlFlag.REQUISITE.toString().indexOf(flag) > 0)
            controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUISITE;
         else if (AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT.toString().indexOf(flag) > 0)
            controlFlag = AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT;
         else if (AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL.toString().indexOf(flag) > 0)
            controlFlag = AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL;
      }
      else if("module-option".equals(name.getLocalPart()))
      {
         addOption((ModuleOption)value);
      }
   }

   public Object instantiate()
   {
      if(options.isEmpty())
      {
         throw new IllegalStateException("No options!");
      }
      return new AppConfigurationEntry(code, controlFlag, options);
   }
}
