/*
 * Copyright 2012-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.elasticsearch;

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.ImmutableSettings;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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;

/**
 * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
 * Auto-configuration} for Elasticsearch.
 *
 * @author Artur Konczak
 * @author Mohsin Husen
 * @author Andy Wilkinson
 * @since 1.1.0
 */
@Configuration
@ConditionalOnClass({ Client.class, TransportClientFactoryBean.class,
		NodeClientFactoryBean.class })
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchAutoConfiguration implements DisposableBean {

	private static Log logger = LogFactory.getLog(ElasticsearchAutoConfiguration.class);

	@Autowired
	private ElasticsearchProperties properties;

	private Releasable releasable;

	@Bean
	public Client elasticsearchClient() {
		try {
			return createClient();
		}
		catch (Exception ex) {
			throw new IllegalStateException(ex);
		}
	}

	private Client createClient() throws Exception {
		if (StringUtils.hasLength(this.properties.getClusterNodes())) {
			return createTransportClient();
		}
		return createNodeClient();
	}

	private Client createNodeClient() throws Exception {
		ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put(
				"http.enabled", String.valueOf(false));
		Node node = new NodeBuilder().settings(settings)
				.clusterName(this.properties.getClusterName()).local(true).node();
		this.releasable = node;
		return node.client();
	}

	private Client createTransportClient() throws Exception {
		TransportClientFactoryBean factory = new TransportClientFactoryBean();
		factory.setClusterName(this.properties.getClusterName());
		factory.setClusterNodes(this.properties.getClusterNodes());
		factory.afterPropertiesSet();
		TransportClient client = factory.getObject();
		this.releasable = client;
		return client;
	}

	@Override
	public void destroy() throws Exception {
		if (this.releasable != null) {
			try {
				if (logger.isInfoEnabled()) {
					logger.info("Closing Elasticsearch client");
				}
				if (this.releasable != null) {
					try {
						this.releasable.close();
					}
					catch (NoSuchMethodError ex) {
						// Earlier versions of Elasticsearch had a different method name
						ReflectionUtils.invokeMethod(
								ReflectionUtils.findMethod(Releasable.class, "release"),
								this.releasable);
					}
				}
			}
			catch (final Exception ex) {
				if (logger.isErrorEnabled()) {
					logger.error("Error closing Elasticsearch client: ", ex);
				}
			}
		}
	}

}

@@ -3613,44 +3613,8 @@
 %09%09%7D%0A
-%09%09%09%09if (this.releasable != null) %7B%0A%09
 %09%09%09%09
@@ -3619,17 +3619,16 @@
 %09%09try %7B%0A
-%09
 %09%09%09%09%09thi
@@ -3649,17 +3649,16 @@
 lose();%0A
-%09
 %09%09%09%09%7D%0A%09%09
@@ -3659,17 +3659,16 @@
 %09%09%7D%0A%09%09%09%09
-%09
 catch (N
@@ -3686,25 +3686,24 @@
 Error ex) %7B%0A
-%09
 %09%09%09%09%09// Earl
@@ -3760,17 +3760,16 @@
 od name%0A
-%09
 %09%09%09%09%09Ref
@@ -3798,25 +3798,24 @@
 hod(%0A%09%09%09%09%09%09%09
-%09
 ReflectionUt
@@ -3862,25 +3862,24 @@
 e%22),%0A%09%09%09%09%09%09%09
-%09
 this.releasa
@@ -3888,15 +3888,8 @@
 e);%0A
-%09%09%09%09%09%7D%0A
 %09%09%09%09

Remove unnecessary if statement