1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/policy/PolicyIntersector.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,152 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.policy; 1.30 + 1.31 +import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger; 1.32 +import java.util.Collection; 1.33 +import java.util.LinkedList; 1.34 +import java.util.List; 1.35 +import java.util.Queue; 1.36 +import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages; 1.37 +import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.NamespaceVersion; 1.38 +import java.util.ArrayList; 1.39 + 1.40 +/** 1.41 + * The instance of this class is intended to provide policy intersection mechanism. 1.42 + * 1.43 + * @author Marek Potociar (marek.potociar@sun.com) 1.44 + */ 1.45 +public final class PolicyIntersector { 1.46 + static enum CompatibilityMode { 1.47 + STRICT, 1.48 + LAX 1.49 + } 1.50 + 1.51 + private static final PolicyIntersector STRICT_INTERSECTOR = new PolicyIntersector(CompatibilityMode.STRICT); 1.52 + private static final PolicyIntersector LAX_INTERSECTOR = new PolicyIntersector(CompatibilityMode.LAX); 1.53 + private static final PolicyLogger LOGGER = PolicyLogger.getLogger(PolicyIntersector.class); 1.54 + 1.55 + private CompatibilityMode mode; 1.56 + 1.57 + /** 1.58 + * Prevents direct instantiation of this class from outside 1.59 + * @param intersectionMode intersection mode 1.60 + */ 1.61 + private PolicyIntersector(CompatibilityMode intersectionMode) { 1.62 + this.mode = intersectionMode; 1.63 + } 1.64 + 1.65 + /** 1.66 + * Returns a strict policy intersector that can be used to intersect group of policies. 1.67 + * 1.68 + * @return policy intersector instance. 1.69 + */ 1.70 + public static PolicyIntersector createStrictPolicyIntersector() { 1.71 + return PolicyIntersector.STRICT_INTERSECTOR; 1.72 + } 1.73 + 1.74 + /** 1.75 + * Returns a strict policy intersector that can be used to intersect group of policies. 1.76 + * 1.77 + * @return policy intersector instance. 1.78 + */ 1.79 + public static PolicyIntersector createLaxPolicyIntersector() { 1.80 + return PolicyIntersector.LAX_INTERSECTOR; 1.81 + } 1.82 + 1.83 + /** 1.84 + * Performs intersection on the input collection of policies and returns the resulting (intersected) policy. If input policy 1.85 + * collection contains only a single policy instance, no intersection is performed and the instance is directly returned 1.86 + * as a method call result. 1.87 + * 1.88 + * @param policies collection of policies to be intersected. Must not be {@code null} nor empty, otherwise exception is thrown. 1.89 + * @return intersected policy as a result of perfromed policy intersection. A {@code null} value is never returned. 1.90 + * 1.91 + * @throws IllegalArgumentException in case {@code policies} argument is either {@code null} or empty collection. 1.92 + */ 1.93 + public Policy intersect(final Policy... policies) { 1.94 + if (policies == null || policies.length == 0) { 1.95 + throw LOGGER.logSevereException(new IllegalArgumentException(LocalizationMessages.WSP_0056_NEITHER_NULL_NOR_EMPTY_POLICY_COLLECTION_EXPECTED())); 1.96 + } else if (policies.length == 1) { 1.97 + return policies[0]; 1.98 + } 1.99 + 1.100 + // check for "null" and "empty" policy: if such policy is found return "null" policy, 1.101 + // or if all policies are "empty", return "empty" policy 1.102 + boolean found = false; 1.103 + boolean allPoliciesEmpty = true; 1.104 + NamespaceVersion latestVersion = null; 1.105 + for (Policy tested : policies) { 1.106 + if (tested.isEmpty()) { 1.107 + found = true; 1.108 + } else { 1.109 + if (tested.isNull()) { 1.110 + found = true; 1.111 + } 1.112 + allPoliciesEmpty = false; 1.113 + } 1.114 + if (latestVersion == null) { 1.115 + latestVersion = tested.getNamespaceVersion(); 1.116 + } else if (latestVersion.compareTo(tested.getNamespaceVersion()) < 0) { 1.117 + latestVersion = tested.getNamespaceVersion(); 1.118 + } 1.119 + 1.120 + if (found && !allPoliciesEmpty) { 1.121 + return Policy.createNullPolicy(latestVersion, null, null); 1.122 + } 1.123 + } 1.124 + latestVersion = (latestVersion != null) ? latestVersion : NamespaceVersion.getLatestVersion(); 1.125 + if (allPoliciesEmpty) { 1.126 + return Policy.createEmptyPolicy(latestVersion, null, null); 1.127 + } 1.128 + 1.129 + // simple tests didn't lead to final answer => let's performe some intersecting ;) 1.130 + final List<AssertionSet> finalAlternatives = new LinkedList<AssertionSet>(policies[0].getContent()); 1.131 + final Queue<AssertionSet> testedAlternatives = new LinkedList<AssertionSet>(); 1.132 + final List<AssertionSet> alternativesToMerge = new ArrayList<AssertionSet>(2); 1.133 + for (int i = 1; i < policies.length; i++) { 1.134 + final Collection<AssertionSet> currentAlternatives = policies[i].getContent(); 1.135 + 1.136 + testedAlternatives.clear(); 1.137 + testedAlternatives.addAll(finalAlternatives); 1.138 + finalAlternatives.clear(); 1.139 + 1.140 + AssertionSet testedAlternative; 1.141 + while ((testedAlternative = testedAlternatives.poll()) != null) { 1.142 + for (AssertionSet currentAlternative : currentAlternatives) { 1.143 + if (testedAlternative.isCompatibleWith(currentAlternative, this.mode)) { 1.144 + alternativesToMerge.add(testedAlternative); 1.145 + alternativesToMerge.add(currentAlternative); 1.146 + finalAlternatives.add(AssertionSet.createMergedAssertionSet(alternativesToMerge)); 1.147 + alternativesToMerge.clear(); 1.148 + } 1.149 + } 1.150 + } 1.151 + } 1.152 + 1.153 + return Policy.createPolicy(latestVersion, null, null, finalAlternatives); 1.154 + } 1.155 +}