FileDocCategorySizeDatePackage
PrivateCredentialPermission.javaAPI DocAndroid 1.5 API12485Wed May 06 22:41:02 BST 2009javax.security.auth

PrivateCredentialPermission

public final class PrivateCredentialPermission extends Permission
Protects private credential objects belonging to a {@code Subject}. It has only one action which is "read". The target name of this permission has a special syntax:
targetName = CredentialClass {PrincipalClass "PrincipalName"}*
First it states a credential class and is followed then by a list of one or more principals identifying the subject.

The principals on their part are specified as the name of the {@code Principal} class followed by the principal name in quotes. For example, the following file may define permission to read the private credentials of a principal named "Bob": "com.sun.PrivateCredential com.sun.Principal \"Bob\""

The syntax also allows the use of the wildcard "*" in place of {@code CredentialClass} or {@code PrincipalClass} and/or {@code PrincipalName}.
see
Principal
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private static final String
READ
private String
credentialClass
private transient int
offset
private transient CredOwner[]
set
Constructors Summary
public PrivateCredentialPermission(String name, String action)
Creates a new permission for private credentials specified by the target name {@code name} and an {@code action}. The action is always {@code "read"}.

param
name the target name of the permission.
param
action the action {@code "read"}.

    
                                                                      
         
        super(name);
        if (READ.equalsIgnoreCase(action)) {
            initTargetName(name);
        } else {
            throw new IllegalArgumentException(Messages.getString("auth.11")); //$NON-NLS-1$
        }
    
PrivateCredentialPermission(String credentialClass, Set principals)
Creates a {@code PrivateCredentialPermission} from the {@code Credential} class and set of principals.

param
credentialClass the credential class name.
param
principals the set of principals.

        super(credentialClass);
        this.credentialClass = credentialClass;

        set = new CredOwner[principals.size()];
        for (Principal p : principals) {
            CredOwner element = new CredOwner(p.getClass().getName(), p.getName());
            // check for duplicate elements
            boolean found = false;
            for (int ii = 0; ii < offset; ii++) {
                if (set[ii].equals(element)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                set[offset++] = element;
            }
        }
    
Methods Summary
public booleanequals(java.lang.Object obj)

        if (obj == this) {
            return true;
        }

        if (obj == null || this.getClass() != obj.getClass()) {
            return false;
        }

        PrivateCredentialPermission that = (PrivateCredentialPermission) obj;

        return credentialClass.equals(that.credentialClass) && (offset == that.offset)
                && sameMembers(set, that.set, offset);
    
public java.lang.StringgetActions()

        return READ;
    
public java.lang.StringgetCredentialClass()
Returns the class name of the credential associated with this permission.

return
the class name of the credential associated with this permission.

        return credentialClass;
    
public java.lang.String[][]getPrincipals()
Returns the principal's classes and names associated with this {@code PrivateCredentialPermission} as a two dimensional array. The first dimension of the array corresponds to the number of principals. The second dimension defines either the name of the {@code PrincipalClass} [x][0] or the value of {@code PrincipalName} [x][1]. This corresponds to the the target name's syntax:
targetName = CredentialClass {PrincipalClass "PrincipalName"}*

return
the principal classes and names associated with this {@code PrivateCredentialPermission}.


        String[][] s = new String[offset][2];

        for (int i = 0; i < s.length; i++) {
            s[i][0] = set[i].principalClass;
            s[i][1] = set[i].principalName;
        }
        return s;
    
public inthashCode()

        int hash = 0;
        for (int i = 0; i < offset; i++) {
            hash = hash + set[i].hashCode();
        }
        return getCredentialClass().hashCode() + hash;
    
public booleanimplies(java.security.Permission permission)


        if (permission == null || this.getClass() != permission.getClass()) {
            return false;
        }

        PrivateCredentialPermission that = (PrivateCredentialPermission) permission;

        if (!("*".equals(credentialClass) || credentialClass //$NON-NLS-1$
                .equals(that.getCredentialClass()))) {
            return false;
        }

        if (that.offset == 0) {
            return true;
        }

        CredOwner[] thisCo = set;
        CredOwner[] thatCo = that.set;
        int thisPrincipalsSize = offset;
        int thatPrincipalsSize = that.offset;
        for (int i = 0, j; i < thisPrincipalsSize; i++) {
            for (j = 0; j < thatPrincipalsSize; j++) {
                if (thisCo[i].implies(thatCo[j])) {
                    break;
                }
            }
            if (j == thatCo.length) {
                return false;
            }
        }
        return true;
    
private voidinitTargetName(java.lang.String name)
Initialize a PrivateCredentialPermission object and checks that a target name has a correct format: CredentialClass 1*(PrincipalClass "PrincipalName")


        if (name == null) {
            throw new NullPointerException(Messages.getString("auth.0E")); //$NON-NLS-1$
        }

        // check empty string
        name = name.trim();
        if (name.length() == 0) {
            throw new IllegalArgumentException(Messages.getString("auth.0F")); //$NON-NLS-1$
        }

        // get CredentialClass
        int beg = name.indexOf(' ");
        if (beg == -1) {
            throw new IllegalArgumentException(Messages.getString("auth.10")); //$NON-NLS-1$
        }
        credentialClass = name.substring(0, beg);

        // get a number of pairs: PrincipalClass "PrincipalName"
        beg++;
        int count = 0;
        int nameLength = name.length();
        for (int i, j = 0; beg < nameLength; beg = j + 2, count++) {
            i = name.indexOf(' ", beg);
            j = name.indexOf('"", i + 2);

            if (i == -1 || j == -1 || name.charAt(i + 1) != '"") {
                throw new IllegalArgumentException(Messages.getString("auth.10")); //$NON-NLS-1$
            }
        }

        // name MUST have one pair at least
        if (count < 1) {
            throw new IllegalArgumentException(Messages.getString("auth.10")); //$NON-NLS-1$
        }

        beg = name.indexOf(' ");
        beg++;

        // populate principal set with instances of CredOwner class
        String principalClass;
        String principalName;

        set = new CredOwner[count];
        for (int index = 0, i, j; index < count; beg = j + 2, index++) {
            i = name.indexOf(' ", beg);
            j = name.indexOf('"", i + 2);

            principalClass = name.substring(beg, i);
            principalName = name.substring(i + 2, j);

            CredOwner element = new CredOwner(principalClass, principalName);
            // check for duplicate elements
            boolean found = false;
            for (int ii = 0; ii < offset; ii++) {
                if (set[ii].equals(element)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                set[offset++] = element;
            }
        }
    
public java.security.PermissionCollectionnewPermissionCollection()

        return null;
    
private voidreadObject(java.io.ObjectInputStream ois)

        ois.defaultReadObject();
        initTargetName(getName());
    
private booleansameMembers(java.lang.Object[] ar1, java.lang.Object[] ar2, int length)
Returns true if the two arrays have the same length, and every member of one array is contained in another array

        if (ar1 == null && ar2 == null) {
            return true;
        }
        if (ar1 == null || ar2 == null) {
            return false;
        }
        boolean found;
        for (int i = 0; i < length; i++) {
            found = false;
            for (int j = 0; j < length; j++) {
                if (ar1[i].equals(ar2[j])) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                return false;
            }
        }
        return true;