001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.commonimpl.acl;
017:
018: import org.outerj.daisy.repository.acl.*;
019: import org.outerj.daisy.repository.acl.AclPermission;
020: import org.outerx.daisy.x10.*;
021:
022: import java.util.EnumMap;
023:
024: public final class AclEntryImpl implements AclEntry {
025: private AclSubjectType subjectType;
026: private long subjectValue;
027: private EnumMap<AclPermission, Access> permissions = new EnumMap<AclPermission, Access>(
028: AclPermission.class);
029: private AclImpl ownerAcl;
030: private boolean isAdded;
031:
032: public AclEntryImpl(AclImpl ownerAcl, AclSubjectType subjectType,
033: long subjectValue) {
034: if (subjectType == null)
035: throw new NullPointerException(
036: "null AclSubjectType not allowed.");
037: if (subjectType == AclSubjectType.EVERYONE
038: && subjectValue != -1)
039: throw new IllegalArgumentException(
040: "subjectValue should be -1 if subjectType is EVERYONE");
041: else if (subjectType != AclSubjectType.EVERYONE
042: && subjectValue == -1)
043: throw new IllegalArgumentException(
044: "subjectValue should only be -1 if subjectType is EVERYONE");
045:
046: this .ownerAcl = ownerAcl;
047: this .subjectType = subjectType;
048: this .subjectValue = subjectValue;
049: }
050:
051: protected AclImpl getOwner() {
052: return ownerAcl;
053: }
054:
055: public AclActionType get(AclPermission aclPermission) {
056: if (!permissions.containsKey(aclPermission))
057: return AclActionType.DO_NOTHING;
058: else
059: return permissions.get(aclPermission).action;
060: }
061:
062: public AccessDetails getDetails(AclPermission aclPermission) {
063: if (!permissions.containsKey(aclPermission))
064: return null;
065: else
066: return permissions.get(aclPermission).details;
067: }
068:
069: public void set(AclPermission permission, AclActionType action) {
070: if (ownerAcl.isReadOnly())
071: throw new RuntimeException(AclImpl.READ_ONLY_MESSAGE);
072:
073: permissions.put(permission, new Access(action));
074: }
075:
076: public void set(AclPermission permission, AclActionType action,
077: AccessDetails accessDetails) {
078: if (ownerAcl.isReadOnly())
079: throw new RuntimeException(AclImpl.READ_ONLY_MESSAGE);
080:
081: if (accessDetails != null && !action.supportsAccessDetails())
082: throw new RuntimeException(
083: "Access details are not support for action type \""
084: + action + "\".");
085:
086: if (permissions.containsKey(permission)) {
087: AccessDetailsImpl currentDetails = (AccessDetailsImpl) permissions
088: .get(permission).details;
089: if (currentDetails != null)
090: currentDetails.setIsAdded(false);
091: }
092:
093: if (accessDetails != null) {
094: preAddChecks(accessDetails);
095: ((AccessDetailsImpl) accessDetails).setIsAdded(true);
096: }
097:
098: permissions.put(permission, new Access(action, accessDetails));
099: }
100:
101: public void setAll(AclActionType action) {
102: if (ownerAcl.isReadOnly())
103: throw new RuntimeException(AclImpl.READ_ONLY_MESSAGE);
104:
105: for (AclPermission permission : AclPermission.values()) {
106: set(permission, action);
107: }
108: }
109:
110: public long getSubjectValue() {
111: return subjectValue;
112: }
113:
114: public AclSubjectType getSubjectType() {
115: return subjectType;
116: }
117:
118: public void setSubjectValue(long value) {
119: if (subjectType == AclSubjectType.EVERYONE && value != -1)
120: throw new IllegalArgumentException(
121: "subjectValue should be -1 if subjectType is EVERYONE");
122: else if (subjectType != AclSubjectType.EVERYONE && value == -1)
123: throw new IllegalArgumentException(
124: "subjectValue should only be -1 if subjectType is EVERYONE");
125:
126: this .subjectValue = value;
127: }
128:
129: public void setSubjectType(AclSubjectType subjectType) {
130: this .subjectType = subjectType;
131: }
132:
133: private void preAddChecks(AccessDetails details) {
134: if (!(details instanceof AccessDetailsImpl))
135: throw new RuntimeException(
136: "Incorrect AccessDetails implementation provided, only the ones obtained using createNewDetail() on this AclEntry may be used.");
137:
138: AccessDetailsImpl detailsImpl = (AccessDetailsImpl) details;
139:
140: if (detailsImpl.getOwner() != ownerAcl)
141: throw new RuntimeException(
142: "The specified AccessDetails belongs to a different Acl, it cannot be added to this AclEntry.");
143:
144: if (detailsImpl.isAdded())
145: throw new RuntimeException(
146: "The specified AccessDetails is already added to the ACL.");
147: }
148:
149: public AclEntryDocument getXml() {
150: AclEntryDocument aclEntryDocument = AclEntryDocument.Factory
151: .newInstance();
152: AclEntryDocument.AclEntry aclEntryXml = aclEntryDocument
153: .addNewAclEntry();
154:
155: aclEntryXml.setSubjectType(AclSubject.Enum
156: .forString(subjectType.toString()));
157: aclEntryXml.setSubjectValue(subjectValue);
158:
159: PermissionsDocument.Permissions permissionsXml = aclEntryXml
160: .addNewPermissions();
161:
162: for (AclPermission permission : AclPermission.values()) {
163: if (permissions.containsKey(permission)) {
164: PermissionsDocument.Permissions.Permission permissionXml = permissionsXml
165: .addNewPermission();
166: permissionXml
167: .setType(org.outerx.daisy.x10.AclPermission.Enum
168: .forString(permission.toString()));
169: permissionXml.setAction(AclAction.Enum
170: .forString(permissions.get(permission).action
171: .toString()));
172:
173: AccessDetails details = getDetails(permission);
174: if (details != null) {
175: permissionXml.setAccessDetails(details.getXml()
176: .getAccessDetails());
177: }
178: }
179: }
180:
181: return aclEntryDocument;
182: }
183:
184: public AccessDetails createNewDetails() {
185: return new AccessDetailsImpl(ownerAcl);
186: }
187:
188: private static class Access {
189: private AclActionType action;
190: private AccessDetails details;
191:
192: public Access(AclActionType action, AccessDetails details) {
193: this .action = action;
194: this .details = details;
195: }
196:
197: public Access(AclActionType action) {
198: this .action = action;
199: this .details = null;
200: }
201: }
202:
203: protected boolean isAdded() {
204: return isAdded;
205: }
206:
207: protected void setIsAdded(boolean isAdded) {
208: this.isAdded = isAdded;
209: }
210: }
|