src/share/jaxws_classes/com/sun/xml/internal/ws/policy/AssertionSet.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

     1 /*
     2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.ws.policy;
    28 import java.util.Collection;
    29 import java.util.Collections;
    30 import java.util.Comparator;
    31 import java.util.Iterator;
    32 import java.util.Set;
    33 import java.util.TreeSet;
    35 import javax.xml.namespace.QName;
    37 import com.sun.xml.internal.ws.policy.privateutil.LocalizationMessages;
    38 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
    39 import java.util.LinkedList;
    40 import java.util.List;
    42 /**
    43  * The AssertionSet is a set of assertions. It represents a single policy alternative.
    44  *
    45  * @author Fabian Ritzmann, Marek Potociar
    46  */
    47 public final class AssertionSet implements Iterable<PolicyAssertion>, Comparable<AssertionSet> {
    48     private static final AssertionSet EMPTY_ASSERTION_SET = new AssertionSet(Collections.unmodifiableList(new LinkedList<PolicyAssertion>()));
    49     /**
    50      * The comparator comapres policy assertions according to their publicly accessible attributes, in the following
    51      * order of attributes:
    52      *
    53      * 1. namespace (not null String)
    54      * 2. local name (not null String)
    55      * 3. value (String): null < "" < "not empty"
    56      * 4. has nested assertions (boolean): false < true
    57      * 5. has nested policy (boolean): false < true
    58      * 6. hashCode comparison
    59      */
    60     private static final Comparator<PolicyAssertion> ASSERTION_COMPARATOR = new Comparator<PolicyAssertion>() {
    61         public int compare(final PolicyAssertion pa1, final PolicyAssertion pa2) {
    62             if (pa1.equals(pa2)) {
    63                 return 0;
    64             }
    66             int result;
    68             result = PolicyUtils.Comparison.QNAME_COMPARATOR.compare(pa1.getName(), pa2.getName());
    69             if (result != 0) {
    70                 return result;
    71             }
    73             result = PolicyUtils.Comparison.compareNullableStrings(pa1.getValue(), pa2.getValue());
    74             if (result != 0) {
    75                 return result;
    76             }
    78             result = PolicyUtils.Comparison.compareBoolean(pa1.hasNestedAssertions(), pa2.hasNestedAssertions());
    79             if (result != 0) {
    80                 return result;
    81             }
    83             result = PolicyUtils.Comparison.compareBoolean(pa1.hasNestedPolicy(), pa2.hasNestedPolicy());
    84             if (result != 0) {
    85                 return result;
    86             }
    88             return Math.round(Math.signum(pa1.hashCode() - pa2.hashCode()));
    89         }
    90     };
    92     private final List<PolicyAssertion> assertions;
    93     private final Set<QName> vocabulary = new TreeSet<QName>(PolicyUtils.Comparison.QNAME_COMPARATOR);
    94     private final Collection<QName> immutableVocabulary = Collections.unmodifiableCollection(vocabulary);
    96     private AssertionSet(List<PolicyAssertion> list) {
    97         assert (list != null) : LocalizationMessages.WSP_0037_PRIVATE_CONSTRUCTOR_DOES_NOT_TAKE_NULL();
    98         this.assertions = list;
    99     }
   101     private AssertionSet(final Collection<AssertionSet> alternatives) {
   102         this.assertions = new LinkedList<PolicyAssertion>();
   103         for (AssertionSet alternative : alternatives) {
   104             addAll(alternative.assertions);
   105         }
   106     }
   108     private boolean add(final PolicyAssertion assertion) {
   109         if (assertion == null) {
   110             return false;
   111         }
   113         if (this.assertions.contains(assertion)) {
   114             return false;
   115         } else {
   116             this.assertions.add(assertion);
   117             this.vocabulary.add(assertion.getName());
   118             return true;
   119         }
   120     }
   122     private boolean addAll(final Collection<? extends PolicyAssertion> assertions) {
   123         boolean result = true;
   125         if (assertions != null) {
   126             for (PolicyAssertion assertion : assertions) {
   127                 result &= add(assertion); // this is here to ensure that vocabulary is built correctly as well
   128             }
   129         }
   131         return result;
   132     }
   134     /**
   135      * Return all assertions contained in this assertion set.
   136      *
   137      * @return All assertions contained in this assertion set
   138      */
   139     Collection<PolicyAssertion> getAssertions() {
   140         return assertions;
   141     }
   143     /**
   144      * Retrieves the vocabulary of this policy expression. The vocabulary is represented by an immutable collection of
   145      * unique QName objects. Each of those objects represents single assertion type contained in the assertion set.
   146      *
   147      * @return immutable collection of assertion types contained in the assertion set (a policy vocabulary).
   148      */
   149     Collection<QName> getVocabulary() {
   150         return immutableVocabulary;
   151     }
   153     /**
   154      * Checks whether this policy alternative is compatible with the provided policy alternative.
   155      *
   156      * @param alternative policy alternative used for compatibility test
   157      * @param mode compatibility mode to be used
   158      * @return {@code true} if the two policy alternatives are compatible, {@code false} otherwise
   159      */
   160     boolean isCompatibleWith(final AssertionSet alternative, PolicyIntersector.CompatibilityMode mode) {
   161         boolean result = (mode == PolicyIntersector.CompatibilityMode.LAX) || this.vocabulary.equals(alternative.vocabulary);
   163         result = result && this.areAssertionsCompatible(alternative, mode);
   164         result = result && alternative.areAssertionsCompatible(this, mode);
   166         return result;
   167     }
   169     private boolean areAssertionsCompatible(final AssertionSet alternative, PolicyIntersector.CompatibilityMode mode) {
   170         nextAssertion: for (PolicyAssertion thisAssertion : this.assertions) {
   171             if ((mode == PolicyIntersector.CompatibilityMode.STRICT) || !thisAssertion.isIgnorable()) {
   172                 for (PolicyAssertion thatAssertion : alternative.assertions) {
   173                     if (thisAssertion.isCompatibleWith(thatAssertion, mode)) {
   174                         continue nextAssertion;
   175                     }
   176                 }
   177                 return false;
   178             }
   179         }
   180         return true;
   181     }
   183     /**
   184      * Creates and returns new assertion set holding content of all provided policy assertion sets.
   185      * <p/>
   186      * This method should not be used to perform a merge of general Policy instances. A client should be aware of the
   187      * method's result meaning and the difference between merge of Policy instances and merge of AssertionSet instances.
   188      *
   189      *
   190      * @param alternatives collection of provided policy assertion sets which content is to be stored in the assertion set.
   191      *        May be {@code null} - empty assertion set is returned in such case.
   192      * @return new instance of assertion set holding the content of all provided policy assertion sets.
   193      */
   194     public static AssertionSet createMergedAssertionSet(final Collection<AssertionSet> alternatives) {
   195         if (alternatives == null || alternatives.isEmpty()) {
   196             return EMPTY_ASSERTION_SET;
   197         }
   199         final AssertionSet result = new AssertionSet(alternatives);
   200         Collections.sort(result.assertions, ASSERTION_COMPARATOR);
   202         return result;
   203     }
   205     /**
   206      * Creates and returns new assertion set holding a set of provided policy assertions.
   207      *
   208      * @param assertions collection of provided policy assertions to be stored in the assertion set. May be {@code null}.
   209      * @return new instance of assertion set holding the provided policy assertions
   210      */
   211     public static AssertionSet createAssertionSet(final Collection<? extends PolicyAssertion> assertions) {
   212         if (assertions == null || assertions.isEmpty()) {
   213             return EMPTY_ASSERTION_SET;
   214         }
   216         final AssertionSet result = new AssertionSet(new LinkedList<PolicyAssertion>());
   217         result.addAll(assertions);
   218         Collections.sort(result.assertions, ASSERTION_COMPARATOR);
   220         return result;
   221     }
   223     public static AssertionSet emptyAssertionSet() {
   224         return EMPTY_ASSERTION_SET;
   225     }
   226     /**
   227      * Returns an iterator over a set of child policy assertion objects.
   228      *
   229      * @return policy assertion Iterator.
   230      */
   231     public Iterator<PolicyAssertion> iterator() {
   232         return this.assertions.iterator();
   233     }
   235     /**
   236      * Searches for assertions with given name. Only assertions that are contained as immediate children of the assertion set are
   237      * searched, i.e. nested policies are not searched.
   238      *
   239      * @param name The fully qualified name of searched assertion
   240      * @return List of all assertions matching the requested name. If no assertions are found, the returned list is empty
   241      * (i.e. {@code null} value is never returned).
   242      */
   243     public Collection<PolicyAssertion> get(final QName name) {
   244         final List<PolicyAssertion> matched = new LinkedList<PolicyAssertion>();
   246         if (vocabulary.contains(name)) {
   247             // we iterate the assertion set only if we are sure we contain such assertion name in our vocabulary
   248             for (PolicyAssertion assertion : assertions) {
   249                 if (assertion.getName().equals(name)) {
   250                     matched.add(assertion);
   251                 }
   252             }
   253         }
   255         return matched;
   256     }
   258     /**
   259      * Returns {@code true} if this assertion set contains no assertions.
   260      *
   261      * @return {@code true} if this assertion set contains no assertions.
   262      */
   263     public boolean isEmpty() {
   264         return assertions.isEmpty();
   265     }
   267     /**
   268      * Returns true if the assertion set contains the assertion name specified in its vocabulary
   269      *
   270      * @param assertionName the fully qualified name of the assertion
   271      * @return {@code true}, if an assertion with the given name could be found in the assertion set vocabulary {@code false} otherwise.
   272      */
   273     public boolean contains(final QName assertionName) {
   274         return vocabulary.contains(assertionName);
   275     }
   277     /**
   278      * An {@code Comparable<T>.compareTo(T o)} interface method implementation.
   279      * @param that other alternative to compare with
   280      */
   281     public int compareTo(final AssertionSet that) {
   282         if (this.equals(that)) {
   283             return 0;
   284         }
   286         // comparing vocabularies
   287         final Iterator<QName> vIterator1 = this.getVocabulary().iterator();
   288         final Iterator<QName> vIterator2 = that.getVocabulary().iterator();
   289         while (vIterator1.hasNext()) {
   290             final QName entry1 = vIterator1.next();
   291             if (vIterator2.hasNext()) {
   292                 final QName entry2 = vIterator2.next();
   293                 final int result = PolicyUtils.Comparison.QNAME_COMPARATOR.compare(entry1, entry2);
   294                 if (result != 0) {
   295                     return result;
   296                 }
   297             } else {
   298                 return 1; // we have more entries in this vocabulary
   299             }
   300         }
   302         if (vIterator2.hasNext()) {
   303             return -1;  // we have more entries in that vocabulary
   304         }
   306         // vocabularies are equal => comparing assertions
   307         final Iterator<PolicyAssertion> pIterator1 = this.getAssertions().iterator();
   308         final Iterator<PolicyAssertion> pIterator2 = that.getAssertions().iterator();
   309         while (pIterator1.hasNext()) {
   310             final PolicyAssertion pa1 = pIterator1.next();
   311             if (pIterator2.hasNext()) {
   312                 final PolicyAssertion pa2 = pIterator2.next();
   313                 final int result = ASSERTION_COMPARATOR.compare(pa1, pa2);
   314                 if (result != 0) {
   315                     return result;
   316                 }
   317             } else {
   318                 return 1; // we have more entries in this assertion set
   319             }
   320         }
   322         if (pIterator2.hasNext()) {
   323             return -1;  // we have more entries in that assertion set
   324         }
   326         // seems like objects are very simmilar although not equal => we must not return 0 otherwise the TreeSet
   327         // holding this element would discard the newly added element. Thus we return that the first argument is
   328         // greater than second (just because it is first...)
   329         return 1;
   330     }
   332     /**
   333      * An {@code Object.equals(Object obj)} method override.
   334      */
   335     public boolean equals(final Object obj) {
   336         if (this == obj) {
   337             return true;
   338         }
   340         if (!(obj instanceof AssertionSet)) {
   341             return false;
   342         }
   344         final AssertionSet that = (AssertionSet) obj;
   345         boolean result = true;
   347         result = result && this.vocabulary.equals(that.vocabulary);
   348         result = result && this.assertions.size() == that.assertions.size() && this.assertions.containsAll(that.assertions);
   350         return result;
   351     }
   353     /**
   354      * An {@code Object.hashCode()} method override.
   355      */
   356     public int hashCode() {
   357         int result = 17;
   359         result = 37 * result + vocabulary.hashCode();
   360         result = 37 * result + assertions.hashCode();
   362         return result;
   363     }
   365     /**
   366      * An {@code Object.toString()} method override.
   367      */
   368     public String toString() {
   369         return toString(0, new StringBuffer()).toString();
   370     }
   372     /**
   373      * A helper method that appends indented string representation of this instance to the input string buffer.
   374      *
   375      * @param indentLevel indentation level to be used.
   376      * @param buffer buffer to be used for appending string representation of this instance
   377      * @return modified buffer containing new string representation of the instance
   378      */
   379     StringBuffer toString(final int indentLevel, final StringBuffer buffer) {
   380         final String indent = PolicyUtils.Text.createIndent(indentLevel);
   381         final String innerIndent = PolicyUtils.Text.createIndent(indentLevel + 1);
   383         buffer.append(indent).append("assertion set {").append(PolicyUtils.Text.NEW_LINE);
   385         if (assertions.isEmpty()) {
   386             buffer.append(innerIndent).append("no assertions").append(PolicyUtils.Text.NEW_LINE);
   387         } else {
   388             for (PolicyAssertion assertion : assertions) {
   389                 assertion.toString(indentLevel + 1, buffer).append(PolicyUtils.Text.NEW_LINE);
   390             }
   391         }
   393         buffer.append(indent).append('}');
   395         return buffer;
   396     }
   397 }

mercurial