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 com.sun.xml.ws.policy.privateutil.LocalizationMessages;
041: import com.sun.xml.ws.policy.spi.PolicyAssertionValidator.Fitness;
042: import java.util.Collection;
043: import java.util.LinkedList;
044:
045: /**
046: * Contains static methods for policy alternative selection. Given policy map is changed so that
047: * each effective policy contains at most one policy alternative. Uses domain
048: * specific @see com.sun.xml.ws.policy.spi.PolicySelector
049: * to find out whether particular policy assertion is actually supported.
050: *
051: * @author Jakub Podlesak (jakub.podlesak at sun.com)
052: */
053: public class EffectiveAlternativeSelector {
054: private enum AlternativeFitness {
055: UNEVALUATED {
056: AlternativeFitness combine(final Fitness assertionFitness) {
057: switch (assertionFitness) {
058: case UNKNOWN:
059: return UNKNOWN;
060: case UNSUPPORTED:
061: return UNSUPPORTED;
062: case SUPPORTED:
063: return SUPPORTED;
064: case INVALID:
065: return INVALID;
066: default:
067: return UNEVALUATED;
068: }
069: }
070: },
071: INVALID {
072: AlternativeFitness combine(final Fitness assertionFitness) {
073: return INVALID;
074: }
075: },
076: UNKNOWN {
077: AlternativeFitness combine(final Fitness assertionFitness) {
078: switch (assertionFitness) {
079: case UNKNOWN:
080: return UNKNOWN;
081: case UNSUPPORTED:
082: return UNSUPPORTED;
083: case SUPPORTED:
084: return PARTIALLY_SUPPORTED;
085: case INVALID:
086: return INVALID;
087: default:
088: return UNEVALUATED;
089: }
090: }
091: },
092: UNSUPPORTED {
093: AlternativeFitness combine(final Fitness assertionFitness) {
094: switch (assertionFitness) {
095: case UNKNOWN:
096: case UNSUPPORTED:
097: return UNSUPPORTED;
098: case SUPPORTED:
099: return PARTIALLY_SUPPORTED;
100: case INVALID:
101: return INVALID;
102: default:
103: return UNEVALUATED;
104: }
105: }
106: },
107: PARTIALLY_SUPPORTED {
108: AlternativeFitness combine(final Fitness assertionFitness) {
109: switch (assertionFitness) {
110: case UNKNOWN:
111: case UNSUPPORTED:
112: case SUPPORTED:
113: return PARTIALLY_SUPPORTED;
114: case INVALID:
115: return INVALID;
116: default:
117: return UNEVALUATED;
118: }
119: }
120: },
121: SUPPORTED_EMPTY {
122: AlternativeFitness combine(final Fitness assertionFitness) {
123: // will not localize - this exception may not occur if there is no programatic error in this class
124: throw new UnsupportedOperationException(
125: "Combine operation was called unexpectedly on 'SUPPORTED_EMPTY' alternative fitness enumeration state.");
126: }
127: },
128: SUPPORTED {
129: AlternativeFitness combine(final Fitness assertionFitness) {
130: switch (assertionFitness) {
131: case UNKNOWN:
132: case UNSUPPORTED:
133: return PARTIALLY_SUPPORTED;
134: case SUPPORTED:
135: return SUPPORTED;
136: case INVALID:
137: return INVALID;
138: default:
139: return UNEVALUATED;
140: }
141: }
142: };
143:
144: abstract AlternativeFitness combine(Fitness assertionFitness);
145: }
146:
147: private static final PolicyLogger LOGGER = PolicyLogger
148: .getLogger(EffectiveAlternativeSelector.class);
149:
150: /**
151: * Does the selection for policy map bound to given modifier
152: *
153: * @param modifier @see EffectivePolicyModifier which the map is bound to
154: */
155: public static final void doSelection(
156: final EffectivePolicyModifier modifier)
157: throws PolicyException {
158: final PolicyMap map = modifier.getMap();
159: final AssertionValidationProcessor validationProcessor = AssertionValidationProcessor
160: .getInstance();
161:
162: for (PolicyMapKey mapKey : map.getAllServiceScopeKeys()) {
163: final Policy oldPolicy = map
164: .getServiceEffectivePolicy(mapKey);
165: modifier.setNewEffectivePolicyForServiceScope(mapKey,
166: selectBestAlternative(oldPolicy,
167: validationProcessor));
168: }
169: for (PolicyMapKey mapKey : map.getAllEndpointScopeKeys()) {
170: final Policy oldPolicy = map
171: .getEndpointEffectivePolicy(mapKey);
172: modifier.setNewEffectivePolicyForEndpointScope(mapKey,
173: selectBestAlternative(oldPolicy,
174: validationProcessor));
175: }
176: for (PolicyMapKey mapKey : map.getAllOperationScopeKeys()) {
177: final Policy oldPolicy = map
178: .getOperationEffectivePolicy(mapKey);
179: modifier.setNewEffectivePolicyForOperationScope(mapKey,
180: selectBestAlternative(oldPolicy,
181: validationProcessor));
182: }
183: for (PolicyMapKey mapKey : map.getAllInputMessageScopeKeys()) {
184: final Policy oldPolicy = map
185: .getInputMessageEffectivePolicy(mapKey);
186: modifier.setNewEffectivePolicyForInputMessageScope(mapKey,
187: selectBestAlternative(oldPolicy,
188: validationProcessor));
189: }
190: for (PolicyMapKey mapKey : map.getAllOutputMessageScopeKeys()) {
191: final Policy oldPolicy = map
192: .getOutputMessageEffectivePolicy(mapKey);
193: modifier.setNewEffectivePolicyForOutputMessageScope(mapKey,
194: selectBestAlternative(oldPolicy,
195: validationProcessor));
196: }
197: for (PolicyMapKey mapKey : map.getAllFaultMessageScopeKeys()) {
198: final Policy oldPolicy = map
199: .getFaultMessageEffectivePolicy(mapKey);
200: modifier.setNewEffectivePolicyForFaultMessageScope(mapKey,
201: selectBestAlternative(oldPolicy,
202: validationProcessor));
203: }
204: }
205:
206: private static Policy selectBestAlternative(final Policy policy,
207: final AssertionValidationProcessor validationProcessor)
208: throws PolicyException {
209: AssertionSet bestAlternative = null;
210: AlternativeFitness bestAlternativeFitness = AlternativeFitness.UNEVALUATED;
211: for (AssertionSet alternative : policy) {
212: AlternativeFitness alternativeFitness = (alternative
213: .isEmpty()) ? AlternativeFitness.SUPPORTED_EMPTY
214: : AlternativeFitness.UNEVALUATED;
215: for (PolicyAssertion assertion : alternative) {
216:
217: final Fitness assertionFitness = validationProcessor
218: .validateClientSide(assertion);
219: switch (assertionFitness) {
220: case UNKNOWN:
221: case UNSUPPORTED:
222: case INVALID:
223: LOGGER.warning(LocalizationMessages
224: .WSP_0075_PROBLEMATIC_ASSERTION_STATE(
225: assertion.getName(),
226: assertionFitness));
227: break;
228: default:
229: break;
230: }
231:
232: alternativeFitness = alternativeFitness
233: .combine(assertionFitness);
234: }
235:
236: if (bestAlternativeFitness.compareTo(alternativeFitness) < 0) {
237: // better alternative found
238: bestAlternative = alternative;
239: bestAlternativeFitness = alternativeFitness;
240: }
241:
242: if (bestAlternativeFitness == AlternativeFitness.SUPPORTED) {
243: // all assertions supported by at least one selector
244: break;
245: }
246: }
247:
248: switch (bestAlternativeFitness) {
249: case INVALID:
250: throw LOGGER
251: .logSevereException(new PolicyException(
252: LocalizationMessages
253: .WSP_0053_INVALID_CLIENT_SIDE_ALTERNATIVE()));
254: case UNKNOWN:
255: case UNSUPPORTED:
256: case PARTIALLY_SUPPORTED:
257: LOGGER
258: .warning(LocalizationMessages
259: .WSP_0019_SUBOPTIMAL_ALTERNATIVE_SELECTED(bestAlternativeFitness));
260: break;
261: default:
262: break;
263: }
264:
265: Collection<AssertionSet> alternativeSet = null;
266: if (bestAlternative != null) {
267: // return a policy containing just the picked alternative
268: alternativeSet = new LinkedList<AssertionSet>();
269: alternativeSet.add(bestAlternative);
270: }
271: return Policy.createPolicy(policy.getName(), policy.getId(),
272: alternativeSet);
273: }
274: }
|