KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > Permissions


1 /**
2  * $RCSfile: Permissions.java,v $
3  * $Revision: 1.1 $
4  * $Date: 2004/12/13 18:06:53 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger;
13
14 import org.jivesoftware.util.CacheSizes;
15 import org.jivesoftware.util.StringUtils;
16 import org.jivesoftware.util.Cacheable;
17
18 /**
19  * Represents a set of permissions that an entity has for an object in the system. For example,
20  * the rights that a user has for a category. Permissions are used by the protection proxy objects
21  * defined for each major component of the system to provide access rights.<p>
22  * <p/>
23  * A Permissions object is internally represented as a long with each bit indicating whether
24  * a particular permission is set. The constants defined by extensions of this class define the bit
25  * masks that can be used for permission operations. For example, the following code creates
26  * permissions:<pre>
27  * <p/>
28  * // Create a permissions object with only read permissions set to true.
29  * Permissions perms1 = new Permissions(ForumPermissions.READ_FORUM);
30  * // Create a permissions object with read and system admin permissions set to true.
31  * Permissions perms2 = new Permissions(ForumPermissions.READ_FORUM |
32  * ForumPermissions.SYSTEM_ADMIN);</pre>
33  * <p/>
34  * If we were to view the bits of each variable, <tt>perms1</tt> would be
35  * <tt>0000000000000000000000000000000000000000000000000000000000000001</tt> and
36  * <tt>perms2</tt> would be
37  * <tt>0000000000000000000000000000000010000000000000000000000000000001</tt>.<p>
38  *
39  * @author Matt Tucker
40  */

41 public class Permissions implements Cacheable {
42
43     /**
44      * No permissions.
45      */

46     public static final long NONE = 0x000000000000000L;
47
48     /**
49      * Permission to see the online status of a particular user.
50      */

51     public static final long VIEW_ONLINE_STATUS = 0x100000000000000L;
52
53     /**
54      * Permission to administer a particular user.
55      */

56     public static final long USER_ADMIN = 0x200000000000000L;
57
58     /**
59      * Permission to administer a particular group.
60      */

61     public static final long GROUP_ADMIN = 0x400000000000000L;
62
63     /**
64      * Permission to administer the entire sytem.
65      */

66     public static final long SYSTEM_ADMIN = 0x800000000000000L;
67
68     /**
69      * A long holding permission values. We use the bits in the number to extract up to 63
70      * different permissions.
71      */

72     private long permissions;
73
74     /**
75      * Create a new permissions object with the specified permissions.
76      *
77      * @param permissions integer bitmask values to for the new Permissions.
78      */

79     public Permissions(long permissions) {
80         this.permissions = permissions;
81     }
82
83     /**
84      * Creates a new ForumPermission object by combining two permissions
85      * objects. The higher permission of each permission type will be used.
86      *
87      * @param permissions1 the first permissions to use when creating the new Permissions.
88      * @param permissions2 the second permissions to use when creating the new Permissions.
89      */

90     public Permissions(Permissions permissions1, Permissions permissions2) {
91         permissions = permissions1.permissions | permissions2.permissions;
92     }
93
94     /**
95      * Returns true if one or more of the permission types is set to true.
96      *
97      * @param permissionTypes
98      * @return true if one or more of the permission types is set to true, false otherwise.
99      */

100     public boolean hasPermission(long permissionTypes) {
101         return (permissions & permissionTypes) != 0;
102     }
103
104     /**
105      * Sets the permissions given by a bit mask to true or false. For example, the following
106      * would set the READ_FORUM and SYSTEM_ADMIN permissions to true:
107      * <p/>
108      * <pre>
109      * permissions.set(ForumPermissions.READ_FORUM | ForumPermissions.SYSTEM_ADMIN, true);
110      * </pre>
111      *
112      * @param permissionTypes the permission types to set.
113      * @param value true to enable the permission, false to disable.
114      */

115     public void set(long permissionTypes, boolean value) {
116         if (value) {
117             permissions = permissions | permissionTypes;
118         }
119         else {
120             permissionTypes = ~permissionTypes;
121             permissions = permissions & permissionTypes;
122         }
123     }
124
125     /**
126      * Returns the long value (bitmask) of the permissions that are set.
127      *
128      * @return the long value of the object.
129      */

130     public long value() {
131         return permissions;
132     }
133
134     public String JavaDoc toString() {
135         return StringUtils.zeroPadString(Long.toBinaryString(permissions), 63);
136     }
137
138     // Cacheable Interface
139

140     public int getCachedSize() {
141         // Approximate the size of the object in bytes by calculating the size
142
// of each field.
143
int size = 0;
144         size += CacheSizes.sizeOfObject(); // overhead of object
145
size += CacheSizes.sizeOfLong(); // permissions bits
146
return size;
147     }
148 }
Popular Tags