[8780cbec] | 1 | package edu.stanford.rt.credential; |
---|
| 2 | |
---|
| 3 | import java.util.Iterator; |
---|
| 4 | |
---|
| 5 | import edu.stanford.rt.datatype.*; |
---|
| 6 | import edu.stanford.rt.util.*; |
---|
| 7 | /** |
---|
| 8 | * @author Ninghui Li, Sandra Qiu<br> |
---|
| 9 | * |
---|
| 10 | * Implementation of <code>ApplicationDomain</code> element. |
---|
| 11 | * |
---|
| 12 | */ |
---|
| 13 | public class ApplicationDomain extends DomainSpecification |
---|
| 14 | { |
---|
| 15 | /** |
---|
| 16 | * Attribute uri (required). |
---|
| 17 | */ |
---|
| 18 | private String uri; |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * A flag indicates whether this domain is the built-in system one. |
---|
| 22 | */ |
---|
| 23 | private boolean isSystemDomain = false; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * Constructor for ApplicationDomain. |
---|
| 27 | * @param uri |
---|
| 28 | * @param isSystemDomain |
---|
| 29 | * @param context |
---|
| 30 | * @throws DomainSpecException |
---|
| 31 | */ |
---|
| 32 | public ApplicationDomain( |
---|
| 33 | String uri, |
---|
| 34 | boolean isSystemDomain, |
---|
| 35 | RTContext context) |
---|
| 36 | throws DomainSpecException |
---|
| 37 | { |
---|
| 38 | super(context); |
---|
| 39 | this.uri = uri; |
---|
| 40 | this.isSystemDomain = isSystemDomain; |
---|
| 41 | } |
---|
| 42 | /** |
---|
| 43 | * Method getUri. |
---|
| 44 | * @return String |
---|
| 45 | */ |
---|
| 46 | public String getUri() |
---|
| 47 | { |
---|
| 48 | return uri; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Method lookupType. |
---|
| 53 | * Look up the DataType declaration by given typeName |
---|
| 54 | * in the current domain. If cannot find one and this domain is |
---|
| 55 | * not system domain, then look up in the system domain. |
---|
| 56 | * @param typeName |
---|
| 57 | * @return DataType |
---|
| 58 | * @throws DomainSpecException |
---|
| 59 | * if cannot find a DataType declaration by the given name. |
---|
| 60 | */ |
---|
| 61 | public DataType lookupType(String typeName) |
---|
| 62 | throws DomainSpecException |
---|
| 63 | { |
---|
| 64 | //RTUtil.debugInfo(" lookupType()>>>> isSystemDomain = " + isSystemDomain); |
---|
| 65 | DataType type = super.lookupType(typeName); |
---|
| 66 | |
---|
| 67 | if (!isSystemDomain && type == null) |
---|
| 68 | { |
---|
| 69 | type = |
---|
| 70 | getContext().getSystemDomain().lookupType(typeName); |
---|
| 71 | } |
---|
| 72 | if (type == null) |
---|
| 73 | { |
---|
| 74 | throw new DomainSpecException( |
---|
| 75 | "Type '" + typeName + "' not found."); |
---|
| 76 | } |
---|
| 77 | return type; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Method lookupRoleDeclaration. |
---|
| 82 | * Look up the role declaration by given roleName in the |
---|
| 83 | * current domain. If cannot find one and this domain is not system |
---|
| 84 | * domain, then look it up in system domain. |
---|
| 85 | * @param roleName |
---|
| 86 | * @return RoleDeclaration |
---|
| 87 | * @throws DomainSpecException |
---|
| 88 | * if cannot find one. |
---|
| 89 | */ |
---|
| 90 | public RoleDeclaration lookupRoleDeclaration(String roleName) |
---|
| 91 | throws DomainSpecException |
---|
| 92 | { |
---|
| 93 | RTUtil.debugInfo( |
---|
| 94 | "DomainSpecification(id=" |
---|
| 95 | + ((getHashID() == null) |
---|
| 96 | ? "null" |
---|
| 97 | : getHashID().toString()) |
---|
| 98 | + ") looking up role '" |
---|
| 99 | + roleName |
---|
| 100 | + "'"); |
---|
| 101 | |
---|
| 102 | RoleDeclaration role = super.lookupRoleDeclaration(roleName); |
---|
| 103 | if (!isSystemDomain && role == null) |
---|
| 104 | { |
---|
| 105 | role = |
---|
| 106 | getContext().getSystemDomain().lookupRoleDeclaration( |
---|
| 107 | roleName); |
---|
| 108 | } |
---|
| 109 | if (role == null) |
---|
| 110 | { |
---|
| 111 | throw new DomainSpecException( |
---|
| 112 | "Role '" + roleName + "' has not been declared."); |
---|
| 113 | } |
---|
| 114 | return role; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | public String toString(String indent) |
---|
| 118 | { |
---|
| 119 | String thisIndent = indent + " "; |
---|
| 120 | StringBuffer sb = new StringBuffer(); |
---|
| 121 | |
---|
| 122 | sb.append(thisIndent).append( |
---|
| 123 | "ApplicationDomain: hashID = " |
---|
| 124 | + getHashID().toString() |
---|
| 125 | + "\n"); |
---|
| 126 | sb.append(thisIndent + " ").append("uri = ").append( |
---|
| 127 | uri).append( |
---|
| 128 | "\n"); |
---|
| 129 | sb |
---|
| 130 | .append(thisIndent + " ") |
---|
| 131 | .append("PrincipalType = ") |
---|
| 132 | .append(getPrincipalType()) |
---|
| 133 | .append("\n"); |
---|
| 134 | |
---|
| 135 | sb.append(thisIndent + " ").append("Imported Domains: \n"); |
---|
| 136 | try |
---|
| 137 | { |
---|
| 138 | Iterator it = getImportedDomains().keyIterator(); |
---|
| 139 | while (it.hasNext()) |
---|
| 140 | { |
---|
| 141 | String key = (String) it.next(); |
---|
| 142 | ApplicationDomain value = |
---|
| 143 | (ApplicationDomain) getImportedDomains().get(key); |
---|
| 144 | sb.append(thisIndent + " ").append(key).append( |
---|
| 145 | "\n"); |
---|
| 146 | sb.append(thisIndent + " ").append( |
---|
| 147 | value.getUri()).append( |
---|
| 148 | "\n"); |
---|
| 149 | |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | catch (DomainSpecException e) |
---|
| 153 | { |
---|
| 154 | e.printStackTrace(); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | sb.append(thisIndent + " ").append("Type Declarations: \n"); |
---|
| 158 | try |
---|
| 159 | { |
---|
| 160 | Iterator it = getTypeDeclarations().keyIterator(); |
---|
| 161 | while (it.hasNext()) |
---|
| 162 | { |
---|
| 163 | String key = (String) it.next(); |
---|
| 164 | DataType value = |
---|
| 165 | (DataType) getTypeDeclarations().get(key); |
---|
| 166 | |
---|
| 167 | sb |
---|
| 168 | .append(thisIndent + " ") |
---|
| 169 | .append(key) |
---|
| 170 | .append("\n") |
---|
| 171 | .append(value.toString(thisIndent + " ")) |
---|
| 172 | .append("\n"); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | catch (DomainSpecException e) |
---|
| 176 | { |
---|
| 177 | e.printStackTrace(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | sb.append(thisIndent + " ").append("Role Declarations: \n"); |
---|
| 181 | try |
---|
| 182 | { |
---|
| 183 | Iterator it = getRoleDeclarations().keyIterator(); |
---|
| 184 | while (it.hasNext()) |
---|
| 185 | { |
---|
| 186 | String key = (String) it.next(); |
---|
| 187 | RoleDeclaration value = |
---|
| 188 | (RoleDeclaration) getRoleDeclarations().get(key); |
---|
| 189 | sb.append(thisIndent + " ").append(key).append( |
---|
| 190 | "\n"); |
---|
| 191 | sb.append( |
---|
| 192 | value.toString(thisIndent + " ")).append( |
---|
| 193 | "\n"); |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | catch (DomainSpecException e) |
---|
| 197 | { |
---|
| 198 | e.printStackTrace(); |
---|
| 199 | } |
---|
| 200 | return sb.toString(); |
---|
| 201 | } // end of toString() |
---|
| 202 | |
---|
| 203 | /* (non-Javadoc) |
---|
| 204 | * @see edu.stanford.rt.credential.DomainSpecification#setHashID(HashID) |
---|
| 205 | */ |
---|
| 206 | public synchronized void setHashID(HashID hashID) |
---|
| 207 | throws DomainSpecException |
---|
| 208 | { |
---|
| 209 | if (isComplete()) |
---|
| 210 | throw new DomainSpecException("ApplicationDomain unmodifiable."); |
---|
| 211 | if (hashID.getHashType() != HashID.APPLICATION_DOMAIN) |
---|
| 212 | throw new DomainSpecException("Wrong type of hash id"); |
---|
| 213 | super.setHashID(hashID); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | } |
---|