1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.security.*; 15 import java.util.*; 16 import org.osgi.service.condpermadmin.Condition; 17 import org.osgi.service.permissionadmin.PermissionInfo; 18 19 23 public class ConditionalPermissionSet extends BundlePermissionCollection { 24 private static final long serialVersionUID = 3258411750729920566L; 25 private ConditionalPermissionInfoImpl cpis[] = ConditionalPermissionAdminImpl.EMPTY_COND_PERM_INFO; 26 private HashMap cachedPermissionCollections = new HashMap(); 27 private boolean hasAllPermission = false; 28 private AbstractBundle bundle; 29 34 private Condition neededConditions[]; 35 36 40 44 public ConditionalPermissionSet(AbstractBundle bundle, ConditionalPermissionInfoImpl cpis[], Condition neededConditions[]) { 45 this.bundle = bundle; 46 this.cpis = cpis; 47 this.neededConditions = neededConditions; 48 checkForAllPermission(); 49 } 50 51 60 void addConditionalPermissionInfo(ConditionalPermissionInfoImpl cpi) { 61 if (neededConditions == null || neededConditions.length > 0) 62 throw new RuntimeException ("Cannot add ConditionalPermissionInfoImpl to a non satisfied set"); synchronized (cachedPermissionCollections) { 64 for (int i = 0; i < cpis.length; i++) 66 if (cpis[i] == null) { cpis[i] = cpi; 68 cachedPermissionCollections.clear(); 69 return; 70 } 71 ConditionalPermissionInfoImpl newcpis[] = new ConditionalPermissionInfoImpl[cpis.length + 1]; 72 System.arraycopy(cpis, 0, newcpis, 0, cpis.length); 73 newcpis[cpis.length] = cpi; 74 cpis = newcpis; 75 81 cachedPermissionCollections.clear(); 82 checkForAllPermission(); 83 } 84 } 85 86 89 private void checkForAllPermission() { 90 if (hasAllPermission) { 91 return; 92 } 93 out: for (int i = 0; i < cpis.length; i++) { 94 if (cpis[i] == null) continue; 96 PermissionInfo perms[] = cpis[i].perms; 97 for (int j = 0; j < perms.length; j++) { 98 if (perms[j].getType().equals(AllPermission.class.getName())) { 99 hasAllPermission = true; 100 break out; 101 } 102 } 103 } 104 } 105 106 112 boolean isNonEmpty() { 113 boolean nonEmpty = false; 114 boolean forceAllPermCheck = false; 115 synchronized (cachedPermissionCollections) { 116 for (int i = 0; i < cpis.length; i++) { 117 if (cpis[i] != null) { 118 if (cpis[i].isDeleted()) { 119 cpis[i] = null; 120 forceAllPermCheck = true; 121 127 cachedPermissionCollections.clear(); 128 } else { 129 nonEmpty = true; 130 } 131 } 132 } 133 if (!nonEmpty) 134 cpis = ConditionalPermissionAdminImpl.EMPTY_COND_PERM_INFO; 135 if (forceAllPermCheck) { 136 hasAllPermission = false; 137 checkForAllPermission(); 138 } 139 } 140 return nonEmpty; 141 } 142 143 152 Condition[] getNeededConditions() { 153 if (neededConditions == null || neededConditions.length == 0) 154 return neededConditions; 155 boolean foundNonNullCondition = false; 156 157 for (int i = 0; i < neededConditions.length; i++) { 158 Condition cond = neededConditions[i]; 159 if (cond == null) 160 continue; 161 if (!cond.isMutable()) { 162 if (cond.isSatisfied()) { 163 neededConditions[i] = null; 164 } else { 165 169 neededConditions = null; 170 break; 171 } 172 } else { 173 foundNonNullCondition = true; 174 } 175 } 176 if (neededConditions != null && !foundNonNullCondition) 177 neededConditions = ConditionalPermissionAdminImpl.EMPTY_COND; 178 return neededConditions; 179 } 180 181 187 public void add(Permission perm) { 188 } 190 191 200 public boolean implies(Permission perm) { 201 if (hasAllPermission) 202 return true; 203 Class permClass = perm.getClass(); 204 PermissionCollection collection; 205 synchronized (cachedPermissionCollections) { 206 collection = (PermissionCollection) cachedPermissionCollections.get(permClass); 207 if (collection == null) { 208 collection = perm.newPermissionCollection(); 209 if (collection == null) 210 collection = new PermissionsHash(); 211 for (int i = 0; i < cpis.length; i++) { 212 try { 213 ConditionalPermissionInfoImpl cpi = cpis[i]; 214 if (cpi != null) 215 cpi.addPermissions(bundle, collection, permClass); 216 } catch (Exception e) { 217 e.printStackTrace(); 219 } 220 } 221 cachedPermissionCollections.put(permClass, collection); 222 } 223 } 224 return collection.implies(perm); 225 } 226 227 233 public Enumeration elements() { 234 return null; 235 } 236 237 242 void unresolvePermissions() { 243 synchronized (cachedPermissionCollections) { 244 cachedPermissionCollections.clear(); 245 } 246 } 247 248 boolean remove(ConditionalPermissionInfoImpl cpi) { 249 synchronized (cachedPermissionCollections) { 250 for (int i = 0; i < cpis.length; i++) 251 if (cpis[i] == cpi) { 252 cpis[i] = null; 253 cachedPermissionCollections.clear(); 254 return true; 255 } 256 } 257 return false; 258 } 259 } 260 | Popular Tags |