package com.yuzhouwan.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * Copyright @ 2016 yuzhouwan.com
 * All right reserved.
 * Function: Properties Utils
 *
 * @author Benedict Jin
 * @since 2016/4/8 0030
 */
public class PropUtils {

    private static final Logger _log = LoggerFactory.getLogger(PropUtils.class);
    private static final Properties properties = new Properties();

    private static volatile PropUtils instance;

    private PropUtils(List<String> confPathList) {
        if (confPathList == null || confPathList.size() == 0) {
            return;
        }
        for (String confPath : confPathList) {
            if (StrUtils.isEmpty(confPath)) {
                continue;
            }
            File confFile = new File(confPath);
            if (!confFile.exists())
                continue;
            FileInputStream fis;
            try {
                fis = new FileInputStream(confFile);
            } catch (FileNotFoundException e) {
                _log.error(e.getMessage());
                continue;
            }
            try {
                properties.load(fis);
            } catch (IOException e) {
                _log.error(e.getMessage());
            }
        }
    }

    public static PropUtils getInstance() {
        if (instance == null)
            synchronized (PropUtils.class) {
                if (instance == null) {
                    List<String> prop = DirUtils.findPath(DirUtils.getClassesPath(), ".properties", true, "prop");
                    if (prop == null)
                        prop = DirUtils.findPath(DirUtils.getProjectBasicPath(), ".properties", true, "prop");
                    instance = new PropUtils(prop);
                }
            }
        return instance;
    }

    /**
     * 1. Only load properties that located within project
     * 2. External properties could rewrite exists k-v in PropUtils
     *
     * @param key
     * @param withinJar
     * @return
     */
    public String getProperty(String key, boolean withinJar) {
        if (StrUtils.isEmpty(key)) {
            return null;
        }
        if (withinJar) {
            String valueJar = JarUtils.getInstance().getProperty(key);
            if (StrUtils.isEmpty(valueJar)) {
                if (properties == null)
                    throw new RuntimeException("Properties is not valid!!");
                String value = properties.getProperty(key);
                return StrUtils.isEmpty(value) ? valueJar : value;
            } else
                return valueJar;
        }
        if (properties == null)
            throw new RuntimeException("Properties is not valid!!");
        return properties.getProperty(key);
    }

    /**
     * Avoid recursion in JarUtils' initializtion
     *
     * @param key
     * @return
     */
    public String getPropertyInternal(String key) {
        return getProperty(key, false);
    }

    public String getProperty(String key) {
        return getProperty(key, true);
    }
}

@@ -2307,158 +2307,8 @@
   %7D%0A
-        if (withinJar) %7B%0A            String valueJar = JarUtils.getInstance().getProperty(key);%0A            if (StrUtils.isEmpty(valueJar)) %7B%0A        
     
@@ -2343,32 +2343,24 @@
             
-      
-  
 throw new Ru
@@ -2400,32 +2400,24 @@
  valid!!%22);%0A
-        
         Stri
@@ -2464,30 +2464,32 @@
         
-   
-     return
+if (withinJar &&
  StrUtil
@@ -2508,234 +2508,179 @@
 lue)
- ? valueJar : value;%0A            %7D else%0A                return valueJar;%0A        %7D%0A        if (properties == null)%0A            throw new RuntimeException(%22Properties is not valid!!%22);%0A        return properties.getProperty(key)
+) %7B%0A            String valueJar = JarUtils.getInstance().getProperty(key);%0A            return StrUtils.isEmpty(valueJar) ? value : valueJar;%0A        %7D%0A        return value
 ;%0A  

revert changes in PropUtils