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