Skip to content

Instantly share code, notes, and snippets.

@anhtv08
Created October 13, 2018 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhtv08/e220a01b21eadc730583f0913b013b0a to your computer and use it in GitHub Desktop.
Save anhtv08/e220a01b21eadc730583f0913b013b0a to your computer and use it in GitHub Desktop.
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure.data.elasticsearch;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.lease.Releasable;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Settings.Builder;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.NodeClientFactoryBean;
import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
@Configuration
@ConditionalOnClass({Client.class, TransportClientFactoryBean.class, NodeClientFactoryBean.class})
@EnableConfigurationProperties({ElasticsearchProperties.class})
public class ElasticsearchAutoConfiguration implements DisposableBean {
private static final Map<String, String> DEFAULTS;
private static final Log logger;
private final ElasticsearchProperties properties;
private Releasable releasable;
public ElasticsearchAutoConfiguration(ElasticsearchProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean
public Client elasticsearchClient() {
try {
return this.createClient();
} catch (Exception var2) {
throw new IllegalStateException(var2);
}
}
private Client createClient() throws Exception {
return StringUtils.hasLength(this.properties.getClusterNodes()) ? this.createTransportClient() : this.createNodeClient();
}
private Client createNodeClient() throws Exception {
Builder settings = Settings.settingsBuilder();
Iterator var2 = DEFAULTS.entrySet().iterator();
while(var2.hasNext()) {
Entry<String, String> entry = (Entry)var2.next();
if (!this.properties.getProperties().containsKey(entry.getKey())) {
settings.put((String)entry.getKey(), (String)entry.getValue());
}
}
settings.put(this.properties.getProperties());
Node node = (new NodeBuilder()).settings(settings).clusterName(this.properties.getClusterName()).node();
this.releasable = node;
return node.client();
}
private Client createTransportClient() throws Exception {
TransportClientFactoryBean factory = new TransportClientFactoryBean();
factory.setClusterNodes(this.properties.getClusterNodes());
factory.setProperties(this.createProperties());
factory.afterPropertiesSet();
TransportClient client = factory.getObject();
this.releasable = client;
return client;
}
private Properties createProperties() {
Properties properties = new Properties();
properties.put("cluster.name", this.properties.getClusterName());
properties.putAll(this.properties.getProperties());
return properties;
}
public void destroy() throws Exception {
if (this.releasable != null) {
try {
if (logger.isInfoEnabled()) {
logger.info("Closing Elasticsearch client");
}
try {
this.releasable.close();
} catch (NoSuchMethodError var2) {
ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(Releasable.class, "release"), this.releasable);
}
} catch (Exception var3) {
if (logger.isErrorEnabled()) {
logger.error("Error closing Elasticsearch client: ", var3);
}
}
}
}
static {
Map<String, String> defaults = new LinkedHashMap();
defaults.put("http.enabled", String.valueOf(false));
defaults.put("node.local", String.valueOf(true));
defaults.put("path.home", System.getProperty("user.dir"));
DEFAULTS = Collections.unmodifiableMap(defaults);
logger = LogFactory.getLog(ElasticsearchAutoConfiguration.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment