package com.nailabs.abac.process; import com.nailabs.abac.credential.RtmlCredential; import com.nailabs.abac.test.*; import java.io.*; import java.util.*; import edu.stanford.peer.rbtm.credential.*; /** * A message contains a set of operations to be transmitted between two * negotiators. */ public class Message implements Serializable, XMLizable { /** A history list of operations */ private LinkedList ops; private transient Iterator opsIterator = null; /** Credentials used for proving edge operations. */ private HashSet evidence; /** Default contructor for a message instance. */ public Message() { ops = new LinkedList(); evidence = new HashSet(10); } /** Public accessor method for iterating over operations in the message */ public Iterator getOperations() { return ops.iterator(); } /** Accessor method for the graph delta queue. */ public Operation getNextOperation() { if(opsIterator == null) opsIterator = ops.iterator(); return (Operation)opsIterator.next(); } /** Adds an operation that contains internal evidence. */ public synchronized void addOperationWithoutEvidence(Operation op) { ops.add(op); } /** Add and operation to end of the operations queue */ public synchronized void addOperation(Operation op) { ops.add(op); //add the evidence if(op instanceof EdgeOperation) { HashSet subEvidence = ((EdgeOperation)op).getEvidence(); if(subEvidence == null) { //Debug.debug("message", "Message: No evidence available..."); return; } Iterator i = subEvidence.iterator(); while(i.hasNext()) { Object proof = i.next(); evidence.add(proof); } } } /** Message count of operations in this message. */ public int getOperationsCount() { return (ops == null)? 0: ops.size(); } /** Accessor method for the combined evidence. */ public Iterator getEvidence() { return evidence.iterator(); } /** Evidence (credentials) count. Convenience method for Iterator */ public int getEvidenceCount() { return (evidence == null)? 0: evidence.size(); } /** Add a new credential to the list of evidence. */ public synchronized void addEvidence(Credential cred) { if(cred instanceof RtmlCredential) { evidence.add(((RtmlCredential)cred).toXML()); } else { evidence.add((Object)cred); } } /** Converts this message instance into an XML formatted string */ public String toXML() { StringBuffer buff = new StringBuffer("\n"); buff.append("\n"); Iterator creds = evidence.iterator(); if(creds.hasNext()) { int count = 0; buff.append("\n"); while(creds.hasNext()) { Object cred = creds.next(); buff.append("\n"); if(cred instanceof RtmlCredential) buff.append(((RtmlCredential)cred).toXML()).append("\n"); else buff.append(cred.toString()).append("\n"); } buff.append("\n"); } ListIterator i = ops.listIterator(); int n = 0; while(i.hasNext()) { Operation op = (Operation)i.next(); buff.append("\n"); buff.append(op.toXML()); } return buff.append("\n").toString(); } /** Prints out this message in human readable format */ public String toString() { StringBuffer buff = new StringBuffer("[Message "); int n = 0; ListIterator i = ops.listIterator(); while(i.hasNext()) { buff.append("\n\t(").append(++n).append(") "); buff.append(i.next()); } buff.append("\n").append(ops.size()).append(" operations total"); buff.append("\n evidence: "); buff.append(evidence.toString()); return buff.append("]").toString(); } }