FileDocCategorySizeDatePackage
PolicyConfigurationImpl.javaAPI DocGlassfish v2 API50349Tue Aug 14 14:23:16 BST 2007com.sun.enterprise.security.provider

PolicyConfigurationImpl

public class PolicyConfigurationImpl extends Object implements javax.security.jacc.PolicyConfiguration
Implementation of Jacc PolicyConfiguration Interface
author
Harpreet Singh (harpreet.singh@sun.com)
author
Ron Monzillo

Fields Summary
private static Logger
logger
private static com.sun.enterprise.util.LocalStringManagerImpl
localStrings
private String
CONTEXT_ID
private Permissions
excludedPermissions
private Permissions
uncheckedPermissions
private HashMap
rolePermissionsTable
private static HashMap
linkTable
private static com.sun.enterprise.deployment.interfaces.SecurityRoleMapperFactory
factory
private static final String
REPOSITORY_HOME_PROP
private static String
policySuffix
private static String
PROVIDER_URL
private static final Class[]
permissionParams
public static final int
OPEN_STATE
public static final int
INSERVICE_STATE
public static final int
DELETED_STATE
protected int
state
private boolean
writeOnCommit
private boolean
wasRefreshed
private Policy
policy
private String
policyUrlValue
private long[]
lastModTimes
private static Object
refreshLock
private static String
repository
private static Permission
setPolicyPermission
Constructors Summary
protected PolicyConfigurationImpl(String contextId)

    
     
        //workaround for deadlock in getPolicy and commit
        //load the resource bundle early 
        localStrings.getLocalString("dummy","dummy");
    
	CONTEXT_ID = contextId;
	// initialize(open,remove,!fromFile)
	initialize(true,true,false);
    
protected PolicyConfigurationImpl(File applicationPolicyDirectory, boolean open, boolean remove)

param
applicationPolicyDirectory, need to have absolute path
param
open, then mark state as open
param
remove, then remove any existing policy statements


	CONTEXT_ID = applicationPolicyDirectory.getParentFile().getName() +
                '/" + applicationPolicyDirectory.getName();

	String name = getPolicyFileName(true);
	File f = new File(name);
	if (!f.exists()) {
            String defMsg="Unable to open Policy file: "+name;
            String msg= localStrings.getLocalString("pc.file_not_found",defMsg,new Object []{ name});
	    logger.log(Level.SEVERE,msg);
	    throw new RuntimeException(defMsg);
	}

	// initialize(open,remove,fromFile)
	initialize(open,remove,true);
    
Methods Summary
public voidaddToExcludedPolicy(java.security.PermissionCollection permissions)
Used to add excluded policy statements to this PolicyConfiguration.

param
permissions the collection of permissions to be added to the excluded policy statements. The collection may be either a homogenous or heterogenous collection.
throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" or "inService" when this method is called.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the addToExcludedPolicy method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.


        assertStateIsOpen();

	assert permissions != null;

	if (permissions != null) {
	    checkSetPolicyPermission();
	    for(Enumeration e = permissions.elements(); e.hasMoreElements();){
		this.getExcludedPermissions().add((Permission) e.nextElement());
		writeOnCommit = true;
	    }
	}
    
public voidaddToExcludedPolicy(java.security.Permission permission)
Used to add a single excluded policy statement to this PolicyConfiguration.

param
permission the permission to be added to the excluded policy statements.
throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" or "inService" when this method is called.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the addToExcludedPolicy method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.


        assertStateIsOpen();

	assert permission != null;

	if (permission != null) {
	    checkSetPolicyPermission();
	    this.getExcludedPermissions().add(permission);
	    writeOnCommit = true;
	}
    
public voidaddToRole(java.lang.String roleName, java.security.PermissionCollection permissions)
Used to add permissions to a named role in this PolicyConfiguration. If the named Role does not exist in the PolicyConfiguration, it is created as a result of the call to this function.

It is the job of the Policy provider to ensure that all the permissions added to a role are granted to principals "mapped to the role".

param
roleName the name of the Role to which the permissions are to be added.

param
permissions the collection of permissions to be added to the role. The collection may be either a homogenous or heterogenous collection.
throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" or "inService" when this method is called.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the addToRole method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.

        assertStateIsOpen();

	assert roleName != null;
	assert permissions != null;
	
	if (roleName != null && permissions != null) {
	    checkSetPolicyPermission();
	    for(Enumeration e = permissions.elements(); e.hasMoreElements();) {
		this.getRolePermissions(roleName).add((Permission)e.nextElement());
		writeOnCommit = true;
	    }
	}
    
public voidaddToRole(java.lang.String roleName, java.security.Permission permission)
Used to add a single permission to a named role in this PolicyConfiguration. If the named Role does not exist in the PolicyConfiguration, it is created as a result of the call to this function.

It is the job of the Policy provider to ensure that all the permissions added to a role are granted to principals "mapped to the role".

param
roleName the name of the Role to which the permission is to be added.

param
permission the permission to be added to the role.
throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" or "inService" when this method is called.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the addToRole method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.


        assertStateIsOpen();

	assert permission != null;
	assert roleName != null;
	
	if (roleName != null && permission != null) {
	    checkSetPolicyPermission();
	    this.getRolePermissions(roleName).add(permission);
	    writeOnCommit = true;
	}
    
public voidaddToUncheckedPolicy(java.security.PermissionCollection permissions)
Used to add unchecked policy statements to this PolicyConfiguration.

param
permissions the collection of permissions to be added as unchecked policy statements. The collection may be either a homogenous or heterogenous collection.
throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" or "inService" when this method is called.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the addToUncheckedPolicy method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.

	

        assertStateIsOpen();

	assert permissions != null;
	
	if (permissions != null) {
	    checkSetPolicyPermission();
	    for(Enumeration e = permissions.elements(); e.hasMoreElements();){
		this.getUncheckedPermissions().add((Permission) e.nextElement());
		writeOnCommit = true;
	    }
	}
    
public voidaddToUncheckedPolicy(java.security.Permission permission)
Used to add a single unchecked policy statement to this PolicyConfiguration.

param
permission the permission to be added to the unchecked policy statements.
throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" or "inService" when this method is called.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the addToUncheckedPolicy method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.

	
        assertStateIsOpen();

	assert permission != null;

	if (permission != null) {
	    checkSetPolicyPermission();
	    this.getUncheckedPermissions().add(permission);
	    writeOnCommit = true;
	}
    
private voidassertStateIsOpen()

      
        if (state != OPEN_STATE){
            String defMsg="Operation invoked on closed or deleted PolicyConfiguration.";
            String msg = localStrings.getLocalString("pc.op_requires_state_open",defMsg); 
	    logger.log(Level.WARNING, msg);
	    throw new UnsupportedOperationException(defMsg);
	}
    
private voidcaptureFileTime(boolean granted)

	String name = getPolicyFileName(granted);
	File f = new File(name);
	lastModTimes[(int) (granted ? 1 : 0)] = f.lastModified();
    
protected static voidcheckSetPolicyPermission()

	SecurityManager sm = System.getSecurityManager();
	if (sm != null) {
	    if (setPolicyPermission == null) {
		setPolicyPermission = new java.security.SecurityPermission("setPolicy");
	    }
	    sm.checkPermission(setPolicyPermission);
	}
    
public voidcommit()
This method is used to set to "inService" the state of the policy context whose interface is this PolicyConfiguration Object. Only those policy contexts whose state is "inService" will be included in the policy contexts processed by the Policy.refresh method. A policy context whose state is "inService" may be returned to the "open" state by calling the getPolicyConfiguration method of the PolicyConfiguration factory with the policy context identifier of the policy context.

When the state of a policy context is "inService", calling any method other than commit, delete, getContextID, or inService on its PolicyConfiguration Object will cause an UnsupportedOperationException to be thrown.

throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" when this method is called.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the commit method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.


	synchronized(refreshLock) {
	    if(state == DELETED_STATE){
                String defMsg="Cannot perform Operation on a deleted PolicyConfiguration";
                String msg=localStrings.getLocalString("pc.invalid_op_for_state_delete",defMsg);
		logger.log(Level.WARNING,msg);
		throw new UnsupportedOperationException(defMsg);

	    } else {
            
		try {

		    checkSetPolicyPermission();

		    if (state == OPEN_STATE) {

			generatePermissions();

			state = INSERVICE_STATE;
		    }
		} catch(Exception e){
                    String defMsg="commit fail for contextod "+CONTEXT_ID;
                    String msg=localStrings.getLocalString("pc.commit_failure",defMsg,new Object[]{CONTEXT_ID,e});
		    logger.log(Level.SEVERE,msg);
		    throw new PolicyContextException(e);
		}
		if (logger.isLoggable(Level.FINE)){
		    logger.fine("JACC Policy Provider: PC.commit "+CONTEXT_ID);
		}
	    }
	    
	}
    
private voidcreatePolicyContextDirectory()


	String contextDirectoryName = getContextDirectoryName();
	File d = new File(contextDirectoryName);
	if (d.exists()) {
	    if(!d.isDirectory()) {
                String defMsg="unable to create policy context directory";
                String msg=localStrings.getLocalString("pc.unable_to_create_context_directory",
                      defMsg,new Object []{contextDirectoryName});
		logger.log(Level.SEVERE,msg);
		throw new RuntimeException(defMsg);
	    } 
	} else {
	    d.mkdirs();
	}
    
private booleancreatePolicyFile(boolean granted, sun.security.provider.PolicyParser parser, boolean woc)


	boolean result = woc;
	createPolicyContextDirectory();
	removePolicyFile(granted);
	String name = getPolicyFileName(granted);
	FileWriter writer = null;
	try {
	    if(logger.isLoggable (Level.FINE)){
		logger.fine("JACC Policy Provider: Writing grant statements to policy file: "+name);
	    }
	    writer = new FileWriter(name);
	    parser.write(writer);
	    result = false;
	} catch(java.io.FileNotFoundException fnfe) {
            String msg=localStrings.getLocalString("pc.file_error","file not found "+name,
                  new Object []{name, fnfe});
	    logger.log(Level.SEVERE,msg);
	    throw fnfe;
	} catch(java.io.IOException ioe){
            String msg=localStrings.getLocalString("pc.file_write_error","file IO error on file "+name,
                  new Object []{name,ioe});
	    logger.log(Level.SEVERE,msg);
	    throw ioe;
	} finally {
	    if (writer != null) {
		try {
		    writer.close();
		    captureFileTime(granted);
		} catch (Exception e) {
                    String defMsg="Unable to close Policy file: "+name;
                    String msg=localStrings.getLocalString("pc.file_close_error",defMsg,new Object []{name,e}); 
		    logger.log(Level.SEVERE,msg);
		    throw new RuntimeException(defMsg);
		}
	    }
	}
	return result;
    
public voiddelete()
Causes all policy statements to be deleted from this PolicyConfiguration and sets its internal state such that calling any method, other than delete, getContextID, or inService on the PolicyConfiguration will be rejected and cause an UnsupportedOperationException to be thrown.

This operation has no affect on any linked PolicyConfigurations other than removing any links involving the deleted PolicyConfiguration.

throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the delete method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.

	checkSetPolicyPermission();
	synchronized(refreshLock) {
	    try {
		removePolicy();
	    } finally {
		state = DELETED_STATE;
	    }
	}
    
private java.lang.StringescapeName(java.lang.String name)

        return (name != null && name.indexOf('"") > 0) ?
                name.replaceAll("\"", "\\\\\"") : name;
    
private booleanfileChanged(boolean granted)

	String name = getPolicyFileName(granted);
	File f = new File(name);
	return !(lastModTimes[(int) (granted ? 1 : 0)] == f.lastModified());
    
private booleanfilesChanged()

	return (fileChanged(true) || fileChanged(false));
    
private voidgeneratePermissions()


	// optimization - return if the rules have not changed

	if (!writeOnCommit) return;

	// otherwise proceed to write policy file

	Map roleToSubjectMap = null;
	if (rolePermissionsTable != null) {
	    // Make sure a role to subject map has been defined for the Policy Context
	    if (factory != null) {
                // the rolemapper is stored against the
                // appname, for a web app get the appname for this contextid
                SecurityRoleMapper srm = factory.getRoleMapper(CONTEXT_ID);
		if (srm != null) {
		    roleToSubjectMap = srm.getRoleToSubjectMapping();
		}
		if (roleToSubjectMap != null) {
		    // make sure all liked PC's have the same roleToSubjectMap
		    Set linkSet = (Set) linkTable.get(CONTEXT_ID);
		    if (linkSet != null) {
			Iterator it = linkSet.iterator();
			while (it.hasNext()) {
			    String contextId = (String)it.next();
			    if (!CONTEXT_ID.equals(contextId)) {
				SecurityRoleMapper otherSrm = factory.getRoleMapper(contextId);
				Map otherRoleToSubjectMap = null;

				if (otherSrm != null) {
				    otherRoleToSubjectMap = otherSrm.getRoleToSubjectMapping();
				}
				
				if (otherRoleToSubjectMap != roleToSubjectMap) {
                                    String defMsg="Linked policy contexts have different roleToSubjectMaps ("+CONTEXT_ID+")<->("+contextId+")";
                                    String msg=localStrings.getLocalString("pc.linked_with_different_role_maps",defMsg,new Object []{CONTEXT_ID,contextId});
				    logger.log(Level.SEVERE,msg); 
				    throw new RuntimeException(defMsg);
				}
			    }
			}
		    }
		}
	    }
	}

	if (roleToSubjectMap == null && rolePermissionsTable != null) {
            String defMsg="This application has no role mapper factory defined";
            String msg=localStrings.getLocalString("pc.role_map_not_defined_at_commit",defMsg,new Object []{CONTEXT_ID});
	    logger.log(Level.SEVERE,msg);
	    throw new RuntimeException
		(localStrings.getLocalString
		 ("enterprise.deployment.deployment.norolemapperfactorydefine",defMsg));
	}

	PolicyParser parser = new PolicyParser(false);

	// load unchecked grants in parser
	if (uncheckedPermissions != null) {
	    Enumeration pEnum = uncheckedPermissions.elements();
	    if (pEnum.hasMoreElements()) {
		GrantEntry grant = new GrantEntry();
		while (pEnum.hasMoreElements()) {
		    Permission p = (Permission) pEnum.nextElement();
		    PermissionEntry entry = 
			new PermissionEntry(p.getClass().getName(),
					    p.getName(),p.getActions());
		    grant.add(entry);
		}
		parser.add(grant);
	    }
	}

	// load role based grants in parser
	if (rolePermissionsTable != null) {
	    Iterator roleIt = rolePermissionsTable.keySet().iterator();
	    while (roleIt.hasNext()) {
		boolean withPrincipals = false;
		String roleName = (String) roleIt.next();
		Permissions rolePerms = getRolePermissions(roleName);
		Subject rolePrincipals = (Subject) roleToSubjectMap.get(roleName);
		if (rolePrincipals != null) {
		    Iterator pit = rolePrincipals.getPrincipals().iterator();
		    while (pit.hasNext()){
			Principal prin = (Principal) pit.next();
			assert prin instanceof java.security.Principal;
			if (prin instanceof java.security.Principal) {
			    withPrincipals = true;
			    PrincipalEntry prinEntry = 
				new PrincipalEntry(prin.getClass().getName(),
						   escapeName(prin.getName()));
			    GrantEntry grant = new GrantEntry();
			    grant.principals.add(prinEntry);
			    Enumeration pEnum = rolePerms.elements();
			    while (pEnum.hasMoreElements()) {
				Permission perm = (Permission) pEnum.nextElement();
				PermissionEntry permEntry = 
				    new PermissionEntry(perm.getClass().getName(),
							perm.getName(),
							perm.getActions());
				grant.add(permEntry);
			    }
			    parser.add(grant);
			}
			else {
                            String msg = localStrings.getLocalString("pc.non_principal_mapped_to_role",
                                         "non principal mapped to role "+roleName,new Object[]{prin,roleName});
			    logger.log(Level.WARNING,msg);
			}
		    }
		} 
		if (!withPrincipals) {
                    String msg = localStrings.getLocalString("pc.no_principals_mapped_to_role",
                                  "no principals mapped to role "+roleName, new Object []{ roleName});
		    logger.log(Level.WARNING,msg);
		}
	    }
	}

	writeOnCommit = createPolicyFile(true,parser,writeOnCommit);

	// load excluded perms in excluded parser
	if (excludedPermissions != null) {

	    PolicyParser excludedParser = new PolicyParser(false);

	    Enumeration pEnum = excludedPermissions.elements();
	    if (pEnum.hasMoreElements()) {
		GrantEntry grant = new GrantEntry();
		while (pEnum.hasMoreElements()) {
		    Permission p = (Permission) pEnum.nextElement();
		    PermissionEntry entry = 
			new PermissionEntry(p.getClass().getName(),
					    p.getName(),p.getActions());
		    grant.add(entry);
		}
		excludedParser.add(grant);
	    }

	    writeOnCommit = createPolicyFile(false,excludedParser,writeOnCommit);
	} 

	if (!writeOnCommit) wasRefreshed = false;
    
private java.lang.StringgetContextDirectoryName()

	if (repository == null) {
	    throw new RuntimeException("JACC Policy provider: repository not initialized");
	}
	return getContextDirectoryName(CONTEXT_ID);
    
protected static java.lang.StringgetContextDirectoryName(java.lang.String contextId)

	if (repository == null) {
	    throw new RuntimeException("JACC Policy provider: repository not initialized");
	}
	return repository+File.separator+contextId;
    
public java.lang.StringgetContextID()
This method returns this object's policy context identifier.

return
this object's policy context identifier.
throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the getContextID method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.

	checkSetPolicyPermission();
	return this.CONTEXT_ID;
    
private java.security.PermissionsgetExcludedPermissions()

	if (excludedPermissions == null) {
	    excludedPermissions = new Permissions();
	}
	return excludedPermissions;
    
protected java.security.PermissionsgetExcludedPolicy()

	synchronized(refreshLock) {
	    return state == INSERVICE_STATE ? this.excludedPermissions : null;
	}
    
private java.security.PolicygetNewPolicy()

	Object wrapper = java.security.Policy.getPolicy();
	if (wrapper != null && wrapper instanceof BasePolicyWrapper) {
	    return ((BasePolicyWrapper) wrapper).getNewPolicy();
	} else {
	    return new sun.security.provider.PolicyFile();
	} 
    
protected java.security.PolicygetPolicy()

	synchronized(refreshLock) {
	    if (state == INSERVICE_STATE) {
		return this.policy;
	    } 
	    if (logger.isLoggable(Level.FINEST)) {
		logger.finest("JACC Policy Provider: getPolicy ("+CONTEXT_ID+") is NOT in service");
	    }
	    return null;
	}
    
private java.lang.StringgetPolicyFileName(boolean granted)

      return granted ?
	  getContextDirectoryName()+File.separator+"granted"+policySuffix :
	  getContextDirectoryName()+File.separator+"excluded"+policySuffix;
    
private java.security.PermissionsgetRolePermissions(java.lang.String roleName)

	if (rolePermissionsTable == null) rolePermissionsTable = new HashMap();
	Permissions rolePermissions = (Permissions) rolePermissionsTable.get(roleName);
	if (rolePermissions == null) {
	    rolePermissions = new Permissions();
	    rolePermissionsTable.put(roleName,rolePermissions);
	}
	return rolePermissions;
    
private java.security.PermissionsgetUncheckedPermissions()

	if (uncheckedPermissions == null) {
	    uncheckedPermissions = new Permissions();
	}
	return uncheckedPermissions;
    
public booleaninService()
This method is used to determine if the policy context whose interface is this PolicyConfiguration Object is in the "inService" state.

return
true if the state of the associated policy context is "inService"; false otherwise.
throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the inService method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.

	checkSetPolicyPermission();	
	return (state == INSERVICE_STATE)? true: false;
    
private voidinitLinkTable()


	synchronized(refreshLock) {
	    // get the linkSet corresponding to this context.
	    Set linkSet = (Set) linkTable.get(CONTEXT_ID);
	    // remobe this context id from the linkSet (which may be shared
	    // with other contexts), and unmap the linkSet form this context.
	    if (linkSet != null) {
		linkSet.remove(CONTEXT_ID);
		linkTable.remove(CONTEXT_ID);
	    }

	    // create a new linkSet with onlythis context id, and put it in the table.
	    linkSet = new HashSet();
	    linkSet.add(CONTEXT_ID);
	    linkTable.put(CONTEXT_ID,linkSet);
	}
    
protected voidinitialize(boolean open, boolean remove, boolean fromFile)

	synchronized(refreshLock) {
	    String name = getPolicyFileName(true);
	    if (open || remove) {
		state = OPEN_STATE;
	    } else {
		state = INSERVICE_STATE;
	    }
	    try {
		if (remove) {
		    removePolicy();
		}

		policyUrlValue = 
		    sun.net.www.ParseUtil.fileToEncodedURL(new File(name)).toString();
		if (fromFile && !remove) {
		    excludedPermissions = loadExcludedPolicy();
		    writeOnCommit = false;
		}
		wasRefreshed = false;
	    } catch (java.net.MalformedURLException mue) {
                String defMsg="Unable to convert Policy file Name to URL: "+name;
                String msg=localStrings.getLocalString("pc.file_to_url",defMsg, new Object[]{name,mue});
		logger.log(Level.SEVERE,msg);
		throw new RuntimeException(defMsg);
	    }
	}
    
private static java.lang.StringinitializeRepository()
Read the repository directory name, create the directory, and save the name in 'repository'


	try {
	    repository = System.getProperty(REPOSITORY_HOME_PROP);
	    if (repository == null) {
                String msg=localStrings.getLocalString("pc.no_repository","no repository");
		logger.log(Level.SEVERE,msg);
	    } else {

		if (logger.isLoggable(Level.FINE)) {
		    logger.fine("JACC policy provider: repository set to: "+repository);
		}

		File rf = new File(repository);
		if (rf.exists()) {
		    if(!rf.isDirectory()) {
                        String  msg=localStrings.getLocalString("pc.unable_to_create_repository",
                              "unable to create repository"+repository,new Object []{repository});
			logger.log(Level.SEVERE,msg);
		    } else {
			// read deployed policy contextes
			File[] appsInService = rf.listFiles();
			if (appsInService != null) { 
			    for (int i = 0; i <appsInService.length; i++) {
                                File[] contextsInService = 
                                    appsInService[i].listFiles(new FileFilter() {
                                        public boolean accept(File pathname) {
                                            return pathname.isDirectory();
                                        }
                                    });
                                if (contextsInService != null) {
                                    for (int j = 0; j < contextsInService.length; j++) {
                                        try {
                                            PolicyConfigurationImpl pc = 
				               new PolicyConfigurationImpl(contextsInService[j],false,false);
				            PolicyConfigurationFactoryImpl.
				               putPolicyConfigurationImpl(pc.CONTEXT_ID,pc);
                                        } catch(Exception ex) {
                                            String msg=localStrings.getLocalString("pc.unable_to_read_repostory",
                                            "unable to read repository"  ,new Object []{contextsInService[i].toString()});
                                            logger.log(Level.WARNING,msg);
                                        }
                                    }
                                }
			    }
			}
		    }
		} else {
		    if(logger.isLoggable(Level.FINE)){
			logger.fine("JACC Policy Provider: creating new policy repository");
		    }
		    rf.mkdirs();
		}
	    }
	} catch (Exception e) {
            String msg=localStrings.getLocalString("pc.unable_to_init_repository",
                     "unable to init repository",new Object []{e});
	    logger.log(Level.SEVERE,msg);
	    repository = null;
	}

	return repository;
    
public voidlinkConfiguration(javax.security.jacc.PolicyConfiguration link)
Creates a relationship between this configuration and another such that they share the same principal-to-role mappings. PolicyConfigurations are linked to apply a common principal-to-role mapping to multiple seperately manageable PolicyConfigurations, as is required when an application is composed of multiple modules.

Note that the policy statements which comprise a role, or comprise the excluded or unchecked policy collections in a PolicyConfiguration are unaffected by the configuration being linked to another.

param
link a reference to a different PolicyConfiguration than this PolicyConfiguration.

The relationship formed by this method is symetric, transitive and idempotent. If the argument PolicyConfiguration does not have a different Policy context identifier than this PolicyConfiguration no relationship is formed, and an exception, as described below, is thrown.

throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" or "inService" when this method is called.
throws
java.lang.IllegalArgumentException if called with an argument PolicyConfiguration whose Policy context is equivalent to that of this PolicyConfiguration.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the linkConfiguration method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.


        assertStateIsOpen();

	String linkId = link.getContextID();
	if (this.CONTEXT_ID == linkId) {
            String defMsg="Operation attempted to link PolicyConfiguration to itself.";
            String msg=localStrings.getLocalString("pc.unsupported_link_operation",defMsg);
	    logger.log(Level.WARNING,msg);
	    throw new IllegalArgumentException(defMsg);
	}

	checkSetPolicyPermission();

	updateLinkTable(linkId);

    
private java.security.PermissionsloadExcludedPolicy()

	Permissions result = null;
	String name = getPolicyFileName(false);
	FileReader reader = null;
	PolicyParser parser = new PolicyParser(false);
	try {
	    captureFileTime(false);
	    reader = new FileReader(name);
	    parser.read(reader);
	} catch (java.io.FileNotFoundException fnf) {
	    //Just means there is no excluded Policy file, which
	    // is the typical case
	    parser = null;
	} catch (java.io.IOException ioe) {
            String defMsg="Error reading Policy file: "+name;
            String msg=localStrings.getLocalString("pc.file_read_error",defMsg,
                       new Object []{name, ioe});
	    logger.log(Level.SEVERE,msg);
	    throw new RuntimeException(defMsg);
	} catch ( sun.security.provider.PolicyParser.ParsingException pe) {
            String defMsg="Unable to parse Policy file: "+name;
            String msg=localStrings.getLocalString("pc.policy_parsing_exception",defMsg,
                       new Object []{name,pe});
	    logger.log(Level.SEVERE,msg);
	    throw new RuntimeException(defMsg);
	} finally {
	    if (reader != null) {
		try {
		    reader.close();
		} catch (Exception e) {
                    String defMsg="Unable to close Policy file: "+name;
                    String msg=localStrings.getLocalString("pc.file_close_error",defMsg,
                                new Object []{name,e});
		    logger.log(Level.SEVERE,msg);
		    throw new RuntimeException(defMsg);
		}
	    }
	}

	if (parser != null) {
	    Enumeration grants = parser.grantElements();
	    while (grants.hasMoreElements()) {
		GrantEntry grant = (GrantEntry) grants.nextElement();
		if (grant.codeBase != null || grant.signedBy != null || 
		    grant.principals.size() != 0) {
                        String msg=localStrings.getLocalString("pc.excluded_grant_context_ignored",
                                   "ignore excluded grant context", new Object []{grant});
		    logger.log(Level.WARNING,msg);
		} else {
		    Enumeration perms = grant.permissionEntries.elements();
		    while (perms.hasMoreElements()) {
			PermissionEntry entry = (PermissionEntry) perms.nextElement();
			Permission p = 
			    loadPermission(entry.permission,entry.name,entry.action);
			if (result == null) {
			    result = new Permissions();
			}
			result.add(p);
		    }
		}
	    }
	}

	return result;
    
private java.security.PermissionloadPermission(java.lang.String className, java.lang.String name, java.lang.String actions)

	Class clazz = null;
	Permission permission = null;
	try{
	    clazz = Class.forName(className);
	    Constructor c = clazz.getConstructor(permissionParams);
	    permission = (Permission) c.newInstance(new Object[] { name, actions });	
	} catch(Exception e){
            String defMsg="PolicyConfiguration error loading permission";
            String msg=localStrings.getLocalString("pc.permission_load_error",defMsg,
                       new Object []{className, e});
	    logger.log(Level.SEVERE,msg);
	    throw new RuntimeException(defMsg,e);
	}
	return permission;
    
protected voidrefresh(boolean force)


	synchronized(refreshLock){
	    if (state == INSERVICE_STATE && 
		(wasRefreshed == false || force || filesChanged())) {

		// find open policy.url
		int i = 0;
		String value = null;
		String urlKey = null;
		while (true) {
		    urlKey = PROVIDER_URL+(++i);
		    value = java.security.Security.getProperty(urlKey);
		    if (value == null || value.equals("")) {
			break;
		    }
		}

		try {
		    java.security.Security.setProperty(urlKey,policyUrlValue);

		    if (fileChanged(false)) {
			excludedPermissions = loadExcludedPolicy();
		    }

		    // capture time before load, to ensure that we
		    // have a time that precedes load
		    captureFileTime(true);

		    if (policy == null) {
			policy = getNewPolicy();
		    } else {
			policy.refresh();
			if (logger.isLoggable(Level.FINE)){
			    logger.fine("JACC Policy Provider: Called Policy.refresh on contextId: "+CONTEXT_ID+" policyUrlValue was "+policyUrlValue);
			}
		    }
		    wasRefreshed = true;
		} finally {
		    // can't setProperty back to null, workaround is to 
		    // use empty string
		    java.security.Security.setProperty(urlKey,"");
		}
	    }
	}
    
public voidremoveExcludedPolicy()
Used to remove any excluded policy statements from this PolicyConfiguration.

throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" or "inService" when this method is called.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the removeExcludedPolicy method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.


        assertStateIsOpen();

	checkSetPolicyPermission();

	if (excludedPermissions != null) {
	    excludedPermissions = null;
	    writeOnCommit = true;
	}
    
private voidremovePolicy()

	excludedPermissions = null;
	uncheckedPermissions = null;
	rolePermissionsTable = null;
	removePolicyFile(true);
	removePolicyFile(false);
	removePolicyContextDirectory();
	initLinkTable();
	policy = null;
	writeOnCommit = true;
    
private voidremovePolicyContextDirectory()

	String directoryName = getContextDirectoryName();
	File f = new File(directoryName);
	if(f.exists()){

            // WORKAROUND: due to existence of timestamp file in given directory
            // for SE/EE synchronization
            File[] files = f.listFiles();
            if (files != null && files.length > 0) {
                for (int i = 0; i < files.length; i++) {
                    files[i].delete();
                }
             }
             //WORKAROUND: End 

	    if (!f.delete()) {
                String defMsg = "Failure removing policy context directory: "+directoryName;
                String msg=localStrings.getLocalString("pc.file_delete_error", defMsg);
		logger.log(Level.SEVERE,msg);
		throw new RuntimeException(defMsg);
	    } else if(logger.isLoggable(Level.FINE)){
		logger.fine("JACC Policy Provider: Policy context directory removed: "+directoryName);
	    }

            File appDir = f.getParentFile();
            // WORKAROUND: due to existence of timestamp file in given directory
            // for SE/EE synchronization
            File[] fs = appDir.listFiles();
            if (fs != null && fs.length > 0) {
                boolean hasDir = false;
                for (int i = 0; i < fs.length; i++) {
                    if (fs[i].isDirectory()) {
                        hasDir = true;
                        break;
                    }
                }
                if (!hasDir) {
                    for (int i = 0; i < fs.length; i++) {
                        fs[i].delete();
                    }
                }
            }
            //WORKAROUND: End 

            File[] moduleDirs = appDir.listFiles();
            if (moduleDirs == null || moduleDirs.length == 0) {
                if (!appDir.delete()) {
                    String defMsg = "Failure removing policy context directory: " + appDir;
                    String msg = localStrings.getLocalString("pc.file_delete_error", defMsg);
		    logger.log(Level.SEVERE,msg);
		    throw new RuntimeException(defMsg);
                }
            }
	}
    
private voidremovePolicyFile(boolean granted)

	String fileName = getPolicyFileName(granted);
	File f = new File(fileName);
	if(f.exists()){
	    if (!f.delete()) {
                String defMsg = "Failure removing policy file: "+fileName; 
                String msg=localStrings.getLocalString("pc.file_delete_error", defMsg,new Object []{ fileName} );
		logger.log(Level.SEVERE,msg);
		throw new RuntimeException(defMsg);
	    } else if(logger.isLoggable(Level.FINE)){
		logger.fine("JACC Policy Provider: Policy file removed: "+fileName);
	    }
	}
    
public voidremoveRole(java.lang.String roleName)
Used to remove a role and all its permissions from this PolicyConfiguration.

param
roleName the name of the Role to remove from this PolicyConfiguration.
throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" or "inService" when this method is called.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the removeRole method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.


        assertStateIsOpen();

	assert roleName != null;

	if(roleName != null && rolePermissionsTable != null) {
	    checkSetPolicyPermission();
	    if (rolePermissionsTable.remove(roleName) != null) 
		writeOnCommit = true;
	}
    
public voidremoveUncheckedPolicy()
Used to remove any unchecked policy statements from this PolicyConfiguration.

throws
java.lang.SecurityException if called by an AccessControlContext that has not been granted the "setPolicy" SecurityPermission.
throws
java.lang.UnsupportedOperationException if the state of the policy context whose interface is this PolicyConfiguration Object is "deleted" or "inService" when this method is called.
throws
javax.security.jacc.PolicyContextException if the implementation throws a checked exception that has not been accounted for by the removeUncheckedPolicy method signature. The exception thrown by the implementation class will be encapsulated (during construction) in the thrown PolicyContextException.


        assertStateIsOpen();

	checkSetPolicyPermission();	

	if (uncheckedPermissions != null) {
	    uncheckedPermissions = null;
	    writeOnCommit = true;
	}
    
private voidupdateLinkTable(java.lang.String otherId)


	synchronized(refreshLock) {

	    // get the linkSet corresponding to this context
	    Set linkSet = (Set) linkTable.get(CONTEXT_ID);
	    // get the linkSet corresponding to the context being linked to this
	    Set otherLinkSet = (Set) linkTable.get(otherId);

	    if (otherLinkSet == null) {
                String defMsg="Linked policy configuration ("+otherId+") does not exist";
                String msg = localStrings.getLocalString("pc.invalid_link_target",defMsg, new Object []{otherId});
		logger.log(Level.SEVERE,"pc.invalid_link_target",otherId);
		throw new RuntimeException(defMsg);
	    } else {
		Iterator it = otherLinkSet.iterator();
		// for each context (id) linked to the context being linked to this
		while (it.hasNext()) {
		    String id = (String) it.next();
		    //add the id to this linkSet
		    linkSet.add(id);
		    //replace the linkset mapped to all the contexts being linked
		    //to this context, with this linkset.
		    linkTable.put(id,linkSet);
		}
	    }
	}