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.outerx.daisy.x10.AclObjectDocument;
020: import org.outerx.daisy.x10.AclEntryDocument;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024:
025: public final class AclObjectImpl implements AclObject {
026: private String objectExpr;
027: private Object compiledExpr;
028: private List<AclEntryImpl> entries = new ArrayList<AclEntryImpl>();
029: private AclImpl ownerAcl;
030: private IntimateAccess intimateAccess = new IntimateAccess();
031: private AclStrategy aclStrategy;
032: private boolean isAdded;
033:
034: public AclObjectImpl(AclImpl ownerAcl, AclStrategy aclStrategy,
035: String objectExpr) {
036: if (objectExpr == null)
037: throw new NullPointerException("objectExpr cannot be null");
038:
039: this .objectExpr = objectExpr;
040: this .ownerAcl = ownerAcl;
041: this .aclStrategy = aclStrategy;
042: }
043:
044: public IntimateAccess getIntimateAccess(AclStrategy aclStrategy) {
045: if (aclStrategy == this .aclStrategy)
046: return intimateAccess;
047: else
048: return null;
049: }
050:
051: protected AclImpl getOwner() {
052: return ownerAcl;
053: }
054:
055: public String getObjectExpr() {
056: return objectExpr;
057: }
058:
059: public void setObjectExpr(String expr) {
060: if (ownerAcl.isReadOnly())
061: throw new RuntimeException(AclImpl.READ_ONLY_MESSAGE);
062:
063: if (expr == null)
064: throw new NullPointerException("expr cannot be null");
065:
066: this .objectExpr = expr;
067: this .compiledExpr = null;
068: }
069:
070: public AclEntry createNewEntry(AclSubjectType subjectType,
071: long subjectValue) {
072: if (ownerAcl.isReadOnly())
073: throw new RuntimeException(AclImpl.READ_ONLY_MESSAGE);
074:
075: return new AclEntryImpl(ownerAcl, subjectType, subjectValue);
076: }
077:
078: public AclEntry get(int index) {
079: return entries.get(index);
080: }
081:
082: public void remove(int index) {
083: if (ownerAcl.isReadOnly())
084: throw new RuntimeException(AclImpl.READ_ONLY_MESSAGE);
085:
086: entries.remove(index).setIsAdded(false);
087: }
088:
089: public void add(AclEntry aclEntry) {
090: preAddChecks(aclEntry);
091: AclEntryImpl aclEntryImpl = (AclEntryImpl) aclEntry;
092: aclEntryImpl.setIsAdded(true);
093: entries.add(aclEntryImpl);
094: }
095:
096: private void preAddChecks(AclEntry aclEntry) {
097: if (ownerAcl.isReadOnly())
098: throw new RuntimeException(AclImpl.READ_ONLY_MESSAGE);
099:
100: if (aclEntry == null)
101: throw new NullPointerException("null AclEntry not allowed.");
102:
103: if (!(aclEntry instanceof AclEntryImpl))
104: throw new RuntimeException(
105: "Incorrect AclEntry implementation provided, only the ones obtained using createNewEntry() on this AclObject may be used.");
106:
107: AclEntryImpl aclEntryImpl = (AclEntryImpl) aclEntry;
108:
109: if (aclEntryImpl.getOwner() != ownerAcl)
110: throw new RuntimeException(
111: "The specified AclEntry belongs to a different Acl, it cannot be added to this AclObject.");
112:
113: if (aclEntryImpl.isAdded())
114: throw new RuntimeException(
115: "The specified AclEntry is already added to the ACL.");
116: }
117:
118: public void add(int index, AclEntry aclEntry) {
119: preAddChecks(aclEntry);
120: AclEntryImpl aclEntryImpl = (AclEntryImpl) aclEntry;
121: aclEntryImpl.setIsAdded(true);
122: entries.add(index, aclEntryImpl);
123: }
124:
125: public void clear() {
126: if (ownerAcl.isReadOnly())
127: throw new RuntimeException(AclImpl.READ_ONLY_MESSAGE);
128:
129: for (AclEntryImpl entry : entries) {
130: entry.setIsAdded(false);
131: }
132:
133: entries.clear();
134: }
135:
136: public int size() {
137: return entries.size();
138: }
139:
140: public AclObjectDocument getXml() {
141: AclObjectDocument aclObjectDocument = AclObjectDocument.Factory
142: .newInstance();
143: AclObjectDocument.AclObject aclObject = aclObjectDocument
144: .addNewAclObject();
145:
146: aclObject.setExpression(objectExpr);
147:
148: AclEntryDocument.AclEntry[] aclEntriesXml = new AclEntryDocument.AclEntry[entries
149: .size()];
150: for (int i = 0; i < aclEntriesXml.length; i++) {
151: AclEntry aclEntry = entries.get(i);
152: aclEntriesXml[i] = aclEntry.getXml().getAclEntry();
153: }
154:
155: aclObject.setAclEntryArray(aclEntriesXml);
156:
157: return aclObjectDocument;
158: }
159:
160: public class IntimateAccess {
161: private IntimateAccess() {
162: }
163:
164: public List<AclEntryImpl> getEntries() {
165: return entries;
166: }
167:
168: public void setCompiledExpression(Object compiledExpression) {
169: compiledExpr = compiledExpression;
170: }
171:
172: public Object getCompiledExpression() {
173: return compiledExpr;
174: }
175: }
176:
177: protected boolean isAdded() {
178: return isAdded;
179: }
180:
181: protected void setIsAdded(boolean isAdded) {
182: this.isAdded = isAdded;
183: }
184: }
|