package org.xcolab.commons;

import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.ResourcePropertySource;

import org.xcolab.commons.exceptions.InternalException;

import java.io.File;
import java.io.IOException;

public class HomeDirPropertyResolver extends StandardEnvironment {

    private static final String HOME_DIR = System.getProperty("user.home");

    private final File propertiesFile;
    private final File yamlFile;

    public HomeDirPropertyResolver(String fileBaseName, boolean required) {
        final String fileBasePath = HOME_DIR + File.separator + fileBaseName;

        final String propertiesFilePath = fileBasePath + ".properties";
        propertiesFile = new File(propertiesFilePath);

        final String yamlFilePath = fileBasePath + ".yml";
        yamlFile = new File(yamlFilePath);

        if (!(propertiesFile.exists() || yamlFile.exists()) && required) {
            throw new InternalException("No " + fileBasePath + ".properties/.yml file found.");
        }
    }

    @Override
    protected void customizePropertySources(MutablePropertySources propertySources) {
        super.customizePropertySources(propertySources);
        if (propertiesFile.exists()) {
            try {
                FileSystemResource fileSystemResource = new FileSystemResource(propertiesFile);
                propertySources.addLast(new ResourcePropertySource(fileSystemResource));
            } catch (IOException e) {
                throw new InternalException("Error loading properties file: " + propertiesFile.getAbsolutePath(), e);
            }
        }
        if (yamlFile.exists()) {
            try {
                FileSystemResource fileSystemResource = new FileSystemResource(yamlFile);
                propertySources.addLast(new ResourcePropertySource(fileSystemResource));
            } catch (IOException e) {
                throw new InternalException("Error loading YAML file: " + yamlFile.getAbsolutePath(), e);
            }
        }
    }
}

@@ -26,68 +26,8 @@
 s;%0A%0A
-import org.springframework.core.env.MutablePropertySources;%0A
 impo
@@ -381,158 +381,8 @@
  %7B%0A%0A
-    private static final String HOME_DIR = System.getProperty(%22user.home%22);%0A%0A    private final File propertiesFile;%0A    private final File yamlFile;%0A%0A
     
@@ -493,16 +493,39 @@
 h = 
-HOME_DIR
+System.getProperty(%22user.home%22)
  + F
@@ -631,24 +631,35 @@
 s%22;%0A        
+final File 
 propertiesFi
@@ -761,16 +761,27 @@
         
+final File 
 yamlFile
@@ -993,97 +993,64 @@
   %7D%0A
-    %7D%0A%0A    @Override%0A    protected void customizePropertySources(MutablePropertySources p
+%0A        addPropertySource(propertiesFile);%0A        addP
 rope
@@ -1062,36 +1062,47 @@
 urce
-s) %7B
+(yamlFile);
 %0A    
+%7D%0A%0A
     
-super.customize
+private void add
 Prop
@@ -1115,27 +1115,31 @@
 urce
-s
 (
+File 
 propert
-ySources);
+iesFile) %7B
 %0A   
@@ -1296,33 +1296,36 @@
                 
-p
+getP
 ropertySources.a
@@ -1314,32 +1314,34 @@
 tPropertySources
+()
 .addLast(new Res
@@ -1501,48 +1501,8 @@
 e: %22
- + propertiesFile.getAbsolutePath(), e);
 %0A   
@@ -1514,366 +1514,32 @@
     
-%7D%0A
      
-   %7D%0A
        
- if (yamlFile.exists()) %7B%0A            try %7B%0A                FileSystemResource fileSystemResource = new FileSystemResource(yamlFile);%0A                propertySources.addLast(new ResourcePropertySource(fileSystemResource));%0A            %7D catch (IOException e) %7B%0A                throw new InternalException(%22Error loading YAML file: %22 + yaml
++ properties
 File

Fix homedir property resolver