Skip to content

Commit 5742dba

Browse files
committed
- Fixed incorrect parsing of time values
- Added test for https://issues.redhat.com/browse/JGRP-2867
1 parent 3d693c8 commit 5742dba

File tree

3 files changed

+85
-18
lines changed

3 files changed

+85
-18
lines changed

src/org/jgroups/conf/PropertyConverters.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.util.List;
1717
import java.util.stream.Collectors;
1818

19+
import static org.jgroups.conf.AttributeType.TIME;
20+
1921
/**
2022
* Groups a set of standard PropertyConverter(s) supplied by JGroups.
2123
*
@@ -164,34 +166,33 @@ public Object convert(Object obj, Class<?> propertyFieldType, String propertyNam
164166
}
165167

166168
@Override
167-
public Object convert(Property annotation, Object obj, Class<?> propertyFieldType, String propertyName, String propertyValue,
169+
public Object convert(Property ann, Object obj, Class<?> propertyFieldType, String propertyName, String propValue,
168170
boolean check_scope, StackType ip_version) throws Exception {
169-
if(propertyValue == null)
171+
if(propValue == null)
170172
throw new NullPointerException("Property value cannot be null");
171173

172174
if(Boolean.TYPE.equals(propertyFieldType))
173-
return Boolean.parseBoolean(propertyValue);
175+
return Boolean.parseBoolean(propValue);
174176
if(Integer.TYPE.equals(propertyFieldType))
175-
return Util.readBytesInteger(propertyValue);
176-
if(Long.TYPE.equals(propertyFieldType)) {
177-
return annotation != null && annotation.type() == AttributeType.TIME ? Util.readDurationLong(propertyValue, annotation.unit()) : Util.readBytesLong(propertyValue);
178-
}
177+
return ann != null && ann.type() == TIME? Util.readDurationInt(propValue, ann.unit()) : Util.readBytesInteger(propValue);
178+
if(Long.TYPE.equals(propertyFieldType))
179+
return ann != null && ann.type() == TIME ? Util.readDurationLong(propValue, ann.unit()) : Util.readBytesLong(propValue);
179180
if(Byte.TYPE.equals(propertyFieldType))
180-
return Byte.parseByte(propertyValue);
181+
return Byte.parseByte(propValue);
181182
if(Double.TYPE.equals(propertyFieldType))
182-
return Util.readBytesDouble(propertyValue);
183+
return Util.readBytesDouble(propValue);
183184
if(Short.TYPE.equals(propertyFieldType))
184-
return Short.parseShort(propertyValue);
185+
return Short.parseShort(propValue);
185186
if(Float.TYPE.equals(propertyFieldType))
186-
return Float.parseFloat(propertyValue);
187+
return Float.parseFloat(propValue);
187188
if(String[].class.equals(propertyFieldType))
188-
return Util.parseStringArray(propertyValue, ",");
189+
return Util.parseStringArray(propValue, ",");
189190
if(propertyFieldType.isEnum())
190-
return Util.createEnum(propertyValue, propertyFieldType);
191+
return Util.createEnum(propValue, propertyFieldType);
191192
if(InetAddress.class.equals(propertyFieldType)) {
192193
InetAddress retval=null;
193-
if(propertyValue.contains(",")) {
194-
List<String> addrs=Util.parseCommaDelimitedStrings(propertyValue);
194+
if(propValue.contains(",")) {
195+
List<String> addrs=Util.parseCommaDelimitedStrings(propValue);
195196
for(String addr : addrs) {
196197
try {
197198
retval=Util.getAddress(addr, ip_version);
@@ -202,10 +203,10 @@ public Object convert(Property annotation, Object obj, Class<?> propertyFieldTyp
202203
}
203204
}
204205
if(retval == null)
205-
throw new IllegalArgumentException(String.format("failed parsing attribute %s with value %s", propertyName, propertyValue));
206+
throw new IllegalArgumentException(String.format("failed parsing attribute %s with value %s", propertyName, propValue));
206207
}
207208
else
208-
retval=Util.getAddress(propertyValue, ip_version);
209+
retval=Util.getAddress(propValue, ip_version);
209210

210211
if(check_scope && retval instanceof Inet6Address && retval.isLinkLocalAddress()) {
211212
// check scope
@@ -220,7 +221,7 @@ public Object convert(Property annotation, Object obj, Class<?> propertyFieldTyp
220221
}
221222
return retval;
222223
}
223-
return propertyValue;
224+
return propValue;
224225
}
225226

226227

src/org/jgroups/util/Util.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,6 +2761,12 @@ public static long readDurationLong(String input, TimeUnit unit) {
27612761
return unit.convert(num.multiply(new BigDecimal(tuple.getVal2())).longValue(), TimeUnit.MILLISECONDS);
27622762
}
27632763

2764+
public static int readDurationInt(String input, TimeUnit unit) {
2765+
Tuple<String,Long> tuple=readDuration(input);
2766+
BigDecimal num = new BigDecimal(tuple.getVal1());
2767+
return (int)unit.convert(num.multiply(new BigDecimal(tuple.getVal2())).longValue(), TimeUnit.MILLISECONDS);
2768+
}
2769+
27642770
private static Tuple<String,Long> readDuration(String input) {
27652771
input=input.trim().toLowerCase();
27662772

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.jgroups.tests;
2+
3+
import org.jgroups.Global;
4+
import org.jgroups.JChannel;
5+
import org.jgroups.protocols.TCP;
6+
import org.testng.annotations.Test;
7+
8+
import java.io.*;
9+
import java.util.Map;
10+
11+
/**
12+
* Tests correct parsing of attribute values: https://issues.redhat.com/browse/JGRP-2867
13+
* @author Bela Ban
14+
* @since 5.4.5, 5.3.16
15+
*/
16+
@Test(groups=Global.FUNCTIONAL)
17+
public class ConfigTest {
18+
19+
public void test1() throws IOException {
20+
File f=generate(Map.of("sock_conn_timeout", "500ms",
21+
"max_send_queue", "1024",
22+
"recv_buf_size", "2m",
23+
"peer_addr_read_timeout", "0.5m"));
24+
try(InputStream in=new FileInputStream(f);JChannel ch=new JChannel(in)) {
25+
TCP tcp=(TCP)ch.stack().getTransport();
26+
int timeout=tcp.getSockConnTimeout();
27+
assert timeout == 500;
28+
int max_send_queue=tcp.maxSendQueue();
29+
assert max_send_queue == 1024;
30+
int recv_buf_size=tcp.getRecvBufSize();
31+
assert recv_buf_size == 2_000_000;
32+
int peer_addr_read_timeout=tcp.getPeerAddrReadTimeout();
33+
assert peer_addr_read_timeout == 30_000;
34+
}
35+
catch(Exception e) {
36+
throw new RuntimeException(e);
37+
}
38+
finally {
39+
f.delete();
40+
}
41+
42+
}
43+
44+
protected static File generate(Map<String,String> attrs) throws IOException {
45+
File f=File.createTempFile("config-test", "xml");
46+
StringBuilder sb=new StringBuilder("<config>\n <TCP");
47+
if(attrs != null) {
48+
for(Map.Entry<String,String> e: attrs.entrySet()) {
49+
sb.append(String.format(" %s=\"%s\"", e.getKey(), e.getValue()));
50+
}
51+
}
52+
53+
sb.append(" />\n</config>");
54+
OutputStream os=new FileOutputStream(f);
55+
os.write(sb.toString().getBytes());
56+
os.close();
57+
return f;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)