Merge pull request #550 from coheigea/AMQ-7512

AMQ-7512 - Consolidate XBean byte parsing
This commit is contained in:
Jean-Baptiste Onofré 2020-08-11 09:47:49 +02:00 committed by GitHub
commit 213f381c95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 201 additions and 82 deletions

View File

@ -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() {

View File

@ -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() {

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -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;
}