001/*
002 * (C) Copyright 2014-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 *     bdelbosc
018 */
019
020package org.nuxeo.elasticsearch.config;
021
022import java.io.File;
023
024import org.nuxeo.common.Environment;
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XObject;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * Configuration of an embedded (same JVM as Nuxeo) Elasticsearch server.
031 */
032@XObject(value = "elasticSearchEmbeddedServer")
033public class ElasticSearchEmbeddedServerConfig {
034
035    @XNode("@enabled")
036    protected boolean isEnabled = true;
037
038    @XNode("@clusterName")
039    protected String clusterName;
040
041    @XNode("@nodeName")
042    protected String nodeName = "Nuxeo";
043
044    // @since 8.4
045    @XNode("@pathHome")
046    private String homePath;
047
048    @XNode("@pathData")
049    protected String dataPath;
050
051    @XNode("@indexStoreType")
052    protected String indexStoreType;
053
054    @XNode("@httpEnabled")
055    protected boolean httpEnabled = false;
056
057    @XNode("@httpPort")
058    protected String httpPort = "9200-9300";
059
060    @XNode("@networkHost")
061    protected String networkHost = "127.0.0.1";
062
063    // @since 8.3
064    @XNode("@useExternalVersion")
065    protected boolean externalVersion = true;
066
067    public String getClusterName() {
068        return clusterName;
069    }
070
071    public void setClusterName(String clusterName) {
072        this.clusterName = clusterName;
073    }
074
075    /**
076     * @since 8.4
077     */
078    public String getHomePath() {
079        if (homePath == null) {
080            // Since ES 2.X we need to set a home path for embedded node, but it is not used by the bundle
081            File dir = new File(Environment.getDefault().getTemp(), "elasticsearch");
082            homePath = dir.getPath();
083        }
084        return homePath;
085    }
086
087    /**
088     * @since 8.4
089     */
090    public void setHomePath(String homePath) {
091        this.homePath = homePath;
092    }
093
094    public String getDataPath() {
095        if (dataPath == null) {
096            File dir = new File(Framework.getRuntime().getHome(), "data/elasticsearch");
097            dataPath = dir.getPath();
098        }
099        return dataPath;
100    }
101
102    public void setDataPath(String dataPath) {
103        this.dataPath = dataPath;
104    }
105
106    public String getIndexStorageType() {
107        if (indexStoreType == null) {
108            indexStoreType = "mmapfs";
109        }
110        return indexStoreType;
111    }
112
113    public void setIndexStorageType(String indexStorageType) {
114        this.indexStoreType = indexStorageType;
115    }
116
117    /**
118     * @since 7.4
119     */
120    public String getNetworkHost() {
121        return networkHost;
122    }
123
124    /**
125     * @since 7.4
126     */
127    public void setNetworkHost(String networkHost) {
128        this.networkHost = networkHost;
129    }
130
131    public String getNodeName() {
132        return nodeName;
133    }
134
135    public void setNodeName(String nodeName) {
136        this.nodeName = nodeName;
137    }
138
139    public boolean httpEnabled() {
140        return httpEnabled;
141    }
142
143    public boolean isEnabled() {
144        return isEnabled;
145    }
146
147    public void setEnabled(boolean isEnabled) {
148        this.isEnabled = isEnabled;
149    }
150
151    public void setHttpEnabled(boolean httpEnabled) {
152        this.httpEnabled = httpEnabled;
153    }
154
155    public boolean useExternalVersion() {
156        return externalVersion;
157    }
158
159    @Override
160    public String toString() {
161        if (isEnabled()) {
162            return String.format("EsLocalConfig(%s, %s, %s, %s)", getClusterName(), getDataPath(), httpEnabled(),
163                    getIndexStorageType());
164        }
165        return "EsLocalConfig disabled";
166    }
167
168    public String getHttpPort() {
169        return httpPort;
170    }
171}