FileDocCategorySizeDatePackage
BasicPermissionCollection.javaAPI DocAndroid 1.5 API8184Wed May 06 22:41:04 BST 2009java.security

BasicPermissionCollection

public final class BasicPermissionCollection extends PermissionCollection
Specific {@code PermissionCollection} for storing {@code BasicPermissions} of arbitrary type.
see
BasicPermission
see
PermissionCollection
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private static final ObjectStreamField[]
serialPersistentFields
private transient Map
items
private transient boolean
allEnabled
private Class
permClass
Constructors Summary
Methods Summary
public voidadd(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.

see
java.security.PermissionCollection#add(java.security.Permission)


                                        
        
        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.Enumerationelements()
Returns enumeration of contained elements.

        return Collections.enumeration(items.values());
    
public booleanimplies(java.security.Permission permission)
Indicates whether the argument permission is implied by the receiver.

return
boolean {@code true} if the argument permission is implied by the receiver, and {@code false} if it is not.
param
permission the permission to check.
see
Permission

        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 voidreadObject(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 voidwriteObject(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();