package org.nutz.mvc.impl.chainconfig;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.NutRuntimeException;
import org.nutz.json.Json;
import org.nutz.lang.Files;
import org.nutz.lang.Lang;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.resource.NutResource;
import org.nutz.resource.Scans;

/**
 * 通过json文件获取配置信息.
 * <p/>默认配置会首先加载,用户文件可以覆盖之
 * @author wendal(wendal1985@gmail.com)
 * TODO 合并到NutConfig
 */
public class JsonActionChainMakerConfiguretion implements ActionChainMakerConfiguration {

    private static final Log log = Logs.get();
    
    protected Map<String,Map<String,Object>> map = new HashMap<String, Map<String,Object>>();
    
    @SuppressWarnings("unchecked")
    public JsonActionChainMakerConfiguretion(String...jsonPaths) {
        List<NutResource> list = Scans.me().loadResource("^(.+[.])(js|json)$", jsonPaths);
        try {
            File defaultChainsFile = Files.findFile("org/nutz/mvc/impl/chainconfig/default-chains.js");
            if (defaultChainsFile == null) {
                log.warn("org/nutz/mvc/impl/chainconfig/default-chains.js NOT Found!!");
                throw new NutRuntimeException("Default Chains File Not FOUND?!");
            }
            map.putAll(Json.fromJsonFile(Map.class, defaultChainsFile));
            for (NutResource nr : list)
                map.putAll(Json.fromJson(Map.class,nr.getReader()));
        }
        catch (IOException e) {
            throw Lang.wrapThrow(e);
        }
    }
    
    @SuppressWarnings("unchecked")
    public List<String> getProcessors(String key) {
        Map<String,Object> config = map.get(key);
        if(config != null && config.containsKey("ps"))
            return (List<String>) config.get("ps");
        return (List<String>) map.get("default").get("ps");
    }
    
    public String getErrorProcessor(String key) {
        Map<String,Object> config = map.get(key);
        if(config != null && config.containsKey("error"))
            return (String) config.get("error");
        return (String) map.get("default").get("error");
    }
    
}

@@ -82,24 +82,59 @@
 Exception;%0D%0A
+import java.io.InputStreamReader;%0D%0A
 import java.
@@ -1170,25 +1170,180 @@
 ile 
-=
+!
 = null
+ && defaultChainsFile.exists())%0D%0A            %09map.putAll(Json.fromJsonFile(Map.class, defaultChainsFile));%0D%0A            else %7B%0D%0A            %09if (!Lang.isAndroid
 ) %7B%0D%0A
-     
     
@@ -1349,16 +1349,19 @@
         
+ %09%09
 log.warn
@@ -1438,24 +1438,28 @@
             
+  
+  
 throw new Nu
@@ -1517,32 +1517,33 @@
 );%0D%0A            
+%09
 %7D%0D%0A            m
@@ -1533,32 +1533,33 @@
 %09%7D%0D%0A            
+%09
 map.putAll(Json.
@@ -1558,36 +1558,32 @@
 ll(Json.fromJson
-File
 (Map.class, defa
@@ -1578,36 +1578,169 @@
 .class, 
+new InputStreamReader(getClass().getClassLoader().getResourceAsStream(%22org/nutz/mvc/impl/chainconfig/
 default
-C
+-c
 hains
-File));
+.js%22))));%0D%0A            %7D%0D%0A            
 %0D%0A      
@@ -1884,24 +1884,91 @@
 ption e) %7B%0D%0A
+        %09if (Lang.isAndroid) %7B //%E6%93%8DAndroid%0D%0A        %09%09%0D%0A        %09%7D%0D%0A
             

fix issue #366