You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by jb...@apache.org on 2020/08/11 07:48:35 UTC

[activemq] branch activemq-5.16.x updated: AMQ-7512 - Consolidate XBean byte parsing

This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch activemq-5.16.x
in repository https://gitbox.apache.org/repos/asf/activemq.git


The following commit(s) were added to refs/heads/activemq-5.16.x by this push:
     new 1b0a47b  AMQ-7512 - Consolidate XBean byte parsing
1b0a47b is described below

commit 1b0a47bed5b8ff426284fb93d87173a26c8542cf
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Fri Jul 10 17:22:09 2020 +0100

    AMQ-7512 - Consolidate XBean byte parsing
    
    (cherry picked from commit 930b18df6575e89f1df4c4c6fa9e1e8ac68b2591)
---
 .../activemq/util/MemoryIntPropertyEditor.java     |  33 +-----
 .../apache/activemq/util/MemoryPropertyEditor.java |  33 +-----
 .../activemq/util/XBeanByteConverterUtil.java      |  66 +++++++++++
 .../apache/activemq/util/PropertyEditorTest.java   | 128 +++++++++++++++++++++
 .../activemq/plugin/PropertiesPlaceHolderUtil.java |  23 +---
 5 files changed, 201 insertions(+), 82 deletions(-)

diff --git a/activemq-broker/src/main/java/org/apache/activemq/util/MemoryIntPropertyEditor.java b/activemq-broker/src/main/java/org/apache/activemq/util/MemoryIntPropertyEditor.java
index 6c167a5..5d48850 100644
--- a/activemq-broker/src/main/java/org/apache/activemq/util/MemoryIntPropertyEditor.java
+++ b/activemq-broker/src/main/java/org/apache/activemq/util/MemoryIntPropertyEditor.java
@@ -17,8 +17,6 @@
 package org.apache.activemq.util;
 
 import java.beans.PropertyEditorSupport;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 /**
  * Used by xbean to set integers.
@@ -31,36 +29,7 @@ import java.util.regex.Pattern;
  */
 public class MemoryIntPropertyEditor extends PropertyEditorSupport {
     public void setAsText(String text) throws IllegalArgumentException {
-
-        Pattern p = Pattern.compile("^\\s*(\\d+)\\s*(b)?\\s*$", Pattern.CASE_INSENSITIVE);
-        Matcher m = p.matcher(text);
-        if (m.matches()) {
-            setValue(Integer.valueOf(Integer.parseInt(m.group(1))));
-            return;
-        }
-
-        p = Pattern.compile("^\\s*(\\d+)\\s*k(b)?\\s*$", Pattern.CASE_INSENSITIVE);
-        m = p.matcher(text);
-        if (m.matches()) {
-            setValue(Integer.valueOf(Integer.parseInt(m.group(1)) * 1024));
-            return;
-        }
-
-        p = Pattern.compile("^\\s*(\\d+)\\s*m(b)?\\s*$", Pattern.CASE_INSENSITIVE);
-        m = p.matcher(text);
-        if (m.matches()) {
-            setValue(Integer.valueOf(Integer.parseInt(m.group(1)) * 1024 * 1024));
-            return;
-        }
-
-        p = Pattern.compile("^\\s*(\\d+)\\s*g(b)?\\s*$", Pattern.CASE_INSENSITIVE);
-        m = p.matcher(text);
-        if (m.matches()) {
-            setValue(Integer.valueOf(Integer.parseInt(m.group(1)) * 1024 * 1024 * 1024));
-            return;
-        }
-
-        throw new IllegalArgumentException("Could convert not to a memory size: " + text);
+        setValue(XBeanByteConverterUtil.convertToIntegerBytes(text));
     }
 
     public String getAsText() {
diff --git a/activemq-broker/src/main/java/org/apache/activemq/util/MemoryPropertyEditor.java b/activemq-broker/src/main/java/org/apache/activemq/util/MemoryPropertyEditor.java
index 09fe06e..4071efa 100644
--- a/activemq-broker/src/main/java/org/apache/activemq/util/MemoryPropertyEditor.java
+++ b/activemq-broker/src/main/java/org/apache/activemq/util/MemoryPropertyEditor.java
@@ -17,8 +17,6 @@
 package org.apache.activemq.util;
 
 import java.beans.PropertyEditorSupport;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 /**
  * Used by xbean to set longs.
@@ -31,36 +29,7 @@ import java.util.regex.Pattern;
  */
 public class MemoryPropertyEditor extends PropertyEditorSupport {
     public void setAsText(String text) throws IllegalArgumentException {
-
-        Pattern p = Pattern.compile("^\\s*(\\d+)\\s*(b)?\\s*$", Pattern.CASE_INSENSITIVE);
-        Matcher m = p.matcher(text);
-        if (m.matches()) {
-            setValue(Long.valueOf(Long.parseLong(m.group(1))));
-            return;
-        }
-
-        p = Pattern.compile("^\\s*(\\d+)\\s*k(b)?\\s*$", Pattern.CASE_INSENSITIVE);
-        m = p.matcher(text);
-        if (m.matches()) {
-            setValue(Long.valueOf(Long.parseLong(m.group(1)) * 1024));
-            return;
-        }
-
-        p = Pattern.compile("^\\s*(\\d+)\\s*m(b)?\\s*$", Pattern.CASE_INSENSITIVE);
-        m = p.matcher(text);
-        if (m.matches()) {
-            setValue(Long.valueOf(Long.parseLong(m.group(1)) * 1024 * 1024));
-            return;
-        }
-
-        p = Pattern.compile("^\\s*(\\d+)\\s*g(b)?\\s*$", Pattern.CASE_INSENSITIVE);
-        m = p.matcher(text);
-        if (m.matches()) {
-            setValue(Long.valueOf(Long.parseLong(m.group(1)) * 1024 * 1024 * 1024));
-            return;
-        }
-
-        throw new IllegalArgumentException("Could convert not to a memory size: " + text);
+        setValue(XBeanByteConverterUtil.convertToLongBytes(text));
     }
 
     public String getAsText() {
diff --git a/activemq-broker/src/main/java/org/apache/activemq/util/XBeanByteConverterUtil.java b/activemq-broker/src/main/java/org/apache/activemq/util/XBeanByteConverterUtil.java
new file mode 100644
index 0000000..731f571
--- /dev/null
+++ b/activemq-broker/src/main/java/org/apache/activemq/util/XBeanByteConverterUtil.java
@@ -0,0 +1,66 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.util;
+
+import java.io.File;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Converts string values like "20 Mb", "1024kb", and "1g" to long or int values in bytes.
+ */
+public final class XBeanByteConverterUtil {
+
+    private static final Pattern[] BYTE_MATCHERS = new Pattern[] {
+            Pattern.compile("^\\s*(\\d+)\\s*(b)?\\s*$", Pattern.CASE_INSENSITIVE),
+            Pattern.compile("^\\s*(\\d+)\\s*k(b)?\\s*$", Pattern.CASE_INSENSITIVE),
+            Pattern.compile("^\\s*(\\d+)\\s*m(b)?\\s*$", Pattern.CASE_INSENSITIVE),
+            Pattern.compile("^\\s*(\\d+)\\s*g(b)?\\s*$", Pattern.CASE_INSENSITIVE)};
+
+    private XBeanByteConverterUtil() {
+        // complete
+    }
+
+    public static Long convertToLongBytes(String str) throws IllegalArgumentException {
+        for (int i = 0; i < BYTE_MATCHERS.length; i++) {
+            Matcher matcher = BYTE_MATCHERS[i].matcher(str);
+            if (matcher.matches()) {
+                long value = Long.parseLong(matcher.group(1));
+                for (int j = 1; j <= i; j++) {
+                    value *= 1024;
+                }
+                return Long.valueOf(value);
+            }
+        }
+        throw new IllegalArgumentException("Could not convert to a memory size: " + str);
+    }
+
+    public static Integer convertToIntegerBytes(String str) throws IllegalArgumentException {
+        for (int i = 0; i < BYTE_MATCHERS.length; i++) {
+            Matcher matcher = BYTE_MATCHERS[i].matcher(str);
+            if (matcher.matches()) {
+                int value = Integer.parseInt(matcher.group(1));
+                for (int j = 1; j <= i; j++) {
+                    value *= 1024;
+                }
+                return Integer.valueOf(value);
+            }
+        }
+        throw new IllegalArgumentException("Could not convert to a memory size: " + str);
+    }
+    
+}
diff --git a/activemq-broker/src/test/java/org/apache/activemq/util/PropertyEditorTest.java b/activemq-broker/src/test/java/org/apache/activemq/util/PropertyEditorTest.java
new file mode 100644
index 0000000..e8352f3
--- /dev/null
+++ b/activemq-broker/src/test/java/org/apache/activemq/util/PropertyEditorTest.java
@@ -0,0 +1,128 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class PropertyEditorTest {
+
+    @Test
+    public void testLongBytes() throws Exception {
+        MemoryPropertyEditor propertyEditor = new MemoryPropertyEditor();
+        String expectedResult = String.valueOf(1000L);
+
+        propertyEditor.setAsText("1000b");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000B");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+    }
+
+    @Test
+    public void testIntegerBytes() throws Exception {
+        MemoryIntPropertyEditor propertyEditor = new MemoryIntPropertyEditor();
+        String expectedResult = String.valueOf(1000);
+
+        propertyEditor.setAsText("1000b");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000B");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+    }
+
+    @Test
+    public void testLongKiloBytes() throws Exception {
+        MemoryPropertyEditor propertyEditor = new MemoryPropertyEditor();
+        String expectedResult = String.valueOf(1024 * 1000L);
+
+        propertyEditor.setAsText("1000kb");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000k");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000KB");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+    }
+
+    @Test
+    public void testIntegerKiloBytes() throws Exception {
+        MemoryIntPropertyEditor propertyEditor = new MemoryIntPropertyEditor();
+        String expectedResult = String.valueOf(1024 * 1000);
+
+        propertyEditor.setAsText("1000kb");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000k");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000KB");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+    }
+
+    @Test
+    public void testLongMegaBytes() throws Exception {
+        MemoryPropertyEditor propertyEditor = new MemoryPropertyEditor();
+        String expectedResult = String.valueOf(1024 * 1024 * 1000L);
+
+        propertyEditor.setAsText("1000mb");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000m");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000MB");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+    }
+
+    @Test
+    public void testIntegerMegaBytes() throws Exception {
+        MemoryIntPropertyEditor propertyEditor = new MemoryIntPropertyEditor();
+        String expectedResult = String.valueOf(1024 * 1024 * 1000);
+
+        propertyEditor.setAsText("1000mb");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000m");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000MB");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+    }
+
+    @Test
+    public void testLongGigaBytes() throws Exception {
+        MemoryPropertyEditor propertyEditor = new MemoryPropertyEditor();
+        String expectedResult = String.valueOf(1024 * 1024 * 1024 * 1000L);
+
+        propertyEditor.setAsText("1000gb");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000g");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000GB");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+    }
+
+    @Test
+    public void testIntegerGigaBytes() throws Exception {
+        MemoryIntPropertyEditor propertyEditor = new MemoryIntPropertyEditor();
+        String expectedResult = String.valueOf(1024 * 1024 * 1024 * 1000);
+
+        propertyEditor.setAsText("1000gb");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000g");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+        propertyEditor.setAsText("1000GB");
+        assertEquals(expectedResult, propertyEditor.getAsText());
+    }
+}
diff --git a/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/PropertiesPlaceHolderUtil.java b/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/PropertiesPlaceHolderUtil.java
index fdfe006..16037c1 100644
--- a/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/PropertiesPlaceHolderUtil.java
+++ b/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/PropertiesPlaceHolderUtil.java
@@ -18,6 +18,7 @@ package org.apache.activemq.plugin;
 
 import org.apache.activemq.broker.BrokerContext;
 import org.apache.activemq.spring.Utils;
+import org.apache.activemq.util.XBeanByteConverterUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.FactoryBean;
@@ -75,27 +76,13 @@ public class PropertiesPlaceHolderUtil {
         return replaceBytePostfix(str);
     }
 
-    static Pattern[] byteMatchers = new Pattern[] {
-            Pattern.compile("^\\s*(\\d+)\\s*(b)?\\s*$", Pattern.CASE_INSENSITIVE),
-            Pattern.compile("^\\s*(\\d+)\\s*k(b)?\\s*$", Pattern.CASE_INSENSITIVE),
-            Pattern.compile("^\\s*(\\d+)\\s*m(b)?\\s*$", Pattern.CASE_INSENSITIVE),
-            Pattern.compile("^\\s*(\\d+)\\s*g(b)?\\s*$", Pattern.CASE_INSENSITIVE)};
-
     // xbean can Xb, Xkb, Xmb, Xg etc
     private String replaceBytePostfix(String str) {
         try {
-            for (int i=0; i< byteMatchers.length; i++) {
-                Matcher matcher = byteMatchers[i].matcher(str);
-                if (matcher.matches()) {
-                    long value = Long.parseLong(matcher.group(1));
-                    for (int j=1; j<=i; j++) {
-                        value *= 1024;
-                    }
-                    return String.valueOf(value);
-                }
-            }
-        } catch (NumberFormatException ignored) {
-            LOG.debug("nfe on: " + str, ignored);
+            Long value = XBeanByteConverterUtil.convertToLongBytes(str);
+            return String.valueOf(value);
+        } catch (IllegalArgumentException ignored) {
+            LOG.debug("iae on: " + str, ignored);
         }
         return str;
     }