KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > acl > JahiaACLEntry


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.services.acl;
14
15 import java.io.Serializable JavaDoc;
16
17 public class JahiaACLEntry implements Cloneable JavaDoc, Serializable JavaDoc {
18     public static final int ACL_YES = 0;
19     public static final int ACL_NO = 1;
20     public static final int ACL_NEUTRAL = 2;
21
22     private int mState = 0;
23     private int mTriState = 0xFFFFFFFF; // inherite by default
24

25
26     //-------------------------------------------------------------------------
27
/**
28      * Default Constructor
29      */

30     public JahiaACLEntry () {
31     }
32
33
34     //-------------------------------------------------------------------------
35
/**
36      * Constructor. Before using this constructor, be sure to understand perfectly
37      * the meaning of the state and tri-state values. Wrong values will lead to
38      * incorrect rights accesses.
39      *
40      * @param state State value of the ACL entry.
41      * @param tristate Tri-state value of the ACL entry.
42      */

43     public JahiaACLEntry (int state, int tristate) {
44         mState = state;
45         mTriState = tristate;
46     }
47
48
49     //-------------------------------------------------------------------------
50
/**
51      * Return the bit state of the ACL entry.
52      *
53      * @param permission bit index.
54      *
55      * @return Return ACL_YES, ACL_NO or ACL_NEUTRAL depending on the bit state.
56      */

57     public int getPermission (int permission) {
58         int mask;
59
60         mask = 1;
61         mask = mask << permission;
62
63         if ((mTriState & mask) != 0) {
64             return ACL_NEUTRAL;
65         }
66
67         if ((mState & mask) != 0) {
68             return ACL_YES;
69         }
70         return ACL_NO;
71     }
72
73
74     //-------------------------------------------------------------------------
75
/**
76      * Set the new bit state.
77      *
78      * @param permission Bit index.
79      * @param state Bit state, must be ACL_YES, ACL_NO or ACL_NEUTRAL. All other
80      * values will be considered as ACL_NO.
81      */

82     public void setPermission (int permission, int state) {
83         int tmp = 0;
84         int tmp_tristate = 0;
85
86         // Set the data bit
87
tmp = 1;
88         tmp = tmp << permission;
89
90         if (state == ACL_YES) {
91             mState |= tmp;
92         } else {
93             mState &= ~tmp;
94         }
95
96         // Set the tristate bit
97
tmp = 1;
98         tmp = tmp << permission;
99
100         if (state == ACL_NEUTRAL) {
101             mTriState |= tmp;
102         } else {
103             mTriState &= ~tmp;
104         }
105     }
106
107
108     //-------------------------------------------------------------------------
109
/**
110      * Return the state of the entry. Be sure to know the meaning of this value
111      * before using it.
112      *
113      * @return Return the internal state value of the ACL entry.
114      */

115     public final int getState () {
116         return mState;
117     }
118
119
120     //-------------------------------------------------------------------------
121
/**
122      * Return the tri-state of the entry. Be sure to know the meaning of this
123      * value before using it.
124      *
125      * @return Return the tri-state state value of the ACL entry.
126      */

127     public final int getTriState () {
128         return mTriState;
129     }
130
131
132     //-------------------------------------------------------------------------
133
/**
134      * Initialize the state and tri-state value of the ACL entry. Before using
135      * this method, be sure to understand perfectly the meaning of these values.
136      * Incorrect values will lead to incorrect right accesses.
137      *
138      * @param state State value.
139      * @param tristate Tri-state value.
140      */

141     public final void set (int state, int tristate) {
142         mState = state;
143         mTriState = tristate;
144     }
145
146
147     //-------------------------------------------------------------------------
148
/**
149      * Clear the state and tri-state value of the ACL entry. Calling this method
150      * will remove all the permission of the ACL entry.
151      */

152     public final void clear () {
153         set (0, 0xFFFF);
154     }
155
156     //-------------------------------------------------------------------------
157
/**
158      * Return true if the passed ACL entry has the same permission as the
159      * encapsulated ACL entry. This method has not the same result as the equal()
160      * method.
161      *
162      * @param entry ACL entry to check.
163      *
164      * @return Return true if the passed ACL entry has the same permission activated
165      * as the encapsulated ACL entry.
166      */

167     public boolean hasSameBitsActivated (JahiaACLEntry entry) {
168         if (entry != null) {
169             int mask = (entry.mState & mState);
170             if (mask == entry.mState) {
171                 return (((mTriState & mask) == 0) && ((entry.mTriState & mask) == 0));
172             } else {
173                 return false;
174             }
175         }
176         return false;
177     }
178
179     //-------------------------------------------------------------------------
180
/**
181      * Return a string representation of the ACL entry content.
182      *
183      * @return Return the string representation of the ACL entry.
184      */

185     public String JavaDoc toString () {
186         String JavaDoc result = "state=[" + Integer.toBinaryString (mState) +
187                 "], tristate=[" + Integer.toBinaryString (mTriState) + "]";
188         return result;
189     }
190
191     //-------------------------------------------------------------------------
192
/**
193      * Merge specified permissions set as non inherited into the encapsulated
194      * ACL.
195      *
196      * @param entry ACL entry to merge
197      */

198     public void merge (JahiaACLEntry entry) {
199         if (entry != null) {
200             mState = mState | entry.getState ();
201             mTriState = mTriState & entry.getTriState ();
202         }
203     }
204
205     //-------------------------------------------------------------------------
206
/**
207      * Clone the ACL Entry.
208      *
209      * @return Return a new ACL Entry with the same permission sets as the
210      * encapsulated entry.
211      */

212     public Object JavaDoc clone () {
213         return new JahiaACLEntry (mState, mTriState);
214     }
215 }
216
Popular Tags