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

PolicyIntersector

public final class PolicyIntersector extends Object
The instance of this class is intended to provide policy intersection mechanism.
author
Marek Potociar (marek.potociar@sun.com)

Fields Summary
private static final PolicyIntersector
INSTANCE
private static final com.sun.xml.ws.policy.privateutil.PolicyLogger
LOGGER
Constructors Summary
private PolicyIntersector()
Prevents direct instantiation of this class from outside

    
                 
      
        // nothing to initialize
    
Methods Summary
public static com.sun.xml.ws.policy.PolicyIntersectorcreatePolicyIntersector()
Returns a policy intersector that can be used to intersect group of policies.

return
policy intersector instance.

        return PolicyIntersector.INSTANCE;
    
public Policyintersect(java.util.Collection policies)
Performs intersection on the input collection of policies and returns the resulting (intersected) policy. If input policy collection contains only a single policy instance, no intersection is performed and the instance is directly returned as a method call result.

param
policies collection of policies to be intersected. Must not be {@code null} nor empty, otherwise exception is thrown.
return
intersected policy as a result of perfromed policy intersection. A {@code null} value is never returned.
throws
IllegalArgumentException in case {@code policies} argument is either {@code null} or empty collection.

        if (policies == null || policies.isEmpty()) {
            throw LOGGER.logSevereException(new IllegalArgumentException(LocalizationMessages.WSP_0056_NEITHER_NULL_NOR_EMPTY_POLICY_COLLECTION_EXPECTED()));
        } else if (policies.size() == 1) {
            return policies.iterator().next();
        }
        
        // check for "null" and "empty" policy: if such policy is found return "null" policy, or if all policies are "empty", return "empty" policy
        boolean found = false;
        boolean allPoliciesEmpty = true;
        for (Policy tested : policies) {
            if (tested.isEmpty()) {
                found = true;
            } else {
                if (tested.isNull()) {
                    found = true;
                }
                allPoliciesEmpty = false;
            }
            
            if (found && !allPoliciesEmpty) {
                return Policy.createNullPolicy();
            }
        }
        if (allPoliciesEmpty) {
            return Policy.createEmptyPolicy();
        }
        
        // simple tests didn't lead to final answer => let's performe some intersecting ;)
        final Iterator<Policy> policyIterator = policies.iterator();
        final List<AssertionSet> finalAlternatives = new LinkedList<AssertionSet>(policyIterator.next().getContent());
        final Queue<AssertionSet> testedAlternatives = new LinkedList<AssertionSet>();
        final List<AssertionSet> alternativesToMerge = new ArrayList<AssertionSet>(2);
        while (policyIterator.hasNext()) {
            final Collection<AssertionSet> currentAlternatives = policyIterator.next().getContent();

            testedAlternatives.clear();
            testedAlternatives.addAll(finalAlternatives);
            finalAlternatives.clear();
            
            AssertionSet testedAlternative;
            while ((testedAlternative = testedAlternatives.poll()) != null) {
                for (AssertionSet currentAlternative : currentAlternatives) {
                    if (testedAlternative.isCompatibleWith(currentAlternative)) {
                        alternativesToMerge.add(testedAlternative);
                        alternativesToMerge.add(currentAlternative);                        
                        finalAlternatives.add(AssertionSet.createMergedAssertionSet(alternativesToMerge));
                        alternativesToMerge.clear();
                    }
                }
            }
        }
        
        return Policy.createPolicy(finalAlternatives);
    
public Policyintersect(Policy policyA, Policy policyB)

        return intersect(Arrays.asList(new Policy[] {policyA, policyB}));