1 | package edu.stanford.rt.credential; |
---|
2 | |
---|
3 | import org.w3c.dom.Element; |
---|
4 | |
---|
5 | import java.util.*; |
---|
6 | |
---|
7 | import edu.stanford.rt.parser.RTParser; |
---|
8 | import edu.stanford.rt.util.*; |
---|
9 | /** |
---|
10 | * @author Ninghui Li, Sandra Qiu<br> |
---|
11 | * |
---|
12 | * This class stores all the <code>CredentialDomain</code>, and |
---|
13 | * <code>RoleDefinition</code> index information. |
---|
14 | * <p> |
---|
15 | * A special principal is stored in the credential store to .... |
---|
16 | * <p> |
---|
17 | * |
---|
18 | */ |
---|
19 | public class CredentialStore extends RTContext |
---|
20 | { |
---|
21 | /** |
---|
22 | */ |
---|
23 | private Principal specialPrincipal; |
---|
24 | |
---|
25 | /** |
---|
26 | * A map of <code>CredentialDomain</code> objects. A CredentialDomain |
---|
27 | * contains all the info about a <code>Credential</code>.<br> |
---|
28 | * |
---|
29 | * Key: <code>HashID</code> <br> |
---|
30 | * Value: <code>CredentialDomain</code> |
---|
31 | */ |
---|
32 | private HashMap credentialDomains; |
---|
33 | |
---|
34 | /** |
---|
35 | * A set of <code>RoleDefinition</code> objects, including all |
---|
36 | * roles defined in the <code>Credential</code> included in this |
---|
37 | * <code>CredentialStore</code>. |
---|
38 | */ |
---|
39 | private HashSet roleDefinitions; |
---|
40 | |
---|
41 | /** |
---|
42 | * Role definitions indexed by issuer. <br> |
---|
43 | * |
---|
44 | * Key: <code>Principal</code>: the issuer <br> |
---|
45 | * Value: an ArrayList of <code>RoleDefinition</code>. |
---|
46 | */ |
---|
47 | private HashMap indexByIssuer; |
---|
48 | |
---|
49 | /** |
---|
50 | * Role definitions indexed by issuer and role declaration. |
---|
51 | * The issuer is the issuer of the credential and the role |
---|
52 | * declaration is the one corresponding to the head role term in the |
---|
53 | * role definition. |
---|
54 | * <br> |
---|
55 | * |
---|
56 | * Key: <code>IssuerAndRoleDeclaration</code> <br> |
---|
57 | * Value: an ArrayList of <code>RoleDefinition</code> |
---|
58 | */ |
---|
59 | private HashMap indexByIssuerAndRoleDeclaration; |
---|
60 | |
---|
61 | /** |
---|
62 | * Constructor for CredentialStore. |
---|
63 | */ |
---|
64 | public CredentialStore(RTParser rtParser) throws Exception |
---|
65 | { |
---|
66 | super(rtParser); |
---|
67 | this.credentialDomains = new HashMap(); |
---|
68 | this.roleDefinitions = new HashSet(); |
---|
69 | this.indexByIssuer = new HashMap(); |
---|
70 | this.indexByIssuerAndRoleDeclaration = new HashMap(); |
---|
71 | } |
---|
72 | |
---|
73 | /** public accesses method for iterating through all the domains */ |
---|
74 | public HashMap getCredentialDomains() { |
---|
75 | return credentialDomains; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Returns the specialPrincipal. |
---|
80 | * @return Principal |
---|
81 | */ |
---|
82 | public Principal getSpecialPrincipal() |
---|
83 | { |
---|
84 | return specialPrincipal; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Sets the specialPrincipal. |
---|
89 | * @param specialPrincipal The specialPrincipal to set |
---|
90 | */ |
---|
91 | public void setSpecialPrincipal(Principal specialPrincipal) |
---|
92 | { |
---|
93 | this.specialPrincipal = specialPrincipal; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Method getCredentialDomain. |
---|
98 | * @param id |
---|
99 | * @return CredentialDomain |
---|
100 | */ |
---|
101 | public CredentialDomain getCredentialDomain(HashID id) |
---|
102 | { |
---|
103 | return (CredentialDomain) credentialDomains.get(id); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Method getCredentialDomain. |
---|
108 | * @param id |
---|
109 | * @return CredentialDomain |
---|
110 | */ |
---|
111 | public CredentialDomain getCredentialDomain(String id) |
---|
112 | { |
---|
113 | return getCredentialDomain( |
---|
114 | new HashID(HashID.CREDENTIAL_DOMAIN, id)); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Method roleDefinitionIterator. |
---|
119 | * returns an Iterator for the set of role definitions. |
---|
120 | * @return Iterator |
---|
121 | */ |
---|
122 | public Iterator roleDefinitionIterator() |
---|
123 | { |
---|
124 | return roleDefinitions.iterator(); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Method getIndexByIssuer. |
---|
129 | * returns a list of role definitions by the given issuer. |
---|
130 | * @param issuer |
---|
131 | * @return ArrayList |
---|
132 | */ |
---|
133 | public ArrayList getIndexByIssuer(Principal issuer) |
---|
134 | { |
---|
135 | return (ArrayList) indexByIssuer.get(issuer); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Method getIndexByIssuerAndRoleDeclaration. |
---|
140 | * @param issuerAndRoleDeclaration |
---|
141 | * @return ArrayList |
---|
142 | */ |
---|
143 | public ArrayList getIndexByIssuerAndRoleDeclaration( |
---|
144 | IssuerAndRoleDeclaration issuerAndRoleDeclaration) |
---|
145 | { |
---|
146 | return (ArrayList) indexByIssuerAndRoleDeclaration.get( |
---|
147 | issuerAndRoleDeclaration); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Method addCredentialDomain. |
---|
152 | * @param id |
---|
153 | * @param credentialDomain |
---|
154 | * @throws DomainSpecException |
---|
155 | */ |
---|
156 | public synchronized void addCredentialDomain( |
---|
157 | HashID id, |
---|
158 | CredentialDomain credentialDomain) |
---|
159 | throws DomainSpecException |
---|
160 | { |
---|
161 | RTUtil.debugInfo( |
---|
162 | "CredentialStore.addCredentialDomain() .... "); |
---|
163 | RTUtil.debugInfo(" id = " + id.toString()); |
---|
164 | RTUtil.debugInfo(credentialDomain.toString(" ")); |
---|
165 | if (this.credentialDomains.get(id) != null) |
---|
166 | throw new DomainSpecException("Duplicated credentials in CredentialStore."); |
---|
167 | this.credentialDomains.put(id, credentialDomain); |
---|
168 | // get role definitions from the credential domain just added and |
---|
169 | // add them to the credential store. |
---|
170 | Iterator it = credentialDomain.roleDefinitionIterator(); |
---|
171 | while (it.hasNext()) |
---|
172 | { |
---|
173 | addRoleDefinition((RoleDefinition) it.next()); |
---|
174 | } |
---|
175 | } |
---|
176 | /** |
---|
177 | * Adds a new role definition to the roleDefinitions. |
---|
178 | * @param roleDefinition The roleDefinition to set |
---|
179 | */ |
---|
180 | private synchronized void addRoleDefinition( |
---|
181 | RoleDefinition roleDefinition) |
---|
182 | throws DomainSpecException |
---|
183 | { |
---|
184 | if (this.roleDefinitions.contains(roleDefinition)) |
---|
185 | throw new DomainSpecException("Duplicated role definition. "); |
---|
186 | this.roleDefinitions.add(roleDefinition); |
---|
187 | addIndexByIssuer(roleDefinition); |
---|
188 | addIndexByIssuerAndRoleDeclaration(roleDefinition); |
---|
189 | |
---|
190 | } |
---|
191 | /** |
---|
192 | * Method addIndexByIssuer. |
---|
193 | * @param roleDefinition |
---|
194 | */ |
---|
195 | private synchronized void addIndexByIssuer(RoleDefinition roleDefinition) |
---|
196 | { |
---|
197 | Role head = roleDefinition.getHead(); |
---|
198 | Principal issuer = head.getPrincipal(); |
---|
199 | ArrayList list = getIndexByIssuer(issuer); |
---|
200 | if (list == null) |
---|
201 | list = new ArrayList(); |
---|
202 | if (!list.contains(roleDefinition)) |
---|
203 | list.add(roleDefinition); |
---|
204 | // update the map entry. |
---|
205 | indexByIssuer.put(issuer, list); |
---|
206 | } |
---|
207 | /** |
---|
208 | * Method addIndexByIssuerAndRoleDeclaration. |
---|
209 | * @param roleDefinition |
---|
210 | */ |
---|
211 | private synchronized void addIndexByIssuerAndRoleDeclaration(RoleDefinition roleDefinition) |
---|
212 | { |
---|
213 | Role head = roleDefinition.getHead(); |
---|
214 | Principal issuer = head.getPrincipal(); |
---|
215 | RoleDeclaration roleDec = head.getRoleDeclaration(); |
---|
216 | IssuerAndRoleDeclaration index = |
---|
217 | new IssuerAndRoleDeclaration(issuer, roleDec); |
---|
218 | ArrayList list = getIndexByIssuerAndRoleDeclaration(index); |
---|
219 | if (list == null) |
---|
220 | list = new ArrayList(); |
---|
221 | if (!list.contains(roleDefinition)) |
---|
222 | list.add(roleDefinition); |
---|
223 | indexByIssuerAndRoleDeclaration.put(index, list); |
---|
224 | } |
---|
225 | |
---|
226 | public String toString(String indent) |
---|
227 | { |
---|
228 | String thisIndent = indent + " "; |
---|
229 | StringBuffer sb = new StringBuffer(); |
---|
230 | |
---|
231 | sb.append( |
---|
232 | "********* CrendentialStore: **********\n\n"); |
---|
233 | |
---|
234 | sb.append(thisIndent).append("Credential Domains: \n"); |
---|
235 | Set entries = credentialDomains.entrySet(); |
---|
236 | Iterator it = entries.iterator(); |
---|
237 | while (it.hasNext()) |
---|
238 | { |
---|
239 | Map.Entry entry = (Map.Entry) it.next(); |
---|
240 | CredentialDomain credDomain = |
---|
241 | (CredentialDomain) entry.getValue(); |
---|
242 | sb.append(credDomain.toString(thisIndent)); |
---|
243 | sb.append("\n"); |
---|
244 | } |
---|
245 | |
---|
246 | sb.append(thisIndent).append("Role definitions: \n"); |
---|
247 | it = roleDefinitionIterator(); |
---|
248 | while (it.hasNext()) |
---|
249 | { |
---|
250 | RoleDefinition roleDef = (RoleDefinition) it.next(); |
---|
251 | sb.append(roleDef.toString(thisIndent)); |
---|
252 | sb.append("\n"); |
---|
253 | } |
---|
254 | |
---|
255 | return sb.toString(); |
---|
256 | |
---|
257 | } |
---|
258 | } |
---|