KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > imap > Rights


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)Rights.java 1.6 05/08/29
24  *
25  * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.imap;
29
30 import java.util.*;
31
32 /**
33  * The Rights class represents the set of rights for an authentication
34  * identifier (for instance, a user or a group). <p>
35  *
36  * A right is represented by the <code>Rights.Right</code>
37  * inner class. <p>
38  *
39  * A set of standard rights are predefined (see RFC 2086). Most folder
40  * implementations are expected to support these rights. Some
41  * implementations may also support site-defined rights. <p>
42  *
43  * The following code sample illustrates how to examine your
44  * rights for a folder. <p>
45  * <pre>
46  *
47  * Rights rights = folder.myRights();
48  *
49  * // Check if I can write this folder
50  * if (rights.contains(Rights.Right.WRITE))
51  * System.out.println("Can write folder");
52  *
53  * // Now give Joe all my rights, except the ability to write the folder
54  * rights.remove(Rights.Right.WRITE);
55  * ACL acl = new ACL("joe", rights);
56  * folder.setACL(acl);
57  * </pre>
58  * <p>
59  *
60  * @author Bill Shannon
61  */

62
63 public class Rights implements Cloneable JavaDoc {
64
65     private boolean[] rights = new boolean[128]; // XXX
66

67     /**
68      * This inner class represents an individual right. A set
69      * of standard rights objects are predefined here.
70      */

71     public static final class Right {
72     private static Right[] cache = new Right[128];
73
74     // XXX - initialization order?
75
/**
76      * Lookup - mailbox is visible to LIST/LSUB commands.
77      */

78     public static final Right LOOKUP = getInstance('l');
79
80     /**
81      * Read - SELECT the mailbox, perform CHECK, FETCH, PARTIAL,
82      * SEARCH, COPY from mailbox
83      */

84     public static final Right READ = getInstance('r');
85
86     /**
87      * Keep seen/unseen information across sessions - STORE \SEEN flag.
88      */

89     public static final Right KEEP_SEEN = getInstance('s');
90
91     /**
92      * Write - STORE flags other than \SEEN and \DELETED.
93      */

94     public static final Right WRITE = getInstance('w');
95
96     /**
97      * Insert - perform APPEND, COPY into mailbox.
98      */

99     public static final Right INSERT = getInstance('i');
100
101     /**
102      * Post - send mail to submission address for mailbox,
103      * not enforced by IMAP4 itself.
104      */

105     public static final Right POST = getInstance('p');
106
107     /**
108      * Create - CREATE new sub-mailboxes in any implementation-defined
109      * hierarchy, RENAME or DELETE mailbox.
110      */

111     public static final Right CREATE = getInstance('c');
112
113     /**
114      * Delete - STORE \DELETED flag, perform EXPUNGE.
115      */

116     public static final Right DELETE = getInstance('d');
117
118     /**
119      * Administer - perform SETACL.
120      */

121     public static final Right ADMINISTER = getInstance('a');
122
123     char right; // the right represented by this Right object
124

125     /**
126      * Private constructor used only by getInstance.
127      */

128     private Right(char right) {
129         if ((int)right >= 128)
130         throw new IllegalArgumentException JavaDoc("Right must be ASCII");
131         this.right = right;
132     }
133
134     /**
135      * Get a Right object representing the specified character.
136      * Characters are assigned per RFC 2086.
137      */

138     public static synchronized Right getInstance(char right) {
139         if ((int)right >= 128)
140         throw new IllegalArgumentException JavaDoc("Right must be ASCII");
141         if (cache[(int)right] == null)
142         cache[(int)right] = new Right(right);
143         return cache[(int)right];
144     }
145
146     public String JavaDoc toString() {
147         return String.valueOf(right);
148     }
149     }
150
151
152     /**
153      * Construct an empty Rights object.
154      */

155     public Rights() { }
156
157     /**
158      * Construct a Rights object initialized with the given rights.
159      *
160      * @param rights the rights for initialization
161      */

162     public Rights(Rights rights) {
163     System.arraycopy(rights.rights, 0, this.rights, 0, this.rights.length);
164     }
165
166     /**
167      * Construct a Rights object initialized with the given rights.
168      *
169      * @param rights the rights for initialization
170      */

171     public Rights(String JavaDoc rights) {
172     for (int i = 0; i < rights.length(); i++)
173         add(Right.getInstance(rights.charAt(i)));
174     }
175
176     /**
177      * Construct a Rights object initialized with the given right.
178      *
179      * @param right the right for initialization
180      */

181     public Rights(Right right) {
182     this.rights[(int)right.right] = true;
183     }
184
185     /**
186      * Add the specified right to this Rights object.
187      *
188      * @param right the right to add
189      */

190     public void add(Right right) {
191     this.rights[(int)right.right] = true;
192     }
193
194     /**
195      * Add all the rights in the given Rights object to this
196      * Rights object.
197      *
198      * @param rights Rights object
199      */

200     public void add(Rights rights) {
201     for (int i = 0; i < rights.rights.length; i++)
202         if (rights.rights[i])
203         this.rights[i] = true;
204     }
205
206     /**
207      * Remove the specified right from this Rights object.
208      *
209      * @param right the right to be removed
210      */

211     public void remove(Right right) {
212     this.rights[(int)right.right] = false;
213     }
214
215     /**
216      * Remove all rights in the given Rights object from this
217      * Rights object.
218      *
219      * @param rights the rights to be removed
220      */

221     public void remove(Rights rights) {
222     for (int i = 0; i < rights.rights.length; i++)
223         if (rights.rights[i])
224         this.rights[i] = false;
225     }
226
227     /**
228      * Check whether the specified right is present in this Rights object.
229      *
230      * @return true of the given right is present, otherwise false.
231      */

232     public boolean contains(Right right) {
233     return this.rights[(int)right.right];
234     }
235
236     /**
237      * Check whether all the rights in the specified Rights object are
238      * present in this Rights object.
239      *
240      * @return true if all rights in the given Rights object are present,
241      * otherwise false.
242      */

243     public boolean contains(Rights rights) {
244     for (int i = 0; i < rights.rights.length; i++)
245         if (rights.rights[i] && !this.rights[i])
246         return false;
247
248     // If we've made it till here, return true
249
return true;
250     }
251
252     /**
253      * Check whether the two Rights objects are equal.
254      *
255      * @return true if they're equal
256      */

257     public boolean equals(Object JavaDoc obj) {
258     if (!(obj instanceof Rights))
259         return false;
260
261     Rights rights = (Rights)obj;
262
263     for (int i = 0; i < rights.rights.length; i++)
264         if (rights.rights[i] != this.rights[i])
265         return false;
266
267     return true;
268     }
269
270     /**
271      * Compute a hash code for this Rights object.
272      *
273      * @return the hash code
274      */

275     public int hashCode() {
276     int hash = 0;
277     for (int i = 0; i < this.rights.length; i++)
278         if (this.rights[i])
279         hash++;
280     return hash;
281     }
282
283     /**
284      * Return all the rights in this Rights object. Returns
285      * an array of size zero if no rights are set.
286      *
287      * @return array of Rights.Right objects representing rights
288      */

289     public Right[] getRights() {
290     Vector v = new Vector();
291     for (int i = 0; i < this.rights.length; i++)
292         if (this.rights[i])
293         v.addElement(Right.getInstance((char)i));
294     Right[] rights = new Right[v.size()];
295     v.copyInto(rights);
296     return rights;
297     }
298
299     /**
300      * Returns a clone of this Rights object.
301      */

302     public Object JavaDoc clone() {
303     return new Rights(this);
304     }
305
306     public String JavaDoc toString() {
307     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
308     for (int i = 0; i < this.rights.length; i++)
309         if (this.rights[i])
310         sb.append((char)i);
311     return sb.toString();
312     }
313
314     /*****
315     public static void main(String argv[]) throws Exception {
316     // a new rights object
317     Rights f1 = new Rights();
318     f1.add(Rights.Right.READ);
319     f1.add(Rights.Right.WRITE);
320     f1.add(Rights.Right.CREATE);
321     f1.add(Rights.Right.DELETE);
322
323     // check copy constructor
324     Rights fc = new Rights(f1);
325     if (f1.equals(fc) && fc.equals(f1))
326         System.out.println("success");
327     else
328         System.out.println("fail");
329
330     // check clone
331     fc = (Rights)f1.clone();
332     if (f1.equals(fc) && fc.equals(f1))
333         System.out.println("success");
334     else
335         System.out.println("fail");
336
337     // add a right and make sure it still works right
338     f1.add(Rights.Right.ADMINISTER);
339
340     // shouldn't be equal here
341     if (!f1.equals(fc) && !fc.equals(f1))
342         System.out.println("success");
343     else
344         System.out.println("fail");
345
346     // check clone
347     fc = (Rights)f1.clone();
348     if (f1.equals(fc) && fc.equals(f1))
349         System.out.println("success");
350     else
351         System.out.println("fail");
352
353     fc.add(Rights.Right.INSERT);
354     if (!f1.equals(fc) && !fc.equals(f1))
355         System.out.println("success");
356     else
357         System.out.println("fail");
358
359     // check copy constructor
360     fc = new Rights(f1);
361     if (f1.equals(fc) && fc.equals(f1))
362         System.out.println("success");
363     else
364         System.out.println("fail");
365
366     // another new rights object
367     Rights f2 = new Rights(Rights.Right.READ);
368     f2.add(Rights.Right.WRITE);
369
370     if (f1.contains(Rights.Right.READ))
371         System.out.println("success");
372     else
373         System.out.println("fail");
374         
375     if (f1.contains(Rights.Right.WRITE))
376         System.out.println("success");
377     else
378         System.out.println("fail");
379
380     if (f1.contains(Rights.Right.CREATE))
381         System.out.println("success");
382     else
383         System.out.println("fail");
384
385     if (f1.contains(Rights.Right.DELETE))
386         System.out.println("success");
387     else
388         System.out.println("fail");
389
390     if (f2.contains(Rights.Right.WRITE))
391         System.out.println("success");
392     else
393         System.out.println("fail");
394
395
396     System.out.println("----------------");
397
398     Right[] r = f1.getRights();
399     for (int i = 0; i < r.length; i++)
400         System.out.println(r[i]);
401     System.out.println("----------------");
402
403     if (f1.contains(f2)) // this should be true
404         System.out.println("success");
405     else
406         System.out.println("fail");
407
408     if (!f2.contains(f1)) // this should be false
409         System.out.println("success");
410     else
411         System.out.println("fail");
412
413     Rights f3 = new Rights();
414     f3.add(Rights.Right.READ);
415     f3.add(Rights.Right.WRITE);
416     f3.add(Rights.Right.CREATE);
417     f3.add(Rights.Right.DELETE);
418     f3.add(Rights.Right.ADMINISTER);
419     f3.add(Rights.Right.LOOKUP);
420
421     f1.add(Rights.Right.LOOKUP);
422
423     if (f1.equals(f3))
424         System.out.println("equals success");
425     else
426         System.out.println("fail");
427     if (f3.equals(f1))
428         System.out.println("equals success");
429     else
430         System.out.println("fail");
431     System.out.println("f1 hash code " + f1.hashCode());
432     System.out.println("f3 hash code " + f3.hashCode());
433     if (f1.hashCode() == f3.hashCode())
434         System.out.println("success");
435     else
436         System.out.println("fail");
437     }
438     ****/

439 }
440
Popular Tags