BasicPermissionCollectionpublic final class BasicPermissionCollection extends PermissionCollection Specific {@code PermissionCollection} for storing {@code BasicPermissions} of
arbitrary type. |
Fields Summary |
---|
private static final long | serialVersionUID | private static final ObjectStreamField[] | serialPersistentFields | private transient Map | items | private transient boolean | allEnabled | private Class | permClass |
Methods Summary |
---|
public void | add(java.security.Permission permission)Adds a permission to the collection. The first added permission must be a
subclass of BasicPermission, next permissions must be of the same class
as the first one.
if (isReadOnly()) {
throw new SecurityException(Messages.getString("security.15")); //$NON-NLS-1$
}
if (permission == null) {
throw new IllegalArgumentException(Messages.getString("security.20")); //$NON-NLS-1$
}
Class<? extends Permission> inClass = permission.getClass();
if (permClass != null) {
if (permClass != inClass) {
throw new IllegalArgumentException(Messages.getString("security.16", //$NON-NLS-1$
permission));
}
} else if( !(permission instanceof BasicPermission)) {
throw new IllegalArgumentException(Messages.getString("security.16", //$NON-NLS-1$
permission));
} else {
// this is the first element provided that another thread did not add
// BEGIN android-changed
// copied from a newer version of harmony
synchronized (this) {
if (permClass != null && inClass != permClass) {
throw new IllegalArgumentException(Messages.getString("security.16", //$NON-NLS-1$
permission));
}
permClass = inClass;
}
// END android-changed
}
String name = permission.getName();
items.put(name, permission);
allEnabled = allEnabled || (name.length() == 1 && '*" == name.charAt(0));
| public java.util.Enumeration | elements()Returns enumeration of contained elements.
return Collections.enumeration(items.values());
| public boolean | implies(java.security.Permission permission)Indicates whether the argument permission is implied by the receiver.
if (permission == null || permission.getClass() != permClass) {
return false;
}
if (allEnabled) {
return true;
}
String checkName = permission.getName();
//first check direct coincidence
if (items.containsKey(checkName)) {
return true;
}
//now check if there are suitable wildcards
//suppose we have "a.b.c", let's check "a.b.*" and "a.*"
char[] name = checkName.toCharArray();
//I presume that "a.b.*" does not imply "a.b."
//so the dot at end is ignored
int pos = name.length - 2;
for (; pos >= 0; pos--) {
if (name[pos] == '.") {
break;
}
}
while (pos >= 0) {
name[pos + 1] = '*";
if (items.containsKey(new String(name, 0, pos + 2))) {
return true;
}
for (--pos; pos >= 0; pos--) {
if (name[pos] == '.") {
break;
}
}
}
return false;
| private void | readObject(java.io.ObjectInputStream in)Reads the object from stream and checks its consistency: all contained
permissions must be of the same subclass of BasicPermission.
ObjectInputStream.GetField fields = in.readFields();
items = new HashMap<String, Permission>();
// BEGIN android-changed
// copied from a newer version of harmony
synchronized (this) {
permClass = (Class<? extends Permission>)fields.get("permClass", null); //$NON-NLS-1$
items.putAll((Hashtable<String, Permission>) fields.get(
"permissions", new Hashtable<String, Permission>())); //$NON-NLS-1$
for (Iterator<Permission> iter = items.values().iterator(); iter.hasNext();) {
if (iter.next().getClass() != permClass) {
throw new InvalidObjectException(Messages.getString("security.24")); //$NON-NLS-1$
}
}
allEnabled = fields.get("all_allowed", false); //$NON-NLS-1$
if (allEnabled && !items.containsKey("*")) { //$NON-NLS-1$
throw new InvalidObjectException(Messages.getString("security.25")); //$NON-NLS-1$
}
}
// END android-changed
| private void | writeObject(java.io.ObjectOutputStream out)Expected format is the following:
- boolean all_allowed
- This is set to true if this BasicPermissionCollection contains a
{@code BasicPermission} with '*' as its permission name.
- Class<T> permClass
- The class to which all {@code BasicPermission}s in this
BasicPermissionCollection belongs.
- Hashtable<K,V> permissions
- The
BasicPermission
s in this {@code BasicPermissionCollection}. All {@code BasicPermission}s
in the collection must belong to the same class. The Hashtable is indexed
by the {@code BasicPermission} name; the value of the Hashtable entry is
the permission.
ObjectOutputStream.PutField fields = out.putFields();
fields.put("all_allowed", allEnabled); //$NON-NLS-1$
fields.put("permissions", new Hashtable<String, Permission>(items)); //$NON-NLS-1$
fields.put("permClass", permClass); //$NON-NLS-1$
out.writeFields();
|
|