/* * StaticCredential.java */ package edu.stanford.peer.rbtm.credential; import edu.stanford.peer.rbtm.util.*; import java.util.*; /** * StaticCredential */ public class StaticCredential extends Credential { private Role role; private EntityExpression subject; public StaticCredential(Role r, EntityExpression s) { role = r; subject = s; } public StaticCredential(String str) throws CredentialParsingException { StringTokenizer st = new StringTokenizer(str, "<-"); //Debug.debugInfo("StaticCredential: cred string " + str + // " contains " + st.countTokens() + " tokens"); String lhs = st.nextToken().trim(); String rhs = st.nextToken().trim(); //Debug.debugInfo("StaticCredential: lhs = " + lhs); //Debug.debugInfo("StaticCredential: rhs = " + rhs); role = (Role)getRole(lhs); subject = getEntityExpression(rhs); } public StaticCredential(Entity issuer, RoleName n, EntityExpression s) { role = new Role(issuer, n); subject = s; } public StaticCredential(String e, String rn, String es) throws CredentialParsingException { role = new Role(new SimpleEntity(e), new SimpleRoleName(rn)); subject = StaticCredential.getEntityExpression(es); } public Entity getIssuer() { return role.getBase(); } //public Role getRole() { return role; } public Role getDefinedRole() { return role; } public EntityExpression getSubject() { return subject; } public String toString() { return role.toString() + "<-" + subject.toString(); } public String toXML() { StringBuffer buff = new StringBuffer("\n"); return buff.toString(); } public int hashCode() { return role.hashCode() * 2 + subject.hashCode(); } public boolean equals(Object o) { return (o instanceof StaticCredential) && role.equals(((StaticCredential)o).role) && subject.equals(((StaticCredential)o).subject); } public static EntityExpression getRole(String str) throws CredentialParsingException { EntityExpression expr = null; StringTokenizer st = new StringTokenizer(str.trim(), "."); int count = st.countTokens(); String error = null; if(count <= 0) { error = "Role expression contained one or less parts!"; } else if(count == 2) { expr = (EntityExpression) new Role(st.nextToken(), st.nextToken()); } else if(count == 3) { expr = (EntityExpression) new LinkedRole(st.nextToken(),st.nextToken(), st.nextToken()); } else { error = "Role " + str + " expression contained too many role names"; } if(expr == null) { throw new CredentialParsingException(error); } return expr; } /** creates a new Intersection from a string */ public static Intersection getIntersection(String str) throws CredentialParsingException { StringTokenizer st = new StringTokenizer(str, "^"); int count = st.countTokens(); Intersection i = new Intersection(); for(int j = 0; j < count; j++) { i.and(getEntityExpression(st.nextToken())); } return i; } /** creates a new SimpleEntity instance from a string */ public static Entity getEntity(String str) throws CredentialParsingException { return new SimpleEntity(str.trim()); } /** * converts a String into an EntityExpression, deals with Entity, Role, * LinkedRole, and Intersection. This method uses helper functions for * parsiing entities, roles, and intersections, so these methods may be * overridden in subclassing implementations. */ public static EntityExpression getEntityExpression(String str) throws CredentialParsingException { if(str.indexOf("^") > 0) return (EntityExpression)getIntersection(str); if(str.indexOf(".") > 0) return getRole(str); return (EntityExpression)getEntity(str); } }