package top.zbeboy.isy.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * 初始化配置.
 *
 * @author zbeboy
 * @version 1.0
 * @since 1.0
 */
@Component
public class InitConfiguration implements CommandLineRunner {

    private final Logger logger = LoggerFactory.getLogger(InitConfiguration.class);

    private final CacheManager cacheManager;

    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    @Autowired
    public InitConfiguration(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    @Override
    public void run(String... strings) throws Exception {
        logger.info("\n\n" + "=========================================================\n"
                + "Using cache manager: " + this.cacheManager.getClass().getName() + "\n"
                + "=========================================================\n\n");
        initUrlMapping();
    }

    /**
     * 初始化url mapping
     */
    public void initUrlMapping() {
        try {
            String filePath = Workbook.URL_MAPPING_FILE_PATH;
            String resourcesPath = Workbook.SETTINGS_PATH;
            File file = new File(resourcesPath);
            if (!file.exists()) {
                if (file.mkdirs()) {
                    PrintWriter printWriter = new PrintWriter(filePath);
                    final String[] url = {""};
                    Map<RequestMappingInfo, HandlerMethod> map = this.handlerMapping.getHandlerMethods();
                    map.forEach((key, value) -> {
                        url[0] = key.toString();
                        url[0] = url[0].split(",")[0];
                        int i1 = url[0].indexOf("[") + 1;
                        int i2 = url[0].lastIndexOf("]");
                        url[0] = url[0].substring(i1, i2);
                        printWriter.println(url[0]);
                    });
                    printWriter.close();
                    logger.info("Init url mapping to {} finish!", filePath);
                }
            }

        } catch (IOException e) {
            logger.error("Init url mapping error : {}", e);
        }
    }
}

@@ -1485,29 +1485,30 @@
     */%0A    p
-ublic
+rivate
  void initUr
@@ -1720,71 +1720,164 @@
     
-if (!file.exists()) %7B%0A                if (file.mkdirs()) %7B%0A    
+boolean mkdirsSuccess = true;%0A            if (!file.exists()) %7B%0A                mkdirsSuccess = file.mkdirs();%0A            %7D%0A            if(mkdirsSuccess)%7B%0A
     
@@ -1957,20 +1957,16 @@
         
-  
-  
 final St
@@ -1984,20 +1984,16 @@
 = %7B%22%22%7D;%0A
-    
         
@@ -2102,20 +2102,16 @@
         
-    
 map.forE
@@ -2132,20 +2132,16 @@
 e) -%3E %7B%0A
-    
         
@@ -2189,36 +2189,32 @@
                 
-    
 url%5B0%5D = url%5B0%5D.
@@ -2228,20 +2228,16 @@
 ,%22)%5B0%5D;%0A
-    
         
@@ -2298,28 +2298,24 @@
             
-    
 int i2 = url
@@ -2348,36 +2348,32 @@
                 
-   
- 
 url%5B0%5D = url%5B0%5D.
@@ -2403,36 +2403,32 @@
                 
-    
 printWriter.prin
@@ -2460,20 +2460,12 @@
     
-   
- 
 %7D);%0A
-    
     
@@ -2509,28 +2509,24 @@
             
-  
-  
 logger.info(
@@ -2574,37 +2574,119 @@
 h);%0A            
-    %7D
+%7D else %7B%0A                logger.error(%22Init url mapping error,is create mkdirs fail.%22);
 %0A            %7D%0A%0A
@@ -2680,25 +2680,24 @@
           %7D%0A
-%0A
         %7D ca

修复url-mapping生成bug