Skip to content
This repository has been archived by the owner on Oct 30, 2022. It is now read-only.

Commit

Permalink
could fetch dict from url
Browse files Browse the repository at this point in the history
  • Loading branch information
childe committed Mar 18, 2016
1 parent 74c9962 commit 7122e71
Showing 1 changed file with 77 additions and 55 deletions.
132 changes: 77 additions & 55 deletions src/main/java/com/ctrip/ops/sysdev/filters/Translate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,96 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.yaml.snakeyaml.Yaml;

public class Translate extends BaseFilter {
private static final Logger logger = Logger.getLogger(Translate.class
.getName());
private static final Logger logger = Logger.getLogger(Translate.class
.getName());

public Translate(Map config) {
super(config);
}
public Translate(Map config) {
super(config);
}

private String target;
private String source;
private String dictionaryPath;
private int refreshInterval;
private long nextLoadTime;
private HashMap dictionary;
private String target;
private String source;
private String dictionaryPath;
private int refreshInterval;
private long nextLoadTime;
private HashMap dictionary;

private void loadDictionary() {
if (dictionaryPath == null) {
dictionary = null;
logger.warn("dictionary_path is null");
return;
}
Yaml yaml = new Yaml();
FileInputStream input;
try {
input = new FileInputStream(new File(dictionaryPath));
dictionary = (HashMap) yaml.load(input);
} catch (FileNotFoundException e) {
logger.error(dictionaryPath + " is not found");
logger.error(e.getMessage());
dictionary = null;
}
}
private void loadDictionary() {
if (dictionaryPath == null) {
dictionary = null;
logger.warn("dictionary_path is null");
return;
}
Yaml yaml = new Yaml();

protected void prepare() {
target = (String) config.get("target");
source = (String) config.get("source");
if (dictionaryPath.startsWith("http://") || dictionaryPath.startsWith("https://")) {
URL httpUrl;
URLConnection connection;
try {
httpUrl = new URL(dictionaryPath);
connection = httpUrl.openConnection();
connection.connect();
dictionary = (HashMap) yaml.load(connection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
logger.error("failed to load " + dictionaryPath);
System.exit(1);
}
} else {
FileInputStream input;
try {
input = new FileInputStream(new File(dictionaryPath));
dictionary = (HashMap) yaml.load(input);
} catch (FileNotFoundException e) {
logger.error(dictionaryPath + " is not found");
logger.error(e.getMessage());
System.exit(1);
}
}

dictionaryPath = (String) config.get("dictionary_path");
}

loadDictionary();
protected void prepare() {
target = (String) config.get("target");
source = (String) config.get("source");

if (config.containsKey("refresh_interval")) {
this.refreshInterval = (int) config.get("refresh_interval") * 1000;
} else {
this.refreshInterval = 300 * 1000;
}
nextLoadTime = System.currentTimeMillis() + refreshInterval * 1000;
};
dictionaryPath = (String) config.get("dictionary_path");

@Override
protected Map filter(final Map event) {
if (dictionary == null || !event.containsKey(this.source)) {
return event;
}
if (System.currentTimeMillis() >= nextLoadTime) {
loadDictionary();
nextLoadTime += refreshInterval;
}
Object t = dictionary.get(event.get(source));
if (t != null) {
event.put(target, t);
}
return event;
}
loadDictionary();

if (config.containsKey("refresh_interval")) {
this.refreshInterval = (int) config.get("refresh_interval") * 1000;
} else {
this.refreshInterval = 300 * 1000;
}
nextLoadTime = System.currentTimeMillis() + refreshInterval * 1000;
}

;

@Override
protected Map filter(final Map event) {
if (dictionary == null || !event.containsKey(this.source)) {
return event;
}
if (System.currentTimeMillis() >= nextLoadTime) {
loadDictionary();
nextLoadTime += refreshInterval;
}
Object t = dictionary.get(event.get(source));
if (t != null) {
event.put(target, t);
}
return event;
}
}

0 comments on commit 7122e71

Please sign in to comment.