FileDocCategorySizeDatePackage
PermissionCacheFactory.javaAPI DocGlassfish v2 API8591Fri May 04 22:32:04 BST 2007com.sun.enterprise.security

PermissionCacheFactory

public class PermissionCacheFactory extends Object
This class is the factory for creating and managing PermissionCache.
author
Shing Wai Chan

Fields Summary
private static HashMap
cacheMap
private static int
factoryKey
private static boolean
supportsReuse
private static Permission[]
protoPerms
private static PermissionCache
securityManagerCache
Constructors Summary
Methods Summary
public static synchronized PermissionCachecreatePermissionCache(java.lang.String pcID, java.security.CodeSource codesource, java.security.Permission[] perms, java.lang.String name)
Create a PermissionCache object. If the corresponding object exists, then it will overwrite the previous one.

param
pcID - a string identifying the policy context and which must be set when getPermissions is called (internally). This value may be null, in which case the permisions of the default policy context will be cached.
param
codesource - the codesource argument to be used in the call to getPermissions. This value may be null.
param
perms - an array of Permission objects identifying the permission types that will be managed by the cache. This value may be null, in which case all permissions obtained by the getPermissions call will be cached.
param
name - a string corresponding to a value returned by Permission.getName() only permissions whose getName() value matches the name parameter will be included in the cache. This value may be null, in which case permission name dos not factor into the permission caching.


        if (!supportsReuse) {
            return null;
        }

        Integer key = getNextKey();

	PermissionCache cache = 
	    new PermissionCache(key, pcID, codesource, perms, name);

        return registerPermissionCache(cache);
    
public static synchronized PermissionCachecreatePermissionCache(java.lang.String pcID, java.security.CodeSource codesource, java.lang.Class clazz, java.lang.String name)
Create a PermissionCache object. If the corresponding object exists, then it will overwrite the previous one.

param
pcID - a string identifying the policy context and which must be set when getPermissions is called (internally). This value may be null, in which case the permisions of the default policy context will be cached.
param
codesource - the codesource argument to be used in the call to getPermissions. This value may be null.
param
clazz - a class object identifying the permission type that will be managed by the cache. This value may be null, in which case all permissions obtained by the getPermissions call will be cached.
param
name - a string corresponding to a value returned by Permission.getName() only permissions whose getName() value matches the name parameter will be included in the cache. This value may be null, in which case permission name dos not factor into the permission caching.

  
	if (!supportsReuse) {
	    return null;
	}
  
	Integer key = getNextKey();
 
 	PermissionCache cache = 
 	    new PermissionCache(key, pcID, codesource, clazz, name);
	
 	return registerPermissionCache(cache);
    
private static synchronized PermissionCachecreateSecurityManagerCache()


        Integer key = getNextKey();

	PermissionCache cache = 

	    new PermissionCache(key, null, null, protoPerms, null);

        return registerPermissionCache(cache);
    
private static java.lang.IntegergetNextKey()
Reserve the next Cache Key for subsequent registration.

return
the key as an Integer object.


     
        try {
	    // make a call to policy.refresh() to see if the provider
	    // calls the supportsReuse callback (see resetCaches below).
	    // which will set supportsReuse to true (to enable caching).
	    Policy policy = Policy.getPolicy();
	    if (policy != null) {
		policy.refresh();
	    }
        } catch(Exception pe) {
        }
    
   
   	Integer key = Integer.valueOf(factoryKey++);
   
   	while (cacheMap.get(key) != null) {
   	    key = Integer.valueOf(factoryKey++);
   	}
   	
  	return key;
    
private static PermissionCacheregisterPermissionCache(PermissionCache cache)
Register a PermissionCache object with the factory. If an object is already registered at the key, it will be overidden.

param
cache a cache with an internal key value.
return
the cache object

	cacheMap.put(cache.getFactoryKey(),cache);
    
	return cache;
    
public static synchronized PermissionCacheremovePermissionCache(PermissionCache cache)

  
	PermissionCache rvalue = null;
  
	if (cache != null) {
	    
	    Object value = cacheMap.remove(cache.getFactoryKey());
  
	    if (value != null && value instanceof PermissionCache) {
		rvalue = (PermissionCache) value;
		rvalue.reset();
	    }
	}
	return rvalue;
    
public static synchronized voidresetCaches()
This resets all caches inside the factory.


	supportsReuse = true;

	SecurityManager sm = System.getSecurityManager();
	if (sm != null && sm instanceof J2EESecurityManager) {
	    if (!((J2EESecurityManager)sm).cacheEnabled()) {
		((J2EESecurityManager)sm).enablePermissionCache
		    (securityManagerCache);
	    }
	}

	Iterator iter = cacheMap.values().iterator();
	while (iter.hasNext()) {
  	    Object cache = iter.next();
  	    if (cache instanceof PermissionCache) {
  		((PermissionCache) cache).reset();
  	    }
	}