aoqi@0: /*
aoqi@0: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.xml.internal.ws.policy;
aoqi@0:
aoqi@0: import java.util.Collection;
aoqi@0: import java.util.Iterator;
aoqi@0: import java.util.Map;
aoqi@0: import java.util.Set;
aoqi@0: import javax.xml.namespace.QName;
aoqi@0: import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
aoqi@0: import com.sun.xml.internal.ws.policy.sourcemodel.AssertionData;
aoqi@0: import com.sun.xml.internal.ws.policy.sourcemodel.ModelNode;
aoqi@0:
aoqi@0: /**
aoqi@0: * Base class for any policy assertion implementations. It defines the common
aoqi@0: * interface and provides some default implentation for common policy assertion
aoqi@0: * functionality.
aoqi@0: *
aoqi@0: * NOTE: Assertion implementers should not extend this class directly. {@link SimpleAssertion}
aoqi@0: * or {@link ComplexAssertion} should be used as a base class instead.
aoqi@0: *
aoqi@0: * @author Marek Potociar (marek.potociar at sun.com)
aoqi@0: * @author Fabian Ritzmann
aoqi@0: */
aoqi@0: public abstract class PolicyAssertion {
aoqi@0:
aoqi@0: private final AssertionData data;
aoqi@0: private AssertionSet parameters;
aoqi@0: private NestedPolicy nestedPolicy; // TODO: remove
aoqi@0:
aoqi@0: protected PolicyAssertion() {
aoqi@0: this.data = AssertionData.createAssertionData(null);
aoqi@0: this.parameters = AssertionSet.createAssertionSet(null);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates generic assertionand stores the data specified in input parameters
aoqi@0: *
aoqi@0: * @param assertionData assertion creation data specifying the details of newly created assertion. May be {@code null}.
aoqi@0: * @param assertionParameters collection of assertions parameters of this policy assertion. May be {@code null}.
aoqi@0: * @param nestedAlternative assertion set specifying nested policy alternative. May be {@code null}.
aoqi@0: *
aoqi@0: * @deprecated Non-abstract assertion types should derive from {@link SimpleAssertion}
aoqi@0: * or {@link ComplexAssertion} instead. {@link Policy} class will not provide support for
aoqi@0: * nested policy alternatives in the future. This responsibility is delegated to
aoqi@0: * {@link ComplexAssertion} class instead.
aoqi@0: */
aoqi@0: @Deprecated
aoqi@0: protected PolicyAssertion(final AssertionData assertionData, final Collection extends PolicyAssertion> assertionParameters, final AssertionSet nestedAlternative) {
aoqi@0: this.data = assertionData;
aoqi@0: if (nestedAlternative != null) {
aoqi@0: this.nestedPolicy = NestedPolicy.createNestedPolicy(nestedAlternative);
aoqi@0: }
aoqi@0:
aoqi@0: this.parameters = AssertionSet.createAssertionSet(assertionParameters);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates generic assertionand stores the data specified in input parameters
aoqi@0: *
aoqi@0: * @param assertionData assertion creation data specifying the details of newly created assertion
aoqi@0: * @param assertionParameters collection of assertions parameters of this policy assertion. May be {@code null}.
aoqi@0: */
aoqi@0: protected PolicyAssertion(final AssertionData assertionData, final Collection extends PolicyAssertion> assertionParameters) {
aoqi@0: if (assertionData == null) {
aoqi@0: this.data = AssertionData.createAssertionData(null);
aoqi@0: } else {
aoqi@0: this.data = assertionData;
aoqi@0: }
aoqi@0: this.parameters = AssertionSet.createAssertionSet(assertionParameters);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the fully qualified name of the assertion.
aoqi@0: *
aoqi@0: * @return assertion's fully qualified name.
aoqi@0: */
aoqi@0: public final QName getName() {
aoqi@0: return data.getName();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the value of the assertion - the character data content contained in the assertion element representation.
aoqi@0: *
aoqi@0: * @return assertion's value. May return {@code null} if there is no value set for the assertion.
aoqi@0: */
aoqi@0: public final String getValue() {
aoqi@0: return data.getValue();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Method specifies whether the assertion is otpional or not.
aoqi@0: *
aoqi@0: * This is a default implementation that may be overriden. The method returns {@code true} if the {@code wsp:optional} attribute
aoqi@0: * is present on the assertion and its value is {@code 'true'}. Otherwise the method returns {@code false}.
aoqi@0: *
aoqi@0: * @return {@code 'true'} if the assertion is optional. Returns {@code false} otherwise.
aoqi@0: */
aoqi@0: public boolean isOptional() {
aoqi@0: return data.isOptionalAttributeSet();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Method specifies whether the assertion is ignorable or not.
aoqi@0: *
aoqi@0: * This is a default implementation that may be overriden. The method returns {@code true} if the {@code wsp:Ignorable} attribute
aoqi@0: * is present on the assertion and its value is {@code 'true'}. Otherwise the method returns {@code false}.
aoqi@0: *
aoqi@0: * @return {@code 'true'} if the assertion is ignorable. Returns {@code false} otherwise.
aoqi@0: */
aoqi@0: public boolean isIgnorable() {
aoqi@0: return data.isIgnorableAttributeSet();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Method specifies whether the assertion is private or not. This is specified by our proprietary visibility element.
aoqi@0: *
aoqi@0: * @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: */
aoqi@0: public final boolean isPrivate() {
aoqi@0: return data.isPrivateAttributeSet();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the disconnected set of attributes attached to the assertion. Each attribute is represented as a single
aoqi@0: * {@code Map.Entry} element.
aoqi@0: *
aoqi@0: * 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is
aoqi@0: * also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's
aoqi@0: * attributes.
aoqi@0: *
aoqi@0: * @return disconected set of attributes attached to the assertion.
aoqi@0: */
aoqi@0: public final Set> getAttributesSet() {
aoqi@0: return data.getAttributesSet();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the disconnected map of attributes attached to the assertion.
aoqi@0: *
aoqi@0: * 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is
aoqi@0: * also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's
aoqi@0: * attributes.
aoqi@0: *
aoqi@0: * @return disconnected map of attributes attached to the assertion.
aoqi@0: */
aoqi@0: public final Map getAttributes() {
aoqi@0: return data.getAttributes();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the value of an attribute. Returns null if an attribute with the given name does not exist.
aoqi@0: *
aoqi@0: * @param name The fully qualified name of the attribute
aoqi@0: * @return The value of the attribute. Returns {@code null} if there is no such attribute or if it's value is null.
aoqi@0: */
aoqi@0: public final String getAttributeValue(final QName name) {
aoqi@0: return data.getAttributeValue(name);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the boolean information whether this assertion contains any parameters.
aoqi@0: *
aoqi@0: * @return {@code true} if the assertion contains parameters. Returns {@code false} otherwise.
aoqi@0: *
aoqi@0: * @deprecated Use hasParameters() instead
aoqi@0: */
aoqi@0: @Deprecated
aoqi@0: public final boolean hasNestedAssertions() {
aoqi@0: // TODO: remove
aoqi@0: return !parameters.isEmpty();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the boolean information whether this assertion contains any parameters.
aoqi@0: *
aoqi@0: * @return {@code true} if the assertion contains parameters. Returns {@code false} otherwise.
aoqi@0: */
aoqi@0: public final boolean hasParameters() {
aoqi@0: return !parameters.isEmpty();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the assertion's parameter collection iterator.
aoqi@0: *
aoqi@0: * @return the assertion's parameter collection iterator.
aoqi@0: *
aoqi@0: * @deprecated Use getNestedParametersIterator() instead
aoqi@0: */
aoqi@0: @Deprecated
aoqi@0: public final Iterator getNestedAssertionsIterator() {
aoqi@0: // TODO: remove
aoqi@0: return parameters.iterator();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the assertion's parameter collection iterator.
aoqi@0: *
aoqi@0: * @return the assertion's parameter collection iterator.
aoqi@0: */
aoqi@0: public final Iterator getParametersIterator() {
aoqi@0: return parameters.iterator();
aoqi@0: }
aoqi@0:
aoqi@0: boolean isParameter() {
aoqi@0: return data.getNodeType() == ModelNode.Type.ASSERTION_PARAMETER_NODE;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the boolean information whether this assertion contains nested policy.
aoqi@0: *
aoqi@0: * @return {@code true} if the assertion contains child (nested) policy. Returns {@code false} otherwise.
aoqi@0: */
aoqi@0: public boolean hasNestedPolicy() {
aoqi@0: // TODO: make abstract
aoqi@0: return getNestedPolicy() != null;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the nested policy if any.
aoqi@0: *
aoqi@0: * @return the nested policy if the assertion contains a nested policy. Returns {@code null} otherwise.
aoqi@0: */
aoqi@0: public NestedPolicy getNestedPolicy() {
aoqi@0: // TODO: make abstract
aoqi@0: return nestedPolicy;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Casts the assertion to the implementation type. Returns null if that is not
aoqi@0: * possible.
aoqi@0: *
aoqi@0: * @param The implementation type of the assertion.
aoqi@0: * @param type The implementation type of the assertion. May not be null.
aoqi@0: * @return The instance of the implementation type. Null otherwise.
aoqi@0: */
aoqi@0: public T getImplementation(Class type) {
aoqi@0: if (type.isAssignableFrom(this.getClass())) {
aoqi@0: return type.cast(this);
aoqi@0: }
aoqi@0: else {
aoqi@0: return null;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * An {@code Object.toString()} method override.
aoqi@0: */
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: return toString(0, new StringBuffer()).toString();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * A helper method that appends indented string representation of this instance to the input string buffer.
aoqi@0: *
aoqi@0: * @param indentLevel indentation level to be used.
aoqi@0: * @param buffer buffer to be used for appending string representation of this instance
aoqi@0: * @return modified buffer containing new string representation of the instance
aoqi@0: */
aoqi@0: protected StringBuffer toString(final int indentLevel, final StringBuffer buffer) {
aoqi@0: final String indent = PolicyUtils.Text.createIndent(indentLevel);
aoqi@0: final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1);
aoqi@0:
aoqi@0: buffer.append(indent).append("Assertion[").append(this.getClass().getName()).append("] {").append(PolicyUtils.Text.NEW_LINE);
aoqi@0: data.toString(indentLevel + 1, buffer);
aoqi@0: buffer.append(PolicyUtils.Text.NEW_LINE);
aoqi@0:
aoqi@0: if (hasParameters()) {
aoqi@0: buffer.append(innerIndent).append("parameters {").append(PolicyUtils.Text.NEW_LINE);
aoqi@0: for (PolicyAssertion parameter : parameters) {
aoqi@0: parameter.toString(indentLevel + 2, buffer).append(PolicyUtils.Text.NEW_LINE);
aoqi@0: }
aoqi@0: buffer.append(innerIndent).append('}').append(PolicyUtils.Text.NEW_LINE);
aoqi@0: } else {
aoqi@0: buffer.append(innerIndent).append("no parameters").append(PolicyUtils.Text.NEW_LINE);
aoqi@0: }
aoqi@0:
aoqi@0: if (hasNestedPolicy()) {
aoqi@0: getNestedPolicy().toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE);
aoqi@0: } else {
aoqi@0: buffer.append(innerIndent).append("no nested policy").append(PolicyUtils.Text.NEW_LINE);
aoqi@0: }
aoqi@0:
aoqi@0: buffer.append(indent).append('}');
aoqi@0:
aoqi@0: return buffer;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Checks whether this policy alternative is compatible with the provided policy alternative.
aoqi@0: *
aoqi@0: * @param assertion policy alternative used for compatibility test
aoqi@0: * @param mode compatibility mode to be used
aoqi@0: * @return {@code true} if the two policy alternatives are compatible, {@code false} otherwise
aoqi@0: */
aoqi@0: boolean isCompatibleWith(final PolicyAssertion assertion, PolicyIntersector.CompatibilityMode mode) {
aoqi@0: boolean result = this.data.getName().equals(assertion.data.getName()) && (this.hasNestedPolicy() == assertion.hasNestedPolicy());
aoqi@0:
aoqi@0: if (result && this.hasNestedPolicy()) {
aoqi@0: result = this.getNestedPolicy().getAssertionSet().isCompatibleWith(assertion.getNestedPolicy().getAssertionSet(), mode);
aoqi@0: }
aoqi@0:
aoqi@0: return result;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * An {@code Object.equals(Object obj)} method override.
aoqi@0: */
aoqi@0: @Override
aoqi@0: public boolean equals(final Object obj) {
aoqi@0: if (this == obj) {
aoqi@0: return true;
aoqi@0: }
aoqi@0:
aoqi@0: if (!(obj instanceof PolicyAssertion)) {
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: final PolicyAssertion that = (PolicyAssertion) obj;
aoqi@0: boolean result = true;
aoqi@0:
aoqi@0: result = result && this.data.equals(that.data);
aoqi@0: result = result && this.parameters.equals(that.parameters);
aoqi@0: result = result && ((this.getNestedPolicy() == null) ? ((that.getNestedPolicy() == null) ? true : false) : this.getNestedPolicy().equals(that.getNestedPolicy()));
aoqi@0:
aoqi@0: return result;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * An {@code Object.hashCode()} method override.
aoqi@0: */
aoqi@0: @Override
aoqi@0: public int hashCode() {
aoqi@0: int result = 17;
aoqi@0:
aoqi@0: result = 37 * result + data.hashCode();
aoqi@0: result = 37 * result + ((hasParameters()) ? 17 : 0);
aoqi@0: result = 37 * result + ((hasNestedPolicy()) ? 17 : 0);
aoqi@0:
aoqi@0: return result;
aoqi@0: }
aoqi@0: }