206 } 207 } 208 209 /** 210 * A ExecOptionPermissionCollection stores a collection 211 * of ExecOptionPermission permissions. ExecOptionPermission objects 212 * must be stored in a manner that allows them to be inserted in any 213 * order, but enable the implies function to evaluate the implies 214 * method in an efficient (and consistent) manner. 215 * 216 * A ExecOptionPermissionCollection handles comparing a permission like 217 * "a.b.c.d.e" * with a Permission such as "a.b.*", or "*". 218 * 219 * @serial include 220 */ 221 private static class ExecOptionPermissionCollection 222 extends PermissionCollection 223 implements java.io.Serializable 224 { 225 226 private Hashtable permissions; 227 private boolean all_allowed; // true if "*" is in the collection 228 private static final long serialVersionUID = -1242475729790124375L; 229 230 /** 231 * Create an empty ExecOptionPermissionCollection. 232 */ 233 public ExecOptionPermissionCollection() { 234 permissions = new Hashtable(11); 235 all_allowed = false; 236 } 237 238 /** 239 * Adds a permission to the collection. The key for the hash is 240 * permission.name. 241 * 242 * @param permission the Permission object to add. 243 * 244 * @exception IllegalArgumentException - if the permission is not a 245 * ExecOptionPermission 246 * 247 * @exception SecurityException - if this ExecOptionPermissionCollection 248 * object has been marked readonly 249 */ 250 251 public void add(Permission permission) 252 { 253 if (! (permission instanceof ExecOptionPermission)) 254 throw new IllegalArgumentException("invalid permission: "+ 274 * @return true if "permission" is a proper subset of a permission in 275 * the set, false if not. 276 */ 277 public boolean implies(Permission permission) 278 { 279 if (! (permission instanceof ExecOptionPermission)) 280 return false; 281 282 ExecOptionPermission p = (ExecOptionPermission) permission; 283 284 // short circuit if the "*" Permission was added 285 if (all_allowed) 286 return true; 287 288 // strategy: 289 // Check for full match first. Then work our way up the 290 // name looking for matches on a.b.* 291 292 String pname = p.getName(); 293 294 Permission x = (Permission) permissions.get(pname); 295 296 if (x != null) 297 // we have a direct hit! 298 return x.implies(permission); 299 300 301 // work our way up the tree... 302 int last, offset; 303 304 offset = pname.length() - 1; 305 306 while ((last = pname.lastIndexOf(".", offset)) != -1) { 307 308 pname = pname.substring(0, last+1) + "*"; 309 x = (Permission) permissions.get(pname); 310 311 if (x != null) { 312 return x.implies(permission); 313 } 314 offset = last - 1; 315 } 316 317 // check for "=*" wildcard match 318 pname = p.getName(); 319 offset = pname.length() - 1; 320 321 while ((last = pname.lastIndexOf("=", offset)) != -1) { 322 323 pname = pname.substring(0, last+1) + "*"; 324 x = (Permission) permissions.get(pname); 325 326 if (x != null) { 327 return x.implies(permission); 328 } 329 offset = last - 1; 330 } 331 332 // we don't have to check for "*" as it was already checked 333 // at the top (all_allowed), so we just return false 334 return false; 335 } 336 337 /** 338 * Returns an enumeration of all the ExecOptionPermission objects in the 339 * container. 340 * 341 * @return an enumeration of all the ExecOptionPermission objects. 342 */ 343 344 public Enumeration elements() 345 { 346 return permissions.elements(); 347 } 348 } 349 } | 206 } 207 } 208 209 /** 210 * A ExecOptionPermissionCollection stores a collection 211 * of ExecOptionPermission permissions. ExecOptionPermission objects 212 * must be stored in a manner that allows them to be inserted in any 213 * order, but enable the implies function to evaluate the implies 214 * method in an efficient (and consistent) manner. 215 * 216 * A ExecOptionPermissionCollection handles comparing a permission like 217 * "a.b.c.d.e" * with a Permission such as "a.b.*", or "*". 218 * 219 * @serial include 220 */ 221 private static class ExecOptionPermissionCollection 222 extends PermissionCollection 223 implements java.io.Serializable 224 { 225 226 private Hashtable<String, Permission> permissions; 227 private boolean all_allowed; // true if "*" is in the collection 228 private static final long serialVersionUID = -1242475729790124375L; 229 230 /** 231 * Create an empty ExecOptionPermissionCollection. 232 */ 233 public ExecOptionPermissionCollection() { 234 permissions = new Hashtable<String, Permission>(11); 235 all_allowed = false; 236 } 237 238 /** 239 * Adds a permission to the collection. The key for the hash is 240 * permission.name. 241 * 242 * @param permission the Permission object to add. 243 * 244 * @exception IllegalArgumentException - if the permission is not a 245 * ExecOptionPermission 246 * 247 * @exception SecurityException - if this ExecOptionPermissionCollection 248 * object has been marked readonly 249 */ 250 251 public void add(Permission permission) 252 { 253 if (! (permission instanceof ExecOptionPermission)) 254 throw new IllegalArgumentException("invalid permission: "+ 274 * @return true if "permission" is a proper subset of a permission in 275 * the set, false if not. 276 */ 277 public boolean implies(Permission permission) 278 { 279 if (! (permission instanceof ExecOptionPermission)) 280 return false; 281 282 ExecOptionPermission p = (ExecOptionPermission) permission; 283 284 // short circuit if the "*" Permission was added 285 if (all_allowed) 286 return true; 287 288 // strategy: 289 // Check for full match first. Then work our way up the 290 // name looking for matches on a.b.* 291 292 String pname = p.getName(); 293 294 Permission x = permissions.get(pname); 295 296 if (x != null) 297 // we have a direct hit! 298 return x.implies(permission); 299 300 301 // work our way up the tree... 302 int last, offset; 303 304 offset = pname.length() - 1; 305 306 while ((last = pname.lastIndexOf(".", offset)) != -1) { 307 308 pname = pname.substring(0, last+1) + "*"; 309 x = permissions.get(pname); 310 311 if (x != null) { 312 return x.implies(permission); 313 } 314 offset = last - 1; 315 } 316 317 // check for "=*" wildcard match 318 pname = p.getName(); 319 offset = pname.length() - 1; 320 321 while ((last = pname.lastIndexOf("=", offset)) != -1) { 322 323 pname = pname.substring(0, last+1) + "*"; 324 x = permissions.get(pname); 325 326 if (x != null) { 327 return x.implies(permission); 328 } 329 offset = last - 1; 330 } 331 332 // we don't have to check for "*" as it was already checked 333 // at the top (all_allowed), so we just return false 334 return false; 335 } 336 337 /** 338 * Returns an enumeration of all the ExecOptionPermission objects in the 339 * container. 340 * 341 * @return an enumeration of all the ExecOptionPermission objects. 342 */ 343 344 public Enumeration<Permission> elements() 345 { 346 return permissions.elements(); 347 } 348 } 349 } |