FileDocCategorySizeDatePackage
Policy.javaAPI DocExample23482Tue May 29 16:56:34 BST 2007com.sun.xml.ws.policy

Policy

public class Policy extends Object implements Iterable
A policy represents normalized policy as a wrapper of available policy alternatives represented by child {@link AssertionSet AssertionSets}.
author
Fabian Ritzmann, Marek Potociar

Fields Summary
private static final String
POLICY_TOSTRING_NAME
A string constant used in package private constructor to customize the object's toString() method output.
private static final List
NULL_POLICY_ASSERTION_SETS
Constant represents empty list of assertion sets. This represents the content of a 'NULL' policy - a policy with no alternatives. The constant supports memory effective creation of 'NULL' policy objects.
private static final List
EMPTY_POLICY_ASSERTION_SETS
Constant represents list of assertion sets with single empty assertion set. This represents the content of an 'EMPTY' policy - a policy with a single empty alternative. The constant supports memory effective creation of 'EMPTY' policy objects.
private static final Set
EMPTY_VOCABULARY
Constant represents empty vocabulary of a 'NULL' or 'EMPTY' policies. The constant supports memory effective creation of 'NULL' and 'EMPTY' policy objects.
private static final Policy
ANONYMOUS_NULL_POLICY
Constant representation of all 'NULL' policies returned by createNullPolicy() factory method. This is to optimize the memory footprint.
private static final Policy
ANONYMOUS_EMPTY_POLICY
Constant representation of all 'EMPTY' policies returned by createEmptyPolicy() factory method. This constant is to optimize the memory footprint.
private String
policyId
Policy ID holder
private String
name
Policy name holder
private final List
assertionSets
internal collection of policy alternatives
private final Set
vocabulary
internal collection of policy vocabulary entries (qualified names of all assertion types present in the policy expression)
private final Collection
immutableVocabulary
immutable version of policy vocabulary that is made available to clients via getter method
private final String
toStringName
policy object name used in a toString() method. This ensures that Policy class children can customize (via package private Policy constructors) the toString() method without having to override it.
Constructors Summary
private Policy(String name, String policyId, List assertionSets, Set vocabulary)
A most flexible policy object constructor that allows private creation of policy objects and direct setting of all its attributes.

param
name global URI of the policy. May be {@code null}.
param
policyId local URI of the policy. May be {@code null}.
param
sets represents the collection of policy alternatives of the policy object created. The list is directly assigned to the policy object internal attribute. Subsequent manipulations on the collection must be handled with care.
param
vocabulary represents the vocabulary of the policy object. Subsequent manipulations on the collection must be handled with care.

        this.toStringName = POLICY_TOSTRING_NAME;
        this.name = name;
        this.policyId = policyId;
        this.assertionSets = assertionSets;
        this.vocabulary = vocabulary;
        this.immutableVocabulary = Collections.unmodifiableCollection(this.vocabulary);
    
Policy(String toStringName, Collection sets)
Constructor that should be overridden by child implementation. The constructor allows for easy toString() output customization.

param
toStringName a general name of the object (such as 'policy' or 'nested policy') that will be used in the toString() method to identify the object.
param
sets represents the collection of policy alternatives of the policy object created. During the creation of the new policy object, the content of the alternatives collection is copied into an internal policy object structure, thus any subsequent operations on the collection will have no impact on the newly constructed policy object. The collection may be {@code null} or empty. In such case a 'NULL' policy object is constructed.

        this.toStringName = toStringName;
        
        if (sets == null || sets.isEmpty()) {
            this.assertionSets = NULL_POLICY_ASSERTION_SETS;
            this.vocabulary = EMPTY_VOCABULARY;
            this.immutableVocabulary = EMPTY_VOCABULARY;
        } else {
            this.assertionSets = new LinkedList<AssertionSet>();
            this.vocabulary = new TreeSet<QName>(PolicyUtils.Comparison.QNAME_COMPARATOR);
            this.immutableVocabulary = Collections.unmodifiableCollection(this.vocabulary);
            
            addAll(sets);
        }
    
Policy(String toStringName, String name, String policyId, Collection sets)
Constructor that should be overridden by child implementation. The constructor allows for easy toString() output customization.

param
toStringName a general name of the object (such as 'policy' or 'nested policy') that will be used in the toString() method to identify the object.
param
name global URI of the policy. May be {@code null}.
param
policyId local URI of the policy. May be {@code null}.
param
sets represents the collection of policy alternatives of the policy object created. During the creation of the new policy object, the content of the alternatives collection is copied into an internal policy object structure, thus any subsequent operations on the collection will have no impact on the newly constructed policy object. The collection may be {@code null} or empty. In such case a 'NULL' policy object is constructed.

        this(toStringName, sets);
        this.name = name;
        this.policyId = policyId;
    
Methods Summary
private booleanadd(AssertionSet set)
Adds single alternative to the internal alternatives set of the policy object.

param
set assertion set (policy alternative) object to be added. May be {@code null}; in such case the method returns false.
return
{@code true} or {@code false} depending on whether the new alternative was added to the policy object or not.

        if (set == null) {
            return false;
        }
        
        if (this.assertionSets.contains(set)) {
            return false;
        } else {
            this.assertionSets.add(set);
            this.vocabulary.addAll(set.getVocabulary());
            return true;
        }
    
private booleanaddAll(java.util.Collection sets)
Adds all alternatives from the input collection of assertion sets to the policy object's internal set of alternatives. The policy object's vocabulary structure is updated as well.

param
sets collection of new alternatives. Must NOT be {@code null} or empty. The check for null or empty input parameter is performed with help of {@code assert} keyword, thus during the testing and development of this class {@code -ea} switch should be turned on for this class.
return
{@code true} if all elements in the input collection were added to internal structure, {@code false} otherwise.

        assert (sets != null && !sets.isEmpty()) : LocalizationMessages.WSP_0036_PRIVATE_METHOD_DOES_NOT_ACCEPT_NULL_OR_EMPTY_COLLECTION();
        
        boolean result = true;
        for (AssertionSet set : sets) {
            result &= add(set); // this is here to ensure that vocabulary is built correctly as well
        }
        Collections.sort(this.assertionSets);
        
        return result;
    
public booleancontains(java.lang.String namespaceUri)
Returns true if the policy contains the assertion names with specified namespace in its vocabulary

param
namespaceUri the assertion namespace URI (identifying assertion domain)
return
{@code true}, if an assertion with the given name could be found in the policy vocabulary {@code false} otherwise.

        for (QName entry : vocabulary) {
            if (entry.getNamespaceURI().equals(namespaceUri)) {
                return true;
            }
        }
        
        return false;
    
public booleancontains(javax.xml.namespace.QName assertionName)
Determines if the policy instance contains the assertion with the name specified in its vocabulary.

param
assertionName the name of the assertion to be tested.
return
{@code true} if the assertion with the specified name is part of the policy instance's vocabulary, {@code false} otherwise.

        return vocabulary.contains(assertionName);
    
public static com.sun.xml.ws.policy.PolicycreateEmptyPolicy()
The factory method creates an immutable policy instance which represents a 'anything allowed' policy expression.

return
policy instance which represents a 'anything allowed' (empty policy alternative with no plicy assertions prescribed).

        return ANONYMOUS_EMPTY_POLICY;
    
public static com.sun.xml.ws.policy.PolicycreateEmptyPolicy(java.lang.String name, java.lang.String policyId)
The factory method creates an immutable policy instance which represents a 'anything allowed' policy expression.

param
name global URI of the policy. May be {@code null}.
param
policyId local URI of the policy. May be {@code null}.
return
policy instance which represents a 'anything allowed' (empty policy alternative with no plicy assertions prescribed).

        if (name == null && policyId == null) {
            return ANONYMOUS_EMPTY_POLICY;
        } else {
            return new Policy(name, policyId, EMPTY_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY);
        }
    
public static com.sun.xml.ws.policy.PolicycreateNullPolicy()
The factory method creates an immutable policy instance which represents a 'nothing allowed' policy expression.

return
policy instance which represents a 'nothing allowed' (no policy alternatives).

    
                                   
        
        return ANONYMOUS_NULL_POLICY;
    
public static com.sun.xml.ws.policy.PolicycreateNullPolicy(java.lang.String name, java.lang.String policyId)
The factory method creates an immutable policy instance which represents a 'nothing allowed' policy expression.

param
name global URI of the policy. May be {@code null}.
param
policyId local URI of the policy. May be {@code null}.
return
policy instance which represents a 'nothing allowed' (no policy alternatives).

        if (name == null && policyId == null) {
            return ANONYMOUS_NULL_POLICY;
        } else {
            return new Policy(name, policyId, NULL_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY);
        }
    
public static com.sun.xml.ws.policy.PolicycreatePolicy(java.util.Collection sets)
The factory method creates an immutable policy instance which represents a policy expression with alternatives specified by {@code sets} input parameter. If the collection of policy alternatives is null or empty an object representing a 'NULL' policy expression is returned. However, in such case it is better to use {@link #createNullPolicy()} factory method directly.

param
sets represents the collection of policy alternatives of the policy object created. During the creation of the new policy object, the content of the alternatives collection is copied into an internal policy object structure, thus any subsequent operations on the collection will have no impact on the newly constructed policy object.
return
policy instance which represents the policy with given alternatives.

        if (sets == null || sets.isEmpty()) {
            return createNullPolicy();
        } else {
            return new Policy(POLICY_TOSTRING_NAME, sets);
        }
    
public static com.sun.xml.ws.policy.PolicycreatePolicy(java.lang.String name, java.lang.String policyId, java.util.Collection sets)
The factory method creates an immutable policy instance which represents a policy expression with alternatives specified by {@code sets} input parameter. If the collection of policy alternatives is null or empty an object representing a 'NULL' policy expression is returned. However, in such case it is better to use {@link #createNullPolicy(String, String)} factory method directly.

param
name global URI of the policy. May be {@code null}.
param
policyId local URI of the policy. May be {@code null}.
param
sets represents the collection of policy alternatives of the policy object created. During the creation of the new policy object, the content of the alternatives collection is copied into an internal policy object structure, thus any subsequent operations on the collection will have no impact on the newly constructed policy object.
return
policy instance which represents the policy with given alternatives.

        if (sets == null || sets.isEmpty()) {
            return createNullPolicy(name, policyId);
        } else {
            return new Policy(POLICY_TOSTRING_NAME, name, policyId, sets);
        }
    
public booleanequals(java.lang.Object obj)
An {@code Object.equals(Object obj)} method override.

        if (this == obj) {
            return true;
        }
        
        if (!(obj instanceof Policy)) {
            return false;
        }
        
        final Policy that = (Policy) obj;
        
        boolean result = true;
        
        result = result && this.vocabulary.equals(that.vocabulary);
        result = result && this.assertionSets.size() == that.assertionSets.size() && this.assertionSets.containsAll(that.assertionSets);
        
        return result;
    
java.util.CollectiongetContent()

        return assertionSets;
    
public java.lang.StringgetId()
Returns the policy identifier that serves as a local relative policy URI.

return
policy identifier - a local relative policy URI. If no policy identifier is set, returns {@code null}.

        return policyId;
    
public java.lang.StringgetIdOrName()
Returns the policy ID or if that is null the policy name. May return null if both attributes are null.

see
#getId()
see
#getName()
return
The policy ID if it was set, or the name or null if no attribute was set.

        if (policyId != null) {
            return policyId;
        }
        return name;
    
public java.lang.StringgetName()
Returns the policy name that serves as a global policy URI.

return
policy name - a global policy URI. If no policy name is set, returns {@code null}.

        return name;
    
public intgetNumberOfAssertionSets()
Method returns how many policy alternatives this policy instance contains.

return
number of policy alternatives contained in this policy instance

        return assertionSets.size();
    
public java.util.CollectiongetVocabulary()
Retrieves the vocabulary of this policy expression. The vocabulary is represented by an immutable collection of unique QName objects. Each of those objects represents single assertion type contained in the policy.

return
immutable collection of assertion types contained in the policy (a policy vocabulary).

        return immutableVocabulary;
    
public inthashCode()
An {@code Object.hashCode()} method override.

        int result = 17;
        
        result = 37 * result + vocabulary.hashCode();
        result = 37 * result + assertionSets.hashCode();
        
        return result;
    
public booleanisEmpty()
Returns {@code true} if the policy instance represents "anything allowed" policy expression

return
{@code true} if the policy instance represents "anything allowed" policy expression, {@code false} otherwise.

        return assertionSets.size() == 1 && assertionSets.get(0).isEmpty();
    
public booleanisNull()
Returns {@code true} if the policy instance represents "nothing allowed" policy expression

return
{@code true} if the policy instance represents "nothing allowed" policy expression, {@code false} otherwise.

        return assertionSets.size() == 0;
    
public java.util.Iteratoriterator()
A policy usually contains one or more assertion sets. Each assertion set corresponds to a policy alternative as defined by WS-Policy.

return
An iterator to iterate through all contained assertion sets

        return assertionSets.iterator();
    
public java.lang.StringtoString()
An {@code Object.toString()} method override.

        return toString(0, new StringBuffer()).toString();
    
java.lang.StringBuffertoString(int indentLevel, java.lang.StringBuffer buffer)
A helper method that appends indented string representation of this instance to the input string buffer.

param
indentLevel indentation level to be used.
param
buffer buffer to be used for appending string representation of this instance
return
modified buffer containing new string representation of the instance

        final String indent = PolicyUtils.Text.createIndent(indentLevel);
        final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1);
        final String innerDoubleIndent = PolicyUtils.Text.createIndent(indentLevel + 2);
        
        buffer.append(indent).append(toStringName).append(" {").append(PolicyUtils.Text.NEW_LINE);
        buffer.append(innerIndent).append("id = '").append(policyId).append('\'").append(PolicyUtils.Text.NEW_LINE);
        buffer.append(innerIndent).append("name = '").append(name).append('\'").append(PolicyUtils.Text.NEW_LINE);
        
        buffer.append(innerIndent).append("vocabulary {").append(PolicyUtils.Text.NEW_LINE);
        if (vocabulary.isEmpty()) {
            buffer.append(innerDoubleIndent).append("no entries").append(PolicyUtils.Text.NEW_LINE);
        } else {
            int index = 1;
            for (QName entry : vocabulary) {
                buffer.append(innerDoubleIndent).append(index++).append(". entry = '").append(entry.getNamespaceURI()).append(':").append(entry.getLocalPart()).append('\'").append(PolicyUtils.Text.NEW_LINE);
            }
        }
        buffer.append(innerIndent).append('}").append(PolicyUtils.Text.NEW_LINE);
        
        if (assertionSets.isEmpty()) {
            buffer.append(innerIndent).append("no assertion sets").append(PolicyUtils.Text.NEW_LINE);
        } else {
            for (AssertionSet set : assertionSets) {
                set.toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE);
            }
        }
        
        buffer.append(indent).append('}");
        
        return buffer;