FileDocCategorySizeDatePackage
Permissions.javaAPI DocAndroid 1.5 API8967Wed May 06 22:41:06 BST 2009java.security

Permissions

public final class Permissions extends PermissionCollection implements Serializable
{@code Permissions} represents a {@code PermissionCollection} where the contained permissions can be of different types. The permissions are organized in their appropriate {@code PermissionCollection} obtained by {@link Permission#newPermissionCollection()}. For permissions which do not provide a dedicated {@code PermissionCollection}, a default permission collection, based on a hash table, will be used.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private static final ObjectStreamField[]
serialPersistentFields
private transient Map
klasses
private boolean
allEnabled
Constructors Summary
Methods Summary
public voidadd(java.security.Permission permission)
Adds the given {@code Permission} to this heterogeneous {@code PermissionCollection}. The {@code permission} is stored in its appropriate {@code PermissionCollection}.

param
permission the {@code Permission} to be added.
throws
SecurityException if this collection's {@link #isReadOnly()} method returns {@code true}.
throws
NullPointerException if {@code permission} is {@code null}.
since
Android 1.0

  // = false;

                                                                                                           
        
        if (isReadOnly()) {
            throw new SecurityException(Messages.getString("security.15")); //$NON-NLS-1$
        }

        if (permission == null) {
            throw new NullPointerException(Messages.getString("security.20")); //$NON-NLS-1$
        }

        Class klass = permission.getClass();
        PermissionCollection klassMates = (PermissionCollection)klasses
            .get(klass);

        if (klassMates == null) {
            synchronized (klasses) {
                klassMates = (PermissionCollection)klasses.get(klass);
                if (klassMates == null) {

                    klassMates = permission.newPermissionCollection();
                    if (klassMates == null) {
                        klassMates = new PermissionsHash();
                    }
                    klasses.put(klass, klassMates);
                }
            }
        }
        klassMates.add(permission);

        if (klass == AllPermission.class) {
            allEnabled = true;
        }
    
public java.util.Enumerationelements()

        return new MetaEnumeration(klasses.values().iterator());
    
public booleanimplies(java.security.Permission permission)

        if (permission == null) {
            // RI compatible
            throw new NullPointerException(Messages.getString("security.21")); //$NON-NLS-1$
        }
        if (allEnabled) {
            return true;
        }
        Class klass = permission.getClass();
        PermissionCollection klassMates = null;

        UnresolvedPermissionCollection billets = (UnresolvedPermissionCollection)klasses
            .get(UnresolvedPermission.class);
        if (billets != null && billets.hasUnresolved(permission)) {
            // try to fill up klassMates with freshly resolved permissions
            synchronized (klasses) {
                klassMates = (PermissionCollection)klasses.get(klass);
                try {
                    klassMates = billets.resolveCollection(permission,
                                                           klassMates);
                } catch (Exception ignore) {
                    //TODO log warning
                    ignore.printStackTrace();
                }

                if (klassMates != null) {
                    //maybe klassMates were just created
                    // so put them into common map
                    klasses.put(klass, klassMates);
                    // very uncommon case, but not improbable one
                    if (klass == AllPermission.class) {
                        allEnabled = true;
                    }
                }
            }
        } else {
            klassMates = (PermissionCollection)klasses.get(klass);
        }

        if (klassMates != null) {
            return klassMates.implies(permission);
        }
        return false;
    
private voidreadObject(java.io.ObjectInputStream in)
Reads the object from stream and checks for consistency.

        ObjectInputStream.GetField fields = in.readFields();
        Map perms = (Map)fields.get("perms", null); //$NON-NLS-1$
        klasses = new HashMap();
        synchronized (klasses) {
            for (Iterator iter = perms.keySet().iterator(); iter.hasNext();) {
                Class key = (Class)iter.next();
                PermissionCollection pc = (PermissionCollection)perms.get(key);
                if (key != pc.elements().nextElement().getClass()) {
                    throw new InvalidObjectException(Messages.getString("security.22")); //$NON-NLS-1$
                }
                klasses.put(key, pc);
            }
        }
        allEnabled = fields.get("allPermission", null) != null; //$NON-NLS-1$
        if (allEnabled && !klasses.containsKey(AllPermission.class)) {
            throw new InvalidObjectException(Messages.getString("security.23")); //$NON-NLS-1$
        }
    
private voidwriteObject(java.io.ObjectOutputStream out)
Outputs fields via default mechanism.

        ObjectOutputStream.PutField fields = out.putFields();
        fields.put("perms", new Hashtable(klasses)); //$NON-NLS-1$
        fields.put("allPermission", allEnabled ? klasses //$NON-NLS-1$
            .get(AllPermission.class) : null);
        out.writeFields();