ARTEMIS-853 Support for exclusive consumers

Rationalise and re-use URISupport.

Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/38c45c92
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/38c45c92
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/38c45c92

Branch: refs/heads/master
Commit: 38c45c92142cd6a3b9f49f2d436d7a0665309c00
Parents: dc41f3c
Author: Michael André Pearce <michael.andre.pea...@me.com>
Authored: Fri Feb 2 18:19:40 2018 +0000
Committer: Michael Andre Pearce <michael.andre.pea...@me.com>
Committed: Wed Feb 7 15:27:29 2018 +0000

----------------------------------------------------------------------
 .../artemis/api/core/ParameterisedAddress.java  | 55 +++++++-------------
 .../activemq/artemis/utils/uri/URISupport.java  | 32 ++++++++----
 .../jms/client/ActiveMQParameterTest.java       | 17 ++++++
 3 files changed, 58 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/38c45c92/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java
----------------------------------------------------------------------
diff --git 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java
 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java
index bbc3c4d..6a6d45c 100644
--- 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java
+++ 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/ParameterisedAddress.java
@@ -16,46 +16,32 @@
  */
 package org.apache.activemq.artemis.api.core;
 
+import static 
org.apache.activemq.artemis.utils.uri.URISupport.appendParameters;
+import static org.apache.activemq.artemis.utils.uri.URISupport.parseQuery;
+
+import java.net.URISyntaxException;
 import java.util.Map;
 
+import org.apache.activemq.artemis.utils.uri.URISupport;
+
 public class ParameterisedAddress {
 
-   public static SimpleString toParameterisedAddress(SimpleString address, 
Map<String, String> parameters) {
-      if (parameters != null && parameters.size() > 0) {
+   public static SimpleString toParameterisedAddress(SimpleString address, 
Map<String, String> parameters) throws URISyntaxException {
+      if (parameters != null && !parameters.isEmpty()) {
          return 
SimpleString.toSimpleString(toParameterisedAddress(address.toString(), 
parameters));
       } else {
          return address;
       }
    }
 
-   public static String toParameterisedAddress(String address, Map<String, 
String> parameters) {
-      if (parameters != null && parameters.size() > 0) {
-         StringBuilder stringBuilder = new 
StringBuilder(address).append(PARAMETER_MARKER);
-         return toParameterString(stringBuilder, parameters).toString();
+   public static String toParameterisedAddress(String address, Map<String, 
String> parameters) throws URISyntaxException {
+      if (parameters != null && !parameters.isEmpty()) {
+         return appendParameters(new StringBuilder(address), 
parameters).toString();
       } else {
          return address;
       }
    }
 
-   private static StringBuilder toParameterString(StringBuilder stringBuilder, 
Map<String, String> parameters) {
-      boolean first = true;
-      for (Map.Entry<String, String> entry : parameters.entrySet()) {
-         if (first) {
-            first = false;
-         } else {
-            stringBuilder.append(PARAMETER_SEPERATOR);
-         }
-         
stringBuilder.append(entry.getKey()).append(PARAMETER_KEY_VALUE_SEPERATOR).append(entry.getValue());
-      }
-      return stringBuilder;
-   }
-
-   public static char PARAMETER_SEPERATOR = '&';
-   public static char PARAMETER_KEY_VALUE_SEPERATOR = '=';
-   public static char PARAMETER_MARKER = '?';
-   public static String PARAMETER_SEPERATOR_STRING = 
Character.toString(PARAMETER_SEPERATOR);
-   public static String PARAMETER_KEY_VALUE_SEPERATOR_STRING = 
Character.toString(PARAMETER_KEY_VALUE_SEPERATOR);
-   public static String PARAMETER_MARKER_STRING = 
Character.toString(PARAMETER_MARKER);
    private final SimpleString address;
    private final QueueAttributes queueAttributes;
 
@@ -81,22 +67,17 @@ public class ParameterisedAddress {
    }
 
    public ParameterisedAddress(String address) {
-      int index = address.indexOf(PARAMETER_MARKER);
+      int index = address.indexOf('?');
       if (index == -1) {
          this.address = SimpleString.toSimpleString(address);
          this.queueAttributes = null;
       } else {
          this.address = SimpleString.toSimpleString(address.substring(0, 
index));
-         String parametersString = address.substring(index + 1, 
address.length());
-         String[] parameterPairs = 
parametersString.split(PARAMETER_SEPERATOR_STRING);
          QueueAttributes queueAttributes = new QueueAttributes();
-         for (String param : parameterPairs) {
-            String[] keyValue = 
param.split(PARAMETER_KEY_VALUE_SEPERATOR_STRING);
-            if (keyValue.length != 2) {
-               throw new IllegalArgumentException("Malformed parameter section 
" + param);
-            } else {
-               queueAttributes.set(keyValue[0], keyValue[1]);
-            }
+         try {
+            parseQuery(address).forEach(queueAttributes::set);
+         } catch (URISyntaxException use) {
+            throw new IllegalArgumentException("Malformed parameters in 
address " + address);
          }
          this.queueAttributes = queueAttributes;
       }
@@ -107,11 +88,11 @@ public class ParameterisedAddress {
    }
 
    public static boolean isParameterised(String address) {
-      return address.contains(PARAMETER_MARKER_STRING);
+      return URISupport.containsQuery(address);
    }
 
    public static boolean isParameterised(SimpleString address) {
-      return address.contains(PARAMETER_MARKER);
+      return URISupport.containsQuery(address);
    }
 
 }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/38c45c92/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java
----------------------------------------------------------------------
diff --git 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java
 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java
index e26993d..7530759 100644
--- 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java
+++ 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/URISupport.java
@@ -27,6 +27,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.activemq.artemis.api.core.SimpleString;
+
 /**
  * Utility class that provides methods for parsing URI's
  *
@@ -75,7 +77,7 @@ public class URISupport {
       }
 
       public URI toURI() throws URISyntaxException {
-         StringBuffer sb = new StringBuffer();
+         StringBuilder sb = new StringBuilder();
          if (scheme != null) {
             sb.append(scheme);
             sb.append(':');
@@ -98,18 +100,23 @@ public class URISupport {
             sb.append('/');
             sb.append(path);
          }
-         if (!parameters.isEmpty()) {
-            sb.append("?");
-            sb.append(createQueryString(parameters));
-         }
+         appendParameters(sb, parameters);
          if (fragment != null) {
-            sb.append("#");
+            sb.append('#');
             sb.append(fragment);
          }
          return new URI(sb.toString());
       }
    }
 
+   public static StringBuilder appendParameters(StringBuilder sb, Map<String, 
String> parameters) throws URISyntaxException {
+      if (!parameters.isEmpty()) {
+         sb.append('?');
+         sb.append(createQueryString(parameters));
+      }
+      return sb;
+   }
+
    /**
     * Give a URI break off any URI options and store them in a Key / Value 
Mapping.
     *
@@ -122,8 +129,7 @@ public class URISupport {
          uri = uri.substring(uri.lastIndexOf("?") + 1); // get only the 
relevant part of the query
          Map<String, String> rc = new HashMap<>();
          if (uri != null && !uri.isEmpty()) {
-            parseParameters(rc, uri.split("&"));
-            parseParameters(rc, uri.split(";"));
+            parseParameters(rc, uri.split("[&;]"));
          }
          return rc;
       } catch (UnsupportedEncodingException e) {
@@ -131,6 +137,14 @@ public class URISupport {
       }
    }
 
+   public static boolean containsQuery(String uri) {
+      return uri.contains("?");
+   }
+
+   public static boolean containsQuery(SimpleString uri) {
+      return uri.contains('?');
+   }
+
    private static void parseParameters(Map<String, String> rc,
                                        String[] parameters) throws 
UnsupportedEncodingException {
       for (String parameter : parameters) {
@@ -198,7 +212,7 @@ public class URISupport {
                                      Map<String, String> queryParameters,
                                      String optionPrefix) throws 
URISyntaxException {
       if (queryParameters != null && !queryParameters.isEmpty()) {
-         StringBuffer newQuery = uri.getRawQuery() != null ? new 
StringBuffer(uri.getRawQuery()) : new StringBuffer();
+         StringBuilder newQuery = uri.getRawQuery() != null ? new 
StringBuilder(uri.getRawQuery()) : new StringBuilder();
          for (Map.Entry<String, String> param : queryParameters.entrySet()) {
             if (param.getKey().startsWith(optionPrefix)) {
                if (newQuery.length() != 0) {

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/38c45c92/artemis-jms-client/src/test/java/org/apache/activemq/artemis/jms/client/ActiveMQParameterTest.java
----------------------------------------------------------------------
diff --git 
a/artemis-jms-client/src/test/java/org/apache/activemq/artemis/jms/client/ActiveMQParameterTest.java
 
b/artemis-jms-client/src/test/java/org/apache/activemq/artemis/jms/client/ActiveMQParameterTest.java
index cf42fe3..1cb179d 100644
--- 
a/artemis-jms-client/src/test/java/org/apache/activemq/artemis/jms/client/ActiveMQParameterTest.java
+++ 
b/artemis-jms-client/src/test/java/org/apache/activemq/artemis/jms/client/ActiveMQParameterTest.java
@@ -18,6 +18,7 @@ package org.apache.activemq.artemis.jms.client;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
@@ -42,5 +43,21 @@ public class ActiveMQParameterTest {
 
       activeMQDestination = new 
ActiveMQQueue("jms.queue.foo?last-value=false");
       assertFalse(activeMQDestination.getQueueAttributes().getLastValue());
+
+   }
+
+   @Test
+   public void testMultipleQueueParameters() {
+      ActiveMQDestination activeMQDestination = new 
ActiveMQQueue("jms.queue.foo?last-value=true&exclusive=true");
+      assertEquals("jms.queue.foo", activeMQDestination.getAddress());
+      assertTrue(activeMQDestination.getQueueAttributes().getLastValue());
+      assertTrue(activeMQDestination.getQueueAttributes().getExclusive());
+   }
+
+   @Test
+   public void testNoQueueParameters() {
+      ActiveMQDestination activeMQDestination = new 
ActiveMQQueue("jms.queue.foo");
+      assertEquals("jms.queue.foo", activeMQDestination.getAddress());
+      assertNull(activeMQDestination.getQueueAttributes());
    }
 }

Reply via email to