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.outerj.daisy.repository.commonimpl.AuthenticatedUser;
021: import org.outerj.daisy.repository.RepositoryException;
022: import org.outerx.daisy.x10.*;
023:
024: import java.util.*;
025:
026: public final class AclImpl implements Acl {
027: private List<AclObjectImpl> objects = new ArrayList<AclObjectImpl>();
028: private boolean readOnly = false;
029: private AclStrategy aclStrategy;
030: private Date lastModified;
031: private long lastModifier = -1;
032: private long id;
033: private AuthenticatedUser currentModifier;
034: private long updateCount = 0;
035: private IntimateAccess intimateAccess = new IntimateAccess();
036: protected static final String READ_ONLY_MESSAGE = "This ACL cannot be modified.";
037:
038: public AclImpl(AclStrategy aclStrategy, Date lastModified,
039: long lastModifier, long id,
040: AuthenticatedUser currentModifier, long updateCount) {
041: this .aclStrategy = aclStrategy;
042: this .lastModified = lastModified;
043: this .lastModifier = lastModifier;
044: this .id = id;
045: this .currentModifier = currentModifier;
046: this .updateCount = updateCount;
047: }
048:
049: public IntimateAccess getIntimateAccess(AclStrategy aclStrategy) {
050: if (aclStrategy == this .aclStrategy)
051: return intimateAccess;
052: else
053: return null;
054: }
055:
056: public AclObject createNewObject(String objectExpression) {
057: if (readOnly)
058: throw new RuntimeException(READ_ONLY_MESSAGE);
059:
060: return new AclObjectImpl(this , aclStrategy, objectExpression);
061: }
062:
063: public boolean isReadOnly() {
064: return readOnly;
065: }
066:
067: public void makeReadOnly() {
068: this .readOnly = true;
069: }
070:
071: public AclObject get(int index) {
072: return objects.get(index);
073: }
074:
075: public void remove(int index) {
076: if (readOnly)
077: throw new RuntimeException(READ_ONLY_MESSAGE);
078:
079: objects.remove(index).setIsAdded(false);
080: }
081:
082: public void add(AclObject aclObject) {
083: preAddChecks(aclObject);
084: AclObjectImpl aclObjectImpl = (AclObjectImpl) aclObject;
085: aclObjectImpl.setIsAdded(true);
086: objects.add(aclObjectImpl);
087: }
088:
089: public void add(int index, AclObject aclObject) {
090: preAddChecks(aclObject);
091: AclObjectImpl aclObjectImpl = (AclObjectImpl) aclObject;
092: aclObjectImpl.setIsAdded(true);
093: objects.add(index, aclObjectImpl);
094: }
095:
096: private void preAddChecks(AclObject aclObject) {
097: if (readOnly)
098: throw new RuntimeException(READ_ONLY_MESSAGE);
099:
100: if (aclObject == null)
101: throw new NullPointerException(
102: "null AclObject not allowed.");
103:
104: if (!(aclObject instanceof AclObjectImpl))
105: throw new RuntimeException(
106: "Incorrect AclObject implementation provided, only the ones obtained using createNewObject() on this AclObject may be used.");
107:
108: AclObjectImpl aclObjectImpl = (AclObjectImpl) aclObject;
109:
110: if (aclObjectImpl.getOwner() != this )
111: throw new RuntimeException(
112: "The specified AclObject belongs to a different Acl, it cannot be added to this Acl.");
113:
114: if (aclObjectImpl.isAdded())
115: throw new RuntimeException(
116: "The specified AclObject is already added to the ACL.");
117: }
118:
119: public void clear() {
120: if (readOnly)
121: throw new RuntimeException(READ_ONLY_MESSAGE);
122:
123: for (AclObjectImpl object : objects) {
124: object.setIsAdded(false);
125: }
126:
127: objects.clear();
128: }
129:
130: public int size() {
131: return objects.size();
132: }
133:
134: public Date getLastModified() {
135: if (lastModified != null)
136: return (Date) lastModified.clone();
137: else
138: return null;
139: }
140:
141: public long getLastModifier() {
142: return lastModifier;
143: }
144:
145: public void save() throws RepositoryException {
146: aclStrategy.storeAcl(this );
147: }
148:
149: public AclDocument getXml() {
150: AclDocument aclDocument = AclDocument.Factory.newInstance();
151: AclDocument.Acl aclXml = aclDocument.addNewAcl();
152:
153: aclXml.setId(id);
154: GregorianCalendar lastModifiedCalendar = new GregorianCalendar();
155: lastModifiedCalendar.setTime(lastModified);
156: aclXml.setLastModified(lastModifiedCalendar);
157: aclXml.setLastModifier(lastModifier);
158: aclXml.setUpdateCount(updateCount);
159:
160: AclObjectDocument.AclObject[] aclObjectsXml = new AclObjectDocument.AclObject[objects
161: .size()];
162: for (int i = 0; i < aclObjectsXml.length; i++) {
163: AclObject aclObject = objects.get(i);
164: aclObjectsXml[i] = aclObject.getXml().getAclObject();
165: }
166:
167: aclXml.setAclObjectArray(aclObjectsXml);
168:
169: return aclDocument;
170: }
171:
172: public void setFromXml(AclDocument.Acl aclXml) {
173: if (readOnly)
174: throw new RuntimeException(READ_ONLY_MESSAGE);
175:
176: clear();
177:
178: for (AclObjectDocument.AclObject objectXml : aclXml
179: .getAclObjectList()) {
180: AclObject object = createNewObject(objectXml
181: .getExpression());
182:
183: for (AclEntryDocument.AclEntry entryXml : objectXml
184: .getAclEntryList()) {
185: AclSubjectType subjectType = AclSubjectType
186: .fromString(entryXml.getSubjectType()
187: .toString());
188: AclEntry entry = object.createNewEntry(subjectType,
189: entryXml.getSubjectValue());
190:
191: for (PermissionsDocument.Permissions.Permission permissionXml : entryXml
192: .getPermissions().getPermissionList()) {
193: AclPermission permission = AclPermission
194: .fromString(permissionXml.getType()
195: .toString());
196: AclActionType actionType = AclActionType
197: .fromString(permissionXml.getAction()
198: .toString());
199:
200: AccessDetails details = null;
201: if (permissionXml.isSetAccessDetails()) {
202: details = new AccessDetailsImpl(this );
203: details.setFromXml(permissionXml
204: .getAccessDetails());
205: }
206: entry.set(permission, actionType, details);
207: }
208:
209: object.add(entry);
210: }
211:
212: add(object);
213: }
214: }
215:
216: public long getUpdateCount() {
217: return updateCount;
218: }
219:
220: public class IntimateAccess {
221: private IntimateAccess() {
222: }
223:
224: public long getId() {
225: return id;
226: }
227:
228: public void setId(long id) {
229: if (readOnly)
230: throw new RuntimeException(READ_ONLY_MESSAGE);
231:
232: AclImpl.this .id = id;
233: }
234:
235: public AuthenticatedUser getCurrentModifier() {
236: return currentModifier;
237: }
238:
239: public void setLastModified(Date lastModified) {
240: if (readOnly)
241: throw new RuntimeException(READ_ONLY_MESSAGE);
242:
243: AclImpl.this .lastModified = lastModified;
244: }
245:
246: public void setLastModifier(long lastModifier) {
247: if (readOnly)
248: throw new RuntimeException(READ_ONLY_MESSAGE);
249:
250: AclImpl.this .lastModifier = lastModifier;
251: }
252:
253: public void setUpdateCount(long updateCount) {
254: if (readOnly)
255: throw new RuntimeException(READ_ONLY_MESSAGE);
256:
257: AclImpl.this .updateCount = updateCount;
258: }
259:
260: public List<AclObjectImpl> getObjects() {
261: return objects;
262: }
263: }
264: }
|