/*
 *
 * CODENVY CONFIDENTIAL
 * ________________
 *
 * [2012] - [2013] Codenvy, S.A.
 * All Rights Reserved.
 * NOTICE: All information contained herein is, and remains
 * the property of Codenvy S.A. and its suppliers,
 * if any. The intellectual and technical concepts contained
 * herein are proprietary to Codenvy S.A.
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Codenvy S.A..
 */
package com.codenvy.analytics;

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

import java.io.*;
import java.util.Properties;

/**
 * It is supposed to keep configuration of all services in one place.
 *
 * @author <a href="mailto:abazko@codenvy.com">Anatoliy Bazko</a>
 */
public class Configurator {

    /** Logger. */
    private static final Logger LOG = LoggerFactory.getLogger(Configurator.class);

    public static final String ANALYTICS_CONF_DIR      = System.getProperty("analytics.conf.dir");
    public static final String ANALYTICS_TMP_DIRECTORY = System.getProperty("analytics.tmp.dir");

    private static final String RESOURCE = "analytics.conf";

    private static Properties properties = new Properties();

    static {
        try {
            loadFromResource();
        } catch (IOException e) {
            try {
                loadFromFile();
            } catch (IOException e1) {
                throw new IllegalStateException(
                        "Configurator can't be instantiated. There is no configuration to read from");
            }
        }
    }

    /** @return value of the property as the array of String */
    public static String[] getArray(String key) {
        return getString(key).split(",");
    }

    /** @return value of the property of the String type */
    public static String getString(String key) {
        return (String)properties.get(key);
    }

    /** @return value of the property of the boolean type */
    public static boolean getBoolean(String key) {
        return Boolean.parseBoolean(getString(key));
    }


    /** @return value of the property of the int type */
    public static int getInt(String key) {
        return Integer.parseInt(getString(key));
    }

    /** {@link Properties#containsKey(Object)} */
    public static boolean exists(String key) {
        return properties.containsKey(key);
    }

    private static void loadFromResource() throws IOException {
        try (InputStream in = Configurator.class.getClassLoader().getResourceAsStream(RESOURCE)) {
            if (in == null) {
                throw new FileNotFoundException("Resource not found " + RESOURCE);
            }

            properties.load(in);
            LOG.info("Configuration has been read from resource " + RESOURCE);
        }
    }

    private static void loadFromFile() throws IOException {
        File file = new File(ANALYTICS_CONF_DIR, RESOURCE);

        try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
            properties.load(in);
            LOG.info("Configuration has been read from file " + file.getCanonicalPath());
        }
    }
}

@@ -1044,29 +1044,30 @@
 ass);%0A%0A    p
-ublic
+rivate
  static fina
@@ -1079,74 +1079,109 @@
 ing 
-ANALYTICS_CONF_DIR      = System.getProperty(%22analytics.conf.dir%22)
+CODENVY_LOCAL_CONF_DIR = %22CODENVY_LOCAL_CONF_DIR%22;%0A%0A    public static final String ANALYTICS_CONF_DIR
 ;%0A  
@@ -1750,32 +1750,274 @@
      %7D%0A        %7D
+%0A%0A        if (System.getenv().containsKey(CODENVY_LOCAL_CONF_DIR)) %7B%0A            ANALYTICS_CONF_DIR = System.getenv(CODENVY_LOCAL_CONF_DIR);%0A        %7D else %7B%0A            ANALYTICS_CONF_DIR = System.getProperty(%22analytics.conf.dir%22);%0A        %7D
 %0A    %7D%0A%0A    /** 

Read configuration from CODENVY_LOCAL_CONF_DIR