001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.policy;
038:
039: import com.sun.xml.ws.policy.privateutil.PolicyLogger;
040: import java.util.Collection;
041: import java.util.LinkedList;
042: import java.util.List;
043: import java.util.Queue;
044: import com.sun.xml.ws.policy.privateutil.LocalizationMessages;
045: import java.util.ArrayList;
046:
047: /**
048: * The instance of this class is intended to provide policy intersection mechanism.
049: *
050: * @author Marek Potociar (marek.potociar@sun.com)
051: */
052: public final class PolicyIntersector {
053: static enum CompatibilityMode {
054: STRICT, LAX
055: }
056:
057: private static final PolicyIntersector STRICT_INTERSECTOR = new PolicyIntersector(
058: CompatibilityMode.STRICT);
059: private static final PolicyIntersector LAX_INTERSECTOR = new PolicyIntersector(
060: CompatibilityMode.LAX);
061: private static final PolicyLogger LOGGER = PolicyLogger
062: .getLogger(PolicyIntersector.class);
063:
064: private CompatibilityMode mode;
065:
066: /**
067: * Prevents direct instantiation of this class from outside
068: * @param intersectionMode intersection mode
069: */
070: private PolicyIntersector(CompatibilityMode intersectionMode) {
071: this .mode = intersectionMode;
072: }
073:
074: /**
075: * Returns a strict policy intersector that can be used to intersect group of policies.
076: *
077: * @return policy intersector instance.
078: */
079: public static PolicyIntersector createStrictPolicyIntersector() {
080: return PolicyIntersector.STRICT_INTERSECTOR;
081: }
082:
083: /**
084: * Returns a strict policy intersector that can be used to intersect group of policies.
085: *
086: * @return policy intersector instance.
087: */
088: public static PolicyIntersector createLaxPolicyIntersector() {
089: return PolicyIntersector.LAX_INTERSECTOR;
090: }
091:
092: /**
093: * Performs intersection on the input collection of policies and returns the resulting (intersected) policy. If input policy
094: * collection contains only a single policy instance, no intersection is performed and the instance is directly returned
095: * as a method call result.
096: *
097: * @param policies collection of policies to be intersected. Must not be {@code null} nor empty, otherwise exception is thrown.
098: * @return intersected policy as a result of perfromed policy intersection. A {@code null} value is never returned.
099: *
100: * @throws IllegalArgumentException in case {@code policies} argument is either {@code null} or empty collection.
101: */
102: public Policy intersect(final Policy... policies) {
103: if (policies == null || policies.length == 0) {
104: throw LOGGER
105: .logSevereException(new IllegalArgumentException(
106: LocalizationMessages
107: .WSP_0056_NEITHER_NULL_NOR_EMPTY_POLICY_COLLECTION_EXPECTED()));
108: } else if (policies.length == 1) {
109: return policies[0];
110: }
111:
112: // check for "null" and "empty" policy: if such policy is found return "null" policy,
113: // or if all policies are "empty", return "empty" policy
114: boolean found = false;
115: boolean allPoliciesEmpty = true;
116: for (Policy tested : policies) {
117: if (tested.isEmpty()) {
118: found = true;
119: } else {
120: if (tested.isNull()) {
121: found = true;
122: }
123: allPoliciesEmpty = false;
124: }
125:
126: if (found && !allPoliciesEmpty) {
127: return Policy.createNullPolicy();
128: }
129: }
130: if (allPoliciesEmpty) {
131: return Policy.createEmptyPolicy();
132: }
133:
134: // simple tests didn't lead to final answer => let's performe some intersecting ;)
135: final List<AssertionSet> finalAlternatives = new LinkedList<AssertionSet>(
136: policies[0].getContent());
137: final Queue<AssertionSet> testedAlternatives = new LinkedList<AssertionSet>();
138: final List<AssertionSet> alternativesToMerge = new ArrayList<AssertionSet>(
139: 2);
140: for (int i = 1; i < policies.length; i++) {
141: final Collection<AssertionSet> currentAlternatives = policies[i]
142: .getContent();
143:
144: testedAlternatives.clear();
145: testedAlternatives.addAll(finalAlternatives);
146: finalAlternatives.clear();
147:
148: AssertionSet testedAlternative;
149: while ((testedAlternative = testedAlternatives.poll()) != null) {
150: for (AssertionSet currentAlternative : currentAlternatives) {
151: if (testedAlternative.isCompatibleWith(
152: currentAlternative, this.mode)) {
153: alternativesToMerge.add(testedAlternative);
154: alternativesToMerge.add(currentAlternative);
155: finalAlternatives
156: .add(AssertionSet
157: .createMergedAssertionSet(alternativesToMerge));
158: alternativesToMerge.clear();
159: }
160: }
161: }
162: }
163:
164: return Policy.createPolicy(finalAlternatives);
165: }
166: }
|