1 | package edu.stanford.rt.credential; |
---|
2 | |
---|
3 | import java.util.*; |
---|
4 | |
---|
5 | import edu.stanford.rt.datatype.*; |
---|
6 | import edu.stanford.rt.credential.*; |
---|
7 | import edu.stanford.rt.util.*; |
---|
8 | |
---|
9 | /** |
---|
10 | * @author Ninghui Li, Sandra Qiu<br> |
---|
11 | * |
---|
12 | * This class stores the common information about an <code>ApplicationDomain</code> or |
---|
13 | * a <code>CredentialDomain</code>. The info includes the following: |
---|
14 | * principal type, imported application domains, type declarations, |
---|
15 | * role declarations, hash id, RTML context, and the parsing status. |
---|
16 | * |
---|
17 | */ |
---|
18 | public class DomainSpecification |
---|
19 | { |
---|
20 | /** |
---|
21 | * Principal type |
---|
22 | */ |
---|
23 | private SimpleType principalType; |
---|
24 | |
---|
25 | /** |
---|
26 | * Stores all imported domains in this domain, |
---|
27 | * mapping string (domain name) to ApplicationDomain object. |
---|
28 | */ |
---|
29 | private OrderedMap importedDomains; |
---|
30 | |
---|
31 | /** |
---|
32 | * Stores all type decalarations in this domain, |
---|
33 | * mapping string(type name) to type declaration object. |
---|
34 | */ |
---|
35 | private OrderedMap typeDeclarations; |
---|
36 | |
---|
37 | /** |
---|
38 | * Stores all role declarations in this domain, |
---|
39 | * mapping string(role name) to role declaration object. |
---|
40 | */ |
---|
41 | private OrderedMap roleDeclarations; |
---|
42 | |
---|
43 | /** |
---|
44 | * A flag indicates the parsing status of the domain. |
---|
45 | */ |
---|
46 | private boolean completeState; |
---|
47 | |
---|
48 | /** The hash value of the domain. */ |
---|
49 | private HashID hashID; |
---|
50 | |
---|
51 | /** |
---|
52 | * RTML Context |
---|
53 | */ |
---|
54 | private RTContext context; |
---|
55 | |
---|
56 | /** |
---|
57 | * Constuctor for DomainSpecification. |
---|
58 | Contructs a DomainSpecification object with the specific uri. |
---|
59 | Include System DomainSpecification for non-system DomainSpecification |
---|
60 | object. |
---|
61 | * @param uri |
---|
62 | * @param isSystemDomain |
---|
63 | * @param context |
---|
64 | * @throws DomainSpecException |
---|
65 | */ |
---|
66 | DomainSpecification(RTContext context) throws DomainSpecException |
---|
67 | { |
---|
68 | this.completeState = false; |
---|
69 | this.importedDomains = |
---|
70 | new OrderedMap(String.class, DomainSpecification.class); |
---|
71 | this.typeDeclarations = |
---|
72 | new OrderedMap(String.class, DataType.class); |
---|
73 | this.roleDeclarations = |
---|
74 | new OrderedMap(String.class, RoleDeclaration.class); |
---|
75 | this.context = context; |
---|
76 | } |
---|
77 | |
---|
78 | // /** |
---|
79 | // Copies over the type declarations and role declarations from the |
---|
80 | // included domain specification objects. |
---|
81 | // */ |
---|
82 | // private void includeDomain(DomainSpecification ds) |
---|
83 | // throws DomainSpecException |
---|
84 | // { |
---|
85 | // SimpleType prinType = ds.getPrincipalType(); |
---|
86 | // if (prinType != null) |
---|
87 | // { |
---|
88 | // if (this.principalType == null) |
---|
89 | // { |
---|
90 | // this.principalType = prinType; |
---|
91 | // } |
---|
92 | // else if (!prinType.equals(this.principalType)) |
---|
93 | // { |
---|
94 | // throw new DomainSpecException("Pincipal Type Conflict!"); |
---|
95 | // } |
---|
96 | // } |
---|
97 | // typeDeclarations.putAll(ds.getTypeDeclarations()); |
---|
98 | // roleDeclarations.putAll(ds.getRoleDeclarations()); |
---|
99 | // } |
---|
100 | |
---|
101 | /** |
---|
102 | * Method importDomain. |
---|
103 | * Add a newly imported domain map. |
---|
104 | * @param name |
---|
105 | * the short name to refer to spec with the domain. |
---|
106 | * @param spec |
---|
107 | * @throws DomainSpecException |
---|
108 | */ |
---|
109 | public synchronized void importDomain( |
---|
110 | String name, |
---|
111 | ApplicationDomain spec) |
---|
112 | throws DomainSpecException |
---|
113 | { |
---|
114 | importedDomains.put(name, spec); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Method addType. |
---|
119 | * Add a new type declaration to the domain.<br> |
---|
120 | * @param type |
---|
121 | * @throws DomainSpecException |
---|
122 | * DomainSpecException when a duplicated type declaration, |
---|
123 | * i.e. a type declaration with the same name and same data |
---|
124 | * type, is added. |
---|
125 | */ |
---|
126 | public synchronized void addType(DataType type) |
---|
127 | throws DomainSpecException |
---|
128 | { |
---|
129 | String typeName = type.getName(); |
---|
130 | if (typeExistsInSystemDomain(typeName)) |
---|
131 | throw new DomainSpecException( |
---|
132 | "Type '" |
---|
133 | + typeName |
---|
134 | + "' already declared in system domain"); |
---|
135 | typeDeclarations.put(typeName, type); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Method addType. |
---|
140 | * Add XML built-in type. |
---|
141 | * @param typeName |
---|
142 | * @throws DomainSpecException |
---|
143 | */ |
---|
144 | public synchronized void addType(String typeName) |
---|
145 | throws DomainSpecException |
---|
146 | { |
---|
147 | if (typeExistsInSystemDomain(typeName)) |
---|
148 | throw new DomainSpecException( |
---|
149 | "Type '" |
---|
150 | + typeName |
---|
151 | + "' already declared in system domain"); |
---|
152 | typeDeclarations.put(typeName, null); |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * Method addRole. |
---|
157 | * Add a new role declaration to the domain. |
---|
158 | * @param role |
---|
159 | * @throws DomainSpecException |
---|
160 | */ |
---|
161 | public synchronized void addRole(RoleDeclaration role) |
---|
162 | throws DomainSpecException |
---|
163 | { |
---|
164 | RTUtil.debugInfo( |
---|
165 | "DomainSpecification(id=" |
---|
166 | + getHashID().toString() |
---|
167 | + ") adding role '" |
---|
168 | + role.getName() |
---|
169 | + "'"); |
---|
170 | |
---|
171 | String roleName = role.getName(); |
---|
172 | if (roleExistsInSystemDomain(roleName)) |
---|
173 | throw new DomainSpecException( |
---|
174 | "Role '" |
---|
175 | + roleName |
---|
176 | + "' already declared in system domain"); |
---|
177 | roleDeclarations.put(roleName, role); |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Method setPrincipalType. |
---|
182 | * @param prinType |
---|
183 | * @throws DomainSpecException |
---|
184 | */ |
---|
185 | public synchronized void setPrincipalType(SimpleType prinType) |
---|
186 | throws DomainSpecException |
---|
187 | { |
---|
188 | if (this.principalType == null) |
---|
189 | this.principalType = prinType; |
---|
190 | else if (!prinType.equals(this.principalType)) |
---|
191 | throw new DomainSpecException("Pincipal Type Conflict!"); |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * Method isComplete. |
---|
196 | * checks whether the parsing is completed. |
---|
197 | * @return boolean |
---|
198 | */ |
---|
199 | public boolean isComplete() |
---|
200 | { |
---|
201 | return completeState; |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | * Method setComplete. |
---|
206 | * marks the parsing status as complete, so no modification |
---|
207 | * is allowed for imported domains, type declarations , and |
---|
208 | * role declarations maps. |
---|
209 | */ |
---|
210 | public synchronized void setComplete() |
---|
211 | { |
---|
212 | completeState = true; |
---|
213 | importedDomains.setUneditable(); |
---|
214 | typeDeclarations.setUneditable(); |
---|
215 | roleDeclarations.setUneditable(); |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * Method lookupImportedDomain. |
---|
220 | * Lookup an imported domain by the given domainRef |
---|
221 | * in the currnt domain. |
---|
222 | * @param domainRef |
---|
223 | * @return DomainSpecification |
---|
224 | * @throws DomainSpecException |
---|
225 | * if cannot find one |
---|
226 | */ |
---|
227 | public DomainSpecification lookupImportedDomain(String domainRef) |
---|
228 | throws DomainSpecException |
---|
229 | { |
---|
230 | DomainSpecification spec = |
---|
231 | (DomainSpecification) importedDomains.get(domainRef); |
---|
232 | if (spec == null) |
---|
233 | { |
---|
234 | throw new DomainSpecException( |
---|
235 | "Domain '" + domainRef + "' not found."); |
---|
236 | } |
---|
237 | |
---|
238 | return spec; |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Method lookupType. |
---|
243 | * Look up the DataType declaration by given typeName |
---|
244 | * in the current domain. |
---|
245 | * @param typeName |
---|
246 | * @return DataType |
---|
247 | * returns null if cannot find one. |
---|
248 | * @throws DomainSpecException |
---|
249 | * if cannot find a DataType declaration by the given name. |
---|
250 | */ |
---|
251 | public DataType lookupType(String typeName) |
---|
252 | throws DomainSpecException |
---|
253 | { |
---|
254 | return (DataType) typeDeclarations.get(typeName); |
---|
255 | } |
---|
256 | |
---|
257 | /** |
---|
258 | * Method lookupType. |
---|
259 | * Look up the DataType declaration by given typeName in the given domain. |
---|
260 | * @param domainRef |
---|
261 | * specifies which domain to look up in. If domainRef is null or an |
---|
262 | * empty string, then look up in the current domain. |
---|
263 | * @param typeName |
---|
264 | * @return DataType |
---|
265 | * @throws DomainSpecException |
---|
266 | * if cannot find a DataType with given name. |
---|
267 | */ |
---|
268 | public DataType lookupType(String domainRef, String typeName) |
---|
269 | throws DomainSpecException |
---|
270 | { |
---|
271 | DataType type; |
---|
272 | |
---|
273 | if (domainRef == null || domainRef.length() == 0) |
---|
274 | { |
---|
275 | type = this.lookupType(typeName); |
---|
276 | } |
---|
277 | else |
---|
278 | { |
---|
279 | DomainSpecification importedSpec = |
---|
280 | this.lookupImportedDomain(domainRef); |
---|
281 | type = importedSpec.lookupType(typeName); |
---|
282 | } |
---|
283 | return type; |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * Method lookupRoleDeclaration. |
---|
288 | * Look up the role declaration by given roleName in the current domain. |
---|
289 | * @param roleName |
---|
290 | * @return RoleDeclaration |
---|
291 | * returns null if cannot find one. |
---|
292 | * @throws DomainSpecException |
---|
293 | * if cannot find one. |
---|
294 | */ |
---|
295 | public RoleDeclaration lookupRoleDeclaration(String roleName) |
---|
296 | throws DomainSpecException |
---|
297 | { |
---|
298 | return (RoleDeclaration) roleDeclarations.get(roleName); |
---|
299 | } |
---|
300 | |
---|
301 | /** |
---|
302 | * Method lookupRoleDeclaration. |
---|
303 | * looks up a role declaration by given roleName in the given domain. |
---|
304 | * @param domainRef |
---|
305 | * specifies which domain to look up in. If domainRef is null or an |
---|
306 | * empty string, then look up in the current domain. |
---|
307 | * @param roleName |
---|
308 | * @return RoleDeclaration |
---|
309 | * @throws DomainSpecException |
---|
310 | * if cannot find a one. |
---|
311 | */ |
---|
312 | public RoleDeclaration lookupRoleDeclaration( |
---|
313 | String domainRef, |
---|
314 | String roleName) |
---|
315 | throws DomainSpecException |
---|
316 | { |
---|
317 | RoleDeclaration role; |
---|
318 | |
---|
319 | if (domainRef == null || domainRef.length() == 0) |
---|
320 | { |
---|
321 | role = this.lookupRoleDeclaration(roleName); |
---|
322 | } |
---|
323 | else |
---|
324 | { |
---|
325 | DomainSpecification importedSpec = |
---|
326 | this.lookupImportedDomain(domainRef); |
---|
327 | role = importedSpec.lookupRoleDeclaration(roleName); |
---|
328 | } |
---|
329 | return role; |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | * Method getContext. |
---|
334 | * @return RTContext |
---|
335 | */ |
---|
336 | public RTContext getContext() |
---|
337 | { |
---|
338 | return context; |
---|
339 | } |
---|
340 | /** |
---|
341 | * Method getTypeDeclarations. |
---|
342 | * @return OrderedMap |
---|
343 | * @throws DomainSpecException |
---|
344 | * when parsing is not completed. |
---|
345 | */ |
---|
346 | public OrderedMap getTypeDeclarations() |
---|
347 | throws DomainSpecException |
---|
348 | { |
---|
349 | if (!isComplete()) |
---|
350 | throw new DomainSpecException("Still parsing current domain specification"); |
---|
351 | return typeDeclarations; |
---|
352 | } |
---|
353 | |
---|
354 | /** |
---|
355 | * Method getRoleDeclarations. |
---|
356 | * @return OrderedMap |
---|
357 | * @throws DomainSpecException |
---|
358 | * when parsing is not completed. |
---|
359 | */ |
---|
360 | public OrderedMap getRoleDeclarations() |
---|
361 | throws DomainSpecException |
---|
362 | { |
---|
363 | if (!isComplete()) |
---|
364 | throw new DomainSpecException("Still parsing current domain specification"); |
---|
365 | return roleDeclarations; |
---|
366 | } |
---|
367 | |
---|
368 | /** |
---|
369 | * Method getImportedDomains. |
---|
370 | * @return OrderedMap |
---|
371 | * @throws DomainSpecException |
---|
372 | * when parsing is not completed. |
---|
373 | */ |
---|
374 | public OrderedMap getImportedDomains() throws DomainSpecException |
---|
375 | { |
---|
376 | if (!isComplete()) |
---|
377 | throw new DomainSpecException("Still parsing current domain specification"); |
---|
378 | return importedDomains; |
---|
379 | |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * Method getPrincipalType. |
---|
384 | * @return SimpleType |
---|
385 | */ |
---|
386 | public SimpleType getPrincipalType() |
---|
387 | { |
---|
388 | return this.principalType; |
---|
389 | } |
---|
390 | |
---|
391 | /** |
---|
392 | * Returns the hashID. |
---|
393 | * @return ApplicationDomainHashID |
---|
394 | */ |
---|
395 | public HashID getHashID() |
---|
396 | { |
---|
397 | return this.hashID; |
---|
398 | } |
---|
399 | |
---|
400 | /** |
---|
401 | * Sets the hashID. |
---|
402 | * @param hashID The hashID to set |
---|
403 | */ |
---|
404 | public synchronized void setHashID(HashID hashID) |
---|
405 | throws DomainSpecException |
---|
406 | { |
---|
407 | this.hashID = hashID; |
---|
408 | } |
---|
409 | |
---|
410 | /** |
---|
411 | * Method typeExistsInSystemDomain. |
---|
412 | * checks whether a DataType with given typeName is already |
---|
413 | * declcared in system domain. |
---|
414 | * @param typeName |
---|
415 | * @return boolean |
---|
416 | */ |
---|
417 | private boolean typeExistsInSystemDomain(String typeName) |
---|
418 | { |
---|
419 | if (context == null) |
---|
420 | return false; |
---|
421 | |
---|
422 | try |
---|
423 | { |
---|
424 | DataType t = |
---|
425 | context.getSystemDomain().lookupType(typeName); |
---|
426 | return true; |
---|
427 | } |
---|
428 | catch (DomainSpecException e) |
---|
429 | { |
---|
430 | return false; |
---|
431 | } |
---|
432 | } |
---|
433 | |
---|
434 | /** |
---|
435 | * Method roleExistsInSystemDomain. |
---|
436 | * checks whether a RoleDeclaration with given roleName is already |
---|
437 | * declcared in system domain. |
---|
438 | * @param roleName |
---|
439 | * @return boolean |
---|
440 | */ |
---|
441 | private boolean roleExistsInSystemDomain(String roleName) |
---|
442 | { |
---|
443 | if (context == null) |
---|
444 | return false; |
---|
445 | |
---|
446 | try |
---|
447 | { |
---|
448 | RoleDeclaration r = |
---|
449 | context.getSystemDomain().lookupRoleDeclaration( |
---|
450 | roleName); |
---|
451 | return true; |
---|
452 | } |
---|
453 | catch (DomainSpecException e) |
---|
454 | { |
---|
455 | return false; |
---|
456 | } |
---|
457 | } |
---|
458 | } |
---|