Coverage Report - org.acegisecurity.acls.domain.BasePermission
 
Classes in this File Line Coverage Branch Coverage Complexity
BasePermission
63% 
40% 
2.2
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 package org.acegisecurity.acls.domain;
 16  
 
 17  
 import org.acegisecurity.acls.AclFormattingUtils;
 18  
 import org.acegisecurity.acls.Permission;
 19  
 
 20  
 import org.springframework.util.Assert;
 21  
 
 22  
 import java.lang.reflect.Field;
 23  
 
 24  
 import java.util.HashMap;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import java.util.Vector;
 28  
 
 29  
 
 30  
 /**
 31  
  * A set of standard permissions.
 32  
  *
 33  
  * @author Ben Alex
 34  
  * @version $Id: BasePermission.java 1868 2007-05-25 02:28:40Z benalex $
 35  
  */
 36  
 public final class BasePermission implements Permission {
 37  
     //~ Static fields/initializers =====================================================================================
 38  
 
 39  1
     public static final Permission READ = new BasePermission(1 << 0, 'R'); // 1
 40  1
     public static final Permission WRITE = new BasePermission(1 << 1, 'W'); // 2
 41  1
     public static final Permission CREATE = new BasePermission(1 << 2, 'C'); // 4
 42  1
     public static final Permission DELETE = new BasePermission(1 << 3, 'D'); // 8
 43  1
     public static final Permission ADMINISTRATION = new BasePermission(1 << 4, 'A'); // 16
 44  1
     private static Map locallyDeclaredPermissionsByInteger = new HashMap();
 45  1
     private static Map locallyDeclaredPermissionsByName = new HashMap();
 46  
 
 47  
     static {
 48  2
         Field[] fields = BasePermission.class.getDeclaredFields();
 49  
 
 50  11
         for (int i = 0; i < fields.length; i++) {
 51  
             try {
 52  10
                 Object fieldValue = fields[i].get(null);
 53  
 
 54  8
                 if (BasePermission.class.isAssignableFrom(fieldValue.getClass())) {
 55  
                     // Found a BasePermission static field
 56  5
                     BasePermission perm = (BasePermission) fieldValue;
 57  5
                     locallyDeclaredPermissionsByInteger.put(new Integer(perm.getMask()), perm);
 58  5
                     locallyDeclaredPermissionsByName.put(fields[i].getName(), perm);
 59  
                 }
 60  8
             } catch (Exception ignore) {}
 61  
         }
 62  1
     }
 63  
 
 64  
     //~ Instance fields ================================================================================================
 65  
 
 66  
     private char code;
 67  
     private int mask;
 68  
 
 69  
     //~ Constructors ===================================================================================================
 70  
 
 71  5
     private BasePermission(int mask, char code) {
 72  5
         this.mask = mask;
 73  5
         this.code = code;
 74  5
     }
 75  
 
 76  
     //~ Methods ========================================================================================================
 77  
 
 78  
     /**
 79  
      * Dynamically creates a <code>CumulativePermission</code> or <code>BasePermission</code> representing the
 80  
      * active bits in the passed mask.
 81  
      *
 82  
      * @param mask to build
 83  
      *
 84  
      * @return a Permission representing the requested object
 85  
      */
 86  
     public static Permission buildFromMask(int mask) {
 87  12
         if (locallyDeclaredPermissionsByInteger.containsKey(new Integer(mask))) {
 88  
             // The requested mask has an exactly match against a statically-defined BasePermission, so return it
 89  11
             return (Permission) locallyDeclaredPermissionsByInteger.get(new Integer(mask));
 90  
         }
 91  
 
 92  
         // To get this far, we have to use a CumulativePermission
 93  1
         CumulativePermission permission = new CumulativePermission();
 94  
 
 95  33
         for (int i = 0; i < 32; i++) {
 96  32
             int permissionToCheck = 1 << i;
 97  
 
 98  32
             if ((mask & permissionToCheck) == permissionToCheck) {
 99  3
                 Permission p = (Permission) locallyDeclaredPermissionsByInteger.get(new Integer(permissionToCheck));
 100  3
                 Assert.state(p != null,
 101  
                     "Mask " + permissionToCheck + " does not have a corresponding static BasePermission");
 102  3
                 permission.set(p);
 103  
             }
 104  
         }
 105  
 
 106  1
         return permission;
 107  
     }
 108  
 
 109  
     public static Permission[] buildFromMask(int[] masks) {
 110  0
         if ((masks == null) || (masks.length == 0)) {
 111  0
             return new Permission[] {};
 112  
         }
 113  
 
 114  0
         List list = new Vector();
 115  
 
 116  0
         for (int i = 0; i < masks.length; i++) {
 117  0
             list.add(BasePermission.buildFromMask(masks[i]));
 118  
         }
 119  
 
 120  0
         return (Permission[]) list.toArray(new Permission[] {});
 121  
     }
 122  
 
 123  
     public static Permission buildFromName(String name) {
 124  0
         Assert.isTrue(locallyDeclaredPermissionsByName.containsKey(name), "Unknown permission '" + name + "'");
 125  
 
 126  0
         return (Permission) locallyDeclaredPermissionsByName.get(name);
 127  
     }
 128  
 
 129  
     public static Permission[] buildFromName(String[] names) {
 130  0
         if ((names == null) || (names.length == 0)) {
 131  0
             return new Permission[] {};
 132  
         }
 133  
 
 134  0
         List list = new Vector();
 135  
 
 136  0
         for (int i = 0; i < names.length; i++) {
 137  0
             list.add(BasePermission.buildFromName(names[i]));
 138  
         }
 139  
 
 140  0
         return (Permission[]) list.toArray(new Permission[] {});
 141  
     }
 142  
 
 143  
     public boolean equals(Object arg0) {
 144  0
         if (!(arg0 instanceof BasePermission)) {
 145  0
             return false;
 146  
         }
 147  
 
 148  0
         BasePermission rhs = (BasePermission) arg0;
 149  
 
 150  0
         return (this.mask == rhs.getMask());
 151  
     }
 152  
 
 153  
     public int getMask() {
 154  99
         return mask;
 155  
     }
 156  
 
 157  
     public String getPattern() {
 158  38
         return AclFormattingUtils.printBinary(mask, code);
 159  
     }
 160  
 
 161  
     public String toString() {
 162  8
         return "BasePermission[" + getPattern() + "=" + mask + "]";
 163  
     }
 164  
     
 165  
         public int hashCode() {
 166  0
                 return this.mask;
 167  
         }
 168  
 }