src/share/jaxws_classes/com/sun/xml/internal/ws/policy/sourcemodel/AssertionData.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.sourcemodel;
aoqi@0 27
aoqi@0 28 import com.sun.xml.internal.ws.policy.PolicyConstants;
aoqi@0 29 import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages;
aoqi@0 30 import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
aoqi@0 31 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
aoqi@0 32
aoqi@0 33 import java.io.Serializable;
aoqi@0 34 import java.util.HashMap;
aoqi@0 35 import java.util.HashSet;
aoqi@0 36 import java.util.Map;
aoqi@0 37 import java.util.Set;
aoqi@0 38 import javax.xml.namespace.QName;
aoqi@0 39
aoqi@0 40 /**
aoqi@0 41 * Wrapper class for possible data that each "assertion" and "assertion parameter content" policy source model node may
aoqi@0 42 * have attached.
aoqi@0 43 * <p/>
aoqi@0 44 * This data, when stored in an 'assertion' model node, is intended to be used as input parameter when creating
aoqi@0 45 * {@link com.sun.xml.internal.ws.policy.PolicyAssertion} objects via {@link com.sun.xml.internal.ws.policy.spi.PolicyAssertionCreator}
aoqi@0 46 * implementations.
aoqi@0 47 *
aoqi@0 48 * @author Marek Potociar (marek.potociar@sun.com)
aoqi@0 49 * @author Fabian Ritzmann
aoqi@0 50 */
aoqi@0 51 public final class AssertionData implements Cloneable, Serializable {
aoqi@0 52 private static final long serialVersionUID = 4416256070795526315L;
aoqi@0 53 private static final PolicyLogger LOGGER = PolicyLogger.getLogger(AssertionData.class);
aoqi@0 54
aoqi@0 55 private final QName name;
aoqi@0 56 private final String value;
aoqi@0 57 private final Map<QName, String> attributes;
aoqi@0 58 private ModelNode.Type type;
aoqi@0 59
aoqi@0 60 private boolean optional;
aoqi@0 61 private boolean ignorable;
aoqi@0 62
aoqi@0 63 /**
aoqi@0 64 * Constructs assertion data wrapper instance for an assertion that does not
aoqi@0 65 * contain any value nor any attributes.
aoqi@0 66 *
aoqi@0 67 * @param name the FQN of the assertion
aoqi@0 68 *
aoqi@0 69 * @throws IllegalArgumentException in case the {@code type} parameter is not
aoqi@0 70 * {@link ModelNode.Type#ASSERTION ASSERTION} or
aoqi@0 71 * {@link ModelNode.Type#ASSERTION_PARAMETER_NODE ASSERTION_PARAMETER_NODE}
aoqi@0 72 */
aoqi@0 73 public static AssertionData createAssertionData(final QName name) throws IllegalArgumentException {
aoqi@0 74 return new AssertionData(name, null, null, ModelNode.Type.ASSERTION, false, false);
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 /**
aoqi@0 78 * Constructs assertion data wrapper instance for an assertion parameter that
aoqi@0 79 * does not contain any value nor any attributes.
aoqi@0 80 *
aoqi@0 81 * @param name the FQN of the assertion parameter
aoqi@0 82 *
aoqi@0 83 * @throws IllegalArgumentException in case the {@code type} parameter is not
aoqi@0 84 * {@link ModelNode.Type#ASSERTION ASSERTION} or
aoqi@0 85 * {@link ModelNode.Type#ASSERTION_PARAMETER_NODE ASSERTION_PARAMETER_NODE}
aoqi@0 86 */
aoqi@0 87 public static AssertionData createAssertionParameterData(final QName name) throws IllegalArgumentException {
aoqi@0 88 return new AssertionData(name, null, null, ModelNode.Type.ASSERTION_PARAMETER_NODE, false, false);
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 /**
aoqi@0 92 * Constructs assertion data wrapper instance for an assertion that does
aoqi@0 93 * contain a value or attributes.
aoqi@0 94 *
aoqi@0 95 * @param name the FQN of the assertion
aoqi@0 96 * @param value a {@link String} representation of model node value
aoqi@0 97 * @param attributes map of model node's &lt;attribute name, attribute value&gt; pairs
aoqi@0 98 * @param optional flag indicating whether the assertion is optional or not
aoqi@0 99 * @param ignorable flag indicating whether the assertion is ignorable or not
aoqi@0 100 *
aoqi@0 101 * @throws IllegalArgumentException in case the {@code type} parameter is not
aoqi@0 102 * {@link ModelNode.Type#ASSERTION ASSERTION} or
aoqi@0 103 * {@link ModelNode.Type#ASSERTION_PARAMETER_NODE ASSERTION_PARAMETER_NODE}
aoqi@0 104 */
aoqi@0 105 public static AssertionData createAssertionData(final QName name, final String value, final Map<QName, String> attributes, boolean optional, boolean ignorable) throws IllegalArgumentException {
aoqi@0 106 return new AssertionData(name, value, attributes, ModelNode.Type.ASSERTION, optional, ignorable);
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 /**
aoqi@0 110 * Constructs assertion data wrapper instance for an assertion parameter that
aoqi@0 111 * contains a value or attributes
aoqi@0 112 *
aoqi@0 113 * @param name the FQN of the assertion parameter
aoqi@0 114 * @param value a {@link String} representation of model node value
aoqi@0 115 * @param attributes map of model node's &lt;attribute name, attribute value&gt; pairs
aoqi@0 116 *
aoqi@0 117 * @throws IllegalArgumentException in case the {@code type} parameter is not
aoqi@0 118 * {@link ModelNode.Type#ASSERTION ASSERTION} or
aoqi@0 119 * {@link ModelNode.Type#ASSERTION_PARAMETER_NODE ASSERTION_PARAMETER_NODE}
aoqi@0 120 */
aoqi@0 121 public static AssertionData createAssertionParameterData(final QName name, final String value, final Map<QName, String> attributes) throws IllegalArgumentException {
aoqi@0 122 return new AssertionData(name, value, attributes, ModelNode.Type.ASSERTION_PARAMETER_NODE, false, false);
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 /**
aoqi@0 126 * Constructs assertion data wrapper instance for an assertion or assertion parameter that contains a value or
aoqi@0 127 * some attributes. Whether the data wrapper is constructed for assertion or assertion parameter node is distinguished by
aoqi@0 128 * the supplied {@code type} parameter.
aoqi@0 129 *
aoqi@0 130 * @param name the FQN of the assertion or assertion parameter
aoqi@0 131 * @param value a {@link String} representation of model node value
aoqi@0 132 * @param attributes map of model node's &lt;attribute name, attribute value&gt; pairs
aoqi@0 133 * @param type specifies whether the data will belong to the assertion or assertion parameter node. This is
aoqi@0 134 * a workaround solution that allows us to transfer this information about the owner node to
aoqi@0 135 * a policy assertion instance factory without actualy having to touch the {@link PolicyAssertionCreator}
aoqi@0 136 * interface and protected {@link PolicyAssertion} constructors.
aoqi@0 137 *
aoqi@0 138 * @throws IllegalArgumentException in case the {@code type} parameter is not
aoqi@0 139 * {@link ModelNode.Type#ASSERTION ASSERTION} or
aoqi@0 140 * {@link ModelNode.Type#ASSERTION_PARAMETER_NODE ASSERTION_PARAMETER_NODE}
aoqi@0 141 */
aoqi@0 142 AssertionData(QName name, String value, Map<QName, String> attributes, ModelNode.Type type, boolean optional, boolean ignorable) throws IllegalArgumentException {
aoqi@0 143 this.name = name;
aoqi@0 144 this.value = value;
aoqi@0 145 this.optional = optional;
aoqi@0 146 this.ignorable = ignorable;
aoqi@0 147
aoqi@0 148 this.attributes = new HashMap<QName, String>();
aoqi@0 149 if (attributes != null && !attributes.isEmpty()) {
aoqi@0 150 this.attributes.putAll(attributes);
aoqi@0 151 }
aoqi@0 152 setModelNodeType(type);
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 private void setModelNodeType(final ModelNode.Type type) throws IllegalArgumentException {
aoqi@0 156 if (type == ModelNode.Type.ASSERTION || type == ModelNode.Type.ASSERTION_PARAMETER_NODE) {
aoqi@0 157 this.type = type;
aoqi@0 158 } else {
aoqi@0 159 throw LOGGER.logSevereException(new IllegalArgumentException(
aoqi@0 160 LocalizationMessages.WSP_0074_CANNOT_CREATE_ASSERTION_BAD_TYPE(type, ModelNode.Type.ASSERTION, ModelNode.Type.ASSERTION_PARAMETER_NODE)));
aoqi@0 161 }
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 /**
aoqi@0 165 * Copy constructor.
aoqi@0 166 *
aoqi@0 167 * @param data The instance that is to be copied.
aoqi@0 168 */
aoqi@0 169 AssertionData(final AssertionData data) {
aoqi@0 170 this.name = data.name;
aoqi@0 171 this.value = data.value;
aoqi@0 172 this.attributes = new HashMap<QName, String>();
aoqi@0 173 if (!data.attributes.isEmpty()) {
aoqi@0 174 this.attributes.putAll(data.attributes);
aoqi@0 175 }
aoqi@0 176 this.type = data.type;
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 @Override
aoqi@0 180 protected AssertionData clone() throws CloneNotSupportedException {
aoqi@0 181 return (AssertionData) super.clone();
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 /**
aoqi@0 185 * Returns true if the given attribute exists, false otherwise.
aoqi@0 186 *
aoqi@0 187 * @param name The name of the attribute. Must not be null.
aoqi@0 188 * @return True if the given attribute exists, false otherwise.
aoqi@0 189 */
aoqi@0 190 public boolean containsAttribute(final QName name) {
aoqi@0 191 synchronized (attributes) {
aoqi@0 192 return attributes.containsKey(name);
aoqi@0 193 }
aoqi@0 194 }
aoqi@0 195
aoqi@0 196
aoqi@0 197 @Override
aoqi@0 198 public boolean equals(final Object obj) {
aoqi@0 199 if (this == obj) {
aoqi@0 200 return true;
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 if (!(obj instanceof AssertionData)) {
aoqi@0 204 return false;
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 boolean result = true;
aoqi@0 208 final AssertionData that = (AssertionData) obj;
aoqi@0 209
aoqi@0 210 result = result && this.name.equals(that.name);
aoqi@0 211 result = result && ((this.value == null) ? that.value == null : this.value.equals(that.value));
aoqi@0 212 synchronized (attributes) {
aoqi@0 213 result = result && this.attributes.equals(that.attributes);
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 return result;
aoqi@0 217 }
aoqi@0 218
aoqi@0 219
aoqi@0 220 /**
aoqi@0 221 * Returns the value of the given attribute. Returns null if the attribute
aoqi@0 222 * does not exist.
aoqi@0 223 *
aoqi@0 224 * @param name The name of the attribute. Must not be null.
aoqi@0 225 * @return The value of the given attribute. Returns null if the attribute
aoqi@0 226 * does not exist.
aoqi@0 227 */
aoqi@0 228 public String getAttributeValue(final QName name) {
aoqi@0 229 synchronized (attributes) {
aoqi@0 230 return attributes.get(name);
aoqi@0 231 }
aoqi@0 232 }
aoqi@0 233
aoqi@0 234
aoqi@0 235 /**
aoqi@0 236 * Returns the disconnected map of attributes attached to the assertion.
aoqi@0 237 * <p/>
aoqi@0 238 * 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is
aoqi@0 239 * also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's
aoqi@0 240 * attributes.
aoqi@0 241 *
aoqi@0 242 * @return disconnected map of attributes attached to the assertion.
aoqi@0 243 */
aoqi@0 244 public Map<QName, String> getAttributes() {
aoqi@0 245 synchronized (attributes) {
aoqi@0 246 return new HashMap<QName, String>(attributes);
aoqi@0 247 }
aoqi@0 248 }
aoqi@0 249
aoqi@0 250
aoqi@0 251 /**
aoqi@0 252 * Returns the disconnected set of attributes attached to the assertion. Each attribute is represented as a single
aoqi@0 253 * {@code Map.Entry<attributeName, attributeValue>} element.
aoqi@0 254 * <p/>
aoqi@0 255 * 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is
aoqi@0 256 * also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's
aoqi@0 257 * attributes.
aoqi@0 258 *
aoqi@0 259 * @return disconnected set of attributes attached to the assertion.
aoqi@0 260 */
aoqi@0 261 public Set<Map.Entry<QName, String>> getAttributesSet() {
aoqi@0 262 synchronized (attributes) {
aoqi@0 263 return new HashSet<Map.Entry<QName, String>>(attributes.entrySet());
aoqi@0 264 }
aoqi@0 265 }
aoqi@0 266
aoqi@0 267
aoqi@0 268 /**
aoqi@0 269 * Returns the name of the assertion.
aoqi@0 270 *
aoqi@0 271 * @return assertion's name
aoqi@0 272 */
aoqi@0 273 public QName getName() {
aoqi@0 274 return name;
aoqi@0 275 }
aoqi@0 276
aoqi@0 277
aoqi@0 278 /**
aoqi@0 279 * Returns the value of the assertion.
aoqi@0 280 *
aoqi@0 281 * @return assertion's value
aoqi@0 282 */
aoqi@0 283 public String getValue() {
aoqi@0 284 return value;
aoqi@0 285 }
aoqi@0 286
aoqi@0 287
aoqi@0 288 /**
aoqi@0 289 * An {@code Object.hashCode()} method override.
aoqi@0 290 */
aoqi@0 291 @Override
aoqi@0 292 public int hashCode() {
aoqi@0 293 int result = 17;
aoqi@0 294
aoqi@0 295 result = 37 * result + this.name.hashCode();
aoqi@0 296 result = 37 * result + ((this.value == null) ? 0 : this.value.hashCode());
aoqi@0 297 synchronized (attributes) {
aoqi@0 298 result = 37 * result + this.attributes.hashCode();
aoqi@0 299 }
aoqi@0 300 return result;
aoqi@0 301 }
aoqi@0 302
aoqi@0 303
aoqi@0 304 /**
aoqi@0 305 * Method specifies whether the assertion data contain proprietary visibility element set to "private" value.
aoqi@0 306 *
aoqi@0 307 * @return {@code 'true'} if the attribute is present and set properly (i.e. the node containing this assertion data instance should
aoqi@0 308 * not be marshaled into generated WSDL documents). Returns {@code false} otherwise.
aoqi@0 309 */
aoqi@0 310 public boolean isPrivateAttributeSet() {
aoqi@0 311 return PolicyConstants.VISIBILITY_VALUE_PRIVATE.equals(getAttributeValue(PolicyConstants.VISIBILITY_ATTRIBUTE));
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 /**
aoqi@0 315 * Removes the given attribute from the assertion data.
aoqi@0 316 *
aoqi@0 317 * @param name The name of the attribute. Must not be null
aoqi@0 318 * @return The value of the removed attribute.
aoqi@0 319 */
aoqi@0 320 public String removeAttribute(final QName name) {
aoqi@0 321 synchronized (attributes) {
aoqi@0 322 return attributes.remove(name);
aoqi@0 323 }
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 /**
aoqi@0 327 * Adds or overwrites an attribute.
aoqi@0 328 *
aoqi@0 329 * @param name The name of the attribute.
aoqi@0 330 * @param value The value of the attribute.
aoqi@0 331 */
aoqi@0 332 public void setAttribute(final QName name, final String value) {
aoqi@0 333 synchronized (attributes) {
aoqi@0 334 attributes.put(name, value);
aoqi@0 335 }
aoqi@0 336 }
aoqi@0 337
aoqi@0 338 /**
aoqi@0 339 * Sets the optional attribute.
aoqi@0 340 *
aoqi@0 341 * @param value The value of the optional attribute.
aoqi@0 342 */
aoqi@0 343 public void setOptionalAttribute(final boolean value) {
aoqi@0 344 optional = value;
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 /**
aoqi@0 348 * Tests if the optional attribute is set.
aoqi@0 349 *
aoqi@0 350 * @return True if optional is set and is true. False otherwise.
aoqi@0 351 */
aoqi@0 352 public boolean isOptionalAttributeSet() {
aoqi@0 353 return optional;
aoqi@0 354 }
aoqi@0 355
aoqi@0 356 /**
aoqi@0 357 * Sets the ignorable attribute.
aoqi@0 358 *
aoqi@0 359 * @param value The value of the ignorable attribute.
aoqi@0 360 */
aoqi@0 361 public void setIgnorableAttribute(final boolean value) {
aoqi@0 362 ignorable = value;
aoqi@0 363 }
aoqi@0 364
aoqi@0 365 /**
aoqi@0 366 * Tests if the ignorable attribute is set.
aoqi@0 367 *
aoqi@0 368 * @return True if ignorable is set and is true. False otherwise.
aoqi@0 369 */
aoqi@0 370 public boolean isIgnorableAttributeSet() {
aoqi@0 371 return ignorable;
aoqi@0 372 }
aoqi@0 373
aoqi@0 374 @Override
aoqi@0 375 public String toString() {
aoqi@0 376 return toString(0, new StringBuffer()).toString();
aoqi@0 377 }
aoqi@0 378
aoqi@0 379 /**
aoqi@0 380 * A helper method that appends indented string representation of this instance to the input string buffer.
aoqi@0 381 *
aoqi@0 382 * @param indentLevel indentation level to be used.
aoqi@0 383 * @param buffer buffer to be used for appending string representation of this instance
aoqi@0 384 * @return modified buffer containing new string representation of the instance
aoqi@0 385 */
aoqi@0 386 public StringBuffer toString(final int indentLevel, final StringBuffer buffer) {
aoqi@0 387 final String indent = PolicyUtils.Text.createIndent(indentLevel);
aoqi@0 388 final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1);
aoqi@0 389 final String innerDoubleIndent = PolicyUtils.Text.createIndent(indentLevel + 2);
aoqi@0 390
aoqi@0 391 buffer.append(indent);
aoqi@0 392 if (type == ModelNode.Type.ASSERTION) {
aoqi@0 393 buffer.append("assertion data {");
aoqi@0 394 } else {
aoqi@0 395 buffer.append("assertion parameter data {");
aoqi@0 396 }
aoqi@0 397 buffer.append(PolicyUtils.Text.NEW_LINE);
aoqi@0 398
aoqi@0 399 buffer.append(innerIndent).append("namespace = '").append(name.getNamespaceURI()).append('\'').append(PolicyUtils.Text.NEW_LINE);
aoqi@0 400 buffer.append(innerIndent).append("prefix = '").append(name.getPrefix()).append('\'').append(PolicyUtils.Text.NEW_LINE);
aoqi@0 401 buffer.append(innerIndent).append("local name = '").append(name.getLocalPart()).append('\'').append(PolicyUtils.Text.NEW_LINE);
aoqi@0 402 buffer.append(innerIndent).append("value = '").append(value).append('\'').append(PolicyUtils.Text.NEW_LINE);
aoqi@0 403 buffer.append(innerIndent).append("optional = '").append(optional).append('\'').append(PolicyUtils.Text.NEW_LINE);
aoqi@0 404 buffer.append(innerIndent).append("ignorable = '").append(ignorable).append('\'').append(PolicyUtils.Text.NEW_LINE);
aoqi@0 405 synchronized (attributes) {
aoqi@0 406 if (attributes.isEmpty()) {
aoqi@0 407 buffer.append(innerIndent).append("no attributes");
aoqi@0 408 } else {
aoqi@0 409
aoqi@0 410 buffer.append(innerIndent).append("attributes {").append(PolicyUtils.Text.NEW_LINE);
aoqi@0 411 for(Map.Entry<QName, String> entry : attributes.entrySet()) {
aoqi@0 412 final QName aName = entry.getKey();
aoqi@0 413 buffer.append(innerDoubleIndent).append("name = '").append(aName.getNamespaceURI()).append(':').append(aName.getLocalPart());
aoqi@0 414 buffer.append("', value = '").append(entry.getValue()).append('\'').append(PolicyUtils.Text.NEW_LINE);
aoqi@0 415 }
aoqi@0 416 buffer.append(innerIndent).append('}');
aoqi@0 417 }
aoqi@0 418 }
aoqi@0 419
aoqi@0 420 buffer.append(PolicyUtils.Text.NEW_LINE).append(indent).append('}');
aoqi@0 421
aoqi@0 422 return buffer;
aoqi@0 423 }
aoqi@0 424
aoqi@0 425 public ModelNode.Type getNodeType() {
aoqi@0 426 return type;
aoqi@0 427 }
aoqi@0 428
aoqi@0 429 }

mercurial