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/PolicyMerger.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,106 @@ 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.PolicyUtils; 1.32 +import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.NamespaceVersion; 1.33 +import java.util.ArrayList; 1.34 +import java.util.Collection; 1.35 +import java.util.LinkedList; 1.36 + 1.37 +/** 1.38 + * Merge policies and return the effective policy. 1.39 + * 1.40 + * WS-PolicyAttachment defines a merge algorithm for WSDL 1.1 policy attachments. 1.41 + */ 1.42 +public final class PolicyMerger { 1.43 + private static final PolicyMerger merger = new PolicyMerger(); 1.44 + 1.45 + /** 1.46 + * This private constructor is to avoid direct class instantiation from outsied of the package 1.47 + */ 1.48 + private PolicyMerger() { 1.49 + // nothing to instantiate 1.50 + } 1.51 + 1.52 + /** 1.53 + * Factory method for obtaining thread-safe policy merger instance. 1.54 + * 1.55 + * @return policy merger instance. 1.56 + */ 1.57 + public static PolicyMerger getMerger() { 1.58 + return merger; 1.59 + } 1.60 + 1.61 + /** 1.62 + * Takes collection of policies and merges them into a single policy using algorithm described in 1.63 + * WS-PolicyAttachment specification. None of the original policies in the collection are modified in 1.64 + * any way. 1.65 + * 1.66 + * The newly created policy has an ID that is a concatentation of all merged policy IDs. 1.67 + * 1.68 + * @param policies collection of policies to be merged. The collection must not contain '{@code null}' elements! 1.69 + * @return merged policy containing combination of policy alternatives stored in all input policies. 1.70 + * If provided collection of policies is {@code null} or empty, returns {@code null}. If provided 1.71 + * collection of policies contains only single policy, the policy is returned. 1.72 + */ 1.73 + public Policy merge(final Collection<Policy> policies) { 1.74 + if (policies == null || policies.isEmpty()) { 1.75 + return null; 1.76 + } else if (policies.size() == 1) { 1.77 + return policies.iterator().next(); 1.78 + } 1.79 + 1.80 + final Collection<Collection<AssertionSet>> alternativeSets = new LinkedList<Collection<AssertionSet>>(); 1.81 + final StringBuilder id = new StringBuilder(); 1.82 + NamespaceVersion mergedVersion = policies.iterator().next().getNamespaceVersion(); 1.83 + for (Policy policy : policies) { 1.84 + alternativeSets.add(policy.getContent()); 1.85 + if (mergedVersion.compareTo(policy.getNamespaceVersion()) < 0) { 1.86 + mergedVersion = policy.getNamespaceVersion(); 1.87 + } 1.88 + final String policyId = policy.getId(); 1.89 + if (policyId != null) { 1.90 + if (id.length() > 0) { 1.91 + id.append('-'); 1.92 + } 1.93 + id.append(policyId); 1.94 + } 1.95 + } 1.96 + 1.97 + final Collection<Collection<AssertionSet>> combinedAlternatives = PolicyUtils.Collections.combine(null, alternativeSets, false); 1.98 + 1.99 + if (combinedAlternatives == null || combinedAlternatives.isEmpty()) { 1.100 + return Policy.createNullPolicy(mergedVersion, null, id.length() == 0 ? null : id.toString()); 1.101 + } else { 1.102 + final Collection<AssertionSet> mergedSetList = new ArrayList<AssertionSet>(combinedAlternatives.size()); 1.103 + for (Collection<AssertionSet> toBeMerged : combinedAlternatives) { 1.104 + mergedSetList.add(AssertionSet.createMergedAssertionSet(toBeMerged)); 1.105 + } 1.106 + return Policy.createPolicy(mergedVersion, null, id.length() == 0 ? null : id.toString(), mergedSetList); 1.107 + } 1.108 + } 1.109 +}