/*
 * 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.security.Guard;
import java.util.Hashtable;
import org.objectweb.security.context.SecurityCurrent;
import org.objectweb.security.context.SecurityContext;
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 RoleGuard implements Guard {

    static private Logger logger = null;
    
    private String containerName = null;

    private Hashtable roleRefs = new Hashtable();

     public RoleGuard(String containerName) {
 	this.containerName = containerName;
        if(logger == null)
            logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX);
     }

    public void add(String roleRef, String role) {
	logger.log(BasicLevel.DEBUG, "entering for "+role);
	roleRefs.put(roleRef, role) ;
    }

    public void checkGuard(Object roleString) throws SecurityException {
	logger.log(BasicLevel.DEBUG, "asked for "+(String)roleString);
	String roleRef = (String) roleString;
	String role = (String)roleRefs.get(roleRef);
	if (role == null) {
	    logger.log(BasicLevel.DEBUG, roleRef+" has not been defined");
	    throw new SecurityException("the logical role associated with "+roleRef+" has not been defined");
	}
	RoleMechanism rm = RoleMechanism.getInstance();
	SecurityCurrent current = SecurityCurrent.getCurrent();
	SecurityContext ctx = current.getSecurityContext();
	if (ctx == null) {
	    logger.log(BasicLevel.DEBUG, "no security context exists");
	    throw new SecurityException("no security context exists");
	}
	Principal principal = ctx.getCallerPrincipal();
	boolean isCallerInRole = rm.isCallerInRole(principal, role);
	if (isCallerInRole) {
	    logger.log(BasicLevel.DEBUG, "Caller is in role "+roleString);
	    return;
	} else {
	    logger.log(BasicLevel.DEBUG, "Caller is not in role "+roleString);
	    throw new SecurityException("the caller is not in the role "+role);
	}
    }

     public String toString() {
 	return "RoleGuard of container : "+ containerName;
     }
}
