FileDocCategorySizeDatePackage
PolicySourceModel.javaAPI DocExample19949Tue May 29 16:56:40 BST 2007com.sun.xml.ws.policy.sourcemodel

PolicySourceModel

public final class PolicySourceModel extends Object implements Cloneable
This class is a root of unmarshaled policy source structure. Each instance of the class contains factory method to create new {@link com.sun.xml.ws.policy.sourcemodel.ModelNode} instances associated with the actual model instance.
author
Marek Potociar

Fields Summary
private static final com.sun.xml.ws.policy.privateutil.PolicyLogger
LOGGER
private static final Map
defaultNamespaceToPrefixMap
private ModelNode
rootNode
private String
policyId
private String
policyName
private final List
references
private boolean
expanded
Constructors Summary
private PolicySourceModel()
Private constructor that creats new policy source model instance without any id or name identifier. The namespace-to-prefix map is initialized with mapping of policy namespace to the default value set by {@link PolicyConstants#POLICY_NAMESPACE_PREFIX POLICY_NAMESPACE_PREFIX constant}

        this.rootNode = ModelNode.createRootPolicyNode(this);
    
private PolicySourceModel(String policyId, String policyName)
Private constructor that creats new policy source model instance with given id or name identifier.

param
policyId relative policy reference within an XML document. May be {@code null}.
param
policyName absloute IRI of policy expression. May be {@code null}.

        this();
        this.policyId = policyId;
        this.policyName = policyName;
    
Methods Summary
voidaddNewPolicyReference(ModelNode node)
Adds new policy reference to the policy source model. The method is used by the ModelNode instances of type POLICY_REFERENCE that need to register themselves as policy references in the model.

param
node policy reference model node to be registered as a policy reference in this model.

        if (node.getType() != ModelNode.Type.POLICY_REFERENCE) {
            throw new IllegalArgumentException(LocalizationMessages.WSP_0042_POLICY_REFERENCE_NODE_EXPECTED_INSTEAD_OF(node.getType()));
        }
        
        references.add(node);
    
protected com.sun.xml.ws.policy.sourcemodel.PolicySourceModelclone()

        final PolicySourceModel clone = (PolicySourceModel) super.clone();
        
        clone.rootNode = this.rootNode.clone();
        try {
            clone.rootNode.setParentModel(this);
        } catch (IllegalAccessException e) {
            throw LOGGER.logSevereException(new CloneNotSupportedException(LocalizationMessages.WSP_0013_UNABLE_TO_SET_PARENT_MODEL_ON_ROOT()), e);
        }
        
        return clone;
    
public booleancontainsPolicyReferences()
Returns a boolean value indicating whether this policy source model contains references to another policy source models.

Every source model that references other policies must be expanded before it can be translated into a Policy objects. See {@link #expand(PolicySourceModelContext)} and {@link #isExpanded()} for more details.

return
{@code true} or {code false} depending on whether this policy source model contains references to another policy source models.

        return !references.isEmpty();
    
public static com.sun.xml.ws.policy.sourcemodel.PolicySourceModelcreatePolicySourceModel()
Factory method that creates new policy source model instance.

return
newly created policy source model instance

    
                         
        
        return new PolicySourceModel();
    
public static com.sun.xml.ws.policy.sourcemodel.PolicySourceModelcreatePolicySourceModel(java.lang.String policyId, java.lang.String policyName)
Factory method that creates new policy source model instance and initializes it according to parameters provided.

param
policyId local policy identifier - relative URI. May be {@code null}.
param
policyName global policy identifier - absolute policy expression URI. May be {@code null}.
return
newly created policy source model instance with its name and id properly set

        return new PolicySourceModel(policyId, policyName);
    
public booleanequals(java.lang.Object obj)
An {@code Object.equals(Object obj)} method override.

When child nodes are tested for equality, the parent policy source model is not considered. Thus two different policy source models instances may be equal based on their node content.

        if (this == obj) {
            return true;
        }
        
        if (!(obj instanceof PolicySourceModel)) {            
            return false;
        }
        
        boolean result = true;
        final PolicySourceModel that = (PolicySourceModel) obj;
        
        result = result && ((this.policyId == null) ? that.policyId == null : this.policyId.equals(that.policyId));
        result = result && ((this.policyName == null) ? that.policyName == null : this.policyName.equals(that.policyName));
        result = result && this.rootNode.equals(that.rootNode);
//        result = result && ((this.xxx == null) ? that.xxx == null : this.xxx.equals(that.xxx));
        
        return result;
    
public synchronized voidexpand(PolicySourceModelContext context)
Expands current policy model. This means, that if this model contains any (unexpanded) policy references, then the method expands those references by placing the content of the referneced policy source models under the policy reference nodes. This operation merely creates a link between this and referenced policy source models. Thus any change in the referenced models will be visible wihtin this model as well.

Please, notice that the method does not check if the referenced models are already expanded nor does the method try to expand unexpanded referenced models. This must be preformed manually within client's code. Consecutive calls of this method will have no effect.

Every source model that references other policies must be expanded before it can be translated into a Policy object. See {@link #isExpanded()} and {@link #containsPolicyReferences()} for more details.

param
context a policy source model context holding the set of unmarshalled policy source models within the same context.

        if (!isExpanded()) {
            for (ModelNode reference : references) {
                final PolicyReferenceData refData = reference.getPolicyReferenceData();
                final String digest = refData.getDigest();
                PolicySourceModel referencedModel;
                if (digest == null) {
                    referencedModel = context.retrieveModel(refData.getReferencedModelUri());
                } else {
                    referencedModel = context.retrieveModel(refData.getReferencedModelUri(), refData.getDigestAlgorithmUri(), digest);
                }
                
                reference.setReferencedModel(referencedModel);
            }
            expanded = true;
        }
    
private java.lang.StringgetDefaultPrefix(java.lang.String namespace)
Method retrieves default prefix for given namespace. Method returns null if no default prefix is defined..

param
namespace to get default prefix for.
return
default prefix for given namespace. May return {@code null} if the default prefix for given namespace is not defined.

        return defaultNamespaceToPrefixMap.get(namespace);
    
java.util.MapgetNamespaceToPrefixMapping()
Provides information about how namespaces used in this {@link PolicySourceModel} instance should be mapped to thier default prefixes when marshalled.

return
immutable map that holds information about namespaces used in the model and their mapping to prefixes that should be used when marshalling this model.

        final Map<String, String> nsToPrefixMap = new HashMap<String, String>();

        final Collection<String> namespaces = getUsedNamespaces();
        for (String namespace : namespaces) {
            final String prefix = getDefaultPrefix(namespace);
            if (prefix != null) {
                nsToPrefixMap.put(namespace, prefix);
            }
        }        
        
        return nsToPrefixMap;
    
public java.lang.StringgetPolicyId()
Returns a policy ID of this policy source model.

return
policy ID.

        return policyId;
    
public java.lang.StringgetPolicyName()
Returns a policy name of this policy source model.

return
policy name.

        return policyName;
    
public ModelNodegetRootNode()
Returns a root node of this policy source model. It is allways of POLICY type.

return
root policy source model node - allways of POLICY type.

        return rootNode;
    
private java.util.CollectiongetUsedNamespaces()
Iterates through policy vocabulary and extracts set of namespaces used in the policy expression.

param
policy policy instance to check fro used namespaces
return
collection of used namespaces within given policy instance

        final Set<String> namespaces = new HashSet<String>();
        namespaces.add(PolicyConstants.POLICY_NAMESPACE_URI);
        
        if (this.policyId != null) {
            namespaces.add(PolicyConstants.WSU_NAMESPACE_URI);            
        }
        
        final Queue<ModelNode> nodesToBeProcessed = new LinkedList<ModelNode>();
        nodesToBeProcessed.add(rootNode);
        
        ModelNode processedNode;
        while ((processedNode = nodesToBeProcessed.poll()) != null) {
            for (ModelNode child : processedNode.getContent()) {
                if (child.hasChildren()) {
                    nodesToBeProcessed.offer(child);
                }
                
                if (child.isAssertionRelatedNode()) {
                    final AssertionData nodeData = child.getNodeData();
                    namespaces.add(nodeData.getName().getNamespaceURI());
                    if (nodeData.isPrivateAttributeSet()) {
                        namespaces.add(PolicyConstants.SUN_POLICY_NAMESPACE_URI);
                    }
                    
                    for (Entry<QName, String> attribute : nodeData.getAttributesSet()) {
                        namespaces.add(attribute.getKey().getNamespaceURI());
                    }                    
                }
            }
        }
        
        return namespaces;
    
public inthashCode()
An {@code Object.hashCode()} method override.

        int result = 17;
        
        result = 37 * result + ((this.policyId == null) ? 0 : this.policyId.hashCode());
        result = 37 * result + ((this.policyName == null) ? 0 : this.policyName.hashCode());
        result = 37 * result + this.rootNode.hashCode();
//        result = 37 * result + ((this.xxx == null) ? 0 : this.xxx.hashCode());
        
        return result;
    
private booleanisExpanded()
Returns a boolean value indicating whether this policy source model contains is already expanded (i.e. contains no unexpanded policy references) or not. This means that if model does not originally contain any policy references, it is considered as expanded, thus this method returns {@code true} in such case. Also this method does not check whether the references policy source models are expanded as well, so after expanding this model a value of {@code true} is returned even if referenced models are not expanded yet. Thus each model can be considered to be fully expanded only if all policy source models stored in PolicySourceModelContext instance are expanded, provided the PolicySourceModelContext instance contains full set of policy source models.

Every source model that references other policies must be expanded before it can be translated into a Policy object. See {@link #expand(PolicySourceModelContext)} and {@link #containsPolicyReferences()} for more details.

return
{@code true} or {@code false} depending on whether this policy source model contains is expanded or not.

        return references.isEmpty() || expanded;
    
public java.lang.StringtoString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object.

return
a string representation of the object.

        final String innerIndent = PolicyUtils.Text.createIndent(1);
        final StringBuffer buffer = new StringBuffer(60);
        
        buffer.append("Policy source model {").append(PolicyUtils.Text.NEW_LINE);
        buffer.append(innerIndent).append("policy id = '").append(policyId).append('\'").append(PolicyUtils.Text.NEW_LINE);
        buffer.append(innerIndent).append("policy name = '").append(policyName).append('\'").append(PolicyUtils.Text.NEW_LINE);
        rootNode.toString(1, buffer).append(PolicyUtils.Text.NEW_LINE).append('}");
        
        return buffer.toString();