KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > Ace


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/Ace.java,v 1.4.2.1 2004/11/23 12:57:16 unico Exp $
3  * $Revision: 1.4.2.1 $
4  * $Date: 2004/11/23 12:57:16 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib;
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 /**
30  * This interface models a DAV Access control entry.
31  *
32  * @version $Revision: 1.4.2.1 $
33  */

34 public class Ace {
35     private static final PropertyName DEFAULT_PROPERTY
36         = new PropertyName("DAV:", "owner");
37
38     // ----------------------------------------------------------- Constructors
39

40
41     public Ace(String JavaDoc principal) {
42         this.principal = principal;
43     }
44
45
46     public Ace(String JavaDoc principal, boolean negative, boolean protectedAce,
47                boolean inherited, String JavaDoc inheritedFrom) {
48         this(principal);
49         this.negative = negative;
50         this.protectedAce = protectedAce;
51         this.inherited = inherited;
52         this.inheritedFrom = inheritedFrom;
53     }
54
55
56     // ----------------------------------------------------- Instance Variables
57

58
59     /**
60      * Principal.
61      */

62     protected String JavaDoc principal;
63
64
65     /**
66      * Negative (deny) flag.
67      */

68     protected boolean negative = false;
69
70
71     /**
72      * Privileges this ACE grants or denies.
73      */

74     protected Vector JavaDoc privileges = new Vector JavaDoc();
75
76
77     /**
78      * Protected.
79      */

80     protected boolean protectedAce = false;
81
82
83     /**
84      * Inherited.
85      */

86     protected boolean inherited = false;
87
88
89     /**
90      * Inherited from.
91      */

92     protected String JavaDoc inheritedFrom = null;
93
94
95     /**
96      * Property. Only used if principal.equals("property").
97      */

98     protected PropertyName property = null;
99
100
101     // ------------------------------------------------------------- Properties
102

103
104     /**
105      * Principal accessor.
106      */

107     public String JavaDoc getPrincipal() {
108         return principal;
109     }
110
111
112     /**
113      * Principal mutator.
114      */

115     public void setPrincipal(String JavaDoc principal) {
116         this.principal = principal;
117     }
118
119
120     /**
121      * Negative accessor.
122      */

123     public boolean isNegative() {
124         return (negative);
125     }
126
127
128     /**
129      * Negative mutator.
130      */

131     public void setNegative(boolean negative) {
132         this.negative = negative;
133     }
134
135
136     /**
137      * Protected accessor.
138      */

139     public boolean isProtected() {
140         return (protectedAce);
141     }
142
143
144     /**
145      * Protected mutator.
146      */

147     public void setProtected(boolean protectedAce) {
148         this.protectedAce = protectedAce;
149     }
150
151
152     /**
153      * Inherited accessor.
154      */

155     public boolean isInherited() {
156         return (inherited);
157     }
158
159
160     /**
161      * Inherited mutator.
162      */

163     public void setInherited(boolean inherited) {
164         this.inherited = inherited;
165     }
166
167
168     /**
169      * Inherited from accessor.
170      */

171     public String JavaDoc getInheritedFrom() {
172         return inheritedFrom;
173     }
174
175
176     /**
177      * Inherited from mutator.
178      */

179     public void setInheritedFrom(String JavaDoc inheritedFrom) {
180         this.inheritedFrom = inheritedFrom;
181     }
182
183
184     /**
185      * Property accessor.
186      *
187      * @return the property to compare if the pricipal is "property".
188      * If the property has not been set or has been set to null
189      * return "DAV:owner".
190      * @see #setProperty(PropertyName)
191      */

192     public PropertyName getProperty() {
193         return property != null ? property : DEFAULT_PROPERTY;
194     }
195
196
197     /**
198      * Property mutator.
199      *
200      * @param property the property to compare if the principal is "property"
201      * @see #getProperty()
202      */

203     public void setProperty(PropertyName property) {
204         this.property = property;
205     }
206
207
208     /**
209      * Enumerate privileges.
210      */

211     public Enumeration JavaDoc enumeratePrivileges() {
212         return privileges.elements();
213     }
214
215
216     /**
217      * Add privilege.
218      */

219     public void addPrivilege(Privilege privilege) {
220         privileges.addElement(privilege);
221     }
222
223
224     /**
225      * Remove privilege.
226      */

227     public boolean removePrivilege(Privilege privilege) {
228         return privileges.removeElement(privilege);
229     }
230
231
232     /**
233      * Clear privileges.
234      */

235     public void clearPrivileges() {
236         privileges.clear();
237     }
238
239     public int hashCode() {
240         return toString().hashCode()
241             + (getPrincipal().equals("property")
242                     ? getProperty().hashCode()
243                     : 0);
244     }
245
246     public boolean equals(Object JavaDoc o) {
247         if (o != null && o instanceof Ace) {
248             Ace otherAce = (Ace) o;
249             boolean equals = true;
250             equals &= isNegative() == otherAce.isNegative();
251             equals &= isProtected() == otherAce.isProtected();
252             equals &= isInherited() == otherAce.isInherited();
253             if (equals && isInherited()) {
254                 equals = getInheritedFrom().equals(otherAce.getInheritedFrom());
255             }
256             equals &= getPrincipal().equals(otherAce.getPrincipal());
257             if (equals && getPrincipal().equals("property")) {
258                 equals = getProperty().equals(otherAce.getProperty());
259             }
260             if (equals) {
261                 Enumeration JavaDoc privileges = enumeratePrivileges();
262                 Enumeration JavaDoc otherPrivileges = otherAce.enumeratePrivileges();
263                 while (equals && privileges.hasMoreElements()) {
264                     equals = otherPrivileges.hasMoreElements();
265                     // Only access otherPrivileges if there are more elements
266
if (equals)
267                     {
268                         equals = privileges.nextElement().equals(otherPrivileges.nextElement());
269                     }
270                 }
271                 if (equals)
272                 {
273                     // No more elements in privileges, so there should be no
274
// more elements in otherPrivileges
275
equals = !otherPrivileges.hasMoreElements();
276                 }
277             }
278             return equals;
279         }
280         return false;
281     }
282
283     public String JavaDoc toString() {
284         return ((!isNegative()?"granted":"denied") +
285         " to " + getPrincipal() +
286         " (" + (isProtected()?"protected":"not protected") + ")" +
287         " (" + (isInherited()? ("inherited from '" + getInheritedFrom() + "'"): "not inherited") +")");
288     }
289
290 }
291
Popular Tags