1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.io.IOException ; 15 import java.security.*; 16 import java.util.*; 17 import org.eclipse.osgi.framework.adaptor.PermissionStorage; 18 import org.osgi.framework.FrameworkEvent; 19 import org.osgi.service.condpermadmin.*; 20 import org.osgi.service.permissionadmin.PermissionInfo; 21 22 26 public class ConditionalPermissionAdminImpl implements ConditionalPermissionAdmin { 27 public static final ConditionalPermissionInfoImpl[] EMPTY_COND_PERM_INFO = new ConditionalPermissionInfoImpl[0]; 28 public static final Condition[] EMPTY_COND = new Condition[0]; 29 public static final ConditionInfo[] EMPTY_COND_INFO = new ConditionInfo[0]; 30 public static final PermissionInfo[] EMPTY_PERM_INFO = new PermissionInfo[0]; 31 32 35 private Vector condPerms; 36 private Framework framework; 37 private PermissionStorage storage; 38 private long nextID = System.currentTimeMillis(); 39 40 44 public ConditionalPermissionAdminImpl(Framework framework, PermissionStorage permissionStorage) { 45 ConditionalPermissionInfoImpl.setConditionalPermissionAdminImpl(this); 46 this.framework = framework; 47 this.storage = permissionStorage; 48 try { 49 String [] encodedCondPerms = permissionStorage.getConditionalPermissionInfos(); 50 if (encodedCondPerms != null && encodedCondPerms.length > 0) { 51 condPerms = new Vector(encodedCondPerms.length); 52 for (int i = 0; i < encodedCondPerms.length; i++) 53 condPerms.add(new ConditionalPermissionInfoImpl(encodedCondPerms[i])); 54 } 55 else 56 condPerms = new Vector(0); 57 } catch (IOException e) { 58 framework.publishFrameworkEvent(FrameworkEvent.ERROR, framework.systemBundle, e); 59 condPerms = new Vector(); 60 } 61 } 62 63 66 public ConditionalPermissionInfo addConditionalPermissionInfo(ConditionInfo[] conds, PermissionInfo[] perms) { 67 return setConditionalPermissionInfo(null, conds, perms); 68 } 69 70 public ConditionalPermissionInfo setConditionalPermissionInfo(String name, ConditionInfo conds[], PermissionInfo perms[]) { 71 SecurityManager sm = System.getSecurityManager(); 72 if (sm != null) 73 sm.checkPermission(new AllPermission()); 74 if (name == null) 75 name = "generated_" + Long.toString(nextID++); ConditionalPermissionInfoImpl condPermInfo = null; 77 synchronized (condPerms) { 78 condPermInfo = (ConditionalPermissionInfoImpl) getConditionalPermissionInfo(name); 79 if (condPermInfo == null) { 80 condPermInfo = new ConditionalPermissionInfoImpl(name, conds, perms); 81 condPerms.add(condPermInfo); 82 } else { 83 condPermInfo.conds = conds; 84 condPermInfo.perms = perms; 85 } 86 saveCondPermInfos(); 87 } 88 AbstractBundle bundles[] = framework.getAllBundles(); 89 for (int i = 0; i < bundles.length; i++) { 90 AbstractBundle bundle = bundles[i]; 91 if (bundle.domain == null) { 92 continue; 93 } 94 BundleCombinedPermissions bcp = (BundleCombinedPermissions) bundle.domain.getPermissions(); 95 96 if (perms == null) { 97 continue; 98 } 99 100 bcp.checkConditionalPermissionInfo(condPermInfo); 101 } 102 return condPermInfo; 103 } 104 105 public ConditionalPermissionInfo getConditionalPermissionInfo(String name) { 106 for (Enumeration eCondPerms = condPerms.elements(); eCondPerms.hasMoreElements();) { 107 ConditionalPermissionInfoImpl condPerm = (ConditionalPermissionInfoImpl) eCondPerms.nextElement(); 108 if (name.equals(condPerm.getName())) 109 return condPerm; 110 } 111 return null; 112 } 113 114 120 public Enumeration getConditionalPermissionInfos() { 121 synchronized (condPerms) { 122 return condPerms.elements(); 123 } 124 } 125 126 void deleteConditionalPermissionInfo(ConditionalPermissionInfo cpi) { 127 synchronized (condPerms) { 128 condPerms.remove(cpi); 129 saveCondPermInfos(); 130 } 131 } 132 133 private void saveCondPermInfos() { 134 try { 135 String [] encodedCondPerms = new String [condPerms.size()]; 136 int i = 0; 137 for (Enumeration eCondPerms = condPerms.elements(); eCondPerms.hasMoreElements(); i++) 138 encodedCondPerms[i] = eCondPerms.nextElement().toString(); 139 storage.saveConditionalPermissionInfos(encodedCondPerms); 140 } catch (IOException e) { 141 e.printStackTrace(); 142 framework.publishFrameworkEvent(FrameworkEvent.ERROR, framework.systemBundle, e); 143 } 144 } 145 146 149 public AccessControlContext getAccessControlContext(String [] signers) { 150 Enumeration infos = getConditionalPermissionInfos(); 151 ArrayList permissionInfos = new ArrayList(); 152 if (infos != null) { 153 while (infos.hasMoreElements()) { 154 ConditionalPermissionInfoImpl condPermInfo = (ConditionalPermissionInfoImpl) infos.nextElement(); 155 ConditionInfo[] condInfo = condPermInfo.getConditionInfos(); 156 boolean match = true; 157 for (int i = 0; i < condInfo.length; i++) { 158 if (BundleSignerCondition.class.getName().equals(condInfo[i].getType())) { 159 String [] args = condInfo[i].getArgs(); 160 for (int j = 0; j < args.length; j++) 161 if (!framework.adaptor.matchDNChain(args[j], signers)) { 162 match = false; 163 break; 164 } 165 } else { 166 match = false; 167 break; 168 } 169 } 170 if (match) { 171 PermissionInfo[] addPermInfos = condPermInfo.getPermissionInfos(); 172 for (int i = 0; i < addPermInfos.length; i++) 173 permissionInfos.add(addPermInfos[i]); 174 } 175 } 176 } 177 BundlePermissionCollection collection = framework.permissionAdmin.createPermissions((PermissionInfo[]) permissionInfos.toArray(new PermissionInfo[permissionInfos.size()]), null, false); 178 return new AccessControlContext(collection == null ? new ProtectionDomain[0] : new ProtectionDomain[] {new ProtectionDomain(null, collection)}); 179 } 180 } 181 | Popular Tags |