/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.security;

import java.security.Principal;
import java.util.Hashtable;
import java.util.Enumeration;
import org.objectweb.jonas.common.Log;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * @author Jeff Mesnil
 * Contributor(s): 
 */

public class RoleMechanism {

    static private Logger logger = null;

    private static RoleMechanism unique = null;
    
    private Hashtable principals;

    private RoleMechanism() {
        if(logger == null)
            logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX);
	principals = new Hashtable();
    }

    public static RoleMechanism getInstance() {
 	if (unique == null)
 	    unique = new RoleMechanism();
 	return unique;
    } 

    public void addMapping(String principalName, String[] roles) {
	StringBuffer buff = new StringBuffer();
	buff.append("RoleMechanismImpl.add("+principalName+", roles : ");
	for (int i = 0; i < roles.length - 1; i++) 
	    buff.append(roles[i]+", ");
	buff.append(roles[roles.length - 1]+")");
	logger.log(BasicLevel.DEBUG, new String(buff));
	principals.put(principalName, roles);
    }

    public boolean isCallerInRole(Principal principal, String role) {
	logger.log(BasicLevel.DEBUG, "entering for principal "+principal+", role : "+role);
	boolean isCallerInRole = false;
	String[] principalRoles = (String[])principals.get(principal.getName());
	if (principalRoles == null) {
	    return false;
	}
	for (int i = 0; i < principalRoles.length; i++) {
	    isCallerInRole = isCallerInRole || (role.equals(principalRoles[i]));
	    if (isCallerInRole) 
		break;
	}
	return isCallerInRole;
    }

    public boolean isCallerInRoles(Principal principal, String[] roles) {
	logger.log(BasicLevel.DEBUG, "principal: "+principal);
	boolean isCallerInRole = false; 
	logger.log(BasicLevel.DEBUG,this);

	String[] principalRoles = (String[])principals.get(principal.getName());
	logger.log(BasicLevel.DEBUG, "principalRoles : "+principalRoles); 
	if(principalRoles != null){
	    for (int i = 0; i < roles.length; i++) { 
		for (int j = 0; j < principalRoles.length; j++) {
		    isCallerInRole = isCallerInRole || (roles[i].equals(principalRoles[j]));
		}
		if (isCallerInRole)
		    break;
	    }
	}
	if (isCallerInRole) 
	    logger.log(BasicLevel.DEBUG, "Caller is authorized to access the method");
	else 
	    logger.log(BasicLevel.DEBUG, "Caller is not authorized to access the method");
	return isCallerInRole;
    }

    public String toString() {
	StringBuffer buff = new StringBuffer();
	buff.append("RoleMechanismImpl : ");
	Enumeration keys = principals.keys();
	while (keys.hasMoreElements()) {
	    String  principalName = (String) keys.nextElement();
	    buff.append("\n\t"+principalName);
	    String[] roles = (String[])principals.get(principalName);
	    buff.append(", roles : ");
	    for (int i = 0; i < roles.length; i++) 
		buff.append(roles[i]+", ");	   
	} 
	return new String(buff);
    }

    /**
     * MBean method:
     * @return Hashtable of corepondances between pricipal and declarative roles
     */
    public Hashtable getPrincipalDeclarativeRoleMapping(){
	return principals;
    }    
}
