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