package com.ctrip.ops.sysdev.core;

import com.ctrip.ops.sysdev.baseplugin.BaseInput;
import com.ctrip.ops.sysdev.config.CommandLineValues;
import com.ctrip.ops.sysdev.config.HangoutConfig;
import com.ctrip.ops.sysdev.log.*;
import lombok.extern.log4j.Log4j;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j
public class Main {
    private static LogSetter logSetter = new LogSetter();

    public static void main(String[] args) {
        //Parse CommandLine arguments
        CommandLineValues cm = new CommandLineValues(args);
        cm.parseCmd();

        //Set Log Level and appenders
        logSetter.initLogger(cm);

        // parse configure file
        Map configs = null;
        try {
            configs = HangoutConfig.parse(cm.getConfigFile());
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        log.debug(configs);
        //log.trace("Tracetest");


        // for input in all_inputs
        List<HashMap<String, Map>> inputs = (ArrayList<HashMap<String, Map>>) configs.get("inputs");

        //Go through every input and emit immediately
        for (HashMap<String, Map> input : inputs) {
            for (Map.Entry<String, Map> entry : input.entrySet()) {
                String inputType = entry.getKey();
                Map inputConfig = entry.getValue();
                Class<?> inputClass = null;
                try {
                    inputClass = Class
                            .forName("com.ctrip.ops.sysdev.inputs." + inputType);
                    Constructor<?> ctor = inputClass.getConstructor(Map.class,
                            ArrayList.class, ArrayList.class);
                    BaseInput inputInstance = (BaseInput) ctor.newInstance(
                            inputConfig, configs.get("filters"), configs.get("outputs"));
                    inputInstance.emit();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


@@ -219,9 +219,17 @@
 log.
-*
+LogSetter
 ;%0Aim
@@ -331,60 +331,8 @@
 or;%0A
-import java.lang.reflect.InvocationTargetException;%0A
 impo
@@ -1263,222 +1263,150 @@
     
-for (HashMap%3CString, Map%3E input : inputs) %7B%0A            for (Map.Entry%3CString, Map%3E entry : input.entrySet()) %7B%0A                String inputType = entry.getKey();%0A                Map inputConfig = entry.getValue();
+Map finalConfigs = configs;%0A        inputs.forEach(%0A                input -%3E %7B%0A                    input.forEach((inputType, inputConfig) -%3E %7B
 %0A   
@@ -1418,16 +1418,24 @@
         
+        
 Class%3C?%3E
@@ -1462,38 +1462,54 @@
                 
+     
+   
 try %7B%0A
+        
                 
@@ -1534,37 +1534,8 @@
 lass
-%0A                            
 .for
@@ -1584,16 +1584,85 @@
 tType);%0A
+                            //Get Constructor for each input%0A        
         
@@ -1721,16 +1721,53 @@
 tructor(
+%0A                                    
 Map.clas
@@ -1797,16 +1797,24 @@
         
+        
 ArrayLis
@@ -1821,16 +1821,52 @@
 t.class,
+%0A                                   
  ArrayLi
@@ -1872,24 +1872,84 @@
 ist.class);%0A
+                            //instantiate the input%0A        
             
@@ -2036,24 +2036,32 @@
             
+        
 inputConfig,
@@ -2064,380 +2064,235 @@
 fig,
- configs.get(%22filters%22), configs.get(%22outputs%22));%0A                    inputInstance.emit();%0A                %7D catch (InstantiationException e) %7B%0A                    e.printStackTrace();%0A                %7D catch (InvocationTargetException e) %7B%0A                    e.printStackTrace();%0A                %7D catch (NoSuchMethodException e) %7B%0A                    e.printStackTrace
+%0A                                    finalConfigs.get(%22filters%22),%0A                                    finalConfigs.get(%22outputs%22));%0A                            //Start working,guy.%0A                            inputInstance.emit
 ();%0A
@@ -2299,32 +2299,40 @@
                 
+  
+      
 %7D catch (Illegal
@@ -2328,21 +2328,8 @@
 ch (
-IllegalAccess
 Exce
@@ -2331,32 +2331,40 @@
 (Exception e) %7B%0A
+        
                 
@@ -2408,103 +2408,41 @@
     
-%7D catch (ClassNotFoundException e) %7B%0A                    e.printStackTrace();%0A                %7D
+        %7D%0A                    %7D);
 %0A   
@@ -2438,38 +2438,32 @@
     %7D);%0A        
-    %7D%0A
         %7D%0A    %7D%0A
@@ -2451,22 +2451,24 @@
            %7D
+);
 %0A    %7D%0A%7D%0A%0A

use java8 in main