View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.protocol.monitoring;
22  
23  import org.joda.time.format.DateTimeFormat;
24  import org.joda.time.format.DateTimeFormatter;
25  import org.waarp.common.logging.SysErrLogger;
26  import org.waarp.common.utility.WaarpSystemUtil;
27  
28  /**
29   * Builder of ElasticsearchMonitoringExporterClientFactory
30   */
31  public class ElasticsearchMonitoringExporterClientBuilder {
32    private static ElasticsearchMonitoringExporterClientFactory factory = null;
33  
34    public static final String ELASTIC_WAARPHOST = "%%WAARPHOST%%";
35    public static final String ELASTIC_DATETIME = "%%DATETIME%%";
36    public static final DateTimeFormatter FORMAT_DATETIME =
37        DateTimeFormat.forPattern("yyyy.MM.dd.HH.mm");
38    public static final String ELASTIC_DATEHOUR = "%%DATEHOUR%%";
39    public static final DateTimeFormatter FORMAT_DATEHOUR =
40        DateTimeFormat.forPattern("yyyy.MM.dd.HH");
41    public static final String ELASTIC_DATE = "%%DATE%%";
42    public static final DateTimeFormatter FORMAT_DATE =
43        DateTimeFormat.forPattern("yyyy.MM.dd");
44    public static final String ELASTIC_YEAR_MONTH = "%%YEARMONTH%%";
45    public static final DateTimeFormatter FORMAT_YEAR_MONTH =
46        DateTimeFormat.forPattern("yyyy.MM");
47    public static final String ELASTIC_YEAR = "%%YEAR%%";
48    public static final DateTimeFormatter FORMAT_YEAR =
49        DateTimeFormat.forPattern("yyyy");
50  
51    public static final String ELASTICSEARCH_CLIENT_FACTORY_IMPL =
52        "org.waarp.openr66.elasticsearch.ElasticsearchMonitoringExporterClientFactoryImpl";
53  
54    static {
55      // Try to load ElasticsearchFactory if class exists
56      try {
57        final Class elasticsearchFactoryClass =
58            Class.forName(ELASTICSEARCH_CLIENT_FACTORY_IMPL);
59        factory =
60            (ElasticsearchMonitoringExporterClientFactory) WaarpSystemUtil.newInstance(
61                elasticsearchFactoryClass);
62      } catch (final Exception ignore) {
63        // Not found and ignore
64        SysErrLogger.FAKE_LOGGER.ignoreLog(ignore);
65      }
66    }
67  
68    /**
69     * Set from JRE8 or greather with a valid ElasticsearchMonitoringExporterClientFactory
70     *
71     * @param factoryToSet
72     */
73    public static void setFactory(
74        final ElasticsearchMonitoringExporterClientFactory factoryToSet) {
75      factory = factoryToSet;
76    }
77  
78    /**
79     * @return the current ElasticsearchMonitoringExporterClientFactory
80     *
81     * @throws IllegalArgumentException if no Factory is setup
82     */
83    public static ElasticsearchMonitoringExporterClientFactory getFactory() {
84      if (factory == null) {
85        throw new IllegalArgumentException(
86            "No Factory setup for Elasticsearch client");
87      } else {
88        return factory;
89      }
90    }
91  }