FileDocCategorySizeDatePackage
SecurityHandler.javaAPI DocphoneME MR2 API (J2ME)11375Wed May 02 18:00:26 BST 2007com.sun.midp.security

SecurityHandler

public final class SecurityHandler extends Object
Contains methods to handle with the various security state information of a a MIDlet suite.

Fields Summary
private static SecurityToken
classSecurityToken
The security token for this class.
Constructors Summary
public SecurityHandler(byte[] ApiPermissions, String domain)
Creates a security domain with a list of permitted actions or no list to indicate all actions. The caller must be have permission for Permissions.MIDP or be the first caller of the method for this instance of the VM.

param
ApiPermissions for the token
param
domain name of the security domain
exception
SecurityException if caller is not permitted to call this method

    	// No-op since we don't cache any permissions in Java
    	// Query native security manager every time
    
public SecurityHandler(SecurityToken securityToken, byte[] ApiPermissions, String domain)
Creates a security domain with a list of permitted actions or no list to indicate all actions. The caller must be have permission for Permissions.MIDP or be the first caller of the method for this instance of the VM.

param
securityToken security token of the caller
param
ApiPermissions for the token, can be null
param
domain name of the security domain
exception
SecurityException if caller is not permitted to call this method

    	// Do not cache anything
    	// Ask native security manager everytime
    
Methods Summary
public static booleanaskUserForPermission(SecurityToken token, int title, int question, java.lang.String app, java.lang.String resource, java.lang.String extraValue)
Ask the user yes/no permission question.

param
token security token with the permission to preempt the foreground display
param
title Resource constant for the title of the dialog
param
question Resource constant for the question to ask user
param
oneShotQuestion Resource constant for the oneshot question to ask the user
param
app name of the application to insert into a string can be null if no %1 a string
param
resource string to insert into a string, can be null if no %2 in a string
param
extraValue string to insert into a string, can be null if no %3 in a string
return
true if the user says yes else false
exception
InterruptedException if another thread interrupts the calling thread while this method is waiting to preempt the display.

    	// Allow Push interrupt since the decision is already made
	// at native Push level
        return true;
    
public booleancheckForPermission(int permission, int title, int question, int oneshotQuestion, java.lang.String app, java.lang.String resource, java.lang.String extraValue)
Check for permission and throw an exception if not allowed. May block to ask the user a question.

The title, and question strings will be translated, if a string resource is available. Since the strings can have substitution token in them, if there is a "%" it must changed to "%%". If a string has a %1, the app parameter will be substituted for it. If a string has a "%2, the resource parameter will be substituted for it. If a string has a %3, the extraValue parameter will be substituted for it.

param
permission ID of the permission to check for, the ID must be from {@link com.sun.midp.security.Permissions}
param
title Resource constant for the title of the dialog
param
question Resource constant for the question to ask user
param
app name of the application to insert into a string can be null if no %1 a string
param
resource string to insert into a string, can be null if no %2 in a string
param
extraValue string to insert into a string, can be null if no %3 in a string
return
true if the permission was allow and was not allowed before
exception
SecurityException if the permission is not allowed by this token
exception
InterruptedException if another thread interrupts the calling thread while this method is waiting to preempt the display.


        return checkForPermission(permission, title, question,
            oneshotQuestion, app, resource, extraValue,
            SecurityToken.STD_EX_MSG);
    
public booleancheckForPermission(int permission, int title, int question, int oneShotQuestion, java.lang.String app, java.lang.String resource, java.lang.String extraValue, java.lang.String exceptionMsg)
Check for permission and throw an exception if not allowed. May block to ask the user a question.

The title, question, and answer strings will be translated, if a string resource is available. Since the strings can have substitution token in them, if there is a "%" it must changed to "%%". If a string has a %1, the app parameter will be substituted for it. If a string has a "%2, the resource parameter will be substituted for it. If a string has a %3, the extraValue parameter will be substituted for it.

param
permission ID of the permission to check for, the ID must be from {@link com.sun.midp.security.Permissions}
param
title Resource constant for the title of the dialog
param
question Resource constant for the question to ask user
param
oneShotQuestion Resource constant for the oneshot question to ask the user
param
app name of the application to insert into a string can be null if no %1 a string
param
resource string to insert into a string, can be null if no %2 in a string
param
extraValue string to insert into a string, can be null if no %3 in a string
param
exceptionMsg message if a security exception is thrown
return
true if the permission was allowed and was not allowed before; false, if permission is granted..
exception
SecurityException if the permission is not allowed by this token
exception
InterruptedException if another thread interrupts the calling thread while this method is waiting to preempt the display.

    	
    	MIDletSuite current =
            MIDletStateHandler.getMidletStateHandler().getMIDletSuite();

        if (current == null) {
	    // Deny. Internal suite should not call this function
	    return true;
        } else {
	    return !checkPermission0(current.getID(),
				     Permissions.getName(permission));
        }
    
public intcheckPermission(java.lang.String permission)
Get the status of the specified permission. If no API on the device defines the specific permission requested then it must be reported as denied. If the status of the permission is not known because it might require a user interaction then it should be reported as unknown.

param
permission to check if denied, allowed, or unknown.
return
0 if the permission is denied; 1 if the permission is allowed; -1 if the status is unknown

        boolean found = false;
        int i;

        for (i = 0; i < Permissions.NUMBER_OF_PERMISSIONS; i++) {
        	if (Permissions.getName(i).equals(permission)) {
        		found = true;
        		break;
        	}
        }
        
        if (found) {
        	MIDletSuite current =
		MIDletStateHandler.getMidletStateHandler().getMIDletSuite();

        	if (current != null) {
        		// query native security mgr for status
		    return checkPermissionStatus0(current.getID(), permission);
        	}
        }
        
        return 0; // Deny permission
    
private native booleancheckPermission0(java.lang.String suiteId, java.lang.String permission)
Query native security manager for permission. This call may block if user needs to be asked.

param
suiteId the MIDlet suite the permission should be checked against
param
permission the permission id
return
true if permission is granted. Otherwise, false.

private native intcheckPermissionStatus0(java.lang.String suiteId, java.lang.String permission)
Get the status of the specified permission. This is to implement public API MIDlet.checkPermission() and will not block calling thread. If no API on the device defines the specific permission requested then it must be reported as denied. If the status of the permission is not known because it might require a user interaction then it should be reported as unknown.

param
suiteId the MIDlet suite the permission should be checked against
param
permission to check if denied, allowed, or unknown.
return
0 if the permission is denied; 1 if the permission is allowed; -1 if the status is unknown

static voidinitSecurityToken(SecurityToken token)
Initializes the security token for this class, so it can perform actions that a normal MIDlet Suite cannot.

param
token security token for this class.

        if (classSecurityToken != null) {
            return;
        }

        classSecurityToken = token;