1 | package edu.stanford.rt.credential; |
---|
2 | import java.util.*; |
---|
3 | import edu.stanford.rt.util.*; |
---|
4 | import edu.stanford.rt.datatype.*; |
---|
5 | /** |
---|
6 | * @author Ninghui Li, Sandra Qiu<br> |
---|
7 | * |
---|
8 | * All the information about a declared role. In RTML, this is |
---|
9 | * represented by a <code>RoleDeclaration</code> element in a |
---|
10 | * <code>ApplicationDomainSpecification</code> document. |
---|
11 | * |
---|
12 | * Issue: Do not support identity yet. |
---|
13 | */ |
---|
14 | public class RoleDeclaration |
---|
15 | implements Constants, java.io.Serializable |
---|
16 | { |
---|
17 | /** |
---|
18 | * Issuer traces types |
---|
19 | */ |
---|
20 | static public HashMap issuerTracesTypes; |
---|
21 | /** |
---|
22 | * Subject traces types |
---|
23 | */ |
---|
24 | static public HashMap subjectTracesTypes; |
---|
25 | static { |
---|
26 | issuerTracesTypes = new HashMap(4); |
---|
27 | issuerTracesTypes.put("all", new Integer(ISSUER_TRACES_ALL)); |
---|
28 | issuerTracesTypes.put("def", new Integer(ISSUER_TRACES_DEF)); |
---|
29 | issuerTracesTypes.put( |
---|
30 | "rule", |
---|
31 | new Integer(ISSUER_TRACES_RULE)); |
---|
32 | issuerTracesTypes.put( |
---|
33 | "none", |
---|
34 | new Integer(ISSUER_TRACES_NONE)); |
---|
35 | subjectTracesTypes = new HashMap(3); |
---|
36 | subjectTracesTypes.put( |
---|
37 | "all", |
---|
38 | new Integer(SUBJECT_TRACES_ALL)); |
---|
39 | subjectTracesTypes.put( |
---|
40 | "fact", |
---|
41 | new Integer(SUBJECT_TRACES_FACT)); |
---|
42 | subjectTracesTypes.put( |
---|
43 | "none", |
---|
44 | new Integer(SUBJECT_TRACES_NONE)); |
---|
45 | } |
---|
46 | /** |
---|
47 | * Default issuer traces type: rule |
---|
48 | */ |
---|
49 | static public int DEFAULT_ISSUER_TYPE = ISSUER_TRACES_RULE; |
---|
50 | /** |
---|
51 | * Defualt issuer traces type: fact. |
---|
52 | */ |
---|
53 | static public int DEFAULT_SUBJECT_TYPE = SUBJECT_TRACES_FACT; |
---|
54 | /** |
---|
55 | * Role declaration context. |
---|
56 | * <br> |
---|
57 | * The <code>DomainSpecification</code> in which the current role |
---|
58 | * is declared. |
---|
59 | */ |
---|
60 | private DomainSpecification context; |
---|
61 | /** Role name. <br> |
---|
62 | * In RTML: attribute <code>name</code>. |
---|
63 | */ |
---|
64 | private String name; |
---|
65 | /** Default is DEFAULT_ISSUER_TYPE <br> |
---|
66 | * In RTML: attribute <code>issuerTracesType</code>. |
---|
67 | */ |
---|
68 | private int issuerTracesType = DEFAULT_ISSUER_TYPE; |
---|
69 | /** Default is DEFAULT_SUBJECT_TYPE <br> |
---|
70 | * In RTML: attribute <code>subjectTracesType</code>. |
---|
71 | */ |
---|
72 | private int subjectTracesType = DEFAULT_SUBJECT_TYPE; |
---|
73 | /** Default is 1 <br> |
---|
74 | * In RTML: attribute <code>dimension</code>. |
---|
75 | */ |
---|
76 | private int dimension = 1; |
---|
77 | /** Default is false. <br> |
---|
78 | * In RTML: attribute <code>isIdentity</code>. |
---|
79 | */ |
---|
80 | private boolean isIdentity = false; |
---|
81 | /** Maps String (parameter name) to DataType (parameter type). */ |
---|
82 | private OrderedMap parameterDeclarations; |
---|
83 | /** Declaration type: Restriction, Extension, Projection or Plain. */ |
---|
84 | private int declarationType; |
---|
85 | /** BaseRole Element for Restriction, Extension, Projection. */ |
---|
86 | private RoleDeclaration baseRole; |
---|
87 | /** This map keeps track of the ordersing of the flattend name-prefixes. |
---|
88 | A name-prefix is colon-separated name concatenation.*/ |
---|
89 | private HashMap prefixToPositions; |
---|
90 | private HashMap positionToPrefixes; |
---|
91 | |
---|
92 | // Role declaration types |
---|
93 | static private final int RESTRICTION = 1; |
---|
94 | static private final int EXTENSION = 2; |
---|
95 | static private final int PROJECTION = 3; |
---|
96 | static private final int PLAIN = 4; |
---|
97 | /** |
---|
98 | * Private contructor ensures that no one else can contruct a RoleDeclaration |
---|
99 | * object and the state of a RoleDeclaration object is complete (unmodifiable) |
---|
100 | * once it is created. <br> |
---|
101 | * And the newly created RoleDeclaration's parameter-postion mapping is also |
---|
102 | * calculated. |
---|
103 | * @param context |
---|
104 | * @param name |
---|
105 | * @param issuerTracesType |
---|
106 | * @param subjectTracesType |
---|
107 | * @param dimension |
---|
108 | * @param isIdentity |
---|
109 | * @param declarationType |
---|
110 | * @param baseRole |
---|
111 | * @param parameterDeclarations |
---|
112 | * @throws DomainSpecException |
---|
113 | */ |
---|
114 | private RoleDeclaration( |
---|
115 | DomainSpecification context, |
---|
116 | String name, |
---|
117 | int issuerTracesType, |
---|
118 | int subjectTracesType, |
---|
119 | int dimension, |
---|
120 | boolean isIdentity, |
---|
121 | int declarationType, |
---|
122 | RoleDeclaration baseRole, |
---|
123 | OrderedMap parameterDeclarations) |
---|
124 | throws DomainSpecException |
---|
125 | { |
---|
126 | this.context = context; |
---|
127 | this.name = name; |
---|
128 | this.issuerTracesType = issuerTracesType; |
---|
129 | this.subjectTracesType = subjectTracesType; |
---|
130 | this.dimension = dimension; |
---|
131 | this.isIdentity = isIdentity; |
---|
132 | this.declarationType = declarationType; |
---|
133 | this.baseRole = baseRole; |
---|
134 | this.parameterDeclarations = parameterDeclarations; |
---|
135 | this.parameterDeclarations.setUneditable(); |
---|
136 | this.prefixToPositions = new HashMap(); |
---|
137 | this.positionToPrefixes = new HashMap(); |
---|
138 | // Compute parameters, which includes all parameters of base role plus |
---|
139 | // those in "this" role. |
---|
140 | calculatePrefixPositions( |
---|
141 | new StringBuffer(name), |
---|
142 | 0, |
---|
143 | this.parameterDeclarations, |
---|
144 | this.prefixToPositions, |
---|
145 | this.positionToPrefixes); |
---|
146 | } |
---|
147 | /** |
---|
148 | * Method createRestrictionRole. |
---|
149 | * Public interface to construct a <code>Restriction</code> type |
---|
150 | * <code>RoleDeclaration</code> object. |
---|
151 | * @param name |
---|
152 | * @param isIdentity |
---|
153 | * @param baseRole |
---|
154 | * @param newParameters |
---|
155 | * @return RoleDeclaration |
---|
156 | * @throws DomainSpecException |
---|
157 | */ |
---|
158 | public static RoleDeclaration createRestrictionRole( |
---|
159 | DomainSpecification context, |
---|
160 | String name, |
---|
161 | boolean isIdentity, |
---|
162 | RoleDeclaration baseRole, |
---|
163 | OrderedMap newParameters) |
---|
164 | throws DomainSpecException |
---|
165 | { |
---|
166 | if (baseRole == null) |
---|
167 | throw new DomainSpecException(""); |
---|
168 | int issuerTracesType = baseRole.getIssuerTracesType(); |
---|
169 | int subjectTracesType = baseRole.getSubjectTracesType(); |
---|
170 | int dimension = baseRole.getDimension(); |
---|
171 | OrderedMap parameterDeclarations = |
---|
172 | new OrderedMap(String.class, DataType.class); |
---|
173 | parameterDeclarations.putAll( |
---|
174 | baseRole.getParameterDeclarations()); |
---|
175 | parameterDeclarations.putAll(parameterDeclarations); |
---|
176 | return new RoleDeclaration( |
---|
177 | context, |
---|
178 | name, |
---|
179 | issuerTracesType, |
---|
180 | subjectTracesType, |
---|
181 | dimension, |
---|
182 | isIdentity, |
---|
183 | RESTRICTION, |
---|
184 | baseRole, |
---|
185 | parameterDeclarations); |
---|
186 | } |
---|
187 | /** |
---|
188 | * Method createExtensionRole. |
---|
189 | * Public interface to construct an <code>Extension</code> type |
---|
190 | * <code>RoleDeclaration</code> object. |
---|
191 | * @param name |
---|
192 | * @param isIdentity |
---|
193 | * @param baseRole |
---|
194 | * @param newParameters |
---|
195 | * @return RoleDeclaration |
---|
196 | * @throws DomainSpecException |
---|
197 | */ |
---|
198 | public static RoleDeclaration createExtensionRole( |
---|
199 | DomainSpecification context, |
---|
200 | String name, |
---|
201 | boolean isIdentity, |
---|
202 | RoleDeclaration baseRole, |
---|
203 | OrderedMap newParameters) |
---|
204 | throws DomainSpecException |
---|
205 | { |
---|
206 | if (baseRole == null) |
---|
207 | throw new DomainSpecException(""); |
---|
208 | int issuerTracesType = baseRole.getIssuerTracesType(); |
---|
209 | int subjectTracesType = baseRole.getSubjectTracesType(); |
---|
210 | int dimension = baseRole.getDimension(); |
---|
211 | OrderedMap parameterDeclarations = |
---|
212 | new OrderedMap(String.class, DataType.class); |
---|
213 | parameterDeclarations.putAll( |
---|
214 | baseRole.getParameterDeclarations()); |
---|
215 | parameterDeclarations.putAll(parameterDeclarations); |
---|
216 | return new RoleDeclaration( |
---|
217 | context, |
---|
218 | name, |
---|
219 | issuerTracesType, |
---|
220 | subjectTracesType, |
---|
221 | dimension, |
---|
222 | isIdentity, |
---|
223 | EXTENSION, |
---|
224 | baseRole, |
---|
225 | parameterDeclarations); |
---|
226 | } |
---|
227 | /** |
---|
228 | * Method createProjectionRole. |
---|
229 | * Public interface to construct a <code>Projection</code> type |
---|
230 | * <code>RoleDeclaration</code> object. |
---|
231 | * @param name |
---|
232 | * @param isIdentity |
---|
233 | * @param baseRole |
---|
234 | * @param parameterNames |
---|
235 | * @return RoleDeclaration |
---|
236 | * @throws DomainSpecException |
---|
237 | */ |
---|
238 | public static RoleDeclaration createProjectionRole( |
---|
239 | DomainSpecification context, |
---|
240 | String name, |
---|
241 | boolean isIdentity, |
---|
242 | RoleDeclaration baseRole, |
---|
243 | String[] parameterNames) |
---|
244 | throws DomainSpecException |
---|
245 | { |
---|
246 | if (baseRole == null) |
---|
247 | throw new DomainSpecException(""); |
---|
248 | int issuerTracesType = baseRole.getIssuerTracesType(); |
---|
249 | int subjectTracesType = baseRole.getSubjectTracesType(); |
---|
250 | int dimension = baseRole.getDimension(); |
---|
251 | OrderedMap parameterDeclarations = |
---|
252 | new OrderedMap(String.class, DataType.class); |
---|
253 | OrderedMap baseRoleParameters = |
---|
254 | baseRole.getParameterDeclarations(); |
---|
255 | for (int i = 0; i < parameterNames.length; i++) |
---|
256 | { |
---|
257 | DataType type = |
---|
258 | baseRole.getParameterType(parameterNames[i]); |
---|
259 | parameterDeclarations.put(parameterNames[i], type); |
---|
260 | } |
---|
261 | return new RoleDeclaration( |
---|
262 | context, |
---|
263 | name, |
---|
264 | issuerTracesType, |
---|
265 | subjectTracesType, |
---|
266 | dimension, |
---|
267 | isIdentity, |
---|
268 | PROJECTION, |
---|
269 | baseRole, |
---|
270 | parameterDeclarations); |
---|
271 | } |
---|
272 | /** |
---|
273 | * Method createPlainRole. |
---|
274 | * Public interface to construct a <code>Plain</code> type of |
---|
275 | * <code>RoleDeclaration</code> object. <code>Identity</code> is not supported. |
---|
276 | * @param name |
---|
277 | * @param issuerTracesType |
---|
278 | * @param subjectTracesType |
---|
279 | * @param dimension |
---|
280 | * @param isIdentity |
---|
281 | * @param newParameters |
---|
282 | * @return RoleDeclaration |
---|
283 | * @throws DomainSpecException |
---|
284 | */ |
---|
285 | public static RoleDeclaration createPlainRole( |
---|
286 | DomainSpecification context, |
---|
287 | String name, |
---|
288 | int issuerTracesType, |
---|
289 | int subjectTracesType, |
---|
290 | int dimension, |
---|
291 | boolean isIdentity, |
---|
292 | OrderedMap newParameters) |
---|
293 | throws DomainSpecException |
---|
294 | { |
---|
295 | return new RoleDeclaration( |
---|
296 | context, |
---|
297 | name, |
---|
298 | issuerTracesType, |
---|
299 | subjectTracesType, |
---|
300 | dimension, |
---|
301 | isIdentity, |
---|
302 | PLAIN, |
---|
303 | null, |
---|
304 | newParameters); |
---|
305 | } |
---|
306 | /** Returns the role declaration context. */ |
---|
307 | public DomainSpecification getContext() |
---|
308 | { |
---|
309 | return context; |
---|
310 | } |
---|
311 | /** Returns the role name. */ |
---|
312 | public String getName() |
---|
313 | { |
---|
314 | return name; |
---|
315 | } |
---|
316 | /** Returns the value for <code>issuerTracesType</code>. */ |
---|
317 | public int getIssuerTracesType() |
---|
318 | { |
---|
319 | return issuerTracesType; |
---|
320 | } |
---|
321 | /** Returns the value for <code>subjectTracesType</code>. */ |
---|
322 | public int getSubjectTracesType() |
---|
323 | { |
---|
324 | return subjectTracesType; |
---|
325 | } |
---|
326 | /** |
---|
327 | * Returns the value for <code>dimension</code>. |
---|
328 | */ |
---|
329 | public int getDimension() |
---|
330 | { |
---|
331 | return dimension; |
---|
332 | } |
---|
333 | /** Returns the base role object. */ |
---|
334 | public RoleDeclaration getBaseRole() |
---|
335 | { |
---|
336 | return baseRole; |
---|
337 | } |
---|
338 | /** Checks whether this role is an identity-based role. */ |
---|
339 | public boolean isIdentity() |
---|
340 | { |
---|
341 | return isIdentity; |
---|
342 | } |
---|
343 | /** |
---|
344 | * Method getParameterDeclarations. |
---|
345 | * @return OrderedMap |
---|
346 | * Returns an OrderedMap of parameter declarations in |
---|
347 | * this role declaration object. <br> |
---|
348 | * For Restriction or Extension type <code>RoleDeclaration</code>, |
---|
349 | * the map contains parameters declared both in base role and this role. |
---|
350 | * For Projection type <code>RoleDeclaration</code>, |
---|
351 | * the map contains only those paraeters declared in base role with |
---|
352 | * matching names with this role's declared parameters. |
---|
353 | * |
---|
354 | * @throws DomainSpecException |
---|
355 | */ |
---|
356 | public synchronized OrderedMap getParameterDeclarations() |
---|
357 | throws DomainSpecException |
---|
358 | { |
---|
359 | if (parameterDeclarations.isEditable()) |
---|
360 | throw new IllegalStateException("Parameter declarations are not finalized."); |
---|
361 | return parameterDeclarations; |
---|
362 | } |
---|
363 | /** |
---|
364 | * Method getParameterType. |
---|
365 | * @param paramName name of the declared parameter. |
---|
366 | * @return DataType type of the declared parameter. |
---|
367 | * @throws DomainSpecException |
---|
368 | */ |
---|
369 | public DataType getParameterType(String paramName) |
---|
370 | throws DomainSpecException |
---|
371 | { |
---|
372 | return (DataType) parameterDeclarations.get(paramName); |
---|
373 | } |
---|
374 | /** Returns a unmodifiable view of prefix-to-position map.*/ |
---|
375 | public synchronized Map getPrefixToPositions() |
---|
376 | { |
---|
377 | return Collections.unmodifiableMap(prefixToPositions); |
---|
378 | } |
---|
379 | /** Returns a unmodifiable view of position-to-prefix map.*/ |
---|
380 | public synchronized Map getPositionToPrefixes() |
---|
381 | { |
---|
382 | return Collections.unmodifiableMap(positionToPrefixes); |
---|
383 | } |
---|
384 | /** Returns the total number of the prefixes in the map.*/ |
---|
385 | public int getTotalPrefixes() |
---|
386 | { |
---|
387 | return prefixToPositions.size(); |
---|
388 | } |
---|
389 | /** |
---|
390 | * Method getPosition. |
---|
391 | * @param prefix |
---|
392 | * the prefix to get the position with. |
---|
393 | * @return int |
---|
394 | * the postition of the given prefix. |
---|
395 | */ |
---|
396 | public int getPosition(String prefix) |
---|
397 | { |
---|
398 | Integer pos = (Integer) prefixToPositions.get(prefix); |
---|
399 | return pos.intValue(); |
---|
400 | } |
---|
401 | /** |
---|
402 | * Method getPrefix. |
---|
403 | * @param position |
---|
404 | * the position to get prefix with. |
---|
405 | * @return String |
---|
406 | * the prefix at the given position. |
---|
407 | */ |
---|
408 | public String getPrefix(int position) |
---|
409 | { |
---|
410 | return (String) positionToPrefixes.get(new Integer(position)); |
---|
411 | } |
---|
412 | /** |
---|
413 | * Method toString. |
---|
414 | * @param indent |
---|
415 | * @return String |
---|
416 | */ |
---|
417 | public String toString(String indent) |
---|
418 | { |
---|
419 | String thisIndent = indent + " "; |
---|
420 | StringBuffer sb = new StringBuffer(); |
---|
421 | |
---|
422 | sb.append(thisIndent).append("RoleDeclaration: ").append( |
---|
423 | name).append( |
---|
424 | "\n"); |
---|
425 | sb |
---|
426 | .append(thisIndent + " ") |
---|
427 | .append("issuerTracesType = ") |
---|
428 | .append(getIssuerTracesTypeString(issuerTracesType)) |
---|
429 | .append("\n"); |
---|
430 | sb |
---|
431 | .append(thisIndent + " ") |
---|
432 | .append("subjectTracesType = ") |
---|
433 | .append(getSubjectTracesTypeString(subjectTracesType)) |
---|
434 | .append("\n"); |
---|
435 | |
---|
436 | sb.append(thisIndent + " ").append("Parameters: \n"); |
---|
437 | Iterator it = parameterDeclarations.keyIterator(); |
---|
438 | while (it.hasNext()) |
---|
439 | { |
---|
440 | String key = (String) it.next(); |
---|
441 | sb.append(thisIndent + " ").append(key); |
---|
442 | try |
---|
443 | { |
---|
444 | DataType value = |
---|
445 | (DataType) parameterDeclarations.get(key); |
---|
446 | sb.append(" ").append(value.getName()).append( |
---|
447 | "\n"); |
---|
448 | } |
---|
449 | catch (DomainSpecException e) |
---|
450 | { |
---|
451 | e.printStackTrace(); |
---|
452 | } |
---|
453 | } |
---|
454 | return sb.toString(); |
---|
455 | } |
---|
456 | |
---|
457 | /** |
---|
458 | * Method calculatePrefixPositions. |
---|
459 | * @param prefix |
---|
460 | * current prefix. Prefix is a lolon-saparated string, which |
---|
461 | * is the concatenation of role name and parameter names. |
---|
462 | * @param position |
---|
463 | * the current position of the prefix. |
---|
464 | * @param paramDeclarations |
---|
465 | * all parameters declared in this role. |
---|
466 | * @param prefixToPositionMap |
---|
467 | * a HashMap which maps a prefix to its position. |
---|
468 | * @param positionToPrefixMap |
---|
469 | * a HashMap which maps a position to its prefix |
---|
470 | * @throws DomainSpecException |
---|
471 | * |
---|
472 | */ |
---|
473 | private synchronized void calculatePrefixPositions( |
---|
474 | StringBuffer prefix, |
---|
475 | int position, |
---|
476 | OrderedMap paramDeclarations, |
---|
477 | HashMap prefixToPositionMap, |
---|
478 | HashMap positionToPrefixMap) |
---|
479 | throws DomainSpecException |
---|
480 | { |
---|
481 | RTUtil.debugInfo( |
---|
482 | "RoleDeclaration.calculatePrefixPositions(, , , , ) ...."); |
---|
483 | // RTUtil.debugInfo("prefix = " + prefix); |
---|
484 | Iterator paramNameIt = paramDeclarations.keyIterator(); |
---|
485 | StringBuffer newPrefix = new StringBuffer(prefix.toString()); |
---|
486 | while (paramNameIt.hasNext()) |
---|
487 | { |
---|
488 | newPrefix.append(COLON); |
---|
489 | String paramName = (String) paramNameIt.next(); |
---|
490 | newPrefix.append(paramName); |
---|
491 | RTUtil.debugInfo("newPrefix = " + newPrefix.toString()); |
---|
492 | DataType paramType = getParameterType(paramName); |
---|
493 | if (paramType instanceof RecordType) |
---|
494 | { |
---|
495 | OrderedMap fieldDeclarations = |
---|
496 | ((RecordType) paramType).getFieldDeclarations(); |
---|
497 | calculatePrefixPositions( |
---|
498 | newPrefix, |
---|
499 | position++, |
---|
500 | fieldDeclarations, |
---|
501 | prefixToPositionMap, |
---|
502 | positionToPrefixMap); |
---|
503 | position++; |
---|
504 | } |
---|
505 | else |
---|
506 | { |
---|
507 | position++; |
---|
508 | RTUtil.debugInfo("position = " + position); |
---|
509 | prefixToPositionMap.put( |
---|
510 | newPrefix.toString(), |
---|
511 | new Integer(position)); |
---|
512 | positionToPrefixMap.put( |
---|
513 | new Integer(position), |
---|
514 | newPrefix.toString()); |
---|
515 | } |
---|
516 | //RTUtil.debugInfo("**** prefix = " + prefix.toString()); |
---|
517 | newPrefix = new StringBuffer(prefix.toString()); |
---|
518 | //RTUtil.debugInfo("newPrefix 2 = " + newPrefix.toString()); |
---|
519 | } |
---|
520 | } |
---|
521 | |
---|
522 | private String getIssuerTracesTypeString(int issuerTracesType) |
---|
523 | { |
---|
524 | String res = null; |
---|
525 | switch (issuerTracesType) |
---|
526 | { |
---|
527 | case ISSUER_TRACES_ALL : |
---|
528 | res = "all"; |
---|
529 | break; |
---|
530 | case ISSUER_TRACES_DEF : |
---|
531 | res = "def"; |
---|
532 | break; |
---|
533 | case ISSUER_TRACES_RULE : |
---|
534 | res = "rule"; |
---|
535 | break; |
---|
536 | case ISSUER_TRACES_NONE : |
---|
537 | res = "none"; |
---|
538 | break; |
---|
539 | default : |
---|
540 | res = "rule"; |
---|
541 | break; |
---|
542 | } |
---|
543 | return res; |
---|
544 | } |
---|
545 | private String getSubjectTracesTypeString(int subjectTracesType) |
---|
546 | { |
---|
547 | String res = null; |
---|
548 | switch (subjectTracesType) |
---|
549 | { |
---|
550 | case SUBJECT_TRACES_ALL : |
---|
551 | res = "all"; |
---|
552 | break; |
---|
553 | case SUBJECT_TRACES_FACT : |
---|
554 | res = "fact"; |
---|
555 | break; |
---|
556 | case SUBJECT_TRACES_NONE : |
---|
557 | res = "none"; |
---|
558 | break; |
---|
559 | default : |
---|
560 | res = "fact"; |
---|
561 | break; |
---|
562 | } |
---|
563 | return res; |
---|
564 | } |
---|
565 | } |
---|