1 | package com.nailabs.abac.test; |
---|
2 | |
---|
3 | import java.util.*; |
---|
4 | import java.io.*; |
---|
5 | |
---|
6 | |
---|
7 | import com.nailabs.abac.trust.*; |
---|
8 | import com.nailabs.abac.process.*; |
---|
9 | import edu.stanford.peer.rbtm.credential.*; |
---|
10 | |
---|
11 | public class PropertiesObserver implements Observer { |
---|
12 | PrintStream out; |
---|
13 | int major = 0, minor = 0; |
---|
14 | int policy = 1; |
---|
15 | HashMap nodeMap = new HashMap(10); |
---|
16 | Entity me; |
---|
17 | boolean myLocal = false; |
---|
18 | boolean mySync = false; |
---|
19 | Vector aSync = new Vector(); |
---|
20 | |
---|
21 | public PropertiesObserver(Entity eid) { |
---|
22 | String name = eid.toString(); |
---|
23 | FrontierManager mgr = FrontierManager.getFrontier(eid); |
---|
24 | me = eid; |
---|
25 | try { |
---|
26 | String logName = name + ".log"; |
---|
27 | File propFile = new File(name + ".prop"); |
---|
28 | File logFile = new File(name + ".log"); |
---|
29 | if(propFile.exists()){ |
---|
30 | propFile.renameTo(logFile); |
---|
31 | } |
---|
32 | out = new PrintStream(new FileOutputStream(name + ".prop")); |
---|
33 | } |
---|
34 | catch(Exception ex) { |
---|
35 | ex.printStackTrace(out); |
---|
36 | } |
---|
37 | out.println("subject = " + name); |
---|
38 | printACPolicy(mgr.getACPolicy()); |
---|
39 | printAckPolicy(mgr.getAckPolicy()); |
---|
40 | out.println(); |
---|
41 | } |
---|
42 | |
---|
43 | private void printACPolicy(ACPolicy policy) { |
---|
44 | final String AC = "AC"; |
---|
45 | if(policy == null)return; |
---|
46 | Vector keys = policy.getProtectedCredentials(); |
---|
47 | if(keys == null)return; |
---|
48 | for(int i = 0; i < keys.size(); i++) { |
---|
49 | Credential cred = (Credential)keys.elementAt(i); |
---|
50 | EntityExpression expr = policy.requires(cred); |
---|
51 | printPolicy(AC, cred.toString(), (expr == null)? "true": |
---|
52 | expr.toString()); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | private void printAckPolicy(AckPolicy policy) { |
---|
57 | final String ACK = "Ack"; |
---|
58 | if(policy == null)return; |
---|
59 | Vector keys = policy.getSensitiveRoles(); |
---|
60 | if(keys == null)return; |
---|
61 | for(int i = 0; i < keys.size(); i++) { |
---|
62 | EntityExpression sensitive = (EntityExpression)keys.elementAt(i); |
---|
63 | printPolicy(ACK, sensitive.toString(), |
---|
64 | policy.requires(sensitive).toString()); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | protected void printPolicy(String type, String key, String value) { |
---|
69 | StringBuffer buff = new StringBuffer("policy."); |
---|
70 | buff.append(policy++).append(" = "); |
---|
71 | buff.append(type).append("["); |
---|
72 | buff.append(key).append("] = ").append(value); |
---|
73 | out.println(buff.toString()); |
---|
74 | } |
---|
75 | |
---|
76 | private void printLocation(String msg) { |
---|
77 | if(msg.endsWith("init") || myLocal) { |
---|
78 | myLocal = false; |
---|
79 | return; |
---|
80 | } |
---|
81 | if(msg.endsWith("sync")) { |
---|
82 | mySync = (msg.endsWith("async")); |
---|
83 | //printValue("async", mySync + ""); |
---|
84 | //out.println(); |
---|
85 | return; |
---|
86 | } |
---|
87 | StringBuffer buff = new StringBuffer("update."); |
---|
88 | buff.append(++major).append("."); |
---|
89 | buff.append(msg); |
---|
90 | out.println(buff.toString()); |
---|
91 | out.println(""); |
---|
92 | minor = 1; |
---|
93 | for(int offset = 0, max = aSync.size(); offset < max; offset++) { |
---|
94 | //printValue("offset", offset + "/" + aSync.size()); |
---|
95 | mySync = false; // it should already be set to false (sanity) |
---|
96 | printObject(aSync.elementAt(offset)); |
---|
97 | } |
---|
98 | aSync.clear(); |
---|
99 | } |
---|
100 | |
---|
101 | private void printMessage(String msg) { |
---|
102 | StringBuffer buff = new StringBuffer("update."); |
---|
103 | buff.append(major).append("."); |
---|
104 | buff.append(minor).append("."); |
---|
105 | buff.append(msg); |
---|
106 | out.println(buff.toString()); |
---|
107 | } |
---|
108 | |
---|
109 | private void printValue(String label, String value) { |
---|
110 | StringBuffer buff = new StringBuffer(label); |
---|
111 | buff.append(" = ").append(value); |
---|
112 | printMessage(buff.toString()); |
---|
113 | } |
---|
114 | |
---|
115 | protected void printEdge(EdgeOperation edge) { |
---|
116 | //if(edge.getType().startsWith("LinkingImplication")) { |
---|
117 | //} |
---|
118 | _printEdge(edge); |
---|
119 | } |
---|
120 | protected void _printEdge(EdgeOperation edge) { |
---|
121 | printValue("type", "edge"); |
---|
122 | printValue("edgeType", edge.getType()); |
---|
123 | printValue("parentName", edge.getParent().toString()); |
---|
124 | printValue("childName", edge.getChild().toString()); |
---|
125 | printValue("childType", edge.getChild().getType()); |
---|
126 | printValue("childProcessing", edge.getProcessingState() + ""); |
---|
127 | HashSet evidence = edge.getEvidence(); |
---|
128 | //print out the credentials |
---|
129 | if(evidence != null) { |
---|
130 | Iterator i = evidence.iterator(); |
---|
131 | final String EVIDENCE = "evidence."; |
---|
132 | for(int n = 1; i.hasNext(); n++) { |
---|
133 | StringBuffer key = new StringBuffer(EVIDENCE); |
---|
134 | key.append(n); |
---|
135 | printValue(key.toString(), i.next().toString()); |
---|
136 | } |
---|
137 | } |
---|
138 | out.println(); |
---|
139 | } |
---|
140 | |
---|
141 | protected void printNode(NodeOperation op) { |
---|
142 | Goal g = op.getGoal(); |
---|
143 | if(op.isRoot()) { |
---|
144 | if(g.getVerifier().equals(me)) { |
---|
145 | printLocation("location = local"); |
---|
146 | myLocal = true; |
---|
147 | } |
---|
148 | printValue("type", "node"); |
---|
149 | printValue("name", g.toString()); |
---|
150 | printValue("nodeType", g.getType()); |
---|
151 | printValue("isRoot", "True"); |
---|
152 | } else { |
---|
153 | printValue("type", "node"); |
---|
154 | printValue("name", g.toString()); |
---|
155 | printValue("processing", op.getProcessingState().toString()); |
---|
156 | } |
---|
157 | out.println(); |
---|
158 | } |
---|
159 | |
---|
160 | protected void printNode(TTGNode node) { |
---|
161 | printValue("type", "node"); |
---|
162 | printValue("nodeType", node.getGoal().getType()); |
---|
163 | printValue("name", node.getGoal().toString()); |
---|
164 | printValue("processing", node.getProcessingState().toString()); |
---|
165 | if(node instanceof TargetNode) { |
---|
166 | TargetNode tn = (TargetNode)node; |
---|
167 | int value = tn.getSatisfactionValue(); |
---|
168 | switch(value) { |
---|
169 | case SatisfactionState.SATISFIED: |
---|
170 | printValue("satisfied", "Satisfied"); |
---|
171 | break; |
---|
172 | case SatisfactionState.FAILED: |
---|
173 | printValue("satisfied", "Failed"); |
---|
174 | break; |
---|
175 | default: |
---|
176 | printValue("satisfied", "Unknown"); |
---|
177 | } |
---|
178 | } |
---|
179 | if(node instanceof LinkingGoalNode) { |
---|
180 | LinkingGoalNode lgn = (LinkingGoalNode)node; |
---|
181 | if(lgn.isComplete()) { |
---|
182 | printValue("satisfied", "Complete"); |
---|
183 | } else { |
---|
184 | printValue("satisfied", "Incomplete"); |
---|
185 | } |
---|
186 | } |
---|
187 | out.println(); |
---|
188 | } |
---|
189 | |
---|
190 | protected void printObject(Object obj) { |
---|
191 | if(mySync) { |
---|
192 | aSync.add(obj); |
---|
193 | //minor--; |
---|
194 | return; |
---|
195 | } |
---|
196 | if(obj instanceof EdgeOperation) { |
---|
197 | printEdge((EdgeOperation)obj); |
---|
198 | } |
---|
199 | else if(obj instanceof NodeOperation) { |
---|
200 | printNode((NodeOperation)obj); |
---|
201 | } |
---|
202 | else if(obj instanceof TTGNode) { |
---|
203 | printNode((TTGNode)obj); |
---|
204 | } |
---|
205 | else { |
---|
206 | minor--; |
---|
207 | //printValue("object", obj.toString()); |
---|
208 | } |
---|
209 | //out.println(); |
---|
210 | minor++; |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | public void update(TTGNode node) { |
---|
215 | if(!nodeMap.containsKey(node.getGoal().toString())) { |
---|
216 | nodeMap.put(node.getGoal().toString(), node); |
---|
217 | node.addObserver(this); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | public void update(Observable o, Object arg) { |
---|
223 | if(o instanceof TTG || o instanceof Strategy) { |
---|
224 | if(arg instanceof TTGNode) { |
---|
225 | update((TTGNode)arg); |
---|
226 | } |
---|
227 | if(arg instanceof String) { |
---|
228 | String property = (String)arg; |
---|
229 | if(property.startsWith("location")) { |
---|
230 | printLocation(property); |
---|
231 | return; |
---|
232 | } else { |
---|
233 | printMessage(property); |
---|
234 | } |
---|
235 | } |
---|
236 | printObject(arg); |
---|
237 | } |
---|
238 | printObject(o); |
---|
239 | |
---|
240 | //printValue("printing", "arg"); |
---|
241 | //printObject(arg); |
---|
242 | //out.println(); |
---|
243 | } |
---|
244 | |
---|
245 | public void close() { |
---|
246 | out.flush(); |
---|
247 | out.close(); |
---|
248 | } |
---|
249 | |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | |
---|