package org.ovirt.optimizer.config;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class ConfigProvider {
    static private Logger log = org.slf4j.LoggerFactory.getLogger(ConfigProvider.class);

    public static final String SDK_SERVER = "org.ovirt.optimizer.sdk.server";
    public static final String SDK_PORT = "org.ovirt.optimizer.sdk.port";
    public static final String SDK_PROTOCOL = "org.ovirt.optimizer.sdk.protocol";
    public static final String SDK_REQUEST_TIMEOUT = "org.ovirt.optimizer.sdk.connection.timeout";
    public static final String SDK_USERNAME = "org.ovirt.optimizer.sdk.username";
    public static final String SDK_PASSWORD = "org.ovirt.optimizer.sdk.password";
    public static final String SDK_CA_STORE = "org.ovirt.optimizer.sdk.ca.store";

    public static final String SOLVER_STEPS = "org.ovirt.optimizer.solver.steps";
    public static final String SOLVER_TIMEOUT = "org.ovirt.optimizer.solver.timeout";
    public static final String SOLVER_DATA_REFRESH = "org.ovirt.optimizer.solver.data.refresh";
    public static final String SOLVER_CLUSTER_REFRESH = "org.ovirt.optimizer.solver.cluster.refresh";
    public static final String SOLVER_CUSTOM_RULE_DIR = "org.ovirt.optimizer.solver.custom.rules.d";

    public static final String DEBUG_ENDPOINT_ENABLED = "org.ovirt.optimizer.debug";

    private String configFile;
    private Properties config;

    public ConfigProvider() {
        this.config = new Properties();

        configFile = System.getenv("OVIRT_OPTIMIZER_CONFIG");
        if (configFile == null) {
            configFile = "/etc/ovirt-optimizer/ovirt-optimizer.properties";
        }

        config.setProperty(SDK_SERVER, "localhost");
        config.setProperty(SDK_PROTOCOL, "http");
        config.setProperty(SDK_PORT, "8080");
        config.setProperty(SDK_USERNAME, "admin@internal");
        config.setProperty(SDK_PASSWORD, "letmein");
        config.setProperty(SDK_CA_STORE, "/etc/ovirt-optimizer/ca.store");
        config.setProperty(SDK_REQUEST_TIMEOUT, "10");
        config.setProperty(SOLVER_STEPS, "10");
        config.setProperty(SOLVER_TIMEOUT, "30");
        config.setProperty(SOLVER_DATA_REFRESH, "60");
        config.setProperty(SOLVER_CLUSTER_REFRESH, "300");
        config.setProperty(SOLVER_CUSTOM_RULE_DIR, "/etc/ovirt-optimizer/rules.d");
        config.setProperty(DEBUG_ENDPOINT_ENABLED, "false");
    }

    @SuppressFBWarnings("DM_DEFAULT_ENCODING")
    public ConfigProvider load() {
        FileReader reader = null;

        try {
            reader = new FileReader(configFile);
            config.load(reader);
        } catch (IOException ex) {
            log.error("Connection to oVirt REST server failed", ex);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ex) {
                log.error("The config file could not be closed", ex);
            }
        }

        return this;
    }

    public Properties getConfig() {
        return config;
    }

    public List<File> customRuleFiles() {
        File dir = new File(config.getProperty(SOLVER_CUSTOM_RULE_DIR));
        List<File> listOfDrlFiles = new ArrayList<>();

        File[] candidateFiles = dir.listFiles();

        if (candidateFiles == null) {
            log.warn("Could not get a list of custom DRL files");
            return listOfDrlFiles;
        }

        log.debug("Found {} custom DRL candidate files", candidateFiles.length);

        for (File input: candidateFiles) {
            if (input.isFile()
                    && input.getName().endsWith(".drl")) {
                listOfDrlFiles.add(input);
                log.debug("Using {} custom DRL file", input.getName());
            }
        }

        return listOfDrlFiles;
    }
}

@@ -1641,20 +1641,26 @@
 onfig = 
-new 
+System.get
 Properti
@@ -1860,35 +1860,35 @@
      config.
-setProperty
+putIfAbsent
 (SDK_SERVER,
@@ -1913,35 +1913,35 @@
      config.
-setProperty
+putIfAbsent
 (SDK_PROTOCO
@@ -1963,35 +1963,35 @@
      config.
-setProperty
+putIfAbsent
 (SDK_PORT, %22
@@ -2009,35 +2009,35 @@
      config.
-setProperty
+putIfAbsent
 (SDK_USERNAM
@@ -2069,35 +2069,35 @@
      config.
-setProperty
+putIfAbsent
 (SDK_PASSWOR
@@ -2122,35 +2122,35 @@
      config.
-setProperty
+putIfAbsent
 (SDK_CA_STOR
@@ -2201,27 +2201,27 @@
  config.
-setProperty
+putIfAbsent
 (SDK_REQ
@@ -2252,35 +2252,35 @@
      config.
-setProperty
+putIfAbsent
 (SOLVER_STEP
@@ -2300,35 +2300,35 @@
      config.
-setProperty
+putIfAbsent
 (SOLVER_TIME
@@ -2350,35 +2350,35 @@
      config.
-setProperty
+putIfAbsent
 (SOLVER_DATA
@@ -2405,35 +2405,35 @@
      config.
-setProperty
+putIfAbsent
 (SOLVER_CLUS
@@ -2468,27 +2468,27 @@
  config.
-setProperty
+putIfAbsent
 (SOLVER_
@@ -2556,19 +2556,19 @@
 fig.
-setProperty
+putIfAbsent
 (DEB

Use System configuration to allow passing config arguments on command line