001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Funsho David
018 */
019
020package org.nuxeo.elasticsearch.core;
021
022import org.elasticsearch.client.transport.TransportClient;
023import org.elasticsearch.common.settings.Settings;
024import org.nuxeo.elasticsearch.api.ESClientInitializationService;
025import org.nuxeo.elasticsearch.config.ElasticSearchRemoteConfig;
026
027/**
028 * @since 9.1
029 */
030public class ESClientInitializationServiceImpl implements ESClientInitializationService {
031
032    protected String username;
033
034    protected String password;
035
036    @Override
037    public Settings initializeSettings(ElasticSearchRemoteConfig config) {
038        return initializeSettingsBuilder(config).build();
039    }
040
041    @Override
042    public TransportClient initializeClient(Settings settings) {
043        return initializeClientBuilder().settings(settings).build();
044    }
045
046    @Override
047    public String getUsername() {
048        return username;
049    }
050
051    @Override
052    public void setUsername(String username) {
053        this.username = username;
054    }
055
056    @Override
057    public String getPassword() {
058        return password;
059    }
060
061    @Override
062    public void setPassword(String password) {
063        this.password = password;
064    }
065
066    protected Settings.Builder initializeSettingsBuilder(ElasticSearchRemoteConfig config) {
067        Settings.Builder builder = Settings.settingsBuilder()
068                                           .put("cluster.name", config.getClusterName())
069                                           .put("client.transport.nodes_sampler_interval", config.getSamplerInterval())
070                                           .put("client.transport.ping_timeout", config.getPingTimeout())
071                                           .put("client.transport.ignore_cluster_name", config.isIgnoreClusterName())
072                                           .put("client.transport.sniff", config.isClusterSniff());
073        return builder;
074    }
075
076    protected TransportClient.Builder initializeClientBuilder() {
077        return TransportClient.builder();
078    }
079}