All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jslsolucoes.elasticsearch.ee.ElasticSearchCreator Maven / Gradle / Ivy

There is a newer version: 1.0.32
Show newest version
package com.jslsolucoes.elasticsearch.ee;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import org.apache.http.HttpHost;

import com.jslsolucoes.properties.ee.ApplicationProperties;
import com.jslsolucoes.properties.ee.ApplicationProperty;

public class ElasticSearchCreator {

    @Inject
    @ApplicationProperties(name = "elasticsearch.properties", defaults = {
	    @ApplicationProperty(key = "elasticsearch.endpoints", value = "http://localhost:9200") })
    private Properties properties;

    @Produces
    @ApplicationScoped
    public ElasticSearch getInstance() {
	ElasticSearch elasticSearch = new ElasticSearch();
	elasticSearch.setHosts(hosts(properties.getProperty("elasticsearch.endpoints")));
	return elasticSearch;
    }

    private HttpHost[] hosts(String endpoints) {
	List hosts = new ArrayList<>();
	StringTokenizer stringTokenizer = new StringTokenizer(endpoints, ",");
	while (stringTokenizer.hasMoreTokens()) {
	    try {
		URL endpoint = new URL(stringTokenizer.nextToken());
		hosts.add(new HttpHost(endpoint.getHost(), endpoint.getPort(), endpoint.getProtocol()));
	    } catch (Exception e) {
		throw new RuntimeException(e);
	    }
	}
	return hosts.toArray(new HttpHost[] {});
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy