Skip to content

Commit

Permalink
[service_discovery] making Java 6 compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
truthbk committed Nov 9, 2016
1 parent f9f01ef commit 67bb963
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<properties>
<commons-io.version>2.4</commons-io.version>
<commons-lang.version>2.6</commons-lang.version>
<apache-commons-lang3.version>3.5</apache-commons-lang3.version>
<guava.version>17.0</guava.version>
<java-dogstatsd-client.version>2.1.0</java-dogstatsd-client.version>
<jcommander.version>1.35</jcommander.version>
Expand Down Expand Up @@ -59,7 +60,13 @@
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
</dependency>
<dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache-commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.datadoghq</groupId>
<artifactId>java-dogstatsd-client</artifactId>
Expand Down
31 changes: 18 additions & 13 deletions src/main/java/org/datadog/jmxfetch/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Enumeration;
Expand All @@ -30,6 +29,7 @@
import org.apache.log4j.Appender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.datadog.jmxfetch.reporter.Reporter;
Expand Down Expand Up @@ -156,7 +156,7 @@ private static void clearInstances(List<Instance> instances) {
}

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

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

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

String name = get_sd_name(config);
LOGGER.debug("Attempting to apply config. Name: " + name + "\nconfig: \n" + config);
InputStream stream = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8));
YamlParser yaml = new YamlParser(stream);

if (this.addConfig(name, yaml)){
reinit = true;
LOGGER.debug("Configuration added succesfully reinit in order");
try{
String name = get_sd_name(config);
LOGGER.debug("Attempting to apply config. Name: " + name + "\nconfig: \n" + config);
InputStream stream = new ByteArrayInputStream(config.getBytes(CharEncoding.UTF_8));
YamlParser yaml = new YamlParser(stream);

if (this.addConfig(name, yaml)){
reinit = true;
LOGGER.debug("Configuration added succesfully reinit in order");
}
} catch(UnsupportedEncodingException e) {
LOGGER.debug("Unable to parse byte buffer to UTF-8 String.");
}
}

Expand Down Expand Up @@ -362,7 +366,8 @@ public boolean addConfig(String name, YamlParser config) {
return false;
}

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

0 comments on commit 67bb963

Please sign in to comment.