1 11 package org.eclipse.osgi.framework.internal.core; 12 13 import java.security.Permission ; 14 import java.security.PermissionCollection ; 15 import java.util.Enumeration ; 16 import java.util.Vector ; 17 import org.osgi.framework.FrameworkEvent; 18 import org.osgi.service.condpermadmin.Condition; 19 import org.osgi.service.condpermadmin.ConditionalPermissionAdmin; 20 21 27 public class ConditionalPermissions extends PermissionCollection { 28 private static final long serialVersionUID = 3907215965749000496L; 29 private AbstractBundle bundle; 30 35 private Vector satisfiedCPIs = new Vector (); 36 40 private ConditionalPermissionSet satisfiedCPS; 41 45 private Vector satisfiableCPSs = new Vector (); 46 private boolean empty; 47 48 53 public ConditionalPermissions(AbstractBundle bundle, ConditionalPermissionAdmin cpa) { 54 this.bundle = bundle; 55 satisfiedCPS = new ConditionalPermissionSet(bundle, ConditionalPermissionAdminImpl.EMPTY_COND_PERM_INFO, ConditionalPermissionAdminImpl.EMPTY_COND); 56 Enumeration en = cpa.getConditionalPermissionInfos(); 57 while (en.hasMoreElements()) { 58 ConditionalPermissionInfoImpl cpi = (ConditionalPermissionInfoImpl) en.nextElement(); 59 checkConditionalPermissionInfo(cpi); 60 } 61 } 62 63 void checkConditionalPermissionInfo(ConditionalPermissionInfoImpl cpi) { 64 try { 65 removeCPI(cpi); 67 Condition conds[] = cpi.getConditions(bundle); 68 if (conds == null) { 69 70 return; 71 } 72 boolean satisfied = true; 73 for (int i = 0; i < conds.length; i++) { 74 Condition cond = conds[i]; 75 if (cond.isMutable()) { 76 satisfied = false; 77 } else if (!cond.isSatisfied()) { 82 return; 83 } else { 84 conds[i] = null; 85 } 86 } 87 if (satisfied) { 88 satisfiedCPIs.add(cpi); 89 } else { 90 satisfiableCPSs.add(new ConditionalPermissionSet(bundle, new ConditionalPermissionInfoImpl[] {cpi}, conds)); 91 } 92 } catch (Exception e) { 93 bundle.framework.publishFrameworkEvent(FrameworkEvent.ERROR, bundle, e); 94 } 95 } 96 97 private void removeCPI(ConditionalPermissionInfoImpl cpi) { 98 satisfiedCPIs.remove(cpi); 99 satisfiedCPS.remove(cpi); 100 ConditionalPermissionSet cpsArray[] = (ConditionalPermissionSet[]) satisfiableCPSs.toArray(new ConditionalPermissionSet[0]); 101 for (int i = 0; i < cpsArray.length; i++) 102 if (cpsArray[i].remove(cpi)) 103 satisfiableCPSs.remove(cpsArray[i]); 104 } 105 106 112 public void add(Permission perm) { 113 } 115 116 public boolean implies(Permission perm) { 117 processPending(); 118 boolean newEmpty = !satisfiedCPS.isNonEmpty(); 119 if (!newEmpty && satisfiedCPS.implies(perm)) { 120 this.empty = false; 121 return true; 122 } 123 boolean satisfied = false; 124 Vector unevalCondsSets = null; 125 SecurityManager sm = System.getSecurityManager(); 126 FrameworkSecurityManager fsm = null; 127 if (sm instanceof FrameworkSecurityManager) 128 fsm = (FrameworkSecurityManager) sm; 129 ConditionalPermissionSet cpsArray[] = (ConditionalPermissionSet[]) satisfiableCPSs.toArray(new ConditionalPermissionSet[0]); 130 cpsLoop: for (int i = 0; i < cpsArray.length; i++) { 131 if (cpsArray[i].isNonEmpty()) { 132 newEmpty = false; 133 Condition conds[] = cpsArray[i].getNeededConditions(); 134 if (conds == null) 135 continue; 136 for (int j = 0; j < conds.length; j++) 139 if (conds[j] != null && !conds[j].isPostponed() && !conds[j].isSatisfied()) 140 continue cpsLoop; 141 if (cpsArray[i].implies(perm)) { 143 Vector unevaluatedConds = null; 146 for (int j = 0; j < conds.length; j++) { 147 if (conds[j] != null && conds[j].isPostponed()) { 148 if (fsm == null) { 149 if (!conds[j].isSatisfied()) 151 continue cpsLoop; 152 } else { 153 if (unevaluatedConds == null) 154 unevaluatedConds = new Vector (); 155 unevaluatedConds.add(conds[j]); 156 } 157 } 158 } 159 if (unevaluatedConds == null) { 160 this.empty = false; 162 return true; 163 } 164 if (unevalCondsSets == null) 165 unevalCondsSets = new Vector (2); 166 unevalCondsSets.add(unevaluatedConds.toArray(new Condition[unevaluatedConds.size()])); 167 satisfied = true; 168 } 169 } else { 170 satisfiableCPSs.remove(cpsArray[i]); 171 } 172 } 173 this.empty = newEmpty; 174 if (satisfied && fsm != null) { 175 Condition[][] condArray = (Condition[][]) unevalCondsSets.toArray(new Condition[unevalCondsSets.size()][]); 179 satisfied = fsm.addConditionsForDomain(condArray); 180 } 181 return satisfied; 182 } 183 184 187 private void processPending() { 188 if (satisfiedCPIs.size() > 0) { 189 synchronized (satisfiedCPIs) { 190 for (int i = 0; i < satisfiedCPIs.size(); i++) { 191 ConditionalPermissionInfoImpl cpi = (ConditionalPermissionInfoImpl) satisfiedCPIs.get(i); 192 if (!cpi.isDeleted()) 193 satisfiedCPS.addConditionalPermissionInfo(cpi); 194 } 195 satisfiedCPIs.clear(); 196 } 197 } 198 } 199 200 208 public Enumeration elements() { 209 return null; 210 } 211 212 220 boolean isEmpty() { 221 return empty; 222 } 223 224 void unresolvePermissions() { 225 satisfiedCPS.unresolvePermissions(); 226 synchronized (satisfiableCPSs) { 227 Enumeration en = satisfiableCPSs.elements(); 228 while (en.hasMoreElements()) { 229 ConditionalPermissionSet cs = (ConditionalPermissionSet) en.nextElement(); 230 cs.unresolvePermissions(); 231 } 232 } 233 } 234 } 235 | Popular Tags |