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/Policy.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,661 @@ 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.sourcemodel.wspolicy.NamespaceVersion; 1.32 +import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages; 1.33 +import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; 1.34 +import java.util.Arrays; 1.35 +import java.util.Collection; 1.36 +import java.util.Collections; 1.37 +import java.util.Iterator; 1.38 +import java.util.LinkedList; 1.39 +import java.util.List; 1.40 +import java.util.Set; 1.41 +import java.util.TreeSet; 1.42 +import javax.xml.namespace.QName; 1.43 + 1.44 +/** 1.45 + * A policy represents normalized policy as a wrapper of available policy alternatives represented by 1.46 + * child {@link AssertionSet AssertionSets}. 1.47 + * 1.48 + * @author Fabian Ritzmann, Marek Potociar 1.49 + */ 1.50 +public class Policy implements Iterable<AssertionSet> { 1.51 + /** 1.52 + * A string constant used in package private constructor to customize the object's toString() method output. 1.53 + */ 1.54 + private static final String POLICY_TOSTRING_NAME = "policy"; 1.55 + 1.56 + /** 1.57 + * Constant represents empty list of assertion sets. This represents the content of a 'NULL' policy - a policy with 1.58 + * no alternatives. The constant supports memory effective creation of 'NULL' policy objects. 1.59 + */ 1.60 + private static final List<AssertionSet> NULL_POLICY_ASSERTION_SETS = Collections.unmodifiableList(new LinkedList<AssertionSet>()); 1.61 + 1.62 + /** 1.63 + * Constant represents list of assertion sets with single empty assertion set. This represents the content of 1.64 + * an 'EMPTY' policy - a policy with a single empty alternative. The constant supports memory effective creation 1.65 + * of 'EMPTY' policy objects. 1.66 + */ 1.67 + private static final List<AssertionSet> EMPTY_POLICY_ASSERTION_SETS = Collections.unmodifiableList(new LinkedList<AssertionSet>(Arrays.asList(new AssertionSet[] {AssertionSet.emptyAssertionSet()}))); 1.68 + 1.69 + /** 1.70 + * Constant represents empty vocabulary of a 'NULL' or 'EMPTY' policies. The constant supports memory effective 1.71 + * creation of 'NULL' and 'EMPTY' policy objects. 1.72 + */ 1.73 + private static final Set<QName> EMPTY_VOCABULARY = Collections.unmodifiableSet(new TreeSet<QName>(PolicyUtils.Comparison.QNAME_COMPARATOR)); 1.74 + 1.75 + /** 1.76 + * Constant representation of all 'NULL' policies returned by createNullPolicy() factory method. This is to optimize 1.77 + * the memory footprint. 1.78 + */ 1.79 + private static final Policy ANONYMOUS_NULL_POLICY = new Policy(null, null, NULL_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); 1.80 + 1.81 + /** 1.82 + * Constant representation of all 'EMPTY' policies returned by createEmptyPolicy() factory method. This constant is 1.83 + * to optimize the memory footprint. 1.84 + */ 1.85 + private static final Policy ANONYMOUS_EMPTY_POLICY = new Policy(null, null, EMPTY_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); 1.86 + 1.87 + /** 1.88 + * Policy ID holder 1.89 + */ 1.90 + private String policyId; 1.91 + 1.92 + /** 1.93 + * Policy name holder 1.94 + */ 1.95 + private String name; 1.96 + 1.97 + /** 1.98 + * Namespace version holder 1.99 + */ 1.100 + private NamespaceVersion nsVersion; 1.101 + 1.102 + /** 1.103 + * internal collection of policy alternatives 1.104 + */ 1.105 + private final List<AssertionSet> assertionSets; 1.106 + 1.107 + /** 1.108 + * internal collection of policy vocabulary entries (qualified names of all assertion types present in the policy expression) 1.109 + */ 1.110 + private final Set<QName> vocabulary; 1.111 + 1.112 + /** 1.113 + * immutable version of policy vocabulary that is made available to clients via getter method 1.114 + */ 1.115 + private final Collection<QName> immutableVocabulary; 1.116 + 1.117 + /** 1.118 + * policy object name used in a toString() method. This ensures that Policy class children can customize 1.119 + * (via package private Policy constructors) the toString() method without having to override it. 1.120 + */ 1.121 + private final String toStringName; 1.122 + 1.123 + /** 1.124 + * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'nothing allowed'</emph> 1.125 + * policy expression. The policy is created using the latest namespace version supported. 1.126 + * 1.127 + * @return policy instance which represents a <emph>'nothing allowed'</emph> (no policy alternatives). 1.128 + */ 1.129 + public static Policy createNullPolicy() { 1.130 + return ANONYMOUS_NULL_POLICY; 1.131 + } 1.132 + 1.133 + /** 1.134 + * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'anything allowed'</emph> 1.135 + * policy expression. The policy is created using the latest namespace version supported. 1.136 + * 1.137 + * @return policy instance which represents a <emph>'anything allowed'</emph> (empty policy alternative with no plicy 1.138 + * assertions prescribed). 1.139 + */ 1.140 + public static Policy createEmptyPolicy() { 1.141 + return ANONYMOUS_EMPTY_POLICY; 1.142 + } 1.143 + 1.144 + /** 1.145 + * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'nothing allowed'</emph> 1.146 + * policy expression. The policy is created using the latest namespace version supported. 1.147 + * 1.148 + * @param name global URI of the policy. May be {@code null}. 1.149 + * @param policyId local URI of the policy. May be {@code null}. 1.150 + * @return policy instance which represents a <emph>'nothing allowed'</emph> (no policy alternatives). 1.151 + */ 1.152 + public static Policy createNullPolicy(final String name, final String policyId) { 1.153 + if (name == null && policyId == null) { 1.154 + return ANONYMOUS_NULL_POLICY; 1.155 + } else { 1.156 + return new Policy(name, policyId, NULL_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); 1.157 + } 1.158 + } 1.159 + 1.160 + /** 1.161 + * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'nothing allowed'</emph> 1.162 + * policy expression. The policy is created using the latest namespace version supported. 1.163 + * 1.164 + * @param nsVersion Policy namespace version to be used when marshalling the policy expression 1.165 + * @param name global URI of the policy. May be {@code null}. 1.166 + * @param policyId local URI of the policy. May be {@code null}. 1.167 + * @return policy instance which represents a <emph>'nothing allowed'</emph> (no policy alternatives). 1.168 + */ 1.169 + public static Policy createNullPolicy(final NamespaceVersion nsVersion, final String name, final String policyId) { 1.170 + if ((nsVersion == null || nsVersion == NamespaceVersion.getLatestVersion()) && name == null && policyId == null) { 1.171 + return ANONYMOUS_NULL_POLICY; 1.172 + } else { 1.173 + return new Policy(nsVersion, name, policyId, NULL_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); 1.174 + } 1.175 + } 1.176 + 1.177 + /** 1.178 + * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'anything allowed'</emph> 1.179 + * policy expression. The policy is created using the latest namespace version supported. 1.180 + * 1.181 + * @param name global URI of the policy. May be {@code null}. 1.182 + * @param policyId local URI of the policy. May be {@code null}. 1.183 + * 1.184 + * @return policy instance which represents a <emph>'anything allowed'</emph> (empty policy alternative with no plicy 1.185 + * assertions prescribed). 1.186 + */ 1.187 + public static Policy createEmptyPolicy(final String name, final String policyId) { 1.188 + if (name == null && policyId == null) { 1.189 + return ANONYMOUS_EMPTY_POLICY; 1.190 + } else { 1.191 + return new Policy(name, policyId, EMPTY_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); 1.192 + } 1.193 + } 1.194 + 1.195 + /** 1.196 + * The factory method creates an <b>immutable</b> policy instance which represents a <emph>'anything allowed'</emph> 1.197 + * policy expression. The policy is created using the latest namespace version supported. 1.198 + * 1.199 + * @param nsVersion Policy namespace version to be used when marshalling the policy expression 1.200 + * @param name global URI of the policy. May be {@code null}. 1.201 + * @param policyId local URI of the policy. May be {@code null}. 1.202 + * 1.203 + * @return policy instance which represents a <emph>'anything allowed'</emph> (empty policy alternative with no plicy 1.204 + * assertions prescribed). 1.205 + */ 1.206 + public static Policy createEmptyPolicy(final NamespaceVersion nsVersion, final String name, final String policyId) { 1.207 + if ((nsVersion == null || nsVersion == NamespaceVersion.getLatestVersion()) && name == null && policyId == null) { 1.208 + return ANONYMOUS_EMPTY_POLICY; 1.209 + } else { 1.210 + return new Policy(nsVersion, name, policyId, EMPTY_POLICY_ASSERTION_SETS, EMPTY_VOCABULARY); 1.211 + } 1.212 + } 1.213 + 1.214 + /** 1.215 + * The factory method creates an <b>immutable</b> policy instance which represents a policy expression with 1.216 + * alternatives specified by {@code sets} input parameter. If the collection of policy alternatives is null or empty 1.217 + * an object representing a 'NULL' policy expression is returned. However, in such case it is better to use 1.218 + * {@link #createNullPolicy()} factory method directly. The policy is created using the latest namespace version supported. 1.219 + * 1.220 + * @param sets represents the collection of policy alternatives of the policy object created. During the creation of 1.221 + * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, 1.222 + * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. 1.223 + * 1.224 + * @return policy instance which represents the policy with given alternatives. 1.225 + */ 1.226 + public static Policy createPolicy(final Collection<AssertionSet> sets) { 1.227 + if (sets == null || sets.isEmpty()) { 1.228 + return createNullPolicy(); 1.229 + } else { 1.230 + return new Policy(POLICY_TOSTRING_NAME, sets); 1.231 + } 1.232 + } 1.233 + 1.234 + /** 1.235 + * The factory method creates an <b>immutable</b> policy instance which represents a policy expression with 1.236 + * alternatives specified by {@code sets} input parameter. If the collection of policy alternatives is null or empty 1.237 + * an object representing a 'NULL' policy expression is returned. However, in such case it is better to use 1.238 + * {@link #createNullPolicy(String, String)} factory method directly. The policy is created using the latest namespace version supported. 1.239 + * 1.240 + * @param name global URI of the policy. May be {@code null}. 1.241 + * @param policyId local URI of the policy. May be {@code null}. 1.242 + * @param sets represents the collection of policy alternatives of the policy object created. During the creation of 1.243 + * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, 1.244 + * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. 1.245 + * 1.246 + * @return policy instance which represents the policy with given alternatives. 1.247 + */ 1.248 + public static Policy createPolicy(final String name, final String policyId, final Collection<AssertionSet> sets) { 1.249 + if (sets == null || sets.isEmpty()) { 1.250 + return createNullPolicy(name, policyId); 1.251 + } else { 1.252 + return new Policy(POLICY_TOSTRING_NAME, name, policyId, sets); 1.253 + } 1.254 + } 1.255 + 1.256 + /** 1.257 + * The factory method creates an <b>immutable</b> policy instance which represents a policy expression with 1.258 + * alternatives specified by {@code sets} input parameter. If the collection of policy alternatives is null or empty 1.259 + * an object representing a 'NULL' policy expression is returned. However, in such case it is better to use 1.260 + * {@link #createNullPolicy(String, String)} factory method directly. The policy is created using the latest namespace version supported. 1.261 + * 1.262 + * @param nsVersion Policy namespace version to be used when marshalling the policy expression 1.263 + * @param name global URI of the policy. May be {@code null}. 1.264 + * @param policyId local URI of the policy. May be {@code null}. 1.265 + * @param sets represents the collection of policy alternatives of the policy object created. During the creation of 1.266 + * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, 1.267 + * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. 1.268 + * 1.269 + * @return policy instance which represents the policy with given alternatives. 1.270 + */ 1.271 + public static Policy createPolicy(NamespaceVersion nsVersion, final String name, final String policyId, final Collection<AssertionSet> sets) { 1.272 + if (sets == null || sets.isEmpty()) { 1.273 + return createNullPolicy(nsVersion, name, policyId); 1.274 + } else { 1.275 + return new Policy(nsVersion, POLICY_TOSTRING_NAME, name, policyId, sets); 1.276 + } 1.277 + } 1.278 + 1.279 + /** 1.280 + * A most flexible policy object constructor that allows private creation of policy objects and direct setting 1.281 + * of all its attributes. 1.282 + * 1.283 + * @param name global URI of the policy. May be {@code null}. 1.284 + * @param policyId local URI of the policy. May be {@code null}. 1.285 + * @param assertionSets represents the collection of policy alternatives of the policy object created. The list is directly 1.286 + * assigned to the policy object internal attribute. Subsequent manipulations on the collection must be handled with 1.287 + * care. 1.288 + * @param vocabulary represents the vocabulary of the policy object. Subsequent manipulations on the collection 1.289 + * must be handled with care. 1.290 + */ 1.291 + private Policy(final String name, final String policyId, final List<AssertionSet> assertionSets, final Set<QName> vocabulary) { 1.292 + this.nsVersion = NamespaceVersion.getLatestVersion(); 1.293 + this.toStringName = POLICY_TOSTRING_NAME; 1.294 + this.name = name; 1.295 + this.policyId = policyId; 1.296 + this.assertionSets = assertionSets; 1.297 + this.vocabulary = vocabulary; 1.298 + this.immutableVocabulary = Collections.unmodifiableCollection(this.vocabulary); 1.299 + } 1.300 + 1.301 + /** 1.302 + * Constructor that should be overridden by child implementation. The constructor allows for easy toString() output 1.303 + * customization. 1.304 + * 1.305 + * @param toStringName a general name of the object (such as 'policy' or 'nested policy') that will be used in the 1.306 + * toString() method to identify the object. 1.307 + * @param sets represents the collection of policy alternatives of the policy object created. During the creation of 1.308 + * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, 1.309 + * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. The 1.310 + * collection may be {@code null} or empty. In such case a 'NULL' policy object is constructed. 1.311 + */ 1.312 + Policy(final String toStringName, final Collection<AssertionSet> sets) { 1.313 + this.nsVersion = NamespaceVersion.getLatestVersion(); 1.314 + this.toStringName = toStringName; 1.315 + 1.316 + if (sets == null || sets.isEmpty()) { 1.317 + this.assertionSets = NULL_POLICY_ASSERTION_SETS; 1.318 + this.vocabulary = EMPTY_VOCABULARY; 1.319 + this.immutableVocabulary = EMPTY_VOCABULARY; 1.320 + } else { 1.321 + this.assertionSets = new LinkedList<AssertionSet>(); 1.322 + this.vocabulary = new TreeSet<QName>(PolicyUtils.Comparison.QNAME_COMPARATOR); 1.323 + this.immutableVocabulary = Collections.unmodifiableCollection(this.vocabulary); 1.324 + 1.325 + addAll(sets); 1.326 + } 1.327 + } 1.328 + 1.329 + /** 1.330 + * Constructor that should be overridden by child implementation. The constructor allows for easy toString() output 1.331 + * customization. 1.332 + * 1.333 + * @param toStringName a general name of the object (such as 'policy' or 'nested policy') that will be used in the 1.334 + * toString() method to identify the object. 1.335 + * @param name global URI of the policy. May be {@code null}. 1.336 + * @param policyId local URI of the policy. May be {@code null}. 1.337 + * @param sets represents the collection of policy alternatives of the policy object created. During the creation of 1.338 + * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, 1.339 + * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. The 1.340 + * collection may be {@code null} or empty. In such case a 'NULL' policy object is constructed. 1.341 + */ 1.342 + Policy(final String toStringName, final String name, final String policyId, final Collection<AssertionSet> sets) { 1.343 + this(toStringName, sets); 1.344 + this.name = name; 1.345 + this.policyId = policyId; 1.346 + } 1.347 + 1.348 + /** 1.349 + * A most flexible policy object constructor that allows private creation of policy objects and direct setting 1.350 + * of all its attributes. 1.351 + * 1.352 + * @param nsVersion Policy namespace version to be used when marshalling the policy expression 1.353 + * @param name global URI of the policy. May be {@code null}. 1.354 + * @param policyId local URI of the policy. May be {@code null}. 1.355 + * @param assertionSets represents the collection of policy alternatives of the policy object created. The list is directly 1.356 + * assigned to the policy object internal attribute. Subsequent manipulations on the collection must be handled with 1.357 + * care. 1.358 + * @param vocabulary represents the vocabulary of the policy object. Subsequent manipulations on the collection 1.359 + * must be handled with care. 1.360 + */ 1.361 + private Policy(final NamespaceVersion nsVersion, final String name, final String policyId, final List<AssertionSet> assertionSets, final Set<QName> vocabulary) { 1.362 + this.nsVersion = nsVersion; 1.363 + this.toStringName = POLICY_TOSTRING_NAME; 1.364 + this.name = name; 1.365 + this.policyId = policyId; 1.366 + this.assertionSets = assertionSets; 1.367 + this.vocabulary = vocabulary; 1.368 + this.immutableVocabulary = Collections.unmodifiableCollection(this.vocabulary); 1.369 + } 1.370 + 1.371 + /** 1.372 + * Constructor that should be overridden by child implementation. The constructor allows for easy toString() output 1.373 + * customization. 1.374 + * 1.375 + * @param nsVersion Policy namespace version to be used when marshalling the policy expression 1.376 + * @param toStringName a general name of the object (such as 'policy' or 'nested policy') that will be used in the 1.377 + * toString() method to identify the object. 1.378 + * @param sets represents the collection of policy alternatives of the policy object created. During the creation of 1.379 + * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, 1.380 + * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. The 1.381 + * collection may be {@code null} or empty. In such case a 'NULL' policy object is constructed. 1.382 + */ 1.383 + Policy(final NamespaceVersion nsVersion, final String toStringName, final Collection<AssertionSet> sets) { 1.384 + this.nsVersion = nsVersion; 1.385 + this.toStringName = toStringName; 1.386 + 1.387 + if (sets == null || sets.isEmpty()) { 1.388 + this.assertionSets = NULL_POLICY_ASSERTION_SETS; 1.389 + this.vocabulary = EMPTY_VOCABULARY; 1.390 + this.immutableVocabulary = EMPTY_VOCABULARY; 1.391 + } else { 1.392 + this.assertionSets = new LinkedList<AssertionSet>(); 1.393 + this.vocabulary = new TreeSet<QName>(PolicyUtils.Comparison.QNAME_COMPARATOR); 1.394 + this.immutableVocabulary = Collections.unmodifiableCollection(this.vocabulary); 1.395 + 1.396 + addAll(sets); 1.397 + } 1.398 + } 1.399 + 1.400 + /** 1.401 + * Constructor that should be overridden by child implementation. The constructor allows for easy toString() output 1.402 + * customization. 1.403 + * 1.404 + * @param nsVersion Policy namespace version to be used when marshalling the policy expression 1.405 + * @param toStringName a general name of the object (such as 'policy' or 'nested policy') that will be used in the 1.406 + * toString() method to identify the object. 1.407 + * @param name global URI of the policy. May be {@code null}. 1.408 + * @param policyId local URI of the policy. May be {@code null}. 1.409 + * @param sets represents the collection of policy alternatives of the policy object created. During the creation of 1.410 + * the new policy object, the content of the alternatives collection is copied into an internal policy object structure, 1.411 + * thus any subsequent operations on the collection will have no impact on the newly constructed policy object. The 1.412 + * collection may be {@code null} or empty. In such case a 'NULL' policy object is constructed. 1.413 + */ 1.414 + Policy(final NamespaceVersion nsVersion, final String toStringName, final String name, final String policyId, final Collection<AssertionSet> sets) { 1.415 + this(nsVersion, toStringName, sets); 1.416 + this.name = name; 1.417 + this.policyId = policyId; 1.418 + } 1.419 + 1.420 + /** 1.421 + * Adds single alternative to the internal alternatives set of the policy object. 1.422 + * 1.423 + * @param set assertion set (policy alternative) object to be added. May be {@code null}; in such case the method 1.424 + * returns false. 1.425 + * 1.426 + * @return {@code true} or {@code false} depending on whether the new alternative was added to the policy object or not. 1.427 + */ 1.428 + private boolean add(final AssertionSet set) { 1.429 + if (set == null) { 1.430 + return false; 1.431 + } 1.432 + 1.433 + if (this.assertionSets.contains(set)) { 1.434 + return false; 1.435 + } else { 1.436 + this.assertionSets.add(set); 1.437 + this.vocabulary.addAll(set.getVocabulary()); 1.438 + return true; 1.439 + } 1.440 + } 1.441 + 1.442 + /** 1.443 + * Adds all alternatives from the input collection of assertion sets to the policy object's internal set of alternatives. 1.444 + * The policy object's vocabulary structure is updated as well. 1.445 + * 1.446 + * @param sets collection of new alternatives. Must NOT be {@code null} or empty. The check for null or empty input 1.447 + * parameter is performed with help of {@code assert} keyword, thus during the testing and development of this class 1.448 + * {@code -ea} switch should be turned on for this class. 1.449 + * 1.450 + * @return {@code true} if all elements in the input collection were added to internal structure, {@code false} otherwise. 1.451 + */ 1.452 + private boolean addAll(final Collection<AssertionSet> sets) { 1.453 + assert (sets != null && !sets.isEmpty()) : LocalizationMessages.WSP_0036_PRIVATE_METHOD_DOES_NOT_ACCEPT_NULL_OR_EMPTY_COLLECTION(); 1.454 + 1.455 + boolean result = true; 1.456 + for (AssertionSet set : sets) { 1.457 + result &= add(set); // this is here to ensure that vocabulary is built correctly as well 1.458 + } 1.459 + Collections.sort(this.assertionSets); 1.460 + 1.461 + return result; 1.462 + } 1.463 + 1.464 + Collection<AssertionSet> getContent() { 1.465 + return assertionSets; 1.466 + } 1.467 + 1.468 + /** 1.469 + * Returns the policy identifier that serves as a local relative policy URI. 1.470 + * 1.471 + * @return policy identifier - a local relative policy URI. If no policy identifier is set, returns {@code null}. 1.472 + */ 1.473 + public String getId() { 1.474 + return policyId; 1.475 + } 1.476 + 1.477 + /** 1.478 + * Returns the policy name that serves as a global policy URI. 1.479 + * 1.480 + * @return policy name - a global policy URI. If no policy name is set, returns {@code null}. 1.481 + */ 1.482 + public String getName() { 1.483 + return name; 1.484 + } 1.485 + 1.486 + public NamespaceVersion getNamespaceVersion() { 1.487 + return nsVersion; 1.488 + } 1.489 + 1.490 + /** 1.491 + * Returns the policy ID or if that is null the policy name. May return null 1.492 + * if both attributes are null. 1.493 + * 1.494 + * @see #getId() 1.495 + * @see #getName() 1.496 + * @return The policy ID if it was set, or the name or null if no attribute was set. 1.497 + */ 1.498 + public String getIdOrName() { 1.499 + if (policyId != null) { 1.500 + return policyId; 1.501 + } 1.502 + return name; 1.503 + } 1.504 + 1.505 + /** 1.506 + * Method returns how many policy alternatives this policy instance contains. 1.507 + * 1.508 + * @return number of policy alternatives contained in this policy instance 1.509 + */ 1.510 + public int getNumberOfAssertionSets() { 1.511 + return assertionSets.size(); 1.512 + } 1.513 + 1.514 + /** 1.515 + * A policy usually contains one or more assertion sets. Each assertion set 1.516 + * corresponds to a policy alternative as defined by WS-Policy. 1.517 + * 1.518 + * @return An iterator to iterate through all contained assertion sets 1.519 + */ 1.520 + public Iterator<AssertionSet> iterator() { 1.521 + return assertionSets.iterator(); 1.522 + } 1.523 + 1.524 + /** 1.525 + * Returns {@code true} if the policy instance represents "nothing allowed" policy expression 1.526 + * 1.527 + * @return {@code true} if the policy instance represents "nothing allowed" policy expression, {@code false} otherwise. 1.528 + */ 1.529 + public boolean isNull() { 1.530 + return assertionSets.size() == 0; 1.531 + } 1.532 + 1.533 + /** 1.534 + * Returns {@code true} if the policy instance represents "anything allowed" policy expression 1.535 + * 1.536 + * @return {@code true} if the policy instance represents "anything allowed" policy expression, {@code false} otherwise. 1.537 + */ 1.538 + public boolean isEmpty() { 1.539 + return assertionSets.size() == 1 && assertionSets.get(0).isEmpty(); 1.540 + } 1.541 + 1.542 + /** 1.543 + * Returns true if the policy contains the assertion names with specified namespace in its vocabulary 1.544 + * 1.545 + * @param namespaceUri the assertion namespace URI (identifying assertion domain) 1.546 + * @return {@code true}, if an assertion with the given name could be found in the policy vocabulary {@code false} otherwise. 1.547 + */ 1.548 + public boolean contains(final String namespaceUri) { 1.549 + for (QName entry : vocabulary) { 1.550 + if (entry.getNamespaceURI().equals(namespaceUri)) { 1.551 + return true; 1.552 + } 1.553 + } 1.554 + 1.555 + return false; 1.556 + } 1.557 + 1.558 + /** 1.559 + * Retrieves the vocabulary of this policy expression. The vocabulary is represented by an immutable collection of 1.560 + * unique QName objects. Each of those objects represents single assertion type contained in the policy. 1.561 + * 1.562 + * @return immutable collection of assertion types contained in the policy (a policy vocabulary). 1.563 + */ 1.564 + public Collection<QName> getVocabulary() { 1.565 + return immutableVocabulary; 1.566 + } 1.567 + 1.568 + /** 1.569 + * Determines if the policy instance contains the assertion with the name specified in its vocabulary. 1.570 + * 1.571 + * @param assertionName the name of the assertion to be tested. 1.572 + * 1.573 + * @return {@code true} if the assertion with the specified name is part of the policy instance's vocabulary, 1.574 + * {@code false} otherwise. 1.575 + */ 1.576 + public boolean contains(final QName assertionName) { 1.577 + return vocabulary.contains(assertionName); 1.578 + } 1.579 + 1.580 + /** 1.581 + * An {@code Object.equals(Object obj)} method override. 1.582 + */ 1.583 + @Override 1.584 + public boolean equals(final Object obj) { 1.585 + if (this == obj) { 1.586 + return true; 1.587 + } 1.588 + 1.589 + if (!(obj instanceof Policy)) { 1.590 + return false; 1.591 + } 1.592 + 1.593 + final Policy that = (Policy) obj; 1.594 + 1.595 + boolean result = true; 1.596 + 1.597 + result = result && this.vocabulary.equals(that.vocabulary); 1.598 + result = result && this.assertionSets.size() == that.assertionSets.size() && this.assertionSets.containsAll(that.assertionSets); 1.599 + 1.600 + return result; 1.601 + } 1.602 + 1.603 + /** 1.604 + * An {@code Object.hashCode()} method override. 1.605 + */ 1.606 + @Override 1.607 + public int hashCode() { 1.608 + int result = 17; 1.609 + 1.610 + result = 37 * result + vocabulary.hashCode(); 1.611 + result = 37 * result + assertionSets.hashCode(); 1.612 + 1.613 + return result; 1.614 + } 1.615 + 1.616 + /** 1.617 + * An {@code Object.toString()} method override. 1.618 + */ 1.619 + @Override 1.620 + public String toString() { 1.621 + return toString(0, new StringBuffer()).toString(); 1.622 + } 1.623 + 1.624 + /** 1.625 + * A helper method that appends indented string representation of this instance to the input string buffer. 1.626 + * 1.627 + * @param indentLevel indentation level to be used. 1.628 + * @param buffer buffer to be used for appending string representation of this instance 1.629 + * @return modified buffer containing new string representation of the instance 1.630 + */ 1.631 + StringBuffer toString(final int indentLevel, final StringBuffer buffer) { 1.632 + final String indent = PolicyUtils.Text.createIndent(indentLevel); 1.633 + final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1); 1.634 + final String innerDoubleIndent = PolicyUtils.Text.createIndent(indentLevel + 2); 1.635 + 1.636 + buffer.append(indent).append(toStringName).append(" {").append(PolicyUtils.Text.NEW_LINE); 1.637 + buffer.append(innerIndent).append("namespace version = '").append(nsVersion.name()).append('\'').append(PolicyUtils.Text.NEW_LINE); 1.638 + buffer.append(innerIndent).append("id = '").append(policyId).append('\'').append(PolicyUtils.Text.NEW_LINE); 1.639 + buffer.append(innerIndent).append("name = '").append(name).append('\'').append(PolicyUtils.Text.NEW_LINE); 1.640 + 1.641 + buffer.append(innerIndent).append("vocabulary {").append(PolicyUtils.Text.NEW_LINE); 1.642 + if (vocabulary.isEmpty()) { 1.643 + buffer.append(innerDoubleIndent).append("no entries").append(PolicyUtils.Text.NEW_LINE); 1.644 + } else { 1.645 + int index = 1; 1.646 + for (QName entry : vocabulary) { 1.647 + buffer.append(innerDoubleIndent).append(index++).append(". entry = '").append(entry.getNamespaceURI()).append(':').append(entry.getLocalPart()).append('\'').append(PolicyUtils.Text.NEW_LINE); 1.648 + } 1.649 + } 1.650 + buffer.append(innerIndent).append('}').append(PolicyUtils.Text.NEW_LINE); 1.651 + 1.652 + if (assertionSets.isEmpty()) { 1.653 + buffer.append(innerIndent).append("no assertion sets").append(PolicyUtils.Text.NEW_LINE); 1.654 + } else { 1.655 + for (AssertionSet set : assertionSets) { 1.656 + set.toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE); 1.657 + } 1.658 + } 1.659 + 1.660 + buffer.append(indent).append('}'); 1.661 + 1.662 + return buffer; 1.663 + } 1.664 +}