001package io.ebean.config;
002
003import io.ebean.Transaction;
004import io.ebean.annotation.DocStoreMode;
005
006/**
007 * Configuration for the Document store integration (e.g. ElasticSearch).
008 */
009public class DocStoreConfig {
010
011  /**
012   * True when the Document store integration is active/on.
013   */
014  protected boolean active;
015
016  /**
017   * Set to true means Ebean will generate mapping files on startup.
018   */
019  protected boolean generateMapping;
020
021  /**
022   * When true the Document store should drop and re-create document indexes.
023   */
024  protected boolean dropCreate;
025
026  /**
027   * When true the Document store should create any document indexes that don't already exist.
028   */
029  protected boolean create;
030
031  /**
032   * The URL of the Document store. For example: http://localhost:9200.
033   */
034  protected String url;
035
036  /**
037   * Credential that be used for authentication to document store.
038   */
039  protected String username;
040
041  /**
042   * Password credential that be used for authentication to document store.
043   */
044  protected String password;
045
046  /**
047   * Set to true such that the client allows connections to invalid/self signed SSL certificates.
048   */
049  protected boolean allowAllCertificates;
050
051  /**
052   * The default mode used by indexes.
053   */
054  protected DocStoreMode persist = DocStoreMode.UPDATE;
055
056  /**
057   * The default batch size to use for the Bulk API calls.
058   */
059  protected int bulkBatchSize = 1000;
060
061  /**
062   * Resource path for the Document store mapping files.
063   */
064  protected String mappingPath;
065
066  /**
067   * Suffix used for mapping files.
068   */
069  protected String mappingSuffix;
070
071  /**
072   * Location of resources that mapping files are generated into.
073   */
074  protected String pathToResources = "src/main/resources";
075
076  /**
077   * Return true if the Document store (ElasticSearch) integration is active.
078   */
079  public boolean isActive() {
080    String systemValue = System.getProperty("ebean.docstore.active");
081    if (systemValue != null) {
082      return Boolean.parseBoolean(systemValue);
083    }
084    return active;
085  }
086
087  /**
088   * Set to true to make the Document store (ElasticSearch) integration active.
089   */
090  public void setActive(boolean active) {
091    this.active = active;
092  }
093
094  /**
095   * Return the URL to the Document store.
096   */
097  public String getUrl() {
098    String systemValue = System.getProperty("ebean.docstore.url");
099    if (systemValue != null) {
100      return systemValue;
101    }
102    return url;
103  }
104
105  /**
106   * Return the user credential for connecting to the document store.
107   */
108  public String getUsername() {
109    return username;
110  }
111
112  /**
113   * Set the user credential for connecting to the document store.
114   */
115  public void setUsername(String username) {
116    this.username = username;
117  }
118
119  /**
120   * Return the password credential for connecting to the document store.
121   */
122  public String getPassword() {
123    return password;
124  }
125
126  /**
127   * Set the password credential for connecting to the document store.
128   */
129  public void setPassword(String password) {
130    this.password = password;
131  }
132
133  /**
134   * Set the URL to the Document store.
135   * <p>
136   * For a local ElasticSearch server this would be: http://localhost:9200
137   */
138  public void setUrl(String url) {
139    this.url = url;
140  }
141
142  /**
143   * Return true if Ebean should generate mapping files on server startup.
144   */
145  public boolean isGenerateMapping() {
146    String systemValue = System.getProperty("ebean.docstore.generateMapping");
147    if (systemValue != null) {
148      return Boolean.parseBoolean(systemValue);
149    }
150    return generateMapping;
151  }
152
153  /**
154   * Set to true if Ebean should generate mapping files on server startup.
155   */
156  public void setGenerateMapping(boolean generateMapping) {
157    this.generateMapping = generateMapping;
158  }
159
160  /**
161   * Return true if the document store should recreate mapped indexes.
162   */
163  public boolean isDropCreate() {
164    String systemValue = System.getProperty("ebean.docstore.dropCreate");
165    if (systemValue != null) {
166      return Boolean.parseBoolean(systemValue);
167    }
168    return dropCreate;
169  }
170
171  /**
172   * Set to true if the document store should recreate mapped indexes.
173   */
174  public void setDropCreate(boolean dropCreate) {
175    this.dropCreate = dropCreate;
176  }
177
178  /**
179   * Create true if the document store should create mapped indexes that don't yet exist.
180   * This is only used if dropCreate is false.
181   */
182  public boolean isCreate() {
183    String systemValue = System.getProperty("ebean.docstore.create");
184    if (systemValue != null) {
185      return Boolean.parseBoolean(systemValue);
186    }
187    return create;
188  }
189
190  /**
191   * Set to true if the document store should create mapped indexes that don't yet exist.
192   * This is only used if dropCreate is false.
193   */
194  public void setCreate(boolean create) {
195    this.create = create;
196  }
197
198  /**
199   * Return true if the client allows connections to invalid/self signed SSL certificates.
200   */
201  public boolean isAllowAllCertificates() {
202    return allowAllCertificates;
203  }
204
205  /**
206   * Set to true such that the client allows connections to invalid/self signed SSL certificates.
207   */
208  public void setAllowAllCertificates(boolean allowAllCertificates) {
209    this.allowAllCertificates = allowAllCertificates;
210  }
211
212  /**
213   * Return the default batch size to use for calls to the Bulk API.
214   */
215  public int getBulkBatchSize() {
216    return bulkBatchSize;
217  }
218
219  /**
220   * Set the default batch size to use for calls to the Bulk API.
221   * <p>
222   * The batch size can be set on a transaction via {@link Transaction#setDocStoreBatchSize(int)}.
223   * </p>
224   */
225  public void setBulkBatchSize(int bulkBatchSize) {
226    this.bulkBatchSize = bulkBatchSize;
227  }
228
229  /**
230   * Return the mapping path.
231   */
232  public String getMappingPath() {
233    return mappingPath;
234  }
235
236  /**
237   * Set the mapping path.
238   */
239  public void setMappingPath(String mappingPath) {
240    this.mappingPath = mappingPath;
241  }
242
243  /**
244   * Return the mapping suffix.
245   */
246  public String getMappingSuffix() {
247    return mappingSuffix;
248  }
249
250  /**
251   * Set the mapping suffix.
252   */
253  public void setMappingSuffix(String mappingSuffix) {
254    this.mappingSuffix = mappingSuffix;
255  }
256
257  /**
258   * Return the relative file system path to resources when generating mapping files.
259   */
260  public String getPathToResources() {
261    return pathToResources;
262  }
263
264  /**
265   * Set the relative file system path to resources when generating mapping files.
266   */
267  public void setPathToResources(String pathToResources) {
268    this.pathToResources = pathToResources;
269  }
270
271  /**
272   * Return the default behavior for when Insert, Update and Delete events occur on beans that have an associated
273   * Document store.
274   */
275  public DocStoreMode getPersist() {
276    return persist;
277  }
278
279  /**
280   * Set the default behavior for when Insert, Update and Delete events occur on beans that have an associated
281   * Document store.
282   * <ul>
283   * <li>DocStoreEvent.UPDATE - build and send message to Bulk API</li>
284   * <li>DocStoreEvent.QUEUE - add an entry with the index type and id only into a queue for later processing</li>
285   * <li>DocStoreEvent.IGNORE - ignore. Most likely used when some scheduled batch job handles updating the index</li>
286   * </ul>
287   * <p>
288   * You might choose to use QUEUE if that particular index data is updating very frequently or the cost of indexing
289   * is expensive.  Setting it to QUEUE can mean many changes can be batched together potentially coalescing multiple
290   * updates for an index entry into a single update.
291   * </p>
292   * <p>
293   * You might choose to use IGNORE when you have your own external process for updating the indexes. In this case
294   * you don't want Ebean to do anything when the data changes.
295   * </p>
296   */
297  public void setPersist(DocStoreMode persist) {
298    this.persist = persist;
299  }
300
301  /**
302   * Load settings specified in properties files.
303   */
304  public void loadSettings(PropertiesWrapper properties) {
305
306    active = properties.getBoolean("docstore.active", active);
307    url = properties.get("docstore.url", url);
308    username = properties.get("docstore.username", url);
309    password = properties.get("docstore.password", url);
310    persist = properties.getEnum(DocStoreMode.class, "docstore.persist", persist);
311    bulkBatchSize = properties.getInt("docstore.bulkBatchSize", bulkBatchSize);
312    generateMapping = properties.getBoolean("docstore.generateMapping", generateMapping);
313    dropCreate = properties.getBoolean("docstore.dropCreate", dropCreate);
314    create = properties.getBoolean("docstore.create", create);
315    allowAllCertificates = properties.getBoolean("docstore.allowAllCertificates", allowAllCertificates);
316    mappingPath = properties.get("docstore.mappingPath", mappingPath);
317    mappingSuffix = properties.get("docstore.mappingSuffix", mappingSuffix);
318    pathToResources = properties.get("docstore.pathToResources", pathToResources);
319  }
320}