1 22 package org.jboss.test.security.test; 23 24 import java.security.Permission ; 25 import java.security.PermissionCollection ; 26 import java.util.ArrayList ; 27 import java.util.Enumeration ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.NoSuchElementException ; 31 import java.util.Set ; 32 import java.util.SortedMap ; 33 import java.util.TreeMap ; 34 35 40 public class NamespacePermissionCollection extends PermissionCollection 41 { 42 private TreeMap namespacePerms = new TreeMap (); 43 private TreeMap namespaceKeys = new TreeMap (new PermissionName.NameLengthComparator()); 44 45 46 public NamespacePermissionCollection() 47 { 48 } 49 50 public void add(Permission permission) 51 { 52 if( this.isReadOnly() ) 53 throw new SecurityException ("Cannot add permission to read-only collection"); 54 if( (permission instanceof NamespacePermission) == false ) 55 throw new IllegalArgumentException ("Only NamespacePermission can be added, invalid="+permission); 56 NamespacePermission np = (NamespacePermission) permission; 57 PermissionName key = np.getFullName(); 58 ArrayList tmp = (ArrayList ) namespacePerms.get(key); 59 if( tmp == null ) 60 { 61 tmp = new ArrayList (); 62 namespacePerms.put(key, tmp); 63 namespaceKeys.put(key, key); 64 } 65 tmp.add(np); 66 } 67 68 71 public boolean implies(Permission permission) 72 { 73 boolean implies = false; 74 if( namespacePerms.isEmpty() == true ) 75 return false; 76 77 NamespacePermission np = (NamespacePermission) permission; 78 PermissionName key = np.getFullName(); 80 ArrayList tmp = (ArrayList ) namespacePerms.get(key); 81 if( tmp == null ) 82 { SortedMap headMap = namespacePerms.headMap(key); 84 try 85 { 86 PermissionName lastKey = (PermissionName) headMap.lastKey(); 87 if( lastKey.isParent(key) == true ) 88 tmp = (ArrayList ) namespacePerms.get(lastKey); 89 else 90 { 91 PermissionName[] keys = {}; 92 keys = (PermissionName[]) headMap.keySet().toArray(keys); 93 for(int k = keys.length-1; k >= 0; k --) 94 { 95 lastKey = keys[k]; 96 if( lastKey.isParent(key) == true ) 97 { 98 tmp = (ArrayList ) namespacePerms.get(lastKey); 99 break; 100 } 101 } 102 } 103 } 104 catch(NoSuchElementException e) 105 { 109 } 110 } 111 112 if( tmp != null ) 114 implies = isImplied(tmp, np); 115 return implies; 117 } 118 119 public Enumeration elements() 120 { 121 Set s = namespaceKeys.keySet(); 122 final Iterator iter = s.iterator(); 123 Enumeration elements = new Enumeration () 124 { 125 ArrayList activeEntry; 126 int index; 127 public boolean hasMoreElements() 128 { 129 boolean hasMoreElements = true; 130 if( activeEntry == null || index >= activeEntry.size() ) 131 { 132 hasMoreElements = iter.hasNext(); 133 activeEntry = null; 134 } 135 return hasMoreElements; 136 } 137 public Object nextElement() 138 { 139 Object next = null; 140 if( activeEntry == null ) 141 { 142 Object key = iter.next(); 143 activeEntry = (ArrayList ) namespacePerms.get(key); 144 index = 0; 145 next = activeEntry.get(index ++); 146 } 147 else 148 { 149 next = activeEntry.get(index ++); 150 } 151 return next; 152 } 153 }; 154 return elements; 155 } 156 157 158 private boolean isImplied(ArrayList permissions, NamespacePermission np) 159 { 160 boolean isImplied = false; 161 for(int p = 0; p < permissions.size(); p ++) 162 { 163 Permission perm = (Permission ) permissions.get(p); 164 isImplied |= perm.implies(np); 165 if( isImplied == true ) 166 break; 167 } 168 return isImplied; 169 } 170 } 171 | Popular Tags |