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

changeset 0
373ffda63c9a
     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/PolicyAssertion.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,366 @@
     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 java.util.Collection;
    1.32 +import java.util.Iterator;
    1.33 +import java.util.Map;
    1.34 +import java.util.Set;
    1.35 +import javax.xml.namespace.QName;
    1.36 +import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
    1.37 +import com.sun.xml.internal.ws.policy.sourcemodel.AssertionData;
    1.38 +import com.sun.xml.internal.ws.policy.sourcemodel.ModelNode;
    1.39 +
    1.40 +/**
    1.41 + * Base class for any policy assertion implementations. It defines the common
    1.42 + * interface and provides some default implentation for common policy assertion
    1.43 + * functionality.
    1.44 + * <p/>
    1.45 + * NOTE: Assertion implementers should not extend this class directly. {@link SimpleAssertion}
    1.46 + * or {@link ComplexAssertion} should be used as a base class instead.
    1.47 + *
    1.48 + * @author Marek Potociar (marek.potociar at sun.com)
    1.49 + * @author Fabian Ritzmann
    1.50 + */
    1.51 +public abstract class PolicyAssertion {
    1.52 +
    1.53 +    private final AssertionData data;
    1.54 +    private AssertionSet parameters;
    1.55 +    private NestedPolicy nestedPolicy; // TODO: remove
    1.56 +
    1.57 +    protected PolicyAssertion() {
    1.58 +        this.data = AssertionData.createAssertionData(null);
    1.59 +        this.parameters =  AssertionSet.createAssertionSet(null);
    1.60 +    }
    1.61 +
    1.62 +    /**
    1.63 +     * Creates generic assertionand stores the data specified in input parameters
    1.64 +     *
    1.65 +     * @param assertionData assertion creation data specifying the details of newly created assertion. May be {@code null}.
    1.66 +     * @param assertionParameters collection of assertions parameters of this policy assertion. May be {@code null}.
    1.67 +     * @param nestedAlternative assertion set specifying nested policy alternative. May be {@code null}.
    1.68 +     *
    1.69 +     * @deprecated Non-abstract assertion types should derive from {@link SimpleAssertion}
    1.70 +     * or {@link ComplexAssertion} instead. {@link Policy} class will not provide support for
    1.71 +     * nested policy alternatives in the future. This responsibility is delegated to
    1.72 +     * {@link ComplexAssertion} class instead.
    1.73 +     */
    1.74 +    @Deprecated
    1.75 +    protected PolicyAssertion(final AssertionData assertionData, final Collection<? extends PolicyAssertion> assertionParameters, final AssertionSet nestedAlternative) {
    1.76 +        this.data = assertionData;
    1.77 +        if (nestedAlternative != null) {
    1.78 +            this.nestedPolicy = NestedPolicy.createNestedPolicy(nestedAlternative);
    1.79 +        }
    1.80 +
    1.81 +        this.parameters = AssertionSet.createAssertionSet(assertionParameters);
    1.82 +    }
    1.83 +
    1.84 +    /**
    1.85 +     * Creates generic assertionand stores the data specified in input parameters
    1.86 +     *
    1.87 +     * @param assertionData assertion creation data specifying the details of newly created assertion
    1.88 +     * @param assertionParameters collection of assertions parameters of this policy assertion. May be {@code null}.
    1.89 +     */
    1.90 +    protected PolicyAssertion(final AssertionData assertionData, final Collection<? extends PolicyAssertion> assertionParameters) {
    1.91 +        if (assertionData == null) {
    1.92 +            this.data = AssertionData.createAssertionData(null);
    1.93 +        } else {
    1.94 +            this.data = assertionData;
    1.95 +        }
    1.96 +        this.parameters = AssertionSet.createAssertionSet(assertionParameters);
    1.97 +    }
    1.98 +
    1.99 +    /**
   1.100 +     * Returns the fully qualified name of the assertion.
   1.101 +     *
   1.102 +     * @return assertion's fully qualified name.
   1.103 +     */
   1.104 +    public final QName getName() {
   1.105 +        return data.getName();
   1.106 +    }
   1.107 +
   1.108 +    /**
   1.109 +     * Returns the value of the assertion - the character data content contained in the assertion element representation.
   1.110 +     *
   1.111 +     * @return assertion's value. May return {@code null} if there is no value set for the assertion.
   1.112 +     */
   1.113 +    public final String getValue() {
   1.114 +        return data.getValue();
   1.115 +    }
   1.116 +
   1.117 +    /**
   1.118 +     * Method specifies whether the assertion is otpional or not.
   1.119 +     * <p/>
   1.120 +     * This is a default implementation that may be overriden. The method returns {@code true} if the {@code wsp:optional} attribute
   1.121 +     * is present on the assertion and its value is {@code 'true'}. Otherwise the method returns {@code false}.
   1.122 +     *
   1.123 +     * @return {@code 'true'} if the assertion is optional. Returns {@code false} otherwise.
   1.124 +     */
   1.125 +    public boolean isOptional() {
   1.126 +        return data.isOptionalAttributeSet();
   1.127 +    }
   1.128 +
   1.129 +    /**
   1.130 +     * Method specifies whether the assertion is ignorable or not.
   1.131 +     * <p/>
   1.132 +     * This is a default implementation that may be overriden. The method returns {@code true} if the {@code wsp:Ignorable} attribute
   1.133 +     * is present on the assertion and its value is {@code 'true'}. Otherwise the method returns {@code false}.
   1.134 +     *
   1.135 +     * @return {@code 'true'} if the assertion is ignorable. Returns {@code false} otherwise.
   1.136 +     */
   1.137 +    public boolean isIgnorable() {
   1.138 +        return data.isIgnorableAttributeSet();
   1.139 +    }
   1.140 +
   1.141 +    /**
   1.142 +     * Method specifies whether the assertion is private or not. This is specified by our proprietary visibility element.
   1.143 +     *
   1.144 +     * @return {@code 'true'} if the assertion is marked as private (i.e. should not be marshalled int generated WSDL documents). Returns {@code false} otherwise.
   1.145 +     */
   1.146 +    public final boolean isPrivate() {
   1.147 +        return data.isPrivateAttributeSet();
   1.148 +    }
   1.149 +
   1.150 +    /**
   1.151 +     * Returns the disconnected set of attributes attached to the assertion. Each attribute is represented as a single
   1.152 +     * {@code Map.Entry<attributeName, attributeValue>} element.
   1.153 +     * <p/>
   1.154 +     * 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is
   1.155 +     * also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's
   1.156 +     * attributes.
   1.157 +     *
   1.158 +     * @return disconected set of attributes attached to the assertion.
   1.159 +     */
   1.160 +    public final Set<Map.Entry<QName, String>> getAttributesSet() {
   1.161 +        return data.getAttributesSet();
   1.162 +    }
   1.163 +
   1.164 +    /**
   1.165 +     * Returns the disconnected map of attributes attached to the assertion.
   1.166 +     * <p/>
   1.167 +     * 'Disconnected' means, that the result of this method will not be synchronized with any consequent assertion's attribute modification. It is
   1.168 +     * also important to notice that a manipulation with returned set of attributes will not have any effect on the actual assertion's
   1.169 +     * attributes.
   1.170 +     *
   1.171 +     * @return disconnected map of attributes attached to the assertion.
   1.172 +     */
   1.173 +    public final Map<QName, String> getAttributes() {
   1.174 +        return data.getAttributes();
   1.175 +    }
   1.176 +
   1.177 +    /**
   1.178 +     * Returns the value of an attribute. Returns null if an attribute with the given name does not exist.
   1.179 +     *
   1.180 +     * @param name The fully qualified name of the attribute
   1.181 +     * @return The value of the attribute. Returns {@code null} if there is no such attribute or if it's value is null.
   1.182 +     */
   1.183 +    public final String getAttributeValue(final QName name) {
   1.184 +        return data.getAttributeValue(name);
   1.185 +    }
   1.186 +
   1.187 +    /**
   1.188 +     * Returns the boolean information whether this assertion contains any parameters.
   1.189 +     *
   1.190 +     * @return {@code true} if the assertion contains parameters. Returns {@code false} otherwise.
   1.191 +     *
   1.192 +     * @deprecated Use hasParameters() instead
   1.193 +     */
   1.194 +    @Deprecated
   1.195 +    public final boolean hasNestedAssertions() {
   1.196 +        // TODO: remove
   1.197 +        return !parameters.isEmpty();
   1.198 +    }
   1.199 +
   1.200 +    /**
   1.201 +     * Returns the boolean information whether this assertion contains any parameters.
   1.202 +     *
   1.203 +     * @return {@code true} if the assertion contains parameters. Returns {@code false} otherwise.
   1.204 +     */
   1.205 +    public final boolean hasParameters() {
   1.206 +        return !parameters.isEmpty();
   1.207 +    }
   1.208 +
   1.209 +    /**
   1.210 +     * Returns the assertion's parameter collection iterator.
   1.211 +     *
   1.212 +     * @return the assertion's parameter collection iterator.
   1.213 +     *
   1.214 +     * @deprecated Use getNestedParametersIterator() instead
   1.215 +     */
   1.216 +    @Deprecated
   1.217 +    public final Iterator<PolicyAssertion> getNestedAssertionsIterator() {
   1.218 +        // TODO: remove
   1.219 +        return parameters.iterator();
   1.220 +    }
   1.221 +
   1.222 +    /**
   1.223 +     * Returns the assertion's parameter collection iterator.
   1.224 +     *
   1.225 +     * @return the assertion's parameter collection iterator.
   1.226 +     */
   1.227 +    public final Iterator<PolicyAssertion> getParametersIterator() {
   1.228 +        return parameters.iterator();
   1.229 +    }
   1.230 +
   1.231 +    boolean isParameter() {
   1.232 +        return data.getNodeType() == ModelNode.Type.ASSERTION_PARAMETER_NODE;
   1.233 +    }
   1.234 +
   1.235 +    /**
   1.236 +     * Returns the boolean information whether this assertion contains nested policy.
   1.237 +     *
   1.238 +     * @return {@code true} if the assertion contains child (nested) policy. Returns {@code false} otherwise.
   1.239 +     */
   1.240 +    public boolean hasNestedPolicy() {
   1.241 +        // TODO: make abstract
   1.242 +        return getNestedPolicy() != null;
   1.243 +    }
   1.244 +
   1.245 +    /**
   1.246 +     * Returns the nested policy if any.
   1.247 +     *
   1.248 +     * @return the nested policy if the assertion contains a nested policy. Returns {@code null} otherwise.
   1.249 +     */
   1.250 +    public NestedPolicy getNestedPolicy() {
   1.251 +        // TODO: make abstract
   1.252 +        return nestedPolicy;
   1.253 +    }
   1.254 +
   1.255 +    /**
   1.256 +     * Casts the assertion to the implementation type. Returns null if that is not
   1.257 +     * possible.
   1.258 +     *
   1.259 +     * @param <T> The implementation type of the assertion.
   1.260 +     * @param type The implementation type of the assertion. May not be null.
   1.261 +     * @return The instance of the implementation type. Null otherwise.
   1.262 +     */
   1.263 +    public <T extends PolicyAssertion> T getImplementation(Class<T> type) {
   1.264 +        if (type.isAssignableFrom(this.getClass())) {
   1.265 +            return type.cast(this);
   1.266 +        }
   1.267 +        else {
   1.268 +            return null;
   1.269 +        }
   1.270 +    }
   1.271 +
   1.272 +    /**
   1.273 +     * An {@code Object.toString()} method override.
   1.274 +     */
   1.275 +    @Override
   1.276 +    public String toString() {
   1.277 +        return toString(0, new StringBuffer()).toString();
   1.278 +    }
   1.279 +
   1.280 +    /**
   1.281 +     * A helper method that appends indented string representation of this instance to the input string buffer.
   1.282 +     *
   1.283 +     * @param indentLevel indentation level to be used.
   1.284 +     * @param buffer buffer to be used for appending string representation of this instance
   1.285 +     * @return modified buffer containing new string representation of the instance
   1.286 +     */
   1.287 +    protected StringBuffer toString(final int indentLevel, final StringBuffer buffer) {
   1.288 +        final String indent = PolicyUtils.Text.createIndent(indentLevel);
   1.289 +        final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1);
   1.290 +
   1.291 +        buffer.append(indent).append("Assertion[").append(this.getClass().getName()).append("] {").append(PolicyUtils.Text.NEW_LINE);
   1.292 +        data.toString(indentLevel + 1, buffer);
   1.293 +        buffer.append(PolicyUtils.Text.NEW_LINE);
   1.294 +
   1.295 +        if (hasParameters()) {
   1.296 +            buffer.append(innerIndent).append("parameters {").append(PolicyUtils.Text.NEW_LINE);
   1.297 +            for (PolicyAssertion parameter : parameters) {
   1.298 +                parameter.toString(indentLevel + 2, buffer).append(PolicyUtils.Text.NEW_LINE);
   1.299 +            }
   1.300 +            buffer.append(innerIndent).append('}').append(PolicyUtils.Text.NEW_LINE);
   1.301 +        } else {
   1.302 +            buffer.append(innerIndent).append("no parameters").append(PolicyUtils.Text.NEW_LINE);
   1.303 +        }
   1.304 +
   1.305 +        if (hasNestedPolicy()) {
   1.306 +            getNestedPolicy().toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE);
   1.307 +        } else {
   1.308 +            buffer.append(innerIndent).append("no nested policy").append(PolicyUtils.Text.NEW_LINE);
   1.309 +        }
   1.310 +
   1.311 +        buffer.append(indent).append('}');
   1.312 +
   1.313 +        return buffer;
   1.314 +    }
   1.315 +
   1.316 +    /**
   1.317 +     * Checks whether this policy alternative is compatible with the provided policy alternative.
   1.318 +     *
   1.319 +     * @param assertion policy alternative used for compatibility test
   1.320 +     * @param mode compatibility mode to be used
   1.321 +     * @return {@code true} if the two policy alternatives are compatible, {@code false} otherwise
   1.322 +     */
   1.323 +    boolean isCompatibleWith(final PolicyAssertion assertion, PolicyIntersector.CompatibilityMode mode) {
   1.324 +        boolean result = this.data.getName().equals(assertion.data.getName()) && (this.hasNestedPolicy() == assertion.hasNestedPolicy());
   1.325 +
   1.326 +        if (result && this.hasNestedPolicy()) {
   1.327 +            result = this.getNestedPolicy().getAssertionSet().isCompatibleWith(assertion.getNestedPolicy().getAssertionSet(), mode);
   1.328 +        }
   1.329 +
   1.330 +        return result;
   1.331 +    }
   1.332 +
   1.333 +    /**
   1.334 +     * An {@code Object.equals(Object obj)} method override.
   1.335 +     */
   1.336 +    @Override
   1.337 +    public boolean equals(final Object obj) {
   1.338 +        if (this == obj) {
   1.339 +            return true;
   1.340 +        }
   1.341 +
   1.342 +        if (!(obj instanceof PolicyAssertion)) {
   1.343 +            return false;
   1.344 +        }
   1.345 +
   1.346 +        final PolicyAssertion that = (PolicyAssertion) obj;
   1.347 +        boolean result = true;
   1.348 +
   1.349 +        result = result && this.data.equals(that.data);
   1.350 +        result = result && this.parameters.equals(that.parameters);
   1.351 +        result = result && ((this.getNestedPolicy() == null) ? ((that.getNestedPolicy() == null) ? true : false) : this.getNestedPolicy().equals(that.getNestedPolicy()));
   1.352 +
   1.353 +        return result;
   1.354 +    }
   1.355 +
   1.356 +    /**
   1.357 +     * An {@code Object.hashCode()} method override.
   1.358 +     */
   1.359 +    @Override
   1.360 +    public int hashCode() {
   1.361 +        int result = 17;
   1.362 +
   1.363 +        result = 37 * result + data.hashCode();
   1.364 +        result = 37 * result + ((hasParameters()) ? 17 : 0);
   1.365 +        result = 37 * result + ((hasNestedPolicy()) ? 17 : 0);
   1.366 +
   1.367 +        return result;
   1.368 +    }
   1.369 +}

mercurial