bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: ResourceManager.java,v 1.7 2009/06/30 17:46:02 veiming Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington * Portions Copyrighted 2011-2015 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.policy;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport java.io.ByteArrayInputStream;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport java.security.AccessController;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport java.util.Collections;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport java.util.HashMap;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport java.util.HashSet;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport java.util.Hashtable;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport java.util.Iterator;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport java.util.Map;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport java.util.Set;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport java.util.Stack;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport com.iplanet.am.util.Cache;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport com.iplanet.sso.SSOException;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport com.iplanet.sso.SSOToken;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.policy.interfaces.Referral;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.policy.plugins.OrgReferral;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport com.sun.identity.security.AdminTokenAction;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.debug.Debug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.xml.XMLUtils;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport com.sun.identity.sm.SMSException;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport com.sun.identity.sm.SMSSchema;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport com.sun.identity.sm.ServiceConfig;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport com.sun.identity.sm.ServiceConfigManager;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport com.sun.identity.sm.ServiceManager;
a14393818a78c503f7715c393044b33c86e90195Phill Cunningtonimport org.forgerock.openam.ldap.LDAPUtils;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.forgerock.opendj.ldap.DN;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.w3c.dom.Document;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.w3c.dom.Element;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport org.w3c.dom.Node;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The class <code>ResourceManager</code> manages an index to the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * resources managed by policies in a specific organization/realm.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class ResourceManager {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String org = "/";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private SSOToken token = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private ServiceConfigManager scm = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private boolean canCreateNewRes = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceTypeManager stm = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // resources service config
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private ServiceConfig rConfig = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // key: service type name, value: ServiceType object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Hashtable serviceTypeHash = new Hashtable();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String RESOURCES_XML = "xmlresources";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String RESOURCE_PREFIXES = "resourceprefixes";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String EMPTY_RESOURCE_NAME = "---EMPTY---";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static Debug debug = Debug.getInstance("amPolicy");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //Constants to build XML representation
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String LTS = "<";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String LTSS = "</";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String GTS = ">";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String SGTS = "/>";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String SPACE = " ";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String QUOTE = "\"";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String EQUALS = "=";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String NEW_LINE = "\n";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String RESOURCE_PREFIXES_XML = "resourcePrefixesXml";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String ATTRIBUTE_VALUE_PAIR = "AttributValuePair";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String PREFIX = "Prefix";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String NAME = "name";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String COUNT = "count";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Cache to store the policy names
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Cache policyNames = new Cache(1000);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this constructor is called by PolicyManager
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceManager(String orgName)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SSOException, SMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.token = (SSOToken) AccessController.doPrivileged(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AdminTokenAction.getInstance());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster org = orgName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.scm = new ServiceConfigManager(PolicyManager.POLICY_SERVICE_NAME,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster token);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster stm = ServiceTypeManager.getServiceTypeManager();
a14393818a78c503f7715c393044b33c86e90195Phill Cunnington canCreateNewRes = LDAPUtils.dnEquals(org, ServiceManager.getBaseDN());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns a set of all managed resource names for all the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * service types
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return names of the resources managed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if unable to get the policy services,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and will contain the exception thrown by SMS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Set getManagedResourceNames()throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set managedResources = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig resources = getResourcesServiceConfig(false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (resources == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster managedResources = Collections.EMPTY_SET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set resourceTypes = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourceTypes = resources.getSubConfigNames();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (resourceTypes == null) || (resourceTypes.isEmpty()) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster managedResources = Collections.EMPTY_SET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster managedResources = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator rtIter = resourceTypes.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( rtIter.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String resourceType = (String) rtIter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster managedResources.addAll(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getManagedResourceNames(resourceType));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return managedResources;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns a set of all managed resource names for the given
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * service type.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param serviceType the service type for which the resource
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * names should be returned.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return names of the resources.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if unable to get the policy services,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and will contain the exception thrown by SMS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Set getManagedResourceNames(String serviceType)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig resources = getResourcesServiceConfig(false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (resources == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return Collections.EMPTY_SET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig leafConfig = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig = resources.getSubConfig(serviceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new PolicyException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_sso_token", null, null));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (leafConfig == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // no resource node for this service type
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return Collections.EMPTY_SET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, see if the attribute is there and non-empty
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map existingAttrs = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster existingAttrs = leafConfig.getAttributes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((existingAttrs == null) ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (!existingAttrs.containsKey(RESOURCE_PREFIXES))) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return Collections.EMPTY_SET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, need to look into the attribute
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set existingRes = (Set) existingAttrs.get(RESOURCE_PREFIXES);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set resourcePrefixes = existingRes;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (existingRes != null) && (!existingRes.isEmpty()) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String xmlPrefix = (String) (existingRes.iterator().next());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourcePrefixes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = xmlToResourcePrefixes(xmlPrefix).keySet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return resourcePrefixes;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Determines that with the given organization (or, sub-organization,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * or container) name, if a new resource can be created or not.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Only root level organization/realm has the privilege to create
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * any resource.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param ServiceType the service type
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return <code>true</code> if new resources can be created,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * else <code>false</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException problem with configuration store
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public boolean canCreateNewResource(String ServiceType)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return canCreateNewRes;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns a set of valid service names that are applicable for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the organization. The result will depended if new resources
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * can be created for the organization and also if the organization
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * has managed resources.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return set of service names that are valid for the organization
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception SSOException if the caller's single sign on token has expired
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if not able to get list of services
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * defined for the organization
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Set getValidServiceNames() throws SSOException, PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set answer = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator serviceNames = stm.getServiceTypeNames().iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (serviceNames.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String serviceName = (String) serviceNames.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (canCreateNewResource(serviceName) ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster !getManagedResourceNames(serviceName).isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (answer == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer.add(serviceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return ((answer == null) ? Collections.EMPTY_SET : answer);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns a set of names of all the policies for the given resource
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the given service.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param serviceType the service type which the resource is associated
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param resource the resource for which policies should be returned
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param includePoliciesForSuperResources indicating whether the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * policies for all the super-resources in addition to the ultimate
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (sub)resource should be returned
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return set of names of the policies.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception InvalidFormatException the retrieved resources
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * from the data store have been corrupted or do not have a
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * valid format.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception NoPermissionException the user does not have sufficient
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * privileges.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if unable to get the policy services,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and will contain the exception thrown by SMS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception SSOException single-sign-on token invalid or expired
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Set getPolicyNames(String serviceType,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String resource,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean includePoliciesForSuperResources)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws InvalidFormatException, NoPermissionException,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // %%% Need to flush the cache when policy's are changed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder cacheNameBuffer = new StringBuilder();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cacheName = cacheNameBuffer.append(serviceType)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(resource)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(includePoliciesForSuperResources).toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set answer = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((answer = (Set) policyNames.get(cacheName)) != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (answer);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // This line may impact performance, try to optimize it later
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node rootNode = getXMLRootNode(serviceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (rootNode == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return Collections.EMPTY_SET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceType st = getServiceType(serviceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer = getPolicyNames(rootNode, null, st, resource,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster includePoliciesForSuperResources);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // add it to the cache
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyNames.put(cacheName, answer);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (answer);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private ServiceType getServiceType(String serviceType)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws SSOException, NameNotFoundException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceType st = (ServiceType)serviceTypeHash.get(serviceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (st == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster st = stm.getServiceType(serviceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceTypeHash.put(serviceType, st);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return st;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Adds the resource names of the policy to the resource tree.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param policy the policy to be added
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if unable to get the policy services,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and will contain the exception thrown by SMS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception SSOException single-sign-on token invalid or expired
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster void addPolicyToResourceTree(Policy policy)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set ruleNames = policy.getRuleNames();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator i = ruleNames.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // iterating through each rule
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String ruleName = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Rule rule = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (i.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ruleName = (String) i.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rule = policy.getRule(ruleName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster addRuleToResourceTree(policy.getName(), rule);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //Process Referrals
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Referrals referrals = policy.getReferrals();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( referrals != null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set referralNames = referrals.getReferralNames();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (referralNames != null) && (!referralNames.isEmpty()) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator referralIter = referralNames.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( referralIter.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String referralName= (String) referralIter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Referral referral = referrals.getReferral(referralName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( referral instanceof OrgReferral ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set values = referral.getValues();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (values != null) && (!values.isEmpty()) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator valueIter = values.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( valueIter.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String value = (String) valueIter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager pm
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = new PolicyManager(token, value);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceManager rm = pm.getResourceManager();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set ruleNames1 = policy.getRuleNames();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator ruleIter = ruleNames1.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( ruleIter.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String ruleName1
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = (String) ruleIter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Rule rule1 = policy.getRule(ruleName1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String resourceName =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rule1.getResourceName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( resourceName != null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String serviceTypeName
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = rule1.getServiceTypeName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set resourceNames = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourceNames.add(resourceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rm.addResourcePrefixes(serviceTypeName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourceNames);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removes the resource names of the policy from the resource tree.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param policy the policy to be removed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if unable to get the policy services,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and will contain the exception thrown by SMS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception SSOException single-sign-on token invalid or expired
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster void removePolicyFromResourceTree(Policy policy)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set ruleNames = policy.getRuleNames();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator i = ruleNames.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // iterating through each rule
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String ruleName = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Rule rule = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (i.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ruleName = (String) i.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rule = policy.getRule(ruleName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster removeRuleFromResourceTree(policy.getName(),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rule.getResourceName(),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rule.getServiceTypeName(),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rule.getServiceType());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //Process Referrals
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Referrals referrals = policy.getReferrals();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( referrals != null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set referralNames = referrals.getReferralNames();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (referralNames != null) && (!referralNames.isEmpty()) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator referralIter = referralNames.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( referralIter.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String referralName = (String) referralIter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Referral referral = referrals.getReferral(referralName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( referral instanceof OrgReferral ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set values = referral.getValues();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (values != null) && (!values.isEmpty()) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator valueIter = values.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( valueIter.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String value = (String) valueIter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager pm
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = new PolicyManager(token, value);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceManager rm = pm.getResourceManager();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator ruleIter =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policy.getRuleNames().iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( ruleIter.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String ruleName1
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = (String) ruleIter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Rule rule1 = policy.getRule(ruleName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String resourceName =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rule1.getResourceName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( resourceName != null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String serviceTypeName
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = rule1.getServiceTypeName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set resourceNames = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourceNames.add(resourceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rm.removeResourcePrefixes(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceTypeName, resourceNames);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Replaces resource names of a policy in the resource tree.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param oldPolicy the policy to be replaced
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param newPolicy the policy to replace the existins policy with
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if unable to get the policy services,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and will contain the exception thrown by SMS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception SSOException single-sign-on token invalid or expired
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster void replacePolicyInResourceTree(Policy oldPolicy, Policy newPolicy)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster removePolicyFromResourceTree(oldPolicy);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster addPolicyToResourceTree(newPolicy);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private ServiceConfig getResourcesServiceConfig(boolean create)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (rConfig == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (create) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rConfig = PolicyManager.createOrGetPolicyConfig(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.RESOURCES_POLICY,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.RESOURCES_POLICY, scm, org);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //rConfig = scm.getOrganizationConfig(org, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig oConfig = scm.getOrganizationConfig(org,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rConfig = (oConfig == null) ? null :
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster oConfig.getSubConfig(PolicyManager.RESOURCES_POLICY);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new PolicyException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_sso_token", null, null));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!rConfig.isValid()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("ResourceManager.getResourcesServiceConfig():"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "rConfig is not valid");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster scm = new ServiceConfigManager(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_SERVICE_NAME, token);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig oConfig = scm.getOrganizationConfig(org, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rConfig = (oConfig == null) ? null :
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster oConfig.getSubConfig(PolicyManager.RESOURCES_POLICY);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new PolicyException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_sso_token", null, null));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return rConfig;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void addRuleToResourceTree(String policyName, Rule rule)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // to do: investigate this
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String resourceName = rule.getResourceName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String serviceTypeName = rule.getServiceTypeName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceType st = rule.getServiceType();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (resourceName == null || resourceName.length() == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourceName = EMPTY_RESOURCE_NAME;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig resources = getResourcesServiceConfig(true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (resources == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig leafConfig = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig = resources.getSubConfig(serviceTypeName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (leafConfig == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // no resource node for this service type
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String newResourcesXml = rule.toResourcesXml(policyName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map newAttrs = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set newSet = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newSet.add(newResourcesXml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newAttrs.put(RESOURCES_XML, newSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resources.addSubConfig(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceTypeName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.RESOURCES_POLICY_ID,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster 0,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newAttrs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e2) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e2);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, see if the attribute is there and non-empty
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map existingAttrs = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster existingAttrs = leafConfig.getAttributes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((existingAttrs == null) ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (!existingAttrs.containsKey(RESOURCES_XML))) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String newResourcesXml = rule.toResourcesXml(policyName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set newSet = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newSet.add(newResourcesXml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.addAttribute(RESOURCES_XML, newSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e4) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e4);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, need to look into the attribute
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set existingRes = (Set) existingAttrs.get(RESOURCES_XML);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (existingRes.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String newResourcesXml = rule.toResourcesXml(policyName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map newAttrs = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set newSet = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newSet.add(newResourcesXml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newAttrs.put(RESOURCES_XML, newSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.setAttributes(newAttrs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e5) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e5);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, the attribute really contains something
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object[] retVal = getXMLRootNode(existingRes);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node rootNode = (Node)retVal[0];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Document doc = (Document)retVal[1];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean modified =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster matchAndAddReferenceNode(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster doc, rootNode, resourceName, policyName, st);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!modified) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // finally reset the modified xml content
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String modifiedResourcesXml =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SMSSchema.nodeToString(rootNode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map modifiedAttrs = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set modifiedSet = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster modifiedSet.add(modifiedResourcesXml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster modifiedAttrs.put(RESOURCES_XML, modifiedSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.setAttributes(modifiedAttrs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e6) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e6);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void removeRuleFromResourceTree(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String policyName, String resourceName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String serviceTypeName, ServiceType st)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (resourceName == null || resourceName.length() == 0) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourceName = EMPTY_RESOURCE_NAME;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig resources = getResourcesServiceConfig(false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (resources == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig leafConfig = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resources.getSubConfig(serviceTypeName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (leafConfig == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // no resource node for this service type
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, see if the attribute is there and non-empty
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map existingAttrs = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster existingAttrs = leafConfig.getAttributes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((existingAttrs == null) ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (!existingAttrs.containsKey(RESOURCES_XML))) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, need to look into the attribute
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int n = existingAttrs.size();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set existingRes = (Set) existingAttrs.get(RESOURCES_XML);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (existingRes.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, the attribute really contains something
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object[] retVal = getXMLRootNode(existingRes);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node rootNode = (Node)retVal[0];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean modified =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster matchAndRemoveReferenceNode(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rootNode, resourceName, policyName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster st, new Stack());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!modified) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!rootNode.hasChildNodes()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.removeAttribute(RESOURCES_XML);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (n == 1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resources.removeSubConfig(serviceTypeName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e3) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e3);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // finally reset the modified xml content
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String modifiedResourcesXml =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SMSSchema.nodeToString(rootNode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map modifiedAttrs = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set modifiedSet = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster modifiedSet.add(modifiedResourcesXml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster modifiedAttrs.put(RESOURCES_XML, modifiedSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.setAttributes(modifiedAttrs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e4) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e4);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private boolean matchAndAddReferenceNode(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Document doc, Node node, String resource,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String policyName, ServiceType st)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean modified = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set referenceNodes = XMLUtils.getChildNodes(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster node, PolicyManager.POLICY_INDEX_REFERENCE_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (referenceNodes == null ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNodes.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster addReferenceNodes(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster doc, node, resource, policyName, st);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return modified;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator items = referenceNodes.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node referenceNode = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String referenceName = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceMatch matchResult = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean hasMatch = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // iterating through each reference node
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (items.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode = (Node) items.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceName =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster XMLUtils.getNodeAttributeValue(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_REFERENCE_NODE_NAME_ATTR);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster matchResult = st.compare(resource, referenceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (matchResult.equals(ResourceMatch.EXACT_MATCH)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hasMatch = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set pNames = getPolicyNames(referenceNode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (pNames.contains(policyName)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster modified = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster addPolicyNameNode(doc, referenceNode, policyName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (matchResult.equals(ResourceMatch.SUPER_RESOURCE_MATCH)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster hasMatch = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String subResource = st.getSubResource(resource, referenceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster modified = matchAndAddReferenceNode(doc, referenceNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster subResource, policyName, st);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!hasMatch) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // didn't find any match, need to add (a) reference node(s)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster addReferenceNodes(doc, node, resource, policyName, st);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return modified;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private boolean matchAndRemoveReferenceNode(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node node, String resource,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String policyName, ServiceType st, Stack stack)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set referenceNodes = XMLUtils.getChildNodes(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster node, PolicyManager.POLICY_INDEX_REFERENCE_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (referenceNodes == null || referenceNodes.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator items = referenceNodes.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node referenceNode = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String referenceName = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceMatch matchResult = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // iterating through each reference node
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (items.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode = (Node) items.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceName =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster XMLUtils.getNodeAttributeValue(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_REFERENCE_NODE_NAME_ATTR);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster matchResult = st.compare(resource, referenceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (matchResult.equals(ResourceMatch.EXACT_MATCH)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster stack.push(node);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return removePolicyNameNode(referenceNode, policyName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster stack);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (matchResult.equals(ResourceMatch.SUPER_RESOURCE_MATCH)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String subResource = st.getSubResource(resource, referenceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster stack.push(node);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return matchAndRemoveReferenceNode(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode, subResource,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyName, st, stack);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void addPolicyNameNode(Document doc,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node referenceNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String policyName)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Element element =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster doc.createElement(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_POLICYNAME_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster element.setAttribute(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_POLICYNAME_NODE_NAME_ATTR,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode.appendChild(element);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private boolean removePolicyNameNode(Node referenceNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String policyName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Stack stack)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set policyNameNodes =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster XMLUtils.getChildNodes(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_POLICYNAME_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator items = policyNameNodes.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node policyNameNode = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String policyNameAttr = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (items.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyNameNode = (Node) items.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyNameAttr =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster XMLUtils.getNodeAttributeValue(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyNameNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_POLICYNAME_NODE_NAME_ATTR);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (policyNameAttr.equals(policyName)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode.removeChild(policyNameNode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster removeReferenceNodes(referenceNode, stack);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void addReferenceNodes(Document doc,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node parentNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String resourceName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String policyName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceType st)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] resources = st.split(resourceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int n = resources.length;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( n < 1 ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Element[] nodes = new Element[n];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Element policyNameNode =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster doc.createElement(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_POLICYNAME_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyNameNode.setAttribute(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_POLICYNAME_NODE_NAME_ATTR,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster nodes[n-1] =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster doc.createElement(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_REFERENCE_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster nodes[n-1].setAttribute(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_REFERENCE_NODE_NAME_ATTR,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resources[n-1]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster nodes[n-1].appendChild(policyNameNode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i=n-2; i>=0; i--) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster nodes[i] =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster doc.createElement(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_REFERENCE_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster nodes[i].setAttribute(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_REFERENCE_NODE_NAME_ATTR,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resources[i]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster nodes[i].appendChild(nodes[i+1]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster parentNode.appendChild(nodes[0]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void removeReferenceNodes(Node referenceNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Stack stack) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!referenceNode.hasChildNodes() && !stack.empty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node parentRefNode = (Node) stack.pop();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster parentRefNode.removeChild(referenceNode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster removeReferenceNodes(parentRefNode, stack);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the xml root node for the service type's resources xml blob
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param serviceType the service type which the resources xml blob is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * associated with
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return root node for the resources xml content.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception InvalidFormatException the retrieved resources
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * from the data store have been corrupted or do not have a
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * valid format.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception NoPermissionException the user does not have sufficient
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * privileges.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if unable to get the policy services,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and will contain the exception thrown by SMS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node getXMLRootNode(String serviceType)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws InvalidFormatException, NoPermissionException,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (PolicyManager.debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.message(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "searching for resources of the service type: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceType + " in organization: " + org);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig policyResources = getResourcesServiceConfig(false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (policyResources == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (PolicyManager.debug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.message(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "Resources branch is non-existent" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " in organization: " + org);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig serviceResources =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyResources.getSubConfig(serviceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (serviceResources == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (PolicyManager.debug.warningEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.warning(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceType + " branch under Resources is null" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " in organization: " + org);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Obtain the attributes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map attrs = serviceResources.getAttributes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set res = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (attrs != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster res = (Set) attrs.get(RESOURCES_XML);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (res == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (PolicyManager.debug.warningEnabled()){
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.warning(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "Unable to find resources attribute for the service: "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceType + " under Resources in organization: "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster org);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get the XML blob
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (res.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (PolicyManager.debug.warningEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.warning(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "Unable to find resources attribute value for " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "the service: " + serviceType + " in organization: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster org);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object[] retVal = getXMLRootNode(res);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return ((Node)retVal[0]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException se) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.error(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "Unable to get resources of the service type: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceType + " in organization" + org);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String objs[] = { serviceType, org };
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (se.getExceptionCode() == SMSException.STATUS_NO_PERMISSION) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new NoPermissionException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "unable_to_get_resources_for_service", objs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(se);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException ssoe) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResBundleUtils.rbName,"invalid_sso_token", null, null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Object[] getXMLRootNode(Set xmlBlob)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator it = xmlBlob.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String resourcesXml = (String) it.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Document doc = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster doc = XMLUtils.getXMLDocument(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster new ByteArrayInputStream(resourcesXml.getBytes("UTF8")));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception xmle) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.error("XML parsing error for resourcesXml");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new PolicyException(xmle));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node rootNode = XMLUtils.getRootNode(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster doc, PolicyManager.POLICY_INDEX_ROOT_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (rootNode == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.error(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid (no root node) xml resources blob: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourcesXml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new InvalidFormatException(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResBundleUtils.rbName, "invalid_resources_blob_no_root",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster null, "",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyException.SERVICE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*****
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!rootNode.getNodeName().equalsIgnoreCase(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_ROOT_NODE))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new InvalidFormatException());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ******/
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String referenceType = XMLUtils.getNodeAttributeValue(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster rootNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_ROOT_NODE_TYPE_ATTR);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!referenceType.equals(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_ROOT_NODE_TYPE_ATTR_RESOURCES_VALUE)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.error(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid (no type attr for PolicyCrossReference element) "+
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "xml resources blob: " + resourcesXml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new InvalidFormatException(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResBundleUtils.rbName, "invalid_resources_blob_no_type",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster null, "", PolicyException.SERVICE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (PolicyManager.debug.messageEnabled())
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.message("returning XML root node");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object[] retVal = new Object[2];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal[0] = rootNode;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal[1] = doc;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return retVal;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this method recursively finds the names of the policies corresponding
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to the resource. Depending on the boolean input parameter, it would
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * either returns all the policies including those for super resources,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * or, just returns the policies at the final level with exact match or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the closest match
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Set getPolicyNames(Node node,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String superRes,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceType st,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String resource,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean includePoliciesForSuperResources) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set referenceNodes =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster XMLUtils.getChildNodes(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster node,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_REFERENCE_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator items = referenceNodes.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node referenceNode = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String referenceName = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String combinedName = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceMatch matchResult = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (items.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode = (Node) items.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceName =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster XMLUtils.getNodeAttributeValue(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_REFERENCE_NODE_NAME_ATTR);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (superRes == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster combinedName = referenceName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster combinedName = st.append(superRes, referenceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster matchResult = st.compare(resource, combinedName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (matchResult.equals(ResourceMatch.EXACT_MATCH)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return getPolicyNames(referenceNode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (matchResult.equals(ResourceMatch.SUPER_RESOURCE_MATCH)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!includePoliciesForSuperResources) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return getPolicyNames(referenceNode, combinedName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster st, resource, false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set policyNamesForTheReferenceNode =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getPolicyNames(referenceNode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set policyNamesForChildrenNodes =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getPolicyNames(referenceNode, combinedName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster st, resource, true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (policyNamesForChildrenNodes.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return policyNamesForTheReferenceNode;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (policyNamesForTheReferenceNode.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return policyNamesForChildrenNodes;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyNamesForTheReferenceNode.addAll(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyNamesForChildrenNodes);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return policyNamesForTheReferenceNode;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // didn't find exact match, return policies for the last
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // super-resource match
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( !includePoliciesForSuperResources && superRes!=null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return getPolicyNames(referenceNode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return Collections.EMPTY_SET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this method finds the names of policies in the first
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * level of the node,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Set getPolicyNames(Node referenceNode) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( referenceNode == null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return Collections.EMPTY_SET;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set policyNameNodes =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster XMLUtils.getChildNodes(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster referenceNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_POLICYNAME_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator items = policyNameNodes.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node policyNameNode = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String policyName = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set retVal = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (items.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyNameNode = (Node) items.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyName =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster XMLUtils.getNodeAttributeValue(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster policyNameNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_INDEX_POLICYNAME_NODE_NAME_ATTR);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal.add(policyName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return retVal;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Adds specified resource prefixes for a certain service type
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param serviceTypeName the service type name the resource prefixes are
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * associated with
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param resourcePrefixes the prefixes to be added
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if unable to get the policy services,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and will contain the exception thrown by SMS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster void addResourcePrefixes(String serviceTypeName, Set resourcePrefixes)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig resources = getResourcesServiceConfig(true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (resources == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig leafConfig = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig = resources.getSubConfig(serviceTypeName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new PolicyException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_sso_token", null, null));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (leafConfig == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // no resource node for this service type
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map newAttrs = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set newSet = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map prefixMap
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = addResourcePrefixes(resourcePrefixes, new HashMap());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newSet.clear();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newSet.add(resourcePrefixesToXml(prefixMap));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //newSet.addAll(resourcePrefixes);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newAttrs.put(RESOURCE_PREFIXES, newSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resources.addSubConfig(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceTypeName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.RESOURCES_POLICY_ID,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster 0,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newAttrs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e2) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e2);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new PolicyException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_sso_token", null, null));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, see if the attribute is there and non-empty
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map existingAttrs = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster existingAttrs = leafConfig.getAttributes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((existingAttrs == null) ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (!existingAttrs.containsKey(RESOURCE_PREFIXES))) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set newSet = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map prefixMap
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = addResourcePrefixes(resourcePrefixes, new HashMap());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newSet.clear();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newSet.add(resourcePrefixesToXml(prefixMap));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //newSet.addAll(resourcePrefixes);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.addAttribute(RESOURCE_PREFIXES, newSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e4) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e4);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new PolicyException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_sso_token", null, null));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, need to look into the attribute
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set existingRes = (Set) existingAttrs.get(RESOURCE_PREFIXES);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map newAttrs = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //existingRes.addAll(resourcePrefixes);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map prefixMap = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (existingRes != null) && (!existingRes.isEmpty()) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String prefixXml = (String) (existingRes.iterator().next());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster prefixMap = xmlToResourcePrefixes(prefixXml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster prefixMap = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster prefixMap = addResourcePrefixes(resourcePrefixes, prefixMap);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set newSet = new HashSet(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newSet.add(resourcePrefixesToXml(prefixMap));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newAttrs.put(RESOURCE_PREFIXES, newSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster //newAttrs.put(RESOURCE_PREFIXES, existingRes);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.setAttributes(newAttrs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e5) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e5);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new PolicyException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_sso_token", null, null));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removed specified resource prefixes for a certain service type
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param serviceTypeName the service type name the resource prefixes are
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * associated with
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param resourcePrefixes the prefixes to be removed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if unable to get the policy services,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and will contain the exception thrown by SMS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster void removeResourcePrefixes(String serviceTypeName, Set resourcePrefixes)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig resources = getResourcesServiceConfig(false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (resources == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig leafConfig = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig = resources.getSubConfig(serviceTypeName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new PolicyException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_sso_token", null, null));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (leafConfig == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // no resource node for this service type
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, see if the attribute is there and non-empty
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map existingAttrs = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster existingAttrs = leafConfig.getAttributes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((existingAttrs == null) ||
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (!existingAttrs.containsKey(RESOURCE_PREFIXES))) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // else, need to look into the attribute
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int n = existingAttrs.size();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set existingSet = (Set) existingAttrs.get(RESOURCE_PREFIXES);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map prefixMap = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (existingSet != null) && (!existingSet.isEmpty()) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String prefixXml = (String) (existingSet.iterator().next());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster prefixMap = xmlToResourcePrefixes(prefixXml);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster prefixMap = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map newAttrs = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster prefixMap = removeResourcePrefixes(resourcePrefixes, prefixMap);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set newSet = new HashSet(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newSet.add(resourcePrefixesToXml(prefixMap));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newAttrs.put(RESOURCE_PREFIXES, newSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster existingSet.removeAll(resourcePrefixes);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (existingSet.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.removeAttribute(RESOURCE_PREFIXES);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (n == 1)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resources.removeSubConfig(serviceTypeName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newAttrs.put(RESOURCE_PREFIXES, existingSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.setAttributes(newAttrs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.setAttributes(newAttrs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e5) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e5);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SSOException e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new PolicyException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_sso_token", null, null));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the resource prefix (super-resource) and the rest of the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * resource name (sub-resource)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param serviceTypeName the service type which the resource is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * associated with
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param resourceName the resource name to be split
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return array of two strings, the first being the super-resource
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the second being the sub-resource
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception PolicyException if unable to get the policy services,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and will contain the exception thrown by SMS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception NameNotFoundException service for the given <code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * serviceTypeName</code> does not exist
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception SSOException single-sign-on token invalid or expired
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String[] splitResourceName(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String serviceTypeName, String resourceName
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ) throws NameNotFoundException, SSOException, PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceType st = getServiceType(serviceTypeName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set prefixes = getManagedResourceNames(serviceTypeName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] retVal = new String[2];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (prefixes.isEmpty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal[0] = "";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal[1] = resourceName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return retVal;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator iter = prefixes.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String tmp = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceMatch matchResult = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean foundSuperMatch = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean foundExactMatch = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (iter.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster tmp = (String) iter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster matchResult = st.compare(resourceName, tmp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (matchResult.equals(ResourceMatch.SUPER_RESOURCE_MATCH)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster foundSuperMatch = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (matchResult.equals(ResourceMatch.EXACT_MATCH)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster foundExactMatch = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (foundSuperMatch) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal[0] = tmp;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal[1] = st.getSubResource(resourceName, tmp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return retVal;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (foundExactMatch) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal[0] = tmp;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal[1] = "";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return retVal;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal[0] = "";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster retVal[1] = resourceName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return retVal;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Saves the resource index to data store
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param resourceType resource type
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param indexXML xml representation of index ( index to
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * policies keyed by resource name, in a tree structure)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws PolicyException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SSOException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster void saveResourceIndex(String resourceType, String indexXML)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException ,SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map newAttrs = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set newSet = new HashSet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newSet.add(indexXML);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newAttrs.put(RESOURCES_XML, newSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig resources = getResourcesServiceConfig(true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (resources != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceConfig leafConfig = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig = resources.getSubConfig(resourceType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (leafConfig == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // no resource node for this service type
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resources.addSubConfig(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourceType,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.RESOURCES_POLICY_ID,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster 0,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster newAttrs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster leafConfig.setAttributes(newAttrs);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (SMSException e1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new PolicyException(e1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Converts xml representation of resource prefixes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to a map representation
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Key in the map is the prefix and the value is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a count of how many times the prefix has been
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * effectively added. The count is incremented whenever
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the prefix is added and decremented whenever
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the prefix is removed. The count is not decremented
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * below 0.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param xmlResourcePrefixes xml representation of resource
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * prefixes. This is how it is stored in datastore.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return map representation of resource prefixes.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map xmlToResourcePrefixes(String xmlResourcePrefixes) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map resourcePrefixes = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Document document = XMLUtils.getXMLDocument(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster new ByteArrayInputStream(xmlResourcePrefixes.getBytes("UTF8")));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (document != null) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node rootNode
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = XMLUtils.getRootNode(document, RESOURCE_PREFIXES);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( rootNode != null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set nodeSet
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = XMLUtils.getChildNodes(rootNode, PREFIX);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( nodeSet != null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator nodes = nodeSet.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (nodes.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node node = (Node)nodes.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String prefix
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = XMLUtils.getNodeAttributeValue(node,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster NAME);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String count
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = XMLUtils.getNodeAttributeValue(node,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster COUNT);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (prefix != null) && (count != null) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourcePrefixes.put(prefix, count);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception xmle) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.error("XML parsing error for resource prefixes "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + " in organization: " + org);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return resourcePrefixes;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Converts map representation of resource prefixes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to an xml representation
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param resourcePrefixes map representation of resource
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * prefixes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return xml representation of resource prefixes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String resourcePrefixesToXml(Map resourcePrefixes) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder sb = new StringBuilder(128);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(LTS).append(RESOURCE_PREFIXES).append(GTS)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(NEW_LINE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator prefixes = resourcePrefixes.keySet().iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( prefixes.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String prefix = (String) prefixes.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String value = (String) resourcePrefixes.get(prefix);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(LTS).append(PREFIX).append(SPACE)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(NAME).append(EQUALS)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(QUOTE).append(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster XMLUtils.escapeSpecialCharacters(prefix))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(QUOTE).append(SPACE)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(COUNT).append(EQUALS)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(QUOTE).append(value).append(QUOTE)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(SGTS).append(NEW_LINE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(LTSS).append(RESOURCE_PREFIXES).append(GTS)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(NEW_LINE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return sb.toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Adds a set of resource prefixes to a map of resource prefixes.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Adding a prefix increments the the count for the prefix
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * in map value.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param prefixes a set of resource prefixes to add
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param prefixMap a map of resource prefixes to which to
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * add the prefixes.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return prefixMap modified accounting for the addition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of prefixes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map addResourcePrefixes(Set prefixes, Map prefixMap) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator iter = prefixes.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( iter.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String prefix = (String) iter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int intValue = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String count = (String) prefixMap.get(prefix);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( count != null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster intValue = Integer.parseInt(count);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.error(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "ResourceManager.addResourcePrefixes:", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster intValue++;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster prefixMap.put(prefix, Integer.toString(intValue));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return prefixMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removes a set of resource prefixes from a map of resource prefixes.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removing a prefix decrements the count for the prefix in the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * map value. Count value is not decremented below 0.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param prefixes a set of resource prefixes to remove
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param prefixMap a map of resource prefixes from which to
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * remove the prefixes.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return prefixMap modified accounting for the removal
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of prefixes.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map removeResourcePrefixes(Set prefixes, Map prefixMap) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator iter = prefixes.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( iter.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String prefix = (String) iter.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int intValue = 0;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String count = (String) prefixMap.get(prefix);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( count != null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster intValue = Integer.parseInt(count);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.error(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "ResourceManager.removeResourcePrefixes:", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster intValue--;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( intValue > 0 ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster prefixMap.put(prefix, Integer.toString(intValue));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster prefixMap.remove(prefix);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return prefixMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}