Skip to content

Commit 67bb963

Browse files
committed
[service_discovery] making Java 6 compatible.
1 parent f9f01ef commit 67bb963

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

pom.xml

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<properties>
1515
<commons-io.version>2.4</commons-io.version>
1616
<commons-lang.version>2.6</commons-lang.version>
17+
<apache-commons-lang3.version>3.5</apache-commons-lang3.version>
1718
<guava.version>17.0</guava.version>
1819
<java-dogstatsd-client.version>2.1.0</java-dogstatsd-client.version>
1920
<jcommander.version>1.35</jcommander.version>
@@ -59,7 +60,13 @@
5960
<groupId>commons-lang</groupId>
6061
<artifactId>commons-lang</artifactId>
6162
<version>${commons-lang.version}</version>
62-
</dependency>
63+
</dependency>
64+
<dependency>
65+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
66+
<groupId>org.apache.commons</groupId>
67+
<artifactId>commons-lang3</artifactId>
68+
<version>${apache-commons-lang3.version}</version>
69+
</dependency>
6370
<dependency>
6471
<groupId>com.datadoghq</groupId>
6572
<artifactId>java-dogstatsd-client</artifactId>

src/main/java/org/datadog/jmxfetch/App.java

+18-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.io.InputStream;
1010
import java.io.IOException;
1111
import java.io.UnsupportedEncodingException;
12-
import java.nio.charset.StandardCharsets;
1312
import java.util.Arrays;
1413
import java.util.ArrayList;
1514
import java.util.Enumeration;
@@ -30,6 +29,7 @@
3029
import org.apache.log4j.Appender;
3130
import org.apache.log4j.Level;
3231
import org.apache.log4j.Logger;
32+
import org.apache.commons.lang3.CharEncoding;
3333
import org.apache.commons.io.FileUtils;
3434
import org.apache.commons.io.filefilter.WildcardFileFilter;
3535
import org.datadog.jmxfetch.reporter.Reporter;
@@ -156,7 +156,7 @@ private static void clearInstances(List<Instance> instances) {
156156
}
157157

158158
private String get_sd_name(String config){
159-
String[] splitted = config.split(System.lineSeparator(), 2);
159+
String[] splitted = config.split(System.getProperty("line.separator"), 2);
160160

161161
return SERVICE_DISCOVERY_PREFIX + splitted[0].substring(2, splitted[0].length());
162162
}
@@ -166,8 +166,8 @@ private boolean process_service_discovery(byte[] buffer) {
166166
String[] discovered;
167167

168168
try {
169-
String configs = new String(buffer, "UTF-8");
170-
discovered = configs.split(App.SD_CONFIG_SEP+System.lineSeparator());
169+
String configs = new String(buffer, CharEncoding.UTF_8);
170+
discovered = configs.split(App.SD_CONFIG_SEP + System.getProperty("line.separator"));
171171
} catch(UnsupportedEncodingException e) {
172172
LOGGER.debug("Unable to parse byte buffer to UTF-8 String.");
173173
return false;
@@ -178,14 +178,18 @@ private boolean process_service_discovery(byte[] buffer) {
178178
continue;
179179
}
180180

181-
String name = get_sd_name(config);
182-
LOGGER.debug("Attempting to apply config. Name: " + name + "\nconfig: \n" + config);
183-
InputStream stream = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8));
184-
YamlParser yaml = new YamlParser(stream);
185-
186-
if (this.addConfig(name, yaml)){
187-
reinit = true;
188-
LOGGER.debug("Configuration added succesfully reinit in order");
181+
try{
182+
String name = get_sd_name(config);
183+
LOGGER.debug("Attempting to apply config. Name: " + name + "\nconfig: \n" + config);
184+
InputStream stream = new ByteArrayInputStream(config.getBytes(CharEncoding.UTF_8));
185+
YamlParser yaml = new YamlParser(stream);
186+
187+
if (this.addConfig(name, yaml)){
188+
reinit = true;
189+
LOGGER.debug("Configuration added succesfully reinit in order");
190+
}
191+
} catch(UnsupportedEncodingException e) {
192+
LOGGER.debug("Unable to parse byte buffer to UTF-8 String.");
189193
}
190194
}
191195

@@ -362,7 +366,8 @@ public boolean addConfig(String name, YamlParser config) {
362366
return false;
363367
}
364368

365-
String check = matcher.group("check");
369+
// Java 6 doesn't allow name matching - group 1 is "check"
370+
String check = matcher.group(1);
366371
if (this.configs.containsKey(check)) {
367372
// there was already a file config for the check.
368373
return false;

0 commit comments

Comments
 (0)