package com.ctrip.framework.apollo.core.utils;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.Properties;

public class ResourceUtils {

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

  @SuppressWarnings("unchecked")
  public static Properties readConfigFile(String configPath, Properties defaults) {
    InputStream in = ClassLoaderUtil.getLoader().getResourceAsStream(configPath);
    logger.debug("Reading config from resource {}", configPath);
    Properties props = new Properties();
    try {
      if (in == null) {
        // load outside resource under current user path
        Path path = new File(System.getProperty("user.dir") + configPath).toPath();
        if (Files.isReadable(path)) {
          in = new FileInputStream(path.toFile());
          logger.debug("Reading config from file {} ", path);
        } else {
          logger.warn("Could not find available config file");
        }
      }
      if (defaults != null) {
        props.putAll(defaults);
      }

      if (in != null) {
        props.load(in);
        in.close();
      }
    } catch (Exception ex) {
      logger.warn("Reading config failed: {}", ex.getMessage());
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException ex) {
          logger.warn("Close config failed: {}", ex.getMessage());
        }
      }
    }
    StringBuilder sb = new StringBuilder();
    for (Enumeration<String> e = (Enumeration<String>) props.propertyNames(); e
        .hasMoreElements();) {
      String key = e.nextElement();
      String val = (String) props.getProperty(key);
      sb.append(key).append('=').append(val).append('\n');
    }
    if (sb.length() > 0) {
      logger.debug("Reading properties: \n" + sb.toString());
    } else {
      logger.warn("No available properties");
    }
    return props;
  }
}

@@ -263,16 +263,44 @@
 e.Path;%0A
+import java.nio.file.Paths;%0A
 import j
@@ -317,24 +317,24 @@
 numeration;%0A
-
 import java.
@@ -465,16 +465,113 @@
 .class);
+%0A  private static final String%5B%5D DEFAULT_FILE_SEARCH_LOCATIONS = new String%5B%5D%7B%22./config/%22, %22./%22%7D;
 %0A%0A  @Sup
@@ -706,55 +706,48 @@
 n = 
-ClassLoaderUtil.getLoader().getResourceAsStream
+loadConfigFileFromDefaultSearchLocations
 (con
@@ -2212,11 +2212,531 @@
 ops;%0A  %7D
+%0A%0A  private static InputStream loadConfigFileFromDefaultSearchLocations(String configPath) %7B%0A    for (String searchLocation : DEFAULT_FILE_SEARCH_LOCATIONS) %7B%0A      try %7B%0A        File candidate = Paths.get(searchLocation, configPath).toFile();%0A        if (candidate.exists() && candidate.isFile() && candidate.canRead()) %7B%0A          return new FileInputStream(candidate);%0A        %7D%0A      %7D catch (Throwable ex) %7B%0A        //ignore%0A      %7D%0A    %7D%0A%0A    return ClassLoaderUtil.getLoader().getResourceAsStream(configPath);%0A  %7D
 %0A%7D%0A

load config file(apollo-env.properties) from working directory first, so that it behaves the same as spring boot config files