1 | package edu.stanford.rt.credential; |
---|
2 | |
---|
3 | //imports Apache Xerces API |
---|
4 | import org.apache.xerces.parsers.DOMParser; |
---|
5 | |
---|
6 | import org.xml.sax.InputSource; |
---|
7 | import org.w3c.dom.*; |
---|
8 | |
---|
9 | import java.util.*; |
---|
10 | import java.io.*; |
---|
11 | |
---|
12 | import edu.stanford.rt.parser.RTParser; |
---|
13 | import edu.stanford.rt.util.*; |
---|
14 | /** |
---|
15 | * @author Ninghui Li, Sandra Qiu <br> |
---|
16 | * |
---|
17 | * RTContext contains the info about <code>ApplicationDomainSecification</code>s and |
---|
18 | * <code>Principal</code>s in the system. |
---|
19 | */ |
---|
20 | public class RTContext |
---|
21 | { |
---|
22 | /** |
---|
23 | * Index for <code>ApplicationDomain</code> objects.<br> |
---|
24 | * |
---|
25 | * Key: <code>HashID</code> <br> |
---|
26 | * Value: <code>ApplicationDomain</code> |
---|
27 | */ |
---|
28 | private HashMap applicationDomains; |
---|
29 | |
---|
30 | /** |
---|
31 | * Index for <code>Principal</code> objects.<br> |
---|
32 | * |
---|
33 | * Key: <code>PublicKeyPrincipal</code> <br> |
---|
34 | * Value: <code>PublicKeyPrincipalInfo</code> |
---|
35 | */ |
---|
36 | private HashMap principals; |
---|
37 | |
---|
38 | /** |
---|
39 | * The built-in system domain. |
---|
40 | */ |
---|
41 | private ApplicationDomain systemDomain; |
---|
42 | |
---|
43 | /** |
---|
44 | * Constructor for RTContext. |
---|
45 | */ |
---|
46 | public RTContext(RTParser rtParser) throws Exception |
---|
47 | { |
---|
48 | this.applicationDomains = new HashMap(); |
---|
49 | this.principals = new HashMap(); |
---|
50 | // others need to call parseSystemDomain(InputStream). |
---|
51 | this.systemDomain = rtParser.createSystemDomain(); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Method getSystemDomain. |
---|
56 | * @return ApplicationDomain |
---|
57 | */ |
---|
58 | public ApplicationDomain getSystemDomain() |
---|
59 | { |
---|
60 | return systemDomain; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Method getApplicationDomain. |
---|
65 | * @param id |
---|
66 | * @return ApplicationDomain |
---|
67 | */ |
---|
68 | public ApplicationDomain getApplicationDomain(String id) |
---|
69 | { |
---|
70 | return getApplicationDomain( |
---|
71 | new HashID(HashID.APPLICATION_DOMAIN, id)); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Method getApplicationDomain. |
---|
76 | * @param id |
---|
77 | * @return ApplicationDomain |
---|
78 | */ |
---|
79 | public ApplicationDomain getApplicationDomain(HashID id) |
---|
80 | { |
---|
81 | RTUtil.debugInfo("RTContex.getApplicationDomain() .... "); |
---|
82 | RTUtil.debugInfo(" id = " + id.toString()); |
---|
83 | return (ApplicationDomain) applicationDomains.get(id); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Method addApplicationDomain. |
---|
88 | * @param id |
---|
89 | * @param appDomain |
---|
90 | * @throws DomainSpecException |
---|
91 | */ |
---|
92 | public synchronized void addApplicationDomain( |
---|
93 | HashID id, |
---|
94 | ApplicationDomain appDomain) |
---|
95 | throws DomainSpecException |
---|
96 | { |
---|
97 | // RTUtil.debugInfo("RTContex.addApplicationDomain() .... "); |
---|
98 | // RTUtil.debugInfo(" id = " + id.toString()); |
---|
99 | // RTUtil.debugInfo("&&&&& "); |
---|
100 | // RTUtil.debugInfo(appDomain.toString(" ")); |
---|
101 | // RTUtil.debugInfo("&&&&& "); |
---|
102 | |
---|
103 | if (this.applicationDomains.get(id) != null) |
---|
104 | throw new DomainSpecException("Duplicated domains in CredentialStore."); |
---|
105 | this.applicationDomains.put(id, appDomain); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Method getPrincipal. |
---|
110 | * @param id |
---|
111 | * @return PublicKeyPrincipalInfo |
---|
112 | */ |
---|
113 | public PublicKeyPrincipalInfo getPrincipal(PublicKeyPrincipal id) |
---|
114 | { |
---|
115 | return (PublicKeyPrincipalInfo) principals.get(id); |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Method addPrincipals. |
---|
120 | * @param id |
---|
121 | * the hash value of the principal |
---|
122 | * @param principal |
---|
123 | * the public key info of the principal |
---|
124 | * @throws DomainSpecException |
---|
125 | */ |
---|
126 | public synchronized void addPrincipal( |
---|
127 | PublicKeyPrincipal id, |
---|
128 | PublicKeyPrincipalInfo principal) |
---|
129 | throws DomainSpecException |
---|
130 | { |
---|
131 | if (this.principals.get(id) != null) |
---|
132 | throw new DomainSpecException("Duplicated"); |
---|
133 | this.principals.put(id, principal); |
---|
134 | } |
---|
135 | /** |
---|
136 | * Method hasDomainWithID. |
---|
137 | * checks if there is an ApplicationDomain object with given hash id. |
---|
138 | * @param idref |
---|
139 | * @return boolean |
---|
140 | */ |
---|
141 | public boolean hasDomainWithID(String idref) |
---|
142 | { |
---|
143 | boolean exists = true; |
---|
144 | if (getApplicationDomain(idref) == null) |
---|
145 | exists = false; |
---|
146 | return exists; |
---|
147 | } |
---|
148 | |
---|
149 | public String toString(String indent) |
---|
150 | { |
---|
151 | StringBuffer sb = new StringBuffer(); |
---|
152 | String thisIndent = indent + " "; |
---|
153 | |
---|
154 | sb.append( |
---|
155 | "********** RTContext: **********\n\n"); |
---|
156 | sb.append(thisIndent).append("System Domain: \n"); |
---|
157 | sb.append(systemDomain.toString(thisIndent)); |
---|
158 | |
---|
159 | sb.append(thisIndent).append("Imported Domains: \n"); |
---|
160 | Set entries = applicationDomains.entrySet(); |
---|
161 | Iterator it = entries.iterator(); |
---|
162 | while (it.hasNext()) |
---|
163 | { |
---|
164 | Map.Entry entry = (Map.Entry) it.next(); |
---|
165 | ApplicationDomain domain = |
---|
166 | (ApplicationDomain) entry.getValue(); |
---|
167 | sb.append(domain.toString(thisIndent)); |
---|
168 | sb.append("\n"); |
---|
169 | } |
---|
170 | |
---|
171 | sb.append(thisIndent).append("Principals: \n"); |
---|
172 | entries = principals.entrySet(); |
---|
173 | it = entries.iterator(); |
---|
174 | while (it.hasNext()) |
---|
175 | { |
---|
176 | Map.Entry entry = (Map.Entry) it.next(); |
---|
177 | PublicKeyPrincipalInfo principal = |
---|
178 | (PublicKeyPrincipalInfo) entry.getValue(); |
---|
179 | sb.append(principal.toString(thisIndent)); |
---|
180 | sb.append("\n"); |
---|
181 | } |
---|
182 | |
---|
183 | return sb.toString(); |
---|
184 | } |
---|
185 | } |
---|