package edu.stanford.rt.credential;
import org.w3c.dom.Element;
import java.util.*;
import edu.stanford.rt.parser.RTParser;
import edu.stanford.rt.util.*;
/**
* @author Ninghui Li, Sandra Qiu
*
* This class stores all the CredentialDomain
, and
* RoleDefinition
index information.
*
* A special principal is stored in the credential store to .... *
*
*/
public class CredentialStore extends RTContext
{
/**
*/
private Principal specialPrincipal;
/**
* A map of CredentialDomain
objects. A CredentialDomain
* contains all the info about a Credential
.
*
* Key: HashID
* Value: CredentialDomain
*/
private HashMap credentialDomains;
/**
* A set of RoleDefinition
objects, including all
* roles defined in the Credential
included in this
* CredentialStore
.
*/
private HashSet roleDefinitions;
/**
* Role definitions indexed by issuer.
*
* Key: Principal
: the issuer
* Value: an ArrayList of RoleDefinition
.
*/
private HashMap indexByIssuer;
/**
* Role definitions indexed by issuer and role declaration.
* The issuer is the issuer of the credential and the role
* declaration is the one corresponding to the head role term in the
* role definition.
*
*
* Key: IssuerAndRoleDeclaration
* Value: an ArrayList of RoleDefinition
*/
private HashMap indexByIssuerAndRoleDeclaration;
/**
* Constructor for CredentialStore.
*/
public CredentialStore(RTParser rtParser) throws Exception
{
super(rtParser);
this.credentialDomains = new HashMap();
this.roleDefinitions = new HashSet();
this.indexByIssuer = new HashMap();
this.indexByIssuerAndRoleDeclaration = new HashMap();
}
/** public accesses method for iterating through all the domains */
public HashMap getCredentialDomains() {
return credentialDomains;
}
/**
* Returns the specialPrincipal.
* @return Principal
*/
public Principal getSpecialPrincipal()
{
return specialPrincipal;
}
/**
* Sets the specialPrincipal.
* @param specialPrincipal The specialPrincipal to set
*/
public void setSpecialPrincipal(Principal specialPrincipal)
{
this.specialPrincipal = specialPrincipal;
}
/**
* Method getCredentialDomain.
* @param id
* @return CredentialDomain
*/
public CredentialDomain getCredentialDomain(HashID id)
{
return (CredentialDomain) credentialDomains.get(id);
}
/**
* Method getCredentialDomain.
* @param id
* @return CredentialDomain
*/
public CredentialDomain getCredentialDomain(String id)
{
return getCredentialDomain(
new HashID(HashID.CREDENTIAL_DOMAIN, id));
}
/**
* Method roleDefinitionIterator.
* returns an Iterator for the set of role definitions.
* @return Iterator
*/
public Iterator roleDefinitionIterator()
{
return roleDefinitions.iterator();
}
/**
* Method getIndexByIssuer.
* returns a list of role definitions by the given issuer.
* @param issuer
* @return ArrayList
*/
public ArrayList getIndexByIssuer(Principal issuer)
{
return (ArrayList) indexByIssuer.get(issuer);
}
/**
* Method getIndexByIssuerAndRoleDeclaration.
* @param issuerAndRoleDeclaration
* @return ArrayList
*/
public ArrayList getIndexByIssuerAndRoleDeclaration(
IssuerAndRoleDeclaration issuerAndRoleDeclaration)
{
return (ArrayList) indexByIssuerAndRoleDeclaration.get(
issuerAndRoleDeclaration);
}
/**
* Method addCredentialDomain.
* @param id
* @param credentialDomain
* @throws DomainSpecException
*/
public synchronized void addCredentialDomain(
HashID id,
CredentialDomain credentialDomain)
throws DomainSpecException
{
RTUtil.debugInfo(
"CredentialStore.addCredentialDomain() .... ");
RTUtil.debugInfo(" id = " + id.toString());
RTUtil.debugInfo(credentialDomain.toString(" "));
if (this.credentialDomains.get(id) != null)
throw new DomainSpecException("Duplicated credentials in CredentialStore.");
this.credentialDomains.put(id, credentialDomain);
// get role definitions from the credential domain just added and
// add them to the credential store.
Iterator it = credentialDomain.roleDefinitionIterator();
while (it.hasNext())
{
addRoleDefinition((RoleDefinition) it.next());
}
}
/**
* Adds a new role definition to the roleDefinitions.
* @param roleDefinition The roleDefinition to set
*/
private synchronized void addRoleDefinition(
RoleDefinition roleDefinition)
throws DomainSpecException
{
if (this.roleDefinitions.contains(roleDefinition))
throw new DomainSpecException("Duplicated role definition. ");
this.roleDefinitions.add(roleDefinition);
addIndexByIssuer(roleDefinition);
addIndexByIssuerAndRoleDeclaration(roleDefinition);
}
/**
* Method addIndexByIssuer.
* @param roleDefinition
*/
private synchronized void addIndexByIssuer(RoleDefinition roleDefinition)
{
Role head = roleDefinition.getHead();
Principal issuer = head.getPrincipal();
ArrayList list = getIndexByIssuer(issuer);
if (list == null)
list = new ArrayList();
if (!list.contains(roleDefinition))
list.add(roleDefinition);
// update the map entry.
indexByIssuer.put(issuer, list);
}
/**
* Method addIndexByIssuerAndRoleDeclaration.
* @param roleDefinition
*/
private synchronized void addIndexByIssuerAndRoleDeclaration(RoleDefinition roleDefinition)
{
Role head = roleDefinition.getHead();
Principal issuer = head.getPrincipal();
RoleDeclaration roleDec = head.getRoleDeclaration();
IssuerAndRoleDeclaration index =
new IssuerAndRoleDeclaration(issuer, roleDec);
ArrayList list = getIndexByIssuerAndRoleDeclaration(index);
if (list == null)
list = new ArrayList();
if (!list.contains(roleDefinition))
list.add(roleDefinition);
indexByIssuerAndRoleDeclaration.put(index, list);
}
public String toString(String indent)
{
String thisIndent = indent + " ";
StringBuffer sb = new StringBuffer();
sb.append(
"********* CrendentialStore: **********\n\n");
sb.append(thisIndent).append("Credential Domains: \n");
Set entries = credentialDomains.entrySet();
Iterator it = entries.iterator();
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
CredentialDomain credDomain =
(CredentialDomain) entry.getValue();
sb.append(credDomain.toString(thisIndent));
sb.append("\n");
}
sb.append(thisIndent).append("Role definitions: \n");
it = roleDefinitionIterator();
while (it.hasNext())
{
RoleDefinition roleDef = (RoleDefinition) it.next();
sb.append(roleDef.toString(thisIndent));
sb.append("\n");
}
return sb.toString();
}
}