src/share/jaxws_classes/com/sun/xml/internal/ws/policy/PolicyAssertion.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.ws.policy;
aoqi@0 27
aoqi@0 28 import java.util.Collection;
aoqi@0 29 import java.util.Iterator;
aoqi@0 30 import java.util.Map;
aoqi@0 31 import java.util.Set;
aoqi@0 32 import javax.xml.namespace.QName;
aoqi@0 33 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
aoqi@0 34 import com.sun.xml.internal.ws.policy.sourcemodel.AssertionData;
aoqi@0 35 import com.sun.xml.internal.ws.policy.sourcemodel.ModelNode;
aoqi@0 36
aoqi@0 37 /**
aoqi@0 38 * Base class for any policy assertion implementations. It defines the common
aoqi@0 39 * interface and provides some default implentation for common policy assertion
aoqi@0 40 * functionality.
aoqi@0 41 * <p/>
aoqi@0 42 * NOTE: Assertion implementers should not extend this class directly. {@link SimpleAssertion}
aoqi@0 43 * or {@link ComplexAssertion} should be used as a base class instead.
aoqi@0 44 *
aoqi@0 45 * @author Marek Potociar (marek.potociar at sun.com)
aoqi@0 46 * @author Fabian Ritzmann
aoqi@0 47 */
aoqi@0 48 public abstract class PolicyAssertion {
aoqi@0 49
aoqi@0 50 private final AssertionData data;
aoqi@0 51 private AssertionSet parameters;
aoqi@0 52 private NestedPolicy nestedPolicy; // TODO: remove
aoqi@0 53
aoqi@0 54 protected PolicyAssertion() {
aoqi@0 55 this.data = AssertionData.createAssertionData(null);
aoqi@0 56 this.parameters = AssertionSet.createAssertionSet(null);
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * Creates generic assertionand stores the data specified in input parameters
aoqi@0 61 *
aoqi@0 62 * @param assertionData assertion creation data specifying the details of newly created assertion. May be {@code null}.
aoqi@0 63 * @param assertionParameters collection of assertions parameters of this policy assertion. May be {@code null}.
aoqi@0 64 * @param nestedAlternative assertion set specifying nested policy alternative. May be {@code null}.
aoqi@0 65 *
aoqi@0 66 * @deprecated Non-abstract assertion types should derive from {@link SimpleAssertion}
aoqi@0 67 * or {@link ComplexAssertion} instead. {@link Policy} class will not provide support for
aoqi@0 68 * nested policy alternatives in the future. This responsibility is delegated to
aoqi@0 69 * {@link ComplexAssertion} class instead.
aoqi@0 70 */
aoqi@0 71 @Deprecated
aoqi@0 72 protected PolicyAssertion(final AssertionData assertionData, final Collection<? extends PolicyAssertion> assertionParameters, final AssertionSet nestedAlternative) {
aoqi@0 73 this.data = assertionData;
aoqi@0 74 if (nestedAlternative != null) {
aoqi@0 75 this.nestedPolicy = NestedPolicy.createNestedPolicy(nestedAlternative);
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 this.parameters = AssertionSet.createAssertionSet(assertionParameters);
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * Creates generic assertionand stores the data specified in input parameters
aoqi@0 83 *
aoqi@0 84 * @param assertionData assertion creation data specifying the details of newly created assertion
aoqi@0 85 * @param assertionParameters collection of assertions parameters of this policy assertion. May be {@code null}.
aoqi@0 86 */
aoqi@0 87 protected PolicyAssertion(final AssertionData assertionData, final Collection<? extends PolicyAssertion> assertionParameters) {
aoqi@0 88 if (assertionData == null) {
aoqi@0 89 this.data = AssertionData.createAssertionData(null);
aoqi@0 90 } else {
aoqi@0 91 this.data = assertionData;
aoqi@0 92 }
aoqi@0 93 this.parameters = AssertionSet.createAssertionSet(assertionParameters);
aoqi@0 94 }
aoqi@0 95
aoqi@0 96 /**
aoqi@0 97 * Returns the fully qualified name of the assertion.
aoqi@0 98 *
aoqi@0 99 * @return assertion's fully qualified name.
aoqi@0 100 */
aoqi@0 101 public final QName getName() {
aoqi@0 102 return data.getName();
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 /**
aoqi@0 106 * Returns the value of the assertion - the character data content contained in the assertion element representation.
aoqi@0 107 *
aoqi@0 108 * @return assertion's value. May return {@code null} if there is no value set for the assertion.
aoqi@0 109 */
aoqi@0 110 public final String getValue() {
aoqi@0 111 return data.getValue();
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 /**
aoqi@0 115 * Method specifies whether the assertion is otpional or not.
aoqi@0 116 * <p/>
aoqi@0 117 * This is a default implementation that may be overriden. The method returns {@code true} if the {@code wsp:optional} attribute
aoqi@0 118 * is present on the assertion and its value is {@code 'true'}. Otherwise the method returns {@code false}.
aoqi@0 119 *
aoqi@0 120 * @return {@code 'true'} if the assertion is optional. Returns {@code false} otherwise.
aoqi@0 121 */
aoqi@0 122 public boolean isOptional() {
aoqi@0 123 return data.isOptionalAttributeSet();
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 /**
aoqi@0 127 * Method specifies whether the assertion is ignorable or not.
aoqi@0 128 * <p/>
aoqi@0 129 * This is a default implementation that may be overriden. The method returns {@code true} if the {@code wsp:Ignorable} attribute
aoqi@0 130 * is present on the assertion and its value is {@code 'true'}. Otherwise the method returns {@code false}.
aoqi@0 131 *
aoqi@0 132 * @return {@code 'true'} if the assertion is ignorable. Returns {@code false} otherwise.
aoqi@0 133 */
aoqi@0 134 public boolean isIgnorable() {
aoqi@0 135 return data.isIgnorableAttributeSet();
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 /**
aoqi@0 139 * Method specifies whether the assertion is private or not. This is specified by our proprietary visibility element.
aoqi@0 140 *
aoqi@0 141 * @return {@code 'true'} if the assertion is marked as private (i.e. should not be marshalled int generated WSDL documents). Returns {@code false} otherwise.
aoqi@0 142 */
aoqi@0 143 public final boolean isPrivate() {
aoqi@0 144 return data.isPrivateAttributeSet();
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 /**
aoqi@0 148 * Returns the disconnected set of attributes attached to the assertion. Each attribute is represented as a single
aoqi@0 149 * {@code Map.Entry<attributeName, attributeValue>} element.
aoqi@0 150 * <p/>
aoqi@0 151 * 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is
aoqi@0 152 * also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's
aoqi@0 153 * attributes.
aoqi@0 154 *
aoqi@0 155 * @return disconected set of attributes attached to the assertion.
aoqi@0 156 */
aoqi@0 157 public final Set<Map.Entry<QName, String>> getAttributesSet() {
aoqi@0 158 return data.getAttributesSet();
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 /**
aoqi@0 162 * Returns the disconnected map of attributes attached to the assertion.
aoqi@0 163 * <p/>
aoqi@0 164 * 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is
aoqi@0 165 * also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's
aoqi@0 166 * attributes.
aoqi@0 167 *
aoqi@0 168 * @return disconnected map of attributes attached to the assertion.
aoqi@0 169 */
aoqi@0 170 public final Map<QName, String> getAttributes() {
aoqi@0 171 return data.getAttributes();
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 /**
aoqi@0 175 * Returns the value of an attribute. Returns null if an attribute with the given name does not exist.
aoqi@0 176 *
aoqi@0 177 * @param name The fully qualified name of the attribute
aoqi@0 178 * @return The value of the attribute. Returns {@code null} if there is no such attribute or if it's value is null.
aoqi@0 179 */
aoqi@0 180 public final String getAttributeValue(final QName name) {
aoqi@0 181 return data.getAttributeValue(name);
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 /**
aoqi@0 185 * Returns the boolean information whether this assertion contains any parameters.
aoqi@0 186 *
aoqi@0 187 * @return {@code true} if the assertion contains parameters. Returns {@code false} otherwise.
aoqi@0 188 *
aoqi@0 189 * @deprecated Use hasParameters() instead
aoqi@0 190 */
aoqi@0 191 @Deprecated
aoqi@0 192 public final boolean hasNestedAssertions() {
aoqi@0 193 // TODO: remove
aoqi@0 194 return !parameters.isEmpty();
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 /**
aoqi@0 198 * Returns the boolean information whether this assertion contains any parameters.
aoqi@0 199 *
aoqi@0 200 * @return {@code true} if the assertion contains parameters. Returns {@code false} otherwise.
aoqi@0 201 */
aoqi@0 202 public final boolean hasParameters() {
aoqi@0 203 return !parameters.isEmpty();
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 /**
aoqi@0 207 * Returns the assertion's parameter collection iterator.
aoqi@0 208 *
aoqi@0 209 * @return the assertion's parameter collection iterator.
aoqi@0 210 *
aoqi@0 211 * @deprecated Use getNestedParametersIterator() instead
aoqi@0 212 */
aoqi@0 213 @Deprecated
aoqi@0 214 public final Iterator<PolicyAssertion> getNestedAssertionsIterator() {
aoqi@0 215 // TODO: remove
aoqi@0 216 return parameters.iterator();
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 /**
aoqi@0 220 * Returns the assertion's parameter collection iterator.
aoqi@0 221 *
aoqi@0 222 * @return the assertion's parameter collection iterator.
aoqi@0 223 */
aoqi@0 224 public final Iterator<PolicyAssertion> getParametersIterator() {
aoqi@0 225 return parameters.iterator();
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 boolean isParameter() {
aoqi@0 229 return data.getNodeType() == ModelNode.Type.ASSERTION_PARAMETER_NODE;
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 /**
aoqi@0 233 * Returns the boolean information whether this assertion contains nested policy.
aoqi@0 234 *
aoqi@0 235 * @return {@code true} if the assertion contains child (nested) policy. Returns {@code false} otherwise.
aoqi@0 236 */
aoqi@0 237 public boolean hasNestedPolicy() {
aoqi@0 238 // TODO: make abstract
aoqi@0 239 return getNestedPolicy() != null;
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 /**
aoqi@0 243 * Returns the nested policy if any.
aoqi@0 244 *
aoqi@0 245 * @return the nested policy if the assertion contains a nested policy. Returns {@code null} otherwise.
aoqi@0 246 */
aoqi@0 247 public NestedPolicy getNestedPolicy() {
aoqi@0 248 // TODO: make abstract
aoqi@0 249 return nestedPolicy;
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 /**
aoqi@0 253 * Casts the assertion to the implementation type. Returns null if that is not
aoqi@0 254 * possible.
aoqi@0 255 *
aoqi@0 256 * @param <T> The implementation type of the assertion.
aoqi@0 257 * @param type The implementation type of the assertion. May not be null.
aoqi@0 258 * @return The instance of the implementation type. Null otherwise.
aoqi@0 259 */
aoqi@0 260 public <T extends PolicyAssertion> T getImplementation(Class<T> type) {
aoqi@0 261 if (type.isAssignableFrom(this.getClass())) {
aoqi@0 262 return type.cast(this);
aoqi@0 263 }
aoqi@0 264 else {
aoqi@0 265 return null;
aoqi@0 266 }
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 /**
aoqi@0 270 * An {@code Object.toString()} method override.
aoqi@0 271 */
aoqi@0 272 @Override
aoqi@0 273 public String toString() {
aoqi@0 274 return toString(0, new StringBuffer()).toString();
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 /**
aoqi@0 278 * A helper method that appends indented string representation of this instance to the input string buffer.
aoqi@0 279 *
aoqi@0 280 * @param indentLevel indentation level to be used.
aoqi@0 281 * @param buffer buffer to be used for appending string representation of this instance
aoqi@0 282 * @return modified buffer containing new string representation of the instance
aoqi@0 283 */
aoqi@0 284 protected StringBuffer toString(final int indentLevel, final StringBuffer buffer) {
aoqi@0 285 final String indent = PolicyUtils.Text.createIndent(indentLevel);
aoqi@0 286 final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1);
aoqi@0 287
aoqi@0 288 buffer.append(indent).append("Assertion[").append(this.getClass().getName()).append("] {").append(PolicyUtils.Text.NEW_LINE);
aoqi@0 289 data.toString(indentLevel + 1, buffer);
aoqi@0 290 buffer.append(PolicyUtils.Text.NEW_LINE);
aoqi@0 291
aoqi@0 292 if (hasParameters()) {
aoqi@0 293 buffer.append(innerIndent).append("parameters {").append(PolicyUtils.Text.NEW_LINE);
aoqi@0 294 for (PolicyAssertion parameter : parameters) {
aoqi@0 295 parameter.toString(indentLevel + 2, buffer).append(PolicyUtils.Text.NEW_LINE);
aoqi@0 296 }
aoqi@0 297 buffer.append(innerIndent).append('}').append(PolicyUtils.Text.NEW_LINE);
aoqi@0 298 } else {
aoqi@0 299 buffer.append(innerIndent).append("no parameters").append(PolicyUtils.Text.NEW_LINE);
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 if (hasNestedPolicy()) {
aoqi@0 303 getNestedPolicy().toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE);
aoqi@0 304 } else {
aoqi@0 305 buffer.append(innerIndent).append("no nested policy").append(PolicyUtils.Text.NEW_LINE);
aoqi@0 306 }
aoqi@0 307
aoqi@0 308 buffer.append(indent).append('}');
aoqi@0 309
aoqi@0 310 return buffer;
aoqi@0 311 }
aoqi@0 312
aoqi@0 313 /**
aoqi@0 314 * Checks whether this policy alternative is compatible with the provided policy alternative.
aoqi@0 315 *
aoqi@0 316 * @param assertion policy alternative used for compatibility test
aoqi@0 317 * @param mode compatibility mode to be used
aoqi@0 318 * @return {@code true} if the two policy alternatives are compatible, {@code false} otherwise
aoqi@0 319 */
aoqi@0 320 boolean isCompatibleWith(final PolicyAssertion assertion, PolicyIntersector.CompatibilityMode mode) {
aoqi@0 321 boolean result = this.data.getName().equals(assertion.data.getName()) && (this.hasNestedPolicy() == assertion.hasNestedPolicy());
aoqi@0 322
aoqi@0 323 if (result && this.hasNestedPolicy()) {
aoqi@0 324 result = this.getNestedPolicy().getAssertionSet().isCompatibleWith(assertion.getNestedPolicy().getAssertionSet(), mode);
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 return result;
aoqi@0 328 }
aoqi@0 329
aoqi@0 330 /**
aoqi@0 331 * An {@code Object.equals(Object obj)} method override.
aoqi@0 332 */
aoqi@0 333 @Override
aoqi@0 334 public boolean equals(final Object obj) {
aoqi@0 335 if (this == obj) {
aoqi@0 336 return true;
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 if (!(obj instanceof PolicyAssertion)) {
aoqi@0 340 return false;
aoqi@0 341 }
aoqi@0 342
aoqi@0 343 final PolicyAssertion that = (PolicyAssertion) obj;
aoqi@0 344 boolean result = true;
aoqi@0 345
aoqi@0 346 result = result && this.data.equals(that.data);
aoqi@0 347 result = result && this.parameters.equals(that.parameters);
aoqi@0 348 result = result && ((this.getNestedPolicy() == null) ? ((that.getNestedPolicy() == null) ? true : false) : this.getNestedPolicy().equals(that.getNestedPolicy()));
aoqi@0 349
aoqi@0 350 return result;
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 /**
aoqi@0 354 * An {@code Object.hashCode()} method override.
aoqi@0 355 */
aoqi@0 356 @Override
aoqi@0 357 public int hashCode() {
aoqi@0 358 int result = 17;
aoqi@0 359
aoqi@0 360 result = 37 * result + data.hashCode();
aoqi@0 361 result = 37 * result + ((hasParameters()) ? 17 : 0);
aoqi@0 362 result = 37 * result + ((hasNestedPolicy()) ? 17 : 0);
aoqi@0 363
aoqi@0 364 return result;
aoqi@0 365 }
aoqi@0 366 }

mercurial