1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.io.File ; 15 import java.io.Serializable ; 16 import java.lang.reflect.*; 17 import java.security.*; 18 import java.util.ArrayList ; 19 import org.osgi.framework.Bundle; 20 import org.osgi.service.condpermadmin.*; 21 import org.osgi.service.permissionadmin.PermissionInfo; 22 23 29 public class ConditionalPermissionInfoImpl implements ConditionalPermissionInfo, Serializable { 30 private static final long serialVersionUID = 3258130245704825139L; 31 34 PermissionInfo perms[]; 35 39 ConditionInfo conds[]; 40 41 44 private String name; 45 46 50 private boolean deleted = false; 51 52 56 boolean isDeleted() { 57 return deleted; 58 } 59 60 public ConditionalPermissionInfoImpl(String encoded) { 61 decode(encoded); 62 } 63 64 public ConditionalPermissionInfoImpl(String name, ConditionInfo conds[], PermissionInfo perms[]) { 65 this.name = name; 66 this.conds = conds; 67 this.perms = perms; 68 } 69 70 private void decode(String encoded) { 71 int start = encoded.indexOf('{'); 72 int end = encoded.lastIndexOf('}'); 73 if (start < 0 || end < start) 74 throw new IllegalArgumentException (encoded); 75 if (start != 0) 76 name = encoded.substring(0, start); 77 char[] chars = encoded.substring(start + 1, end).toCharArray(); 78 ArrayList condList = new ArrayList (); 79 ArrayList permList = new ArrayList (); 80 int pos = 0; 81 while (pos < chars.length) { 82 while (pos < chars.length && chars[pos] != '[' && chars[pos] != '(') 83 pos++; 84 if (pos == chars.length) 85 break; int startPos = pos; 87 char endChar = chars[startPos] == '[' ? ']' : ')'; 88 while (chars[pos] != endChar) { 89 if (chars[pos] == '"') { 90 pos++; 91 while (chars[pos] != '"') { 92 if (chars[pos] == '\\') 93 pos++; 94 pos++; 95 } 96 } 97 pos++; 98 } 99 int endPos = pos; 100 String token = new String (chars, startPos, endPos - startPos + 1); 101 if (endChar == ']') 102 condList.add(new ConditionInfo(token)); 103 else 104 permList.add(new PermissionInfo(token)); 105 pos++; 106 } 107 conds = (ConditionInfo[]) condList.toArray(new ConditionInfo[condList.size()]); 108 perms = (PermissionInfo[]) permList.toArray(new PermissionInfo[permList.size()]); 109 } 110 111 public String getName() { 112 return name; 113 } 114 115 118 public ConditionInfo[] getConditionInfos() { 119 if (conds == null) 120 return null; 121 ConditionInfo[] results = new ConditionInfo[conds.length]; 122 System.arraycopy(conds, 0, results, 0, conds.length); 123 return results; 124 } 125 126 127 static private final Class twoStringClassArray[] = new Class [] {String .class, String .class}; 128 static private final Class oneStringClassArray[] = new Class [] {String .class}; 129 static private final Class noArgClassArray[] = new Class [] {}; 130 static private final Class [][] permClassArrayArgs = new Class [][] {noArgClassArray, oneStringClassArray, twoStringClassArray}; 131 132 static private final Class [] condClassArray = new Class [] {Bundle.class, ConditionInfo.class}; 133 134 149 int addPermissions(AbstractBundle bundle, PermissionCollection collection, Class permClass) throws SecurityException , NoSuchMethodException , IllegalArgumentException , InstantiationException , IllegalAccessException , InvocationTargetException { 150 String permClassName = permClass.getName(); 151 Constructor constructor = null; 152 int numArgs = -1; 153 for (int i = permClassArrayArgs.length - 1 ; i >= 0; i--) { 154 try { 155 constructor = permClass.getConstructor(permClassArrayArgs[i]); 156 numArgs = i; 157 break; 158 } catch (NoSuchMethodException e) { 159 } 161 } 162 if (constructor == null) 163 throw new NoSuchMethodException (permClass.getName() + ".<init>()"); int count = 0; 165 168 for (int i = 0; i < perms.length; i++) { 169 if (perms[i].getType().equals(permClassName)) { 170 count++; 171 String args[] = new String [numArgs]; 172 if (numArgs > 0) 173 args[0] = perms[i].getName(); 174 if (numArgs > 1) 175 args[1] = perms[i].getActions(); 176 if (perms[i].getType().equals("java.io.FilePermission")) { if (!args[0].equals("<<ALL FILES>>")) { File file = new File (args[0]); 180 if (!file.isAbsolute()) { if (bundle == null) continue; File target = bundle.framework.getDataFile(bundle, args[0]); 184 if (target == null) continue; args[0] = target.getPath(); 187 } 188 } 189 } 190 collection.add((Permission) constructor.newInstance(args)); 191 } 192 } 193 return count; 194 } 195 196 204 Condition[] getConditions(Bundle bundle) { 205 Condition conditions[] = new Condition[conds.length]; 206 for (int i = 0; i < conds.length; i++) { 207 211 Class clazz; 212 try { 213 clazz = Class.forName(conds[i].getType()); 214 } catch (ClassNotFoundException e) { 215 216 return null; 217 } 218 Constructor constructor = null; 219 Method method = null; 220 try { 221 method = clazz.getMethod("getCondition", condClassArray); if ((method.getModifiers() & Modifier.STATIC) == 0) 223 method = null; 224 } catch (NoSuchMethodException e) { 225 } 227 if (method == null) 228 try { 229 constructor = clazz.getConstructor(condClassArray); 230 } catch (NoSuchMethodException e) { 231 conditions[i] = Condition.FALSE; 233 continue; 234 } 235 236 Object args[] = {bundle, conds[i]}; 237 try { 238 if (method != null) 239 conditions[i] = (Condition) method.invoke(null, args); 240 else 241 conditions[i] = (Condition) constructor.newInstance(args); 242 } catch (Throwable t) { 243 conditions[i] = Condition.FALSE; 245 } 246 } 247 return conditions; 248 } 249 250 253 public PermissionInfo[] getPermissionInfos() { 254 if (perms == null) 255 return null; 256 PermissionInfo[] results = new PermissionInfo[perms.length]; 257 System.arraycopy(perms, 0, results, 0, perms.length); 258 return results; 259 } 260 261 265 public void delete() { 266 SecurityManager sm = System.getSecurityManager(); 267 if (sm != null) 268 sm.checkPermission(new AllPermission()); 269 deleted = true; 270 condAdmin.deleteConditionalPermissionInfo(this); 271 } 272 273 private static ConditionalPermissionAdminImpl condAdmin; 274 275 static void setConditionalPermissionAdminImpl(ConditionalPermissionAdminImpl condAdmin) { 276 ConditionalPermissionInfoImpl.condAdmin = condAdmin; 277 } 278 279 public String toString() { 280 StringBuffer result = new StringBuffer (); 281 if (name != null) 282 result.append(name); 283 ConditionInfo[] curConds = getConditionInfos(); 284 PermissionInfo[] curPerms = getPermissionInfos(); 285 result.append('{').append(' '); 286 if (curConds != null) 287 for (int i = 0; i < curConds.length; i++) 288 result.append(curConds[i].getEncoded()).append(' '); 289 if (curPerms != null) 290 for (int i = 0; i < curPerms.length; i++) 291 result.append(curPerms[i].getEncoded()).append(' '); 292 result.append('}'); 293 return result.toString(); 294 } 295 } 296 | Popular Tags |