001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.acegisecurity.acls.domain;
016:
017: import org.acegisecurity.acls.AclFormattingUtils;
018: import org.acegisecurity.acls.Permission;
019:
020: import org.springframework.util.Assert;
021:
022: import java.lang.reflect.Field;
023:
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Vector;
028:
029: /**
030: * A set of standard permissions.
031: *
032: * @author Ben Alex
033: * @version $Id: BasePermission.java 1868 2007-05-25 02:28:40Z benalex $
034: */
035: public final class BasePermission implements Permission {
036: //~ Static fields/initializers =====================================================================================
037:
038: public static final Permission READ = new BasePermission(1 << 0,
039: 'R'); // 1
040: public static final Permission WRITE = new BasePermission(1 << 1,
041: 'W'); // 2
042: public static final Permission CREATE = new BasePermission(1 << 2,
043: 'C'); // 4
044: public static final Permission DELETE = new BasePermission(1 << 3,
045: 'D'); // 8
046: public static final Permission ADMINISTRATION = new BasePermission(
047: 1 << 4, 'A'); // 16
048: private static Map locallyDeclaredPermissionsByInteger = new HashMap();
049: private static Map locallyDeclaredPermissionsByName = new HashMap();
050:
051: static {
052: Field[] fields = BasePermission.class.getDeclaredFields();
053:
054: for (int i = 0; i < fields.length; i++) {
055: try {
056: Object fieldValue = fields[i].get(null);
057:
058: if (BasePermission.class.isAssignableFrom(fieldValue
059: .getClass())) {
060: // Found a BasePermission static field
061: BasePermission perm = (BasePermission) fieldValue;
062: locallyDeclaredPermissionsByInteger.put(
063: new Integer(perm.getMask()), perm);
064: locallyDeclaredPermissionsByName.put(fields[i]
065: .getName(), perm);
066: }
067: } catch (Exception ignore) {
068: }
069: }
070: }
071:
072: //~ Instance fields ================================================================================================
073:
074: private char code;
075: private int mask;
076:
077: //~ Constructors ===================================================================================================
078:
079: private BasePermission(int mask, char code) {
080: this .mask = mask;
081: this .code = code;
082: }
083:
084: //~ Methods ========================================================================================================
085:
086: /**
087: * Dynamically creates a <code>CumulativePermission</code> or <code>BasePermission</code> representing the
088: * active bits in the passed mask.
089: *
090: * @param mask to build
091: *
092: * @return a Permission representing the requested object
093: */
094: public static Permission buildFromMask(int mask) {
095: if (locallyDeclaredPermissionsByInteger
096: .containsKey(new Integer(mask))) {
097: // The requested mask has an exactly match against a statically-defined BasePermission, so return it
098: return (Permission) locallyDeclaredPermissionsByInteger
099: .get(new Integer(mask));
100: }
101:
102: // To get this far, we have to use a CumulativePermission
103: CumulativePermission permission = new CumulativePermission();
104:
105: for (int i = 0; i < 32; i++) {
106: int permissionToCheck = 1 << i;
107:
108: if ((mask & permissionToCheck) == permissionToCheck) {
109: Permission p = (Permission) locallyDeclaredPermissionsByInteger
110: .get(new Integer(permissionToCheck));
111: Assert
112: .state(
113: p != null,
114: "Mask "
115: + permissionToCheck
116: + " does not have a corresponding static BasePermission");
117: permission.set(p);
118: }
119: }
120:
121: return permission;
122: }
123:
124: public static Permission[] buildFromMask(int[] masks) {
125: if ((masks == null) || (masks.length == 0)) {
126: return new Permission[] {};
127: }
128:
129: List list = new Vector();
130:
131: for (int i = 0; i < masks.length; i++) {
132: list.add(BasePermission.buildFromMask(masks[i]));
133: }
134:
135: return (Permission[]) list.toArray(new Permission[] {});
136: }
137:
138: public static Permission buildFromName(String name) {
139: Assert
140: .isTrue(locallyDeclaredPermissionsByName
141: .containsKey(name), "Unknown permission '"
142: + name + "'");
143:
144: return (Permission) locallyDeclaredPermissionsByName.get(name);
145: }
146:
147: public static Permission[] buildFromName(String[] names) {
148: if ((names == null) || (names.length == 0)) {
149: return new Permission[] {};
150: }
151:
152: List list = new Vector();
153:
154: for (int i = 0; i < names.length; i++) {
155: list.add(BasePermission.buildFromName(names[i]));
156: }
157:
158: return (Permission[]) list.toArray(new Permission[] {});
159: }
160:
161: public boolean equals(Object arg0) {
162: if (!(arg0 instanceof BasePermission)) {
163: return false;
164: }
165:
166: BasePermission rhs = (BasePermission) arg0;
167:
168: return (this .mask == rhs.getMask());
169: }
170:
171: public int getMask() {
172: return mask;
173: }
174:
175: public String getPattern() {
176: return AclFormattingUtils.printBinary(mask, code);
177: }
178:
179: public String toString() {
180: return "BasePermission[" + getPattern() + "=" + mask + "]";
181: }
182:
183: public int hashCode() {
184: return this.mask;
185: }
186: }
|