1 16 package org.outerj.daisy.repository.commonimpl.acl; 17 18 import org.outerj.daisy.repository.acl.*; 19 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 20 import org.outerj.daisy.repository.RepositoryException; 21 import org.outerx.daisy.x10.AclDocument; 22 import org.outerx.daisy.x10.AclObjectDocument; 23 import org.outerx.daisy.x10.AclEntryDocument; 24 25 import java.util.*; 26 27 public final class AclImpl implements Acl { 28 private ArrayList objects = new ArrayList(); 29 private boolean readOnly = false; 30 private AclStrategy aclStrategy; 31 private Date lastModified; 32 private long lastModifier=-1; 33 private long id; 34 private AuthenticatedUser currentModifier; 35 private long updateCount = 0; 36 private IntimateAccess intimateAccess = new IntimateAccess(); 37 protected static final String READ_ONLY_MESSAGE = "This ACL cannot be modified."; 38 39 public AclImpl(AclStrategy aclStrategy, Date lastModified, long lastModifier, long id, AuthenticatedUser currentModifier, long updateCount) { 40 this.aclStrategy = aclStrategy; 41 this.lastModified = lastModified; 42 this.lastModifier = lastModifier; 43 this.id = id; 44 this.currentModifier = currentModifier; 45 this.updateCount = updateCount; 46 } 47 48 public IntimateAccess getIntimateAccess(AclStrategy aclStrategy) { 49 if (aclStrategy == this.aclStrategy) 50 return intimateAccess; 51 else 52 return null; 53 } 54 55 public AclObject createNewObject(String objectExpression) { 56 if (readOnly) 57 throw new RuntimeException (READ_ONLY_MESSAGE); 58 59 return new AclObjectImpl(this, aclStrategy, objectExpression); 60 } 61 62 public boolean isReadOnly() { 63 return readOnly; 64 } 65 66 public void makeReadOnly() { 67 this.readOnly = true; 68 } 69 70 public AclObject get(int index) { 71 return (AclObject) objects.get(index); 72 } 73 74 public void remove(int index) { 75 if (readOnly) 76 throw new RuntimeException (READ_ONLY_MESSAGE); 77 78 objects.remove(index); 79 } 80 81 public void add(AclObject aclObject) { 82 preAddChecks(aclObject); 83 objects.add(aclObject); 84 } 85 86 private void preAddChecks(AclObject aclObject) { 87 if (readOnly) 88 throw new RuntimeException (READ_ONLY_MESSAGE); 89 90 if (aclObject == null) 91 throw new NullPointerException ("null AclObject not allowed."); 92 93 if (!(aclObject instanceof AclObjectImpl)) 94 throw new RuntimeException ("Incorrect AclObject implementation provided, only the ones obtained using createNewObject() on this AclObject may be used."); 95 96 if (((AclObjectImpl) aclObject).getOwner() != this) 97 throw new RuntimeException ("The specified AclObject belongs to a different Acl, it cannot be added to this Acl."); 98 } 99 100 public void add(int index, AclObject aclObject) { 101 preAddChecks(aclObject); 102 objects.add(index, aclObject); 103 } 104 105 public void clear() { 106 if (readOnly) 107 throw new RuntimeException (READ_ONLY_MESSAGE); 108 109 objects.clear(); 110 } 111 112 public int size() { 113 return objects.size(); 114 } 115 116 public Date getLastModified() { 117 if (lastModified != null) 118 return (Date)lastModified.clone(); 119 else 120 return null; 121 } 122 123 public long getLastModifier() { 124 return lastModifier; 125 } 126 127 public void save() throws RepositoryException { 128 aclStrategy.storeAcl(this); 129 } 130 131 public AclDocument getXml() { 132 AclDocument aclDocument = AclDocument.Factory.newInstance(); 133 AclDocument.Acl aclXml = aclDocument.addNewAcl(); 134 135 aclXml.setId(id); 136 GregorianCalendar lastModifiedCalendar = new GregorianCalendar(); 137 lastModifiedCalendar.setTime(lastModified); 138 aclXml.setLastModified(lastModifiedCalendar); 139 aclXml.setLastModifier(lastModifier); 140 aclXml.setUpdateCount(updateCount); 141 142 AclObjectDocument.AclObject[] aclObjectsXml = new AclObjectDocument.AclObject[objects.size()]; 143 for (int i = 0; i < aclObjectsXml.length; i++) { 144 AclObject aclObject = (AclObject)objects.get(i); 145 aclObjectsXml[i] = aclObject.getXml().getAclObject(); 146 } 147 148 aclXml.setAclObjectArray(aclObjectsXml); 149 150 return aclDocument; 151 } 152 153 public void setFromXml(AclDocument.Acl aclXml) { 154 if (readOnly) 155 throw new RuntimeException (READ_ONLY_MESSAGE); 156 157 AclObjectDocument.AclObject[] aclObjectsXml = aclXml.getAclObjectArray(); 158 159 clear(); 160 161 for (int i = 0; i < aclObjectsXml.length; i++) { 162 AclObject object = createNewObject(aclObjectsXml[i].getExpression()); 163 164 AclEntryDocument.AclEntry[] aclEntriesXml = aclObjectsXml[i].getAclEntryArray(); 165 for (int j = 0; j < aclEntriesXml.length; j++) { 166 AclEntryDocument.AclEntry entryXml = aclEntriesXml[j]; 167 AclSubjectType subjectType = AclSubjectType.fromString(entryXml.getSubjectType().toString()); 168 AclEntry entry = object.createNewEntry(subjectType, entryXml.getSubjectValue()); 169 170 if (entryXml.isSetPermReadLive()) 171 entry.set(AclPermission.READ_LIVE, AclActionType.fromString(entryXml.getPermReadLive().toString())); 172 if (entryXml.isSetPermRead()) 173 entry.set(AclPermission.READ, AclActionType.fromString(entryXml.getPermRead().toString())); 174 if (entryXml.isSetPermWrite()) 175 entry.set(AclPermission.WRITE, AclActionType.fromString(entryXml.getPermWrite().toString())); 176 if (entryXml.isSetPermPublish()) 177 entry.set(AclPermission.PUBLISH, AclActionType.fromString(entryXml.getPermPublish().toString())); 178 if (entryXml.isSetPermDelete()) 179 entry.set(AclPermission.DELETE, AclActionType.fromString(entryXml.getPermDelete().toString())); 180 181 object.add(entry); 182 } 183 184 add(object); 185 } 186 } 187 188 public long getUpdateCount() { 189 return updateCount; 190 } 191 192 public class IntimateAccess { 193 private IntimateAccess() { 194 } 195 196 public long getId() { 197 return id; 198 } 199 200 public void setId(long id) { 201 if (readOnly) 202 throw new RuntimeException (READ_ONLY_MESSAGE); 203 204 AclImpl.this.id = id; 205 } 206 207 public AuthenticatedUser getCurrentModifier() { 208 return currentModifier; 209 } 210 211 public void setLastModified(Date lastModified) { 212 if (readOnly) 213 throw new RuntimeException (READ_ONLY_MESSAGE); 214 215 AclImpl.this.lastModified = lastModified; 216 } 217 218 public void setLastModifier(long lastModifier) { 219 if (readOnly) 220 throw new RuntimeException (READ_ONLY_MESSAGE); 221 222 AclImpl.this.lastModifier = lastModifier; 223 } 224 225 public void setUpdateCount(long updateCount) { 226 if (readOnly) 227 throw new RuntimeException (READ_ONLY_MESSAGE); 228 229 AclImpl.this.updateCount = updateCount; 230 } 231 232 public List getObjects() { 233 return objects; 234 } 235 } 236 } 237 | Popular Tags |