// Path: hangout-filters/hangout-filters-translate/src/main/java/com/ctrip/ops/sysdev/filters/Translate.java
// @Log4j2
// public class Translate extends BaseFilter {
// 
//     public Translate(Map config) {
//         super(config);
//     }
// 
//     private FieldSetter target;
//     private TemplateRender source;
//     private String dictionaryPath;
//     private int refreshInterval;
//     private long nextLoadTime;
//     private HashMap dictionary;
// 
//     private void loadDictionary() {
//         log.info("begin to loadDictionary: " + this.dictionaryPath);
// 
//         Yaml yaml = new Yaml();
// 
//         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();
//                 log.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) {
//                 log.error(dictionaryPath + " is not found");
//                 log.error(e.getMessage());
//                 System.exit(1);
//             }
//         }
// 
//         log.info("loadDictionary done: " + this.dictionaryPath);
//     }
// 
//     protected void prepare() {
//         String sourceField = (String) config.get("source");
//         String targetField = (String) config.get("target");
//         try {
//             this.source = TemplateRender.getRender(sourceField, false);
//         } catch (IOException e) {
//             log.fatal("could NOT build template render from " + sourceField);
//             System.exit(1);
//         }
//         this.target = FieldSetter.getFieldSetter(targetField);
// 
//         dictionaryPath = (String) config.get("dictionary_path");
// 
//         if (dictionaryPath == null) {
//             log.fatal("dictionary_path must be inclued in config");
//             System.exit(1);
//         }
// 
//         loadDictionary();
// 
//         if (config.containsKey("refresh_interval")) {
//             this.refreshInterval = (Integer) config.get("refresh_interval") * 1000;
//         } else {
//             this.refreshInterval = 300 * 1000;
//         }
//         nextLoadTime = System.currentTimeMillis() + refreshInterval;
//     }
// 
//     @Override
//     protected Map filter(final Map event) {
//         if (System.currentTimeMillis() >= nextLoadTime) {
//             loadDictionary();
//             nextLoadTime += refreshInterval;
//         }
//         if (this.dictionary == null) {
//             log.debug("dictionary is null, return without any change");
//             return event;
//         }
//         Object t = dictionary.get(this.source.render(event));
//         if (t != null) {
//             this.target.setField(event, t);
//         }
//         return event;
//     }
// }


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.ctrip.ops.sysdev.filters.Translate;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.Yaml;
package com.ctrip.ops.sysdev.test;




public class TestTranslate {
    @Test
    public void testTranslate() throws IOException {
        File temp = File.createTempFile("test-dict", ".yml");
        BufferedWriter output = new BufferedWriter(new FileWriter(temp));
        output.write("abc: xyz\n");
        output.flush();


        String c = String
                .format("%s\n%s\n%s",
                        "dictionary_path: " + temp.getAbsolutePath(),
                        "source: name",
                        "target: nick"
                );
        Yaml yaml = new Yaml();
        Map config = (Map) yaml.load(c);
        Assert.assertNotNull(config);

// Path: hangout-filters/hangout-filters-translate/src/main/java/com/ctrip/ops/sysdev/filters/Translate.java
// @Log4j2
// public class Translate extends BaseFilter {
// 
//     public Translate(Map config) {
//         super(config);
//     }
// 
//     private FieldSetter target;
//     private TemplateRender source;
//     private String dictionaryPath;
//     private int refreshInterval;
//     private long nextLoadTime;
//     private HashMap dictionary;
// 
//     private void loadDictionary() {
//         log.info("begin to loadDictionary: " + this.dictionaryPath);
// 
//         Yaml yaml = new Yaml();
// 
//         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();
//                 log.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) {
//                 log.error(dictionaryPath + " is not found");
//                 log.error(e.getMessage());
//                 System.exit(1);
//             }
//         }
// 
//         log.info("loadDictionary done: " + this.dictionaryPath);
//     }
// 
//     protected void prepare() {
//         String sourceField = (String) config.get("source");
//         String targetField = (String) config.get("target");
//         try {
//             this.source = TemplateRender.getRender(sourceField, false);
//         } catch (IOException e) {
//             log.fatal("could NOT build template render from " + sourceField);
//             System.exit(1);
//         }
//         this.target = FieldSetter.getFieldSetter(targetField);
// 
//         dictionaryPath = (String) config.get("dictionary_path");
// 
//         if (dictionaryPath == null) {
//             log.fatal("dictionary_path must be inclued in config");
//             System.exit(1);
//         }
// 
//         loadDictionary();
// 
//         if (config.containsKey("refresh_interval")) {
//             this.refreshInterval = (Integer) config.get("refresh_interval") * 1000;
//         } else {
//             this.refreshInterval = 300 * 1000;
//         }
//         nextLoadTime = System.currentTimeMillis() + refreshInterval;
//     }
// 
//     @Override
//     protected Map filter(final Map event) {
//         if (System.currentTimeMillis() >= nextLoadTime) {
//             loadDictionary();
//             nextLoadTime += refreshInterval;
//         }
//         if (this.dictionary == null) {
//             log.debug("dictionary is null, return without any change");
//             return event;
//         }
//         Object t = dictionary.get(this.source.render(event));
//         if (t != null) {
//             this.target.setField(event, t);
//         }
//         return event;
//     }
// }


// Path: hangout-filters/hangout-filters-translate/src/test/java/com/ctrip/ops/sysdev/test/TestTranslate.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.ctrip.ops.sysdev.filters.Translate;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.Yaml;

package com.ctrip.ops.sysdev.test;




public class TestTranslate {
    @Test
    public void testTranslate() throws IOException {
        File temp = File.createTempFile("test-dict", ".yml");
        BufferedWriter output = new BufferedWriter(new FileWriter(temp));
        output.write("abc: xyz\n");
        output.flush();


        String c = String
                .format("%s\n%s\n%s",
                        "dictionary_path: " + temp.getAbsolutePath(),
                        "source: name",
                        "target: nick"
                );
        Yaml yaml = new Yaml();
        Map config = (Map) yaml.load(c);
        Assert.assertNotNull(config);

        Translate filter = new Translate(config);