diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/SimpleString.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/SimpleString.java index 91d884b375..b2a36d63aa 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/SimpleString.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/SimpleString.java @@ -447,6 +447,25 @@ public final class SimpleString implements CharSequence, Serializable, Comparabl return false; } + public boolean containsEitherOf(final char c, final char d) { + if (this.str != null) { + return this.str.indexOf(c) != -1 || this.str.indexOf(d) != -1; + } + final byte lowc = (byte) (c & 0xFF); // low byte + final byte highc = (byte) (c >> 8 & 0xFF); // high byte + + final byte lowd = (byte) (d & 0xFF); // low byte + final byte highd = (byte) (d >> 8 & 0xFF); // high byte + + for (int i = 0; i + 1 < data.length; i += 2) { + if ( data[i] == lowc && data[i + 1] == highc || + data[i] == lowd && data[i + 1] == highd ) { + return true; + } + } + return false; + } + /** * Concatenates a SimpleString and a String * diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/AddressImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/AddressImpl.java index 12ef067669..a0d15aaab2 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/AddressImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/AddressImpl.java @@ -17,7 +17,6 @@ package org.apache.activemq.artemis.core.postoffice.impl; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import org.apache.activemq.artemis.api.core.SimpleString; @@ -37,7 +36,7 @@ public class AddressImpl implements Address { private final boolean containsWildCard; - private List
linkedAddresses = null; + private final List linkedAddresses = new ArrayList<>(); private final WildcardConfiguration wildcardConfiguration; @@ -49,7 +48,7 @@ public class AddressImpl implements Address { this.address = address; this.wildcardConfiguration = wildcardConfiguration; addressParts = address.split(wildcardConfiguration.getDelimiter()); - containsWildCard = address.contains(wildcardConfiguration.getSingleWord()) || address.contains(wildcardConfiguration.getAnyWords()); + containsWildCard = address.containsEitherOf(wildcardConfiguration.getSingleWord(), wildcardConfiguration.getAnyWords()); } @Override @@ -69,24 +68,16 @@ public class AddressImpl implements Address { @Override public List getLinkedAddresses() { - return linkedAddresses == null ? Collections.emptyList() : linkedAddresses; + return linkedAddresses; } @Override public void addLinkedAddress(final Address address) { - if (linkedAddresses == null) { - linkedAddresses = new ArrayList<>(1); - } - if (!linkedAddresses.contains(address)) { - linkedAddresses.add(address); - } + linkedAddresses.add(address); } @Override public void removeLinkedAddress(final Address actualAddress) { - if (linkedAddresses == null) { - return; - } linkedAddresses.remove(actualAddress); } diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/WildcardAddressManager.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/WildcardAddressManager.java index 6dc173734b..f30fa62b5d 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/WildcardAddressManager.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/postoffice/impl/WildcardAddressManager.java @@ -183,29 +183,30 @@ public class WildcardAddressManager extends SimpleAddressManager { } private synchronized Address addAndUpdateAddressMap(final SimpleString address) { - Address add = new AddressImpl(address, wildcardConfiguration); Address actualAddress; - if (add.containsWildCard()) { + final boolean containsWildcard = address.containsEitherOf(wildcardConfiguration.getAnyWords(), wildcardConfiguration.getSingleWord()); + if (containsWildcard) { actualAddress = wildCardAddresses.get(address); } else { actualAddress = addresses.get(address); } if (actualAddress == null) { - actualAddress = add; + actualAddress = new AddressImpl(address, wildcardConfiguration); addAddress(address, actualAddress); - } - if (actualAddress.containsWildCard()) { - for (Address destAdd : addresses.values()) { - if (destAdd.matches(actualAddress)) { - destAdd.addLinkedAddress(actualAddress); - actualAddress.addLinkedAddress(destAdd); + + if (containsWildcard) { + for (Address destAdd : addresses.values()) { + if (destAdd.matches(actualAddress)) { + destAdd.addLinkedAddress(actualAddress); + actualAddress.addLinkedAddress(destAdd); + } } - } - } else { - for (Address destAdd : wildCardAddresses.values()) { - if (actualAddress.matches(destAdd)) { - destAdd.addLinkedAddress(actualAddress); - actualAddress.addLinkedAddress(destAdd); + } else { + for (Address destAdd : wildCardAddresses.values()) { + if (actualAddress.matches(destAdd)) { + destAdd.addLinkedAddress(actualAddress); + actualAddress.addLinkedAddress(destAdd); + } } } }