1 | package com.nailabs.abac.process; |
---|
2 | |
---|
3 | import com.nailabs.abac.credential.RtmlCredential; |
---|
4 | |
---|
5 | import com.nailabs.abac.test.*; |
---|
6 | import java.io.*; |
---|
7 | import java.util.*; |
---|
8 | import edu.stanford.peer.rbtm.credential.*; |
---|
9 | |
---|
10 | /** |
---|
11 | * A message contains a set of operations to be transmitted between two |
---|
12 | * negotiators. |
---|
13 | */ |
---|
14 | public class Message implements Serializable, XMLizable { |
---|
15 | /** A history list of operations */ |
---|
16 | private LinkedList ops; |
---|
17 | |
---|
18 | private transient Iterator opsIterator = null; |
---|
19 | |
---|
20 | /** Credentials used for proving edge operations. */ |
---|
21 | private HashSet evidence; |
---|
22 | |
---|
23 | /** Default contructor for a message instance. */ |
---|
24 | public Message() { |
---|
25 | ops = new LinkedList(); |
---|
26 | evidence = new HashSet(10); |
---|
27 | } |
---|
28 | |
---|
29 | /** Public accessor method for iterating over operations in the message */ |
---|
30 | public Iterator getOperations() { return ops.iterator(); } |
---|
31 | |
---|
32 | /** Accessor method for the graph delta queue. */ |
---|
33 | public Operation getNextOperation() { |
---|
34 | if(opsIterator == null) |
---|
35 | opsIterator = ops.iterator(); |
---|
36 | return (Operation)opsIterator.next(); |
---|
37 | } |
---|
38 | |
---|
39 | /** Adds an operation that contains internal evidence. */ |
---|
40 | public synchronized void addOperationWithoutEvidence(Operation op) { |
---|
41 | ops.add(op); |
---|
42 | } |
---|
43 | |
---|
44 | /** Add and operation to end of the operations queue */ |
---|
45 | public synchronized void addOperation(Operation op) { |
---|
46 | ops.add(op); |
---|
47 | //add the evidence |
---|
48 | if(op instanceof EdgeOperation) { |
---|
49 | HashSet subEvidence = ((EdgeOperation)op).getEvidence(); |
---|
50 | if(subEvidence == null) { |
---|
51 | //Debug.debug("message", "Message: No evidence available..."); |
---|
52 | return; |
---|
53 | } |
---|
54 | Iterator i = subEvidence.iterator(); |
---|
55 | while(i.hasNext()) { |
---|
56 | Object proof = i.next(); |
---|
57 | evidence.add(proof); |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | /** Message count of operations in this message. */ |
---|
63 | public int getOperationsCount() { |
---|
64 | return (ops == null)? 0: ops.size(); |
---|
65 | } |
---|
66 | |
---|
67 | /** Accessor method for the combined evidence. */ |
---|
68 | public Iterator getEvidence() { return evidence.iterator(); } |
---|
69 | |
---|
70 | /** Evidence (credentials) count. Convenience method for Iterator */ |
---|
71 | public int getEvidenceCount() { |
---|
72 | return (evidence == null)? 0: evidence.size(); |
---|
73 | } |
---|
74 | |
---|
75 | /** Add a new credential to the list of evidence. */ |
---|
76 | public synchronized void addEvidence(Credential cred) { |
---|
77 | if(cred instanceof RtmlCredential) { |
---|
78 | evidence.add(((RtmlCredential)cred).toXML()); |
---|
79 | } else { |
---|
80 | evidence.add((Object)cred); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /** Converts this message instance into an XML formatted string */ |
---|
85 | public String toXML() { |
---|
86 | StringBuffer buff = new |
---|
87 | StringBuffer("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); |
---|
88 | buff.append("<Message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://isrl.cs.byu.edu/xsd/message.xsd\">\n"); |
---|
89 | Iterator creds = evidence.iterator(); |
---|
90 | if(creds.hasNext()) { |
---|
91 | int count = 0; |
---|
92 | buff.append("<Evidence>\n"); |
---|
93 | while(creds.hasNext()) { |
---|
94 | Object cred = creds.next(); |
---|
95 | buff.append("<!-- credential count = ").append(++count); |
---|
96 | buff.append(" -->\n"); |
---|
97 | if(cred instanceof RtmlCredential) |
---|
98 | buff.append(((RtmlCredential)cred).toXML()).append("\n"); |
---|
99 | else |
---|
100 | buff.append(cred.toString()).append("\n"); |
---|
101 | } |
---|
102 | buff.append("</Evidence>\n"); |
---|
103 | } |
---|
104 | ListIterator i = ops.listIterator(); |
---|
105 | int n = 0; |
---|
106 | while(i.hasNext()) { |
---|
107 | Operation op = (Operation)i.next(); |
---|
108 | buff.append("<!--Operation no. ").append(++n).append("-->\n"); |
---|
109 | buff.append(op.toXML()); |
---|
110 | } |
---|
111 | return buff.append("</Message>\n").toString(); |
---|
112 | } |
---|
113 | |
---|
114 | /** Prints out this message in human readable format */ |
---|
115 | public String toString() { |
---|
116 | StringBuffer buff = new StringBuffer("[Message "); |
---|
117 | int n = 0; |
---|
118 | ListIterator i = ops.listIterator(); |
---|
119 | |
---|
120 | while(i.hasNext()) { |
---|
121 | buff.append("\n\t(").append(++n).append(") "); |
---|
122 | buff.append(i.next()); |
---|
123 | } |
---|
124 | buff.append("\n").append(ops.size()).append(" operations total"); |
---|
125 | buff.append("\n evidence: "); |
---|
126 | buff.append(evidence.toString()); |
---|
127 | return buff.append("]").toString(); |
---|
128 | } |
---|
129 | |
---|
130 | } |
---|