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    protected String sslKeystorePath;
037
038    protected String sslKeystorePassword;
039
040    @Override
041    public Settings initializeSettings(ElasticSearchRemoteConfig config) {
042        return initializeSettingsBuilder(config).build();
043    }
044
045    @Override
046    public TransportClient initializeClient(Settings settings) {
047        return initializeClientBuilder().settings(settings).build();
048    }
049
050    @Override
051    public String getUsername() {
052        return username;
053    }
054
055    @Override
056    public void setUsername(String username) {
057        this.username = username;
058    }
059
060    @Override
061    public String getPassword() {
062        return password;
063    }
064
065    @Override
066    public void setPassword(String password) {
067        this.password = password;
068    }
069
070    @Override
071    public String getSslKeystorePath() {
072        return sslKeystorePath;
073    }
074
075    @Override
076    public void setSslKeystorePath(String sslKeystorePath) {
077        this.sslKeystorePath = sslKeystorePath;
078    }
079
080    @Override
081    public String getSslKeystorePassword() {
082        return sslKeystorePassword;
083    }
084
085    @Override
086    public void setSslKeystorePassword(String sslKeystorePassword) {
087        this.sslKeystorePassword = sslKeystorePassword;
088    }
089
090    protected Settings.Builder initializeSettingsBuilder(ElasticSearchRemoteConfig config) {
091        Settings.Builder builder = Settings.settingsBuilder()
092                                           .put("cluster.name", config.getClusterName())
093                                           .put("client.transport.nodes_sampler_interval", config.getSamplerInterval())
094                                           .put("client.transport.ping_timeout", config.getPingTimeout())
095                                           .put("client.transport.ignore_cluster_name", config.isIgnoreClusterName())
096                                           .put("client.transport.sniff", config.isClusterSniff());
097        return builder;
098    }
099
100    protected TransportClient.Builder initializeClientBuilder() {
101        return TransportClient.builder();
102    }
103}