1 | package edu.stanford.rt.credential; |
---|
2 | |
---|
3 | import java.util.*; |
---|
4 | |
---|
5 | import org.w3c.dom.Element; |
---|
6 | |
---|
7 | import edu.stanford.rt.datatype.*; |
---|
8 | import edu.stanford.rt.util.*; |
---|
9 | |
---|
10 | /** |
---|
11 | * @author Ninghui Li, Sandra Qiu<br> |
---|
12 | * |
---|
13 | * Implementation of <code>Credential</code> element. Only one CredentialDomain object is associated with each Credential |
---|
14 | * object. |
---|
15 | */ |
---|
16 | public class CredentialDomain extends DomainSpecification |
---|
17 | { |
---|
18 | /** |
---|
19 | * Issuer of the associated Credential. |
---|
20 | */ |
---|
21 | private Principal issuer; |
---|
22 | |
---|
23 | /** |
---|
24 | * validity time of the associated Credential. |
---|
25 | */ |
---|
26 | private ValidityTime validityTime; |
---|
27 | |
---|
28 | /** |
---|
29 | * maps String (principal shortName) to Principal object |
---|
30 | */ |
---|
31 | private HashMap principals; |
---|
32 | |
---|
33 | /** |
---|
34 | * reverse lookup of Principal instances in this CredentialDomain |
---|
35 | */ |
---|
36 | private HashMap shortNames; |
---|
37 | |
---|
38 | /** |
---|
39 | * list of role defined in this credential. |
---|
40 | */ |
---|
41 | private ArrayList roleDefinitions; |
---|
42 | |
---|
43 | /** |
---|
44 | * the XML Element of the associated Credential. |
---|
45 | */ |
---|
46 | private Element credentialElement; |
---|
47 | |
---|
48 | /** |
---|
49 | * Constructor for CredentialDomain. |
---|
50 | */ |
---|
51 | public CredentialDomain( |
---|
52 | Element credentialElement, |
---|
53 | RTContext rtContext) |
---|
54 | throws DomainSpecException |
---|
55 | { |
---|
56 | super(rtContext); |
---|
57 | this.credentialElement = credentialElement; |
---|
58 | this.principals = new HashMap(); |
---|
59 | this.shortNames = new HashMap(); |
---|
60 | this.roleDefinitions = new ArrayList(); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Method addPrincipal. |
---|
65 | * @param shortName |
---|
66 | * @param principal |
---|
67 | * @throws DomainSpecException |
---|
68 | */ |
---|
69 | public void addPrincipal(String shortName, Principal principal) |
---|
70 | throws DomainSpecException |
---|
71 | { |
---|
72 | RTUtil.debugInfo("addPrincipal() shortName = " + shortName); |
---|
73 | if (principals.get(shortName) != null) |
---|
74 | throw new DomainSpecException( |
---|
75 | "Duplicated principal entries for shortName '" |
---|
76 | + shortName |
---|
77 | + "'"); |
---|
78 | principals.put(shortName, principal); |
---|
79 | shortNames.put(principal, shortName); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Method getPrincipal. |
---|
84 | * @param shortName |
---|
85 | * @return Principal |
---|
86 | * returns null if cannot find one. |
---|
87 | */ |
---|
88 | public Principal getPrincipal(String shortName) |
---|
89 | { |
---|
90 | return (Principal) principals.get(shortName); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Method getPrincipal. |
---|
95 | * @param principal An instance of a principal object, currently a hash |
---|
96 | * @return the short name id used for human readable formatting |
---|
97 | * returns <CODE>null</CODE> if cannot find one. |
---|
98 | */ |
---|
99 | public String getPrincipal(Principal principal) { |
---|
100 | return (String)shortNames.get(principal); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Method setIssuer. |
---|
105 | * @param issuer |
---|
106 | */ |
---|
107 | public void setIssuer(Principal issuer) |
---|
108 | { |
---|
109 | this.issuer = issuer; |
---|
110 | } |
---|
111 | /** |
---|
112 | * Returns the issuer. |
---|
113 | * @return Principal |
---|
114 | */ |
---|
115 | public Principal getIssuer() |
---|
116 | { |
---|
117 | return issuer; |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * Returns the validityTime. |
---|
122 | * @return ValidityTime |
---|
123 | */ |
---|
124 | public ValidityTime getValidityTime() |
---|
125 | { |
---|
126 | return validityTime; |
---|
127 | } |
---|
128 | /** |
---|
129 | * Method setValidityTime. |
---|
130 | * @param validityTime |
---|
131 | */ |
---|
132 | public void setValidityTime(ValidityTime validityTime) |
---|
133 | { |
---|
134 | this.validityTime = validityTime; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * accessor method for retrieving the underlying DOM object |
---|
139 | */ |
---|
140 | public Element getCredentialElement() { |
---|
141 | return credentialElement; |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Method toString. |
---|
146 | * @param indent |
---|
147 | * @return String |
---|
148 | */ |
---|
149 | public String toString(String indent) |
---|
150 | { |
---|
151 | String thisIndent = indent + " "; |
---|
152 | StringBuffer sb = new StringBuffer(); |
---|
153 | |
---|
154 | sb.append(thisIndent).append( |
---|
155 | "CredentialDomain: hashID = " |
---|
156 | + getHashID().toString() |
---|
157 | + "\n"); |
---|
158 | sb |
---|
159 | .append(thisIndent + " ") |
---|
160 | .append("PrincipalType = ") |
---|
161 | .append(getPrincipalType()) |
---|
162 | .append("\n"); |
---|
163 | |
---|
164 | sb.append(thisIndent + " ").append("Imported Domains: \n"); |
---|
165 | try |
---|
166 | { |
---|
167 | Iterator it = getImportedDomains().keyIterator(); |
---|
168 | while (it.hasNext()) |
---|
169 | { |
---|
170 | String key = (String) it.next(); |
---|
171 | ApplicationDomain value = |
---|
172 | (ApplicationDomain) getImportedDomains().get(key); |
---|
173 | sb.append(thisIndent + " ").append(key).append( |
---|
174 | "\n"); |
---|
175 | sb.append(thisIndent + " ").append( |
---|
176 | value.getUri()).append( |
---|
177 | "\n"); |
---|
178 | } |
---|
179 | |
---|
180 | } |
---|
181 | catch (DomainSpecException e) |
---|
182 | { |
---|
183 | e.printStackTrace(); |
---|
184 | } |
---|
185 | |
---|
186 | sb.append(thisIndent + " ").append("Type Declarations: \n"); |
---|
187 | try |
---|
188 | { |
---|
189 | Iterator it = getTypeDeclarations().keyIterator(); |
---|
190 | while (it.hasNext()) |
---|
191 | { |
---|
192 | String key = (String) it.next(); |
---|
193 | DataType value = |
---|
194 | (DataType) getTypeDeclarations().get(key); |
---|
195 | sb.append(thisIndent + " ").append(key).append( |
---|
196 | "\n"); |
---|
197 | sb.append( |
---|
198 | value.toString(thisIndent + " ")).append( |
---|
199 | "\n"); |
---|
200 | } |
---|
201 | } |
---|
202 | catch (DomainSpecException e) |
---|
203 | { |
---|
204 | e.printStackTrace(); |
---|
205 | } |
---|
206 | |
---|
207 | sb.append(thisIndent + " ").append("Role Declarations: \n"); |
---|
208 | try |
---|
209 | { |
---|
210 | Iterator it = getRoleDeclarations().keyIterator(); |
---|
211 | while (it.hasNext()) |
---|
212 | { |
---|
213 | String key = (String) it.next(); |
---|
214 | RoleDeclaration value = |
---|
215 | (RoleDeclaration) getRoleDeclarations().get(key); |
---|
216 | sb.append(thisIndent + " ").append(key).append( |
---|
217 | "\n"); |
---|
218 | sb.append( |
---|
219 | value.toString(thisIndent + " ")).append( |
---|
220 | "\n"); |
---|
221 | } |
---|
222 | } |
---|
223 | catch (DomainSpecException e) |
---|
224 | { |
---|
225 | e.printStackTrace(); |
---|
226 | } |
---|
227 | return sb.toString(); |
---|
228 | } // end of toString() |
---|
229 | |
---|
230 | /* (non-Javadoc) |
---|
231 | * @see edu.stanford.rt.credential.DomainSpecification#setHashID(HashID) |
---|
232 | */ |
---|
233 | public void setHashID(HashID hashID) throws DomainSpecException |
---|
234 | { |
---|
235 | if (isComplete()) |
---|
236 | throw new DomainSpecException("CredentialDomain unmodifiable."); |
---|
237 | if (hashID.getHashType() != HashID.CREDENTIAL_DOMAIN) |
---|
238 | throw new DomainSpecException("Wrong type of hash id"); |
---|
239 | super.setHashID(hashID); |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * Method roleDefinitionIterator. |
---|
244 | * @return Iterator |
---|
245 | */ |
---|
246 | public Iterator roleDefinitionIterator() |
---|
247 | { |
---|
248 | return roleDefinitions.iterator(); |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * Add a new role definition. |
---|
253 | * @param roleDefinition The roleDefinition to set |
---|
254 | */ |
---|
255 | public void addRoleDefinition(RoleDefinition roleDefinition) |
---|
256 | { |
---|
257 | this.roleDefinitions.add(roleDefinition); |
---|
258 | } |
---|
259 | /** |
---|
260 | * Method lookupType. |
---|
261 | * Look up the DataType declaration by given typeName in the current domain. |
---|
262 | * if cannot find one then look it up in system domain. |
---|
263 | * @param typeName |
---|
264 | * @return DataType |
---|
265 | * @throws DomainSpecException |
---|
266 | * if cannot find a DataType declaration by the given name. |
---|
267 | */ |
---|
268 | public DataType lookupType(String typeName) |
---|
269 | throws DomainSpecException |
---|
270 | { |
---|
271 | //RTUtil.debugInfo(" lookupType()>>>> isSystemDomain = " + isSystemDomain); |
---|
272 | DataType type = super.lookupType(typeName); |
---|
273 | if (type == null) |
---|
274 | { |
---|
275 | type = |
---|
276 | getContext().getSystemDomain().lookupType(typeName); |
---|
277 | } |
---|
278 | if (type == null) |
---|
279 | { |
---|
280 | throw new DomainSpecException( |
---|
281 | "Type '" + typeName + "' not found."); |
---|
282 | } |
---|
283 | return type; |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * Method lookupRoleDeclaration. |
---|
288 | * Look up the role declaration by given roleName in the current domain. |
---|
289 | * If cannot find one then look it up in system domain. |
---|
290 | * @param roleName |
---|
291 | * @return RoleDeclaration |
---|
292 | * @throws DomainSpecException |
---|
293 | * if cannot find one. |
---|
294 | */ |
---|
295 | public RoleDeclaration lookupRoleDeclaration(String roleName) |
---|
296 | throws DomainSpecException |
---|
297 | { |
---|
298 | RTUtil.debugInfo( |
---|
299 | "DomainSpecification(id=" |
---|
300 | + ((getHashID() == null) |
---|
301 | ? "null" |
---|
302 | : getHashID().toString()) |
---|
303 | + ") looking up role '" |
---|
304 | + roleName |
---|
305 | + "'"); |
---|
306 | |
---|
307 | RoleDeclaration role = super.lookupRoleDeclaration(roleName); |
---|
308 | if (role == null) |
---|
309 | { |
---|
310 | role = |
---|
311 | getContext().getSystemDomain().lookupRoleDeclaration( |
---|
312 | roleName); |
---|
313 | } |
---|
314 | if (role == null) |
---|
315 | { |
---|
316 | throw new DomainSpecException( |
---|
317 | "Role '" + roleName + "' has not been declared."); |
---|
318 | } |
---|
319 | return role; |
---|
320 | } |
---|
321 | } |
---|