package com.arquivolivre.elastiky.config;

import java.io.InputStream;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

/**
 *
 * @author Thiago da Silva Gonzaga <thiagosg@sjrp.unesp.br>
 */
public class Configuration {

    protected TransportClientFactory transportFactory;
    private final String CLUSTER_NAME_KEY = "elasticsearch.cluster.name";
    private final String CLUSTER_NODES_KEY = "elasticsearch.cluster.nodes";
    private final String CLUSTER_PORTS_KEY = "elasticsearch.cluster.ports";
    private final String DEFAULT_PORT_KEY = "elasticsearch.port";
    private final String DEFAULT_CLUSTER_NODES_KEY = "elasticsearch.nodes";
    private final String DEFAULT_NODE_KEY = "elasticsearch.node";
    private Map<String, String> environment;

    public Client elasticSearchClient() {
        environment = getResources();
        Settings settings = null;
        String[] hosts = null;
        String[] ports = null;
        String host = null;
        String port = "9300"; //porta padrao
        //verifica configuraçao de cluster ou padrao
        if (environment.containsKey(CLUSTER_NAME_KEY)) {
            settings = ImmutableSettings.settingsBuilder().put("cluster.name", environment.get(CLUSTER_NAME_KEY)).build();
            hosts = environment.get(CLUSTER_NODES_KEY).split(",");
        } else if (environment.containsKey(DEFAULT_CLUSTER_NODES_KEY)) {
            hosts = environment.get(DEFAULT_CLUSTER_NODES_KEY).split(",");
        } else {
            host = environment.get(DEFAULT_NODE_KEY);
        }

        //verifica portas padrão
        if (environment.containsKey(CLUSTER_PORTS_KEY)) {
            ports = environment.get(CLUSTER_PORTS_KEY).split(",");
        } else {
            port = environment.get(DEFAULT_PORT_KEY);
        }

        if (hosts != null) {
            InetSocketTransportAddress[] addrs = new InetSocketTransportAddress[hosts.length];
            for (int i = 0; i < hosts.length; i++) {
                if (ports != null) {
                    port = ports[i];
                }
                addrs[i] = new InetSocketTransportAddress(hosts[i].trim(), Integer.parseInt(port.trim()));
            }
            return transportFactory.getTransport(settings).addTransportAddresses(addrs);
        }

        InetSocketTransportAddress addr = new InetSocketTransportAddress(host, Integer.parseInt(port.trim()));
        return transportFactory.getTransport().addTransportAddress(addr);

    }

    private Map<String, String> getResources() {
        //TODO:default configuration file
        InputStream resourceAsStream = this.getClass().getResourceAsStream(CLUSTER_NAME_KEY);
        Scanner sc = new Scanner(resourceAsStream);
        Map<String, String> result = new TreeMap<>();
        while (sc.hasNext()) {
            String nextLine = sc.nextLine();
            if (nextLine.startsWith("#")) {
                continue;
            }
            String[] split = nextLine.split("=");
            if (split.length > 1) {
                result.put(split[0], split[1]);
            }
        }
        return result;
    }

}

@@ -52,26 +52,26 @@
 ava.io.I
-nputStream
+OException
 ;%0Aimport
@@ -80,16 +80,22 @@
 ava.
-util.Map
+io.InputStream
 ;%0Aim
@@ -113,41 +113,50 @@
 til.
-Scanner;%0Aimport java.util.TreeMap
+Properties;%0Aimport org.apache.log4j.Logger
 ;%0Aim
@@ -475,16 +475,20 @@
 ation %7B%0A
+    
 %0A    pro
@@ -985,41 +985,108 @@
 ate 
-Map%3CString, String%3E environment;%0A
+Properties properties;%0A    private final Logger logger = Logger.getLogger(Configuration.class);%0A    
 %0A   
@@ -1132,27 +1132,26 @@
         
-environment
+properties
  = getRe
@@ -1391,35 +1391,34 @@
         if (
-environment
+properties
 .containsKey
@@ -1518,27 +1518,26 @@
 .name%22, 
-environment
+properties
 .get(CLU
@@ -1577,39 +1577,46 @@
     hosts = 
-environment.get
+properties.getProperty
 (CLUSTER_NOD
@@ -1650,35 +1650,34 @@
  %7D else if (
-environment
+properties
 .containsKey
@@ -1727,31 +1727,38 @@
 hosts = 
-environment.get
+properties.getProperty
 (DEFAULT
@@ -1821,39 +1821,46 @@
      host = 
-environment.get
+properties.getProperty
 (DEFAULT_NOD
@@ -1923,27 +1923,26 @@
     if (
-environment
+properties
 .contain
@@ -1988,31 +1988,38 @@
 ports = 
-environment.get
+properties.getProperty
 (CLUSTER
@@ -2082,23 +2082,30 @@
 t = 
-environment.get
+properties.getProperty
 (DEF
@@ -2122,32 +2122,40 @@
 KEY);%0A        %7D%0A
+        
 %0A        if (hos
@@ -2620,32 +2620,40 @@
 drs);%0A        %7D%0A
+        
 %0A        InetSoc
@@ -2822,23 +2822,35 @@
 (addr);%0A
+        
 %0A    %7D%0A
+    
 %0A    pri
@@ -2854,35 +2854,26 @@
 private 
-Map%3CString, String%3E
+Properties
  getReso
@@ -2923,16 +2923,21 @@
 ion file
+ name
 %0A       
@@ -2941,507 +2941,222 @@
     
-InputStream resourceAsStream = this.getClass().getResourceAsStream(CLUSTER_NAME_KEY);%0A        Scanner sc = new Scanner(resourceAsStream);%0A        Map%3CString, String%3E result = new TreeMap%3C%3E();%0A        while (sc.hasNext()) %7B%0A            String nextLine = sc.nextLine();%0A            if (nextLine.startsWith(%22#%22)) %7B%0A                continue;%0A            %7D%0A            String%5B%5D split = nextLine.split(%22=%22);%0A            if (split.length %3E 1) %7B%0A                result.put(split%5B0%5D, split%5B1%5D);%0A            %7D
+Properties prop = new Properties();%0A        try (InputStream in = getClass().getResourceAsStream(%22es.properties%22)) %7B%0A            prop.load(in);%0A        %7D catch (IOException ex) %7B%0A            logger.error(null, ex);
 %0A   
@@ -3181,21 +3181,19 @@
 urn 
-result
+prop
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -3189,11 +3189,15 @@
 ;%0A    %7D%0A
+    
 %0A%7D%0A

read property file