KickJava   Java API By Example, From Geeks To Geeks.

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


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.AclSubjectType;
19 import org.outerj.daisy.repository.acl.AclEntry;
20 import org.outerj.daisy.repository.acl.AclActionType;
21 import org.outerj.daisy.repository.acl.AclPermission;
22 import org.outerx.daisy.x10.AclEntryDocument;
23 import org.outerx.daisy.x10.AclAction;
24 import org.outerx.daisy.x10.AclSubject;
25
26 public final class AclEntryImpl implements AclEntry {
27     private AclSubjectType subjectType;
28     private long subjectValue;
29     private AclActionType[] permissions = new AclActionType[AclPermission.ENUM.length];
30     private AclImpl ownerAcl;
31
32     public AclEntryImpl(AclImpl ownerAcl, AclSubjectType subjectType, long subjectValue) {
33         if (subjectType == null)
34             throw new NullPointerException JavaDoc("null AclSubjectType not allowed.");
35         if (subjectType == AclSubjectType.EVERYONE && subjectValue != -1)
36             throw new IllegalArgumentException JavaDoc("subjectValue should be -1 if subjectType is EVERYONE");
37         else if (subjectType != AclSubjectType.EVERYONE && subjectValue == -1)
38             throw new IllegalArgumentException JavaDoc("subjectValue should only be -1 if subjectType is EVERYONE");
39
40         this.ownerAcl = ownerAcl;
41         this.subjectType = subjectType;
42         this.subjectValue = subjectValue;
43     }
44
45     protected AclImpl getOwner() {
46         return ownerAcl;
47     }
48
49     public AclActionType get(AclPermission aclPermission) {
50         if (permissions[aclPermission.getPos()] == null)
51             return AclActionType.DO_NOTHING;
52         else
53             return permissions[aclPermission.getPos()];
54     }
55
56     public void set(AclPermission permission, AclActionType action) {
57         if (ownerAcl.isReadOnly())
58             throw new RuntimeException JavaDoc(AclImpl.READ_ONLY_MESSAGE);
59
60         permissions[permission.getPos()] = action;
61     }
62
63     public long getSubjectValue() {
64         return subjectValue;
65     }
66
67     public AclSubjectType getSubjectType() {
68         return subjectType;
69     }
70
71     public void setSubjectValue(long value) {
72         if (subjectType == AclSubjectType.EVERYONE && value != -1)
73             throw new IllegalArgumentException JavaDoc("subjectValue should be -1 if subjectType is EVERYONE");
74         else if (subjectType != AclSubjectType.EVERYONE && value == -1)
75             throw new IllegalArgumentException JavaDoc("subjectValue should only be -1 if subjectType is EVERYONE");
76
77         this.subjectValue = value;
78     }
79
80     public void setSubjectType(AclSubjectType subjectType) {
81         this.subjectType = subjectType;
82     }
83
84     public AclEntryDocument getXml() {
85         AclEntryDocument aclEntryDocument = AclEntryDocument.Factory.newInstance();
86         AclEntryDocument.AclEntry aclEntryXml = aclEntryDocument.addNewAclEntry();
87
88         aclEntryXml.setSubjectType(AclSubject.Enum.forString(subjectType.toString()));
89         aclEntryXml.setSubjectValue(subjectValue);
90
91         if (permissions[AclPermission.READ_LIVE.getPos()] != null)
92             aclEntryXml.setPermReadLive(AclAction.Enum.forString(permissions[AclPermission.READ_LIVE.getPos()].toString()));
93         if (permissions[AclPermission.READ.getPos()] != null)
94             aclEntryXml.setPermRead(AclAction.Enum.forString(permissions[AclPermission.READ.getPos()].toString()));
95         if (permissions[AclPermission.WRITE.getPos()] != null)
96             aclEntryXml.setPermWrite(AclAction.Enum.forString(permissions[AclPermission.WRITE.getPos()].toString()));
97         if (permissions[AclPermission.PUBLISH.getPos()] != null)
98             aclEntryXml.setPermPublish(AclAction.Enum.forString(permissions[AclPermission.PUBLISH.getPos()].toString()));
99         if (permissions[AclPermission.DELETE.getPos()] != null)
100             aclEntryXml.setPermDelete(AclAction.Enum.forString(permissions[AclPermission.DELETE.getPos()].toString()));
101
102         return aclEntryDocument;
103     }
104 }
105
Popular Tags