package com.alibaba.jstorm;

import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class LoadConfig {
	public static final String TOPOLOGY_TYPE = "topology.type";
	
	private static Map LoadProperty(String prop) {
		Map ret = null;
		Properties properties = new Properties();

		try {
			InputStream stream = new FileInputStream(prop);
			properties.load(stream);
			ret = new HashMap<Object, Object>();
			ret.putAll(properties);
		} catch (FileNotFoundException e) {
			System.out.println("No such file " + prop);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		return ret;
	}

	private static Map LoadYaml(String confPath) {
        Map ret = null;
		Yaml yaml = new Yaml();

		try {
			InputStream stream = new FileInputStream(confPath);

			ret = (Map) yaml.load(stream);
			if (ret == null || ret.isEmpty() == true) {
				throw new RuntimeException("Failed to read config file");
			}

		} catch (FileNotFoundException e) {
			System.out.println("No such file " + confPath);
			throw new RuntimeException("No config file");
		} catch (Exception e1) {
			e1.printStackTrace();
			throw new RuntimeException("Failed to read config file");
		}

		return ret;
	}
	
	public static Map LoadConf(String arg) {
		Map ret = null;
		
		if (arg.endsWith("yaml")) {
			ret = LoadYaml(arg);
		} else {
			ret = LoadProperty(arg);
		}
		
		return ret;
	}
}
@@ -257,17 +257,20 @@
 onfig %7B%0A
-%09
+    
 public s
@@ -321,19 +321,21 @@
 .type%22;%0A
-%09%0A%09
+%0A    
 private 
@@ -369,26 +369,32 @@
 ing prop) %7B%0A
-%09%09
+        
 Map ret = nu
@@ -397,18 +397,24 @@
 = null;%0A
-%09%09
+        
 Properti
@@ -444,35 +444,50 @@
 perties();%0A%0A
-%09%09try %7B%0A%09%09%09
+        try %7B%0A            
 InputStream 
@@ -522,19 +522,28 @@
 (prop);%0A
-%09%09%09
+            
 properti
@@ -559,19 +559,28 @@
 tream);%0A
-%09%09%09
+            
 ret = ne
@@ -608,19 +608,28 @@
 ect%3E();%0A
-%09%09%09
+            
 ret.putA
@@ -644,18 +644,24 @@
 rties);%0A
-%09%09
+        
 %7D catch 
@@ -680,35 +680,44 @@
 dException e) %7B%0A
-%09%09%09
+            
 System.out.print
@@ -744,18 +744,24 @@
  prop);%0A
-%09%09
+        
 %7D catch 
@@ -769,35 +769,44 @@
 Exception e1) %7B%0A
-%09%09%09
+            
 e1.printStackTra
@@ -811,25 +811,35 @@
 race();%0A
-%09%09%7D%0A%09%09%0A%09%09
+        %7D%0A%0A        
 return r
@@ -842,21 +842,27 @@
 rn ret;%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 private 
@@ -924,18 +924,24 @@
 = null;%0A
-%09%09
+        
 Yaml yam
@@ -961,19 +961,34 @@
 );%0A%0A
-%09%09try %7B%0A%09%09%09
+        try %7B%0A            
 Inpu
@@ -1036,19 +1036,28 @@
 Path);%0A%0A
-%09%09%09
+            
 ret = (M
@@ -1079,19 +1079,28 @@
 tream);%0A
-%09%09%09
+            
 if (ret 
@@ -1139,12 +1139,24 @@
 ) %7B%0A
-%09%09%09%09
+                
 thro
@@ -1213,16 +1213,31 @@
 %22);%0A
-%09%09%09%7D%0A%0A%09%09
+            %7D%0A%0A        
 %7D ca
@@ -1268,19 +1268,28 @@
 on e) %7B%0A
-%09%09%09
+            
 System.o
@@ -1328,19 +1328,28 @@
 fPath);%0A
-%09%09%09
+            
 throw ne
@@ -1386,18 +1386,24 @@
 file%22);%0A
-%09%09
+        
 %7D catch 
@@ -1419,19 +1419,28 @@
 n e1) %7B%0A
-%09%09%09
+            
 e1.print
@@ -1453,19 +1453,28 @@
 race();%0A
-%09%09%09
+            
 throw ne
@@ -1527,15 +1527,27 @@
 %22);%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A%0A        
 retu
@@ -1558,14 +1558,19 @@
 et;%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 publ
@@ -1606,18 +1606,24 @@
  arg) %7B%0A
-%09%09
+        
 Map ret 
@@ -1630,21 +1630,25 @@
 = null;%0A
-%09%09%0A%09%09
+%0A        
 if (arg.
@@ -1667,19 +1667,28 @@
 ml%22)) %7B%0A
-%09%09%09
+            
 ret = Lo
@@ -1700,18 +1700,24 @@
 l(arg);%0A
-%09%09
+        
 %7D else %7B
@@ -1721,11 +1721,20 @@
 e %7B%0A
-%09%09%09
+            
 ret 
@@ -1758,17 +1758,27 @@
 g);%0A
-%09%09%7D%0A%09%09%0A%09%09
+        %7D%0A%0A        
 retu
@@ -1789,8 +1789,11 @@
 et;%0A
-%09
+    
 %7D%0A%7D

fix format