/** * Adds a permission to the <tt>PackagePermission</tt> objects. The key for the hash is the name. * * @param permission The <tt>PackagePermission</tt> object to add. * @throws IllegalArgumentException If the permission is not a <tt>PackagePermission</tt> * instance. * @throws SecurityException If this <tt>PackagePermissionCollection</tt> object has been marked * read-only. */ @Override public void add(Permission permission) { if (!(permission instanceof PackagePermission)) throw new IllegalArgumentException("invalid permission: " + permission); if (isReadOnly()) throw new SecurityException( "attempt to add a Permission to a " + "readonly PermissionCollection"); PackagePermission pp = (PackagePermission) permission; String name = pp.getName(); PackagePermission existing = (PackagePermission) permissions.get(name); if (existing != null) { int oldMask = existing.getMask(); int newMask = pp.getMask(); if (oldMask != newMask) { permissions.put(name, new PackagePermission(name, oldMask | newMask)); } } else { permissions.put(name, permission); } if (!all_allowed) { if (name.equals("*")) all_allowed = true; } }
/** * Determines if the specified permissions implies the permissions expressed in * <tt>permission</tt>. * * @param p The Permission object to compare with this <tt>PackagePermission</tt> object. * @return <tt>true</tt> if <tt>permission</tt> is a proper subset of a permission in the set; * <tt>false</tt> otherwise. */ @Override public boolean implies(Permission permission) { if (!(permission instanceof PackagePermission)) return (false); PackagePermission pp = (PackagePermission) permission; PackagePermission x; int desired = pp.getMask(); int effective = 0; // short circuit if the "*" Permission was added if (all_allowed) { x = (PackagePermission) permissions.get("*"); if (x != null) { effective |= x.getMask(); if ((effective & desired) == desired) return (true); } } // strategy: // Check for full match first. Then work our way up the // name looking for matches on a.b.* String name = pp.getName(); x = (PackagePermission) permissions.get(name); if (x != null) { // we have a direct hit! effective |= x.getMask(); if ((effective & desired) == desired) return (true); } // work our way up the tree... int last, offset; offset = name.length() - 1; while ((last = name.lastIndexOf(".", offset)) != -1) { name = name.substring(0, last + 1) + "*"; x = (PackagePermission) permissions.get(name); if (x != null) { effective |= x.getMask(); if ((effective & desired) == desired) return (true); } offset = last - 1; } // we don't have to check for "*" as it was already checked // at the top (all_allowed), so we just return false return (false); }
/** * Determines the equality of two <tt>PackagePermission</tt> objects. * * <p>This method checks that specified package has the same package name and * <tt>PackagePermission</tt> actions as this <tt>PackagePermission</tt> object. * * @param obj The object to test for equality with this <tt>PackagePermission</tt> object. * @return <tt>true</tt> if <tt>obj</tt> is a <tt>PackagePermission</tt>, and has the same package * name and actions as this <tt>PackagePermission</tt> object; <tt>false</tt> otherwise. */ @Override public boolean equals(Object obj) { if (obj == this) { return (true); } if (!(obj instanceof PackagePermission)) { return (false); } PackagePermission p = (PackagePermission) obj; return ((action_mask == p.action_mask) && getName().equals(p.getName())); }