package edu.stanford.rt.credential;
//imports Apache Xerces API
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.util.*;
import java.io.*;
import edu.stanford.rt.parser.RTParser;
import edu.stanford.rt.util.*;
/**
* @author Ninghui Li, Sandra Qiu
*
* RTContext contains the info about ApplicationDomainSecification
s and
* Principal
s in the system.
*/
public class RTContext
{
/**
* Index for ApplicationDomain
objects.
*
* Key: HashID
* Value: ApplicationDomain
*/
private HashMap applicationDomains;
/**
* Index for Principal
objects.
*
* Key: PublicKeyPrincipal
* Value: PublicKeyPrincipalInfo
*/
private HashMap principals;
/**
* The built-in system domain.
*/
private ApplicationDomain systemDomain;
/**
* Constructor for RTContext.
*/
public RTContext(RTParser rtParser) throws Exception
{
this.applicationDomains = new HashMap();
this.principals = new HashMap();
// others need to call parseSystemDomain(InputStream).
this.systemDomain = rtParser.createSystemDomain();
}
/**
* Method getSystemDomain.
* @return ApplicationDomain
*/
public ApplicationDomain getSystemDomain()
{
return systemDomain;
}
/**
* Method getApplicationDomain.
* @param id
* @return ApplicationDomain
*/
public ApplicationDomain getApplicationDomain(String id)
{
return getApplicationDomain(
new HashID(HashID.APPLICATION_DOMAIN, id));
}
/**
* Method getApplicationDomain.
* @param id
* @return ApplicationDomain
*/
public ApplicationDomain getApplicationDomain(HashID id)
{
RTUtil.debugInfo("RTContex.getApplicationDomain() .... ");
RTUtil.debugInfo(" id = " + id.toString());
return (ApplicationDomain) applicationDomains.get(id);
}
/**
* Method addApplicationDomain.
* @param id
* @param appDomain
* @throws DomainSpecException
*/
public synchronized void addApplicationDomain(
HashID id,
ApplicationDomain appDomain)
throws DomainSpecException
{
// RTUtil.debugInfo("RTContex.addApplicationDomain() .... ");
// RTUtil.debugInfo(" id = " + id.toString());
// RTUtil.debugInfo("&&&&& ");
// RTUtil.debugInfo(appDomain.toString(" "));
// RTUtil.debugInfo("&&&&& ");
if (this.applicationDomains.get(id) != null)
throw new DomainSpecException("Duplicated domains in CredentialStore.");
this.applicationDomains.put(id, appDomain);
}
/**
* Method getPrincipal.
* @param id
* @return PublicKeyPrincipalInfo
*/
public PublicKeyPrincipalInfo getPrincipal(PublicKeyPrincipal id)
{
return (PublicKeyPrincipalInfo) principals.get(id);
}
/**
* Method addPrincipals.
* @param id
* the hash value of the principal
* @param principal
* the public key info of the principal
* @throws DomainSpecException
*/
public synchronized void addPrincipal(
PublicKeyPrincipal id,
PublicKeyPrincipalInfo principal)
throws DomainSpecException
{
if (this.principals.get(id) != null)
throw new DomainSpecException("Duplicated");
this.principals.put(id, principal);
}
/**
* Method hasDomainWithID.
* checks if there is an ApplicationDomain object with given hash id.
* @param idref
* @return boolean
*/
public boolean hasDomainWithID(String idref)
{
boolean exists = true;
if (getApplicationDomain(idref) == null)
exists = false;
return exists;
}
public String toString(String indent)
{
StringBuffer sb = new StringBuffer();
String thisIndent = indent + " ";
sb.append(
"********** RTContext: **********\n\n");
sb.append(thisIndent).append("System Domain: \n");
sb.append(systemDomain.toString(thisIndent));
sb.append(thisIndent).append("Imported Domains: \n");
Set entries = applicationDomains.entrySet();
Iterator it = entries.iterator();
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
ApplicationDomain domain =
(ApplicationDomain) entry.getValue();
sb.append(domain.toString(thisIndent));
sb.append("\n");
}
sb.append(thisIndent).append("Principals: \n");
entries = principals.entrySet();
it = entries.iterator();
while (it.hasNext())
{
Map.Entry entry = (Map.Entry) it.next();
PublicKeyPrincipalInfo principal =
(PublicKeyPrincipalInfo) entry.getValue();
sb.append(principal.toString(thisIndent));
sb.append("\n");
}
return sb.toString();
}
}