KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > acl > AclObjectImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.commonimpl.acl;
17
18 import org.outerj.daisy.repository.acl.*;
19 import org.outerx.daisy.x10.AclObjectDocument;
20 import org.outerx.daisy.x10.AclEntryDocument;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 public final class AclObjectImpl implements AclObject {
26     private String JavaDoc objectExpr;
27     private Object JavaDoc compiledExpr;
28     private ArrayList JavaDoc entries = new ArrayList JavaDoc();
29     private AclImpl ownerAcl;
30     private IntimateAccess intimateAccess = new IntimateAccess();
31     private AclStrategy aclStrategy;
32
33     public AclObjectImpl(AclImpl ownerAcl, AclStrategy aclStrategy, String JavaDoc objectExpr) {
34         if (objectExpr == null)
35             throw new NullPointerException JavaDoc("objectExpr cannot be null");
36
37         this.objectExpr = objectExpr;
38         this.ownerAcl = ownerAcl;
39         this.aclStrategy = aclStrategy;
40     }
41
42     public IntimateAccess getIntimateAccess(AclStrategy aclStrategy) {
43         if (aclStrategy == this.aclStrategy)
44             return intimateAccess;
45         else
46             return null;
47     }
48
49     protected AclImpl getOwner() {
50         return ownerAcl;
51     }
52
53     public String JavaDoc getObjectExpr() {
54         return objectExpr;
55     }
56
57     public void setObjectExpr(String JavaDoc expr) {
58         if (ownerAcl.isReadOnly())
59             throw new RuntimeException JavaDoc(AclImpl.READ_ONLY_MESSAGE);
60
61         if (expr == null)
62             throw new NullPointerException JavaDoc("expr cannot be null");
63
64         this.objectExpr = expr;
65         this.compiledExpr = null;
66     }
67
68     public AclEntry createNewEntry(AclSubjectType subjectType, long subjectValue) {
69         if (ownerAcl.isReadOnly())
70             throw new RuntimeException JavaDoc(AclImpl.READ_ONLY_MESSAGE);
71
72         return new AclEntryImpl(ownerAcl, subjectType, subjectValue);
73     }
74
75     public AclEntry get(int index) {
76         return (AclEntry)entries.get(index);
77     }
78
79     public void remove(int index) {
80         if (ownerAcl.isReadOnly())
81             throw new RuntimeException JavaDoc(AclImpl.READ_ONLY_MESSAGE);
82
83         entries.remove(index);
84     }
85
86     public void add(AclEntry aclEntry) {
87         preAddChecks(aclEntry);
88         entries.add(aclEntry);
89     }
90
91     private void preAddChecks(AclEntry aclEntry) {
92         if (ownerAcl.isReadOnly())
93             throw new RuntimeException JavaDoc(AclImpl.READ_ONLY_MESSAGE);
94
95         if (aclEntry == null)
96             throw new NullPointerException JavaDoc("null AclEntry not allowed.");
97
98         if (!(aclEntry instanceof AclEntryImpl))
99             throw new RuntimeException JavaDoc("Incorrect AclEntry implementation provided, only the ones obtained using createNewEntry() on this AclObject may be used.");
100
101         if (((AclEntryImpl)aclEntry).getOwner() != ownerAcl)
102             throw new RuntimeException JavaDoc("The specified AclEntry belongs to a different Acl, it cannot be added to this AclObject.");
103     }
104
105     public void add(int index, AclEntry aclEntry) {
106         preAddChecks(aclEntry);
107         entries.add(index, aclEntry);
108     }
109
110     public void clear() {
111         if (ownerAcl.isReadOnly())
112             throw new RuntimeException JavaDoc(AclImpl.READ_ONLY_MESSAGE);
113
114         entries.clear();
115     }
116
117     public int size() {
118         return entries.size();
119     }
120
121     public AclObjectDocument getXml() {
122         AclObjectDocument aclObjectDocument = AclObjectDocument.Factory.newInstance();
123         AclObjectDocument.AclObject aclObject = aclObjectDocument.addNewAclObject();
124
125         aclObject.setExpression(objectExpr);
126
127         AclEntryDocument.AclEntry[] aclEntriesXml = new AclEntryDocument.AclEntry[entries.size()];
128         for (int i = 0; i < aclEntriesXml.length; i++) {
129             AclEntry aclEntry = (AclEntry)entries.get(i);
130             aclEntriesXml[i] = aclEntry.getXml().getAclEntry();
131         }
132
133         aclObject.setAclEntryArray(aclEntriesXml);
134
135         return aclObjectDocument;
136     }
137
138     public class IntimateAccess {
139         private IntimateAccess() {
140         }
141
142         public List JavaDoc getEntries() {
143             return entries;
144         }
145
146         public void setCompiledExpression(Object JavaDoc compiledExpression) {
147             compiledExpr = compiledExpression;
148         }
149
150         public Object JavaDoc getCompiledExpression() {
151             return compiledExpr;
152         }
153     }
154 }
155
Popular Tags