View Javadoc
1   package net.sumaris.core.config;
2   
3   /*-
4    * #%L
5    * SUMARiS :: Sumaris Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2018 SUMARiS Consortium
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  
28  import com.google.common.base.Charsets;
29  import com.google.common.base.Preconditions;
30  import net.sumaris.core.dao.technical.Daos;
31  import net.sumaris.core.exception.SumarisTechnicalException;
32  import org.apache.commons.lang3.StringUtils;
33  import org.nuiton.config.ApplicationConfig;
34  import org.nuiton.config.ApplicationConfigHelper;
35  import org.nuiton.config.ApplicationConfigProvider;
36  import org.nuiton.config.ArgumentsParserException;
37  import org.nuiton.version.Version;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  import org.springframework.beans.factory.BeanInitializationException;
41  import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
42  import org.springframework.context.annotation.Configuration;
43  
44  import java.io.File;
45  import java.io.IOException;
46  import java.io.InputStream;
47  import java.net.MalformedURLException;
48  import java.net.URL;
49  import java.util.*;
50  
51  import static org.nuiton.i18n.I18n.t;
52  
53  /**
54   * Access to configuration options
55   *
56   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
57   */
58  @Configuration
59  public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
60      /** Logger. */
61      private static final Logger log = LoggerFactory.getLogger(SumarisConfiguration.class);
62  
63      private static final String DEFAULT_SHARED_CONFIG_FILE = "sumaris-core-shared.config";
64  
65      protected static String[] args = null;
66  
67      /**
68       * <p>remember app args.</p>
69       */
70      public static void setArgs(String[] sourceArgs) {
71          args = sourceArgs;
72      }
73  
74      /**
75       * Delegate application config.
76       */
77      protected final ApplicationConfig applicationConfig;
78  
79      private static SumarisConfiguration instance;
80  
81      /**
82       * <p>Getter for the field <code>instance</code>.</p>
83       *
84       * @return a {@link SumarisConfiguration} object.
85       */
86      public static SumarisConfiguration getInstance() {
87          return instance;
88      }
89  
90      /**
91       * <p>Setter for the field <code>instance</code>.</p>
92       *
93       * @param instance a {@link SumarisConfiguration} object.
94       */
95      public static void setInstance(SumarisConfiguration instance) {
96          SumarisConfiguration.instance = instance;
97      }
98  
99      private File configFile;
100 
101     /**
102      * <p>initDefault.</p>
103      */
104     public static void initDefault(String configFile) {
105         instance = new SumarisConfiguration(configFile, args);
106         setInstance(instance);
107     }
108 
109     /**
110      * <p>Constructor for SumarisConfiguration.</p>
111      *
112      * @param applicationConfig a {@link ApplicationConfig} object.
113      */
114     public SumarisConfiguration(ApplicationConfig applicationConfig) {
115         super();
116         this.applicationConfig = applicationConfig;
117     }
118 
119     /**
120      * <p>Constructor for SumarisConfiguration.</p>
121      *
122      * @param file a {@link String} object.
123      * @param args a {@link String} object.
124      */
125     public SumarisConfiguration(String file, String... args) {
126         super();
127 
128         this.applicationConfig = new ApplicationConfig();
129         this.applicationConfig.setEncoding(Charsets.UTF_8.name());
130         this.applicationConfig.setConfigFileName(file);
131 
132         // get all config providers
133         Set<ApplicationConfigProvider> providers =
134                 ApplicationConfigHelper.getProviders(null,
135                         null,
136                         null,
137                         true);
138 
139         // load all default options
140         ApplicationConfigHelper.loadAllDefaultOption(applicationConfig,
141                 providers);
142 
143         // Load actions
144         for (ApplicationConfigProvider provider : providers) {
145             applicationConfig.loadActions(provider.getActions());
146         }
147 
148         // Define Alias
149         addAlias(applicationConfig);
150 
151         // Override some external module default config (sumaris)
152         overrideExternalModulesDefaultOptions(applicationConfig);
153 
154         // parse config file and inline arguments
155         try {
156             applicationConfig.parse(args);
157 
158         } catch (ArgumentsParserException e) {
159             throw new SumarisTechnicalException(t("sumaris.config.parse.error"), e);
160         }
161 
162         // Init the application version
163         initVersion(applicationConfig);
164 
165         // Init time zone
166         initTimeZone();
167 
168         // TODO Review this, this is very dirty to do this...
169         File appBasedir = applicationConfig.getOptionAsFile(
170                 SumarisConfigurationOption.BASEDIR.getKey());
171 
172         if (appBasedir == null) {
173             appBasedir = new File("");
174         }
175         if (!appBasedir.isAbsolute()) {
176             appBasedir = new File(appBasedir.getAbsolutePath());
177         }
178         if (appBasedir.getName().equals("..")) {
179             appBasedir = appBasedir.getParentFile().getParentFile();
180         }
181         if (appBasedir.getName().equals(".")) {
182             appBasedir = appBasedir.getParentFile();
183         }
184         if (log.isInfoEnabled()) {
185             String appName = applicationConfig.getOption(SumarisConfigurationOption.APP_NAME.getKey());
186             log.info(String.format("Starting {%s} on basedir {%s}", appName, appBasedir));
187         }
188         applicationConfig.setOption(
189                 SumarisConfigurationOption.BASEDIR.getKey(),
190                 appBasedir.getAbsolutePath());
191     }
192 
193     /**
194      * Add alias to the given ApplicationConfig. <p/>
195      * This method could be override to add specific alias
196      *
197      * @param applicationConfig a {@link ApplicationConfig} object.
198      */
199     protected void addAlias(ApplicationConfig applicationConfig) {
200         applicationConfig.addAlias("-u", "--option", SumarisConfigurationOption.JDBC_USERNAME.getKey());
201         applicationConfig.addAlias("--user", "--option", SumarisConfigurationOption.JDBC_USERNAME.getKey());
202         applicationConfig.addAlias("-p", "--option", SumarisConfigurationOption.JDBC_PASSWORD.getKey());
203         applicationConfig.addAlias("--password", "--option", SumarisConfigurationOption.JDBC_PASSWORD.getKey());
204         applicationConfig.addAlias("-db", "--option", SumarisConfigurationOption.JDBC_URL.getKey());
205         applicationConfig.addAlias("--database", "--option", SumarisConfigurationOption.JDBC_URL.getKey());
206 
207         applicationConfig.addAlias("--output", "--option", SumarisConfigurationOption.LIQUIBASE_OUTPUT_FILE.getKey());
208         applicationConfig.addAlias("-f", "--option", SumarisConfigurationOption.LIQUIBASE_FORCE_OUTPUT_FILE.getKey(), "true");
209 
210     }
211 
212     // Could be subclasses
213     /**
214      * <p>overrideExternalModulesDefaultOptions.</p>
215      *
216      * @param applicationConfig a {@link ApplicationConfig} object.
217      */
218     protected void overrideExternalModulesDefaultOptions(ApplicationConfig applicationConfig) {
219 
220     }
221 
222     /**
223      * Initialization default timezone, from configuration (mantis #24623)
224      */
225     protected void initTimeZone() {
226 
227         String dbTimeZone = applicationConfig.getOption(SumarisConfigurationOption.DB_TIMEZONE.getKey());
228         if (StringUtils.isNotBlank(dbTimeZone)) {
229             if (log.isInfoEnabled()) {
230                 log.info(String.format("Using timezone [%s] for database", dbTimeZone));
231             }
232         } else if (log.isInfoEnabled()) {
233             log.info(String.format("Using default timezone [%s] for database", System.getProperty("user.timezone")));
234         }
235     }
236 
237 
238     /**
239      * Make sure the version default value is the implementation version (using a properties file, filtered by Maven
240      * at build time). This avoid to manually update the version default value, in enumeration SumarisConfigurationOption)
241      *
242      * @param applicationConfig a {@link ApplicationConfig} object.
243      */
244     protected void initVersion(ApplicationConfig applicationConfig) {
245 
246         try {
247             // Load the properties file, from classpath
248             Properties sharedConfigFile = new Properties();
249             InputStream fis = getClass().getClassLoader().getResourceAsStream(DEFAULT_SHARED_CONFIG_FILE);
250             sharedConfigFile.load(fis);
251             fis.close();
252 
253             // If version property has been filled, use it as default version
254             String defaultVersion = applicationConfig.getOption(SumarisConfigurationOption.VERSION.getKey());
255             String implementationVersion = sharedConfigFile.getProperty(SumarisConfigurationOption.VERSION.getKey());
256             if (StringUtils.isNotBlank(implementationVersion)
257                     && StringUtils.isNotBlank(defaultVersion)
258                     && !Objects.equals(implementationVersion, defaultVersion)) {
259                 if (log.isDebugEnabled()) {
260                     log.debug(String.format("Replace default version option value [%s] with implementation value [%s] found in file [%s]",
261                             defaultVersion,
262                             implementationVersion,
263                             DEFAULT_SHARED_CONFIG_FILE));
264                 }
265                 applicationConfig.setDefaultOption(
266                         SumarisConfigurationOption.VERSION.getKey(),
267                         implementationVersion);
268             }
269             else if (StringUtils.isNotBlank(implementationVersion)) {
270                 if (log.isInfoEnabled()) {
271                     log.info("Version: " + implementationVersion);
272                 }
273                 applicationConfig.setDefaultOption(
274                         SumarisConfigurationOption.VERSION.getKey(),
275                         implementationVersion);
276             }
277             else if (StringUtils.isNotBlank(defaultVersion)) {
278                 if (log.isInfoEnabled()) {
279                     log.info("Version: " + defaultVersion);
280                 }
281             }
282             else if (log.isErrorEnabled()) {
283                 log.error(String.format("Could init version, from classpath file [%s]", DEFAULT_SHARED_CONFIG_FILE));
284             }
285         } catch (IOException e) {
286             log.warn(String.format("Could not load implementation version from file [%s]", DEFAULT_SHARED_CONFIG_FILE));
287         }
288 
289     }
290 
291     /**
292      * <p>Getter for the field <code>configFile</code>.</p>
293      *
294      * @return a {@link File} object.
295      */
296     public File getConfigFile() {
297         if (configFile == null) {
298             File dir = getBasedir();
299             if (dir == null || !dir.exists()) {
300                 dir = new File(applicationConfig.getUserConfigDirectory());
301             }
302             configFile = new File(dir, applicationConfig.getConfigFileName());
303         }
304         return configFile;
305     }
306 
307     /**
308      * <p>getBasedir.</p>
309      *
310      * @return a {@link File} object.
311      */
312     public File getBasedir() {
313         return applicationConfig.getOptionAsFile(SumarisConfigurationOption.BASEDIR.getKey());
314     }
315 
316     /**
317      * <p>getDataDirectory.</p>
318      *
319      * @return a {@link File} object.
320      */
321     public File getDataDirectory() {
322         return applicationConfig.getOptionAsFile(SumarisConfigurationOption.DATA_DIRECTORY.getKey());
323     }
324 
325     /**
326      * <p>Getter for the field <code>applicationConfig</code>.</p>
327      *
328      * @return a {@link ApplicationConfig} object.
329      */
330     public ApplicationConfig getApplicationConfig() {
331         return applicationConfig;
332     }
333 
334     /** {@inheritDoc} */
335     @Override
336     protected String resolvePlaceholder(String placeholder, Properties props) {
337         if (applicationConfig == null) {
338             throw new BeanInitializationException(
339                     "Configuration.applicationConfig must not be null. Please initialize Configuration instance with a not null applicationConfig BEFORE starting Spring.");
340         }
341 
342         // Try to resolve placeholder from application configuration
343         String optionValue = applicationConfig.getOption(placeholder);
344         if (optionValue != null) {
345             return optionValue;
346         }
347 
348         // If not found in configuration, delegate to the default Spring mecanism
349         return super.resolvePlaceholder(placeholder, props);
350     }
351 
352     /**
353      * <p>getTempDirectory.</p>
354      *
355      * @return a {@link File} object.
356      */
357     public File getTempDirectory() {
358         return applicationConfig.getOptionAsFile(SumarisConfigurationOption.TMP_DIRECTORY.getKey());
359     }
360 
361     /**
362      * <p>getDbDirectory.</p>
363      *
364      * @return a {@link File} object.
365      */
366     public File getDbDirectory() {
367         return applicationConfig.getOptionAsFile(SumarisConfigurationOption.DB_DIRECTORY.getKey());
368     }
369 
370     /**
371      * <p>setDbDirectory.</p>
372      *
373      * @param dbDirectory a {@link File} object.
374      */
375     public void setDbDirectory(File dbDirectory) {
376         applicationConfig.setOption(SumarisConfigurationOption.DB_DIRECTORY.getKey(), dbDirectory.getPath());
377     }
378 
379     /**
380      * <p>setJdbcUrl.</p>
381      *
382      * @param jdbcUrl a {@link String} object.
383      */
384     public void setJdbcUrl(String jdbcUrl) {
385         applicationConfig.setOption(SumarisConfigurationOption.JDBC_URL.getKey(), jdbcUrl);
386     }
387 
388     /**
389      * <p>getDbTimezone.</p>
390      *
391      * @return a {@link TimeZone} object.
392      */
393     public TimeZone getDbTimezone() {
394         String tz = applicationConfig.getOption(SumarisConfigurationOption.DB_TIMEZONE.getKey());
395         return StringUtils.isNotBlank(tz) ? TimeZone.getTimeZone(tz) : TimeZone.getDefault();
396     }
397 
398     /**
399      * <p>getDbAttachmentDirectory.</p>
400      *
401      * @return a {@link File} object.
402      */
403     public File getDbAttachmentDirectory() {
404         return applicationConfig.getOptionAsFile(SumarisConfigurationOption.DB_ATTACHMENT_DIRECTORY.getKey());
405     }
406 
407 
408     /**
409      * <p>getDbBackupDirectory.</p>
410      *
411      * @return a {@link File} object.
412      */
413     public File getDbBackupDirectory() {
414         return applicationConfig.getOptionAsFile(SumarisConfigurationOption.DB_BACKUP_DIRECTORY.getKey());
415     }
416 
417     /**
418      * <p>useLiquibaseAutoRun.</p>
419      *
420      * @return a boolean.
421      */
422     public boolean useLiquibaseAutoRun() {
423         return applicationConfig.getOptionAsBoolean(SumarisConfigurationOption.LIQUIBASE_RUN_AUTO.getKey());
424     }
425 
426     /**
427      * <p>useCompactAfterLiquibase.</p>
428      *
429      * @return a boolean.
430      */
431     public boolean useLiquibaseCompact() {
432         return applicationConfig.getOptionAsBoolean(SumarisConfigurationOption.LIQUIBASE_RUN_COMPACT.getKey());
433     }
434 
435     /**
436      * <p>getLiquibaseChangeLogPath.</p>
437      *
438      * @return a {@link String} object.
439      */
440     public String getLiquibaseChangeLogPath() {
441         return applicationConfig.getOption(SumarisConfigurationOption.LIQUIBASE_CHANGE_LOG_PATH.getKey());
442     }
443 
444     /**
445      * <p>getDbCreateScriptPath.</p>
446      *
447      * @return a {@link String} object.
448      */
449     public String getDbCreateScriptPath() {
450         return applicationConfig.getOption(SumarisConfigurationOption.DB_CREATE_SCRIPT_PATH.getKey());
451     }
452 
453     /**
454      * <p>getHibernateDialect.</p>
455      *
456      * @return a {@link String} object.
457      */
458     public String getHibernateDialect() {
459         return applicationConfig.getOption(SumarisConfigurationOption.HIBERNATE_DIALECT.getKey());
460     }
461 
462     /**
463      * <p>getHibernateClientQueriesFile.</p>
464      *
465      * @return a {@link String} object.
466      */
467     public String getHibernateEntitiesPackage() {
468         return applicationConfig.getOption(SumarisConfigurationOption.HIBERNATE_ENTITIES_PACKAGE.getKey());
469     }
470 
471     /**
472      * <p>getDatasourceJndiName.</p>
473      *
474      * @return a {@link String} object.
475      */
476     public String getDatasourceJndiName() {
477         return applicationConfig.getOption(SumarisConfigurationOption.DATASOURCE_JNDI_NAME.getKey());
478     }
479 
480     /**
481      * <p>getJdbcDriver.</p>
482      *
483      * @return a {@link String} object.
484      */
485     public String getJdbcDriver() {
486         return applicationConfig.getOption(SumarisConfigurationOption.JDBC_DRIVER.getKey());
487     }
488 
489     /**
490      * <p>getJdbcURL.</p>
491      *
492      * @return a {@link String} object.
493      */
494     public String getJdbcURL() {
495         return applicationConfig.getOption(SumarisConfigurationOption.JDBC_URL.getKey());
496     }
497 
498     /**
499      * <p>getJdbcCatalog.</p>
500      *
501      * @return a {@link String} object.
502      */
503     public String getJdbcCatalog() {
504         return applicationConfig.getOption(SumarisConfigurationOption.JDBC_CATALOG.getKey());
505     }
506 
507     /**
508      * <p>getJdbcSchema.</p>
509      *
510      * @return a {@link String} object.
511      */
512     public String getJdbcSchema() {
513         return applicationConfig.getOption(SumarisConfigurationOption.JDBC_SCHEMA.getKey());
514     }
515 
516     /**
517      * <p>debugEntityLoad.</p>
518      *
519      * @return a boolean.
520      */
521     public boolean debugEntityLoad() {
522         return applicationConfig.getOptionAsBoolean(SumarisConfigurationOption.DEBUG_ENTITY_LOAD.getKey());
523     }
524 
525     /**
526      * <p>getDbName.</p>
527      *
528      * @return a {@link String} object.
529      */
530     public String getDbName() {
531         return applicationConfig.getOption(SumarisConfigurationOption.DB_NAME.getKey());
532     }
533 
534     /**
535      * <p>getDbValidationQuery.</p>
536      *
537      * @return a {@link String} object.
538      */
539     public String getDbValidationQuery() {
540         return applicationConfig.getOption(SumarisConfigurationOption.DB_VALIDATION_QUERY.getKey());
541     }
542 
543     /**
544      * <p>getJdbcUsername.</p>
545      *
546      * @return a {@link String} object.
547      */
548     public String getJdbcUsername() {
549         return applicationConfig.getOption(SumarisConfigurationOption.JDBC_USERNAME.getKey());
550     }
551 
552     /**
553      * <p>getJdbcPassword.</p>
554      *
555      * @return a {@link String} object.
556      */
557     public String getJdbcPassword() {
558         return applicationConfig.getOption(SumarisConfigurationOption.JDBC_PASSWORD.getKey());
559     }
560 
561     /**
562      * <p>getJdbcBatchSize.</p>
563      *
564      * @return a int.
565      */
566     public int getJdbcBatchSize() {
567         return applicationConfig.getOptionAsInt(SumarisConfigurationOption.JDBC_BATCH_SIZE.getKey());
568     }
569 
570     /**
571      * <p>getHibernateSecondLevelCache.</p>
572      *
573      * @return a boolean.
574      */
575     public boolean useHibernateSecondLevelCache() {
576         return applicationConfig.getOptionAsBoolean(SumarisConfigurationOption.HIBERNATE_SECOND_LEVEL_CACHE.getKey());
577     }
578 
579     /**
580      * <p>useHibernateSqlComment.</p>
581      *
582      * @return a boolean.
583      */
584     public boolean getFormatSql() {
585         return applicationConfig.getOptionAsBoolean(SumarisConfigurationOption.HIBERNATE_FORMAT_SQL.getKey());
586     }
587 
588     /**
589      * <p>useHibernateSqlComment.</p>
590      *
591      * @return a boolean.
592      */
593     public boolean useHibernateSqlComment() {
594         return applicationConfig.getOptionAsBoolean(SumarisConfigurationOption.HIBERNATE_USE_SQL_COMMENT.getKey());
595     }
596 
597     /**
598      * <p>getStatusIdTemporary.</p>
599      *
600      * @return a {@link int} object.
601      */
602     public int getStatusIdTemporary() {
603         return applicationConfig.getOptionAsInt(SumarisConfigurationOption.STATUS_ID_TEMPORARY.getKey());
604     }
605 
606     /**
607      * <p>getStatusIdValid.</p>
608      *
609      * @return a {@link int}.
610      */
611     public int getStatusIdValid() {
612         return applicationConfig.getOptionAsInt(SumarisConfigurationOption.STATUS_ID_ENABLE.getKey());
613     }
614 
615     /**
616      * <p>getUnitIdNone.</p>
617      *
618      * @return a {@link int}.
619      */
620     public int getUnitIdNone() {
621         return applicationConfig.getOptionAsInt(SumarisConfigurationOption.UNIT_ID_NONE.getKey());
622     }
623 
624     /**
625      * <p>getMatrixIdIndividual.</p>
626      *
627      * @return a {@link int}.
628      */
629     public int getMatrixIdIndividual() {
630         return applicationConfig.getOptionAsInt(SumarisConfigurationOption.MATRIX_ID_INDIVIDUAL.getKey());
631     }
632 
633     /**
634      * <p>getAppName.</p>
635      *
636      * @return a {@link String} object: the application id.
637      */
638     public String getAppName() {
639         return applicationConfig.getOption(SumarisConfigurationOption.APP_NAME.getKey());
640     }
641 
642     /**
643      * <p>getVersion.</p>
644      *
645      * @return a {@link Version} object.
646      */
647     public Version getVersion() {
648         return applicationConfig.getOptionAsVersion(SumarisConfigurationOption.VERSION.getKey());
649     }
650 
651     /**
652      * <p>getI18nDirectory.</p>
653      *
654      * @return a {@link File} object.
655      */
656     public File getI18nDirectory() {
657         return applicationConfig.getOptionAsFile(
658                 SumarisConfigurationOption.I18N_DIRECTORY.getKey());
659     }
660 
661     /**
662      * <p>getI18nLocale.</p>
663      *
664      * @return a {@link Locale} object.
665      */
666     public Locale getI18nLocale() {
667         return applicationConfig.getOptionAsLocale(
668                 SumarisConfigurationOption.I18N_LOCALE.getKey());
669     }
670 
671     /**
672      * <p>setI18nLocale.</p>
673      *
674      * @param locale a {@link Locale} object.
675      */
676     public void setI18nLocale(Locale locale) {
677         applicationConfig.setOption(SumarisConfigurationOption.I18N_LOCALE.getKey(), locale.toString());
678     }
679 
680     /**
681      * <p>getConnectionProperties.</p>
682      *
683      * @return a {@link Properties} object.
684      */
685     public Properties getConnectionProperties() {
686         return Daos.getConnectionProperties(
687                 getJdbcURL(),
688                 getJdbcUsername(),
689                 getJdbcPassword(),
690                 null,
691                 getHibernateDialect(),
692                 getJdbcDriver());
693     }
694 
695     /**
696      * <p>getLiquibaseDiffTypes.</p>
697      *
698      * @return a {@link String} object.
699      */
700     public String getLiquibaseDiffTypes() {
701         return applicationConfig.getOption(SumarisConfigurationOption.LIQUIBASE_DIFF_TYPES.getKey());
702     }
703 
704     /**
705      * <p>getLiquibaseOutputFile.</p>
706      *
707      * @return a {@link File} object.
708      */
709     public File getLiquibaseOutputFile() {
710         return applicationConfig.getOptionAsFile(SumarisConfigurationOption.LIQUIBASE_OUTPUT_FILE.getKey());
711     }
712 
713     /**
714      * <p>isForceLiquibaseOutputFile.</p>
715      *
716      * @return a boolean.
717      */
718     public boolean isForceLiquibaseOutputFile() {
719         return applicationConfig.getOptionAsBoolean(SumarisConfigurationOption.LIQUIBASE_FORCE_OUTPUT_FILE.getKey());
720     }
721 
722 
723     /**
724      * <p>getLaunchMode.</p>
725      *
726      * @return a {@link java.lang.String} object.
727      */
728     public String getLaunchMode() {
729         return applicationConfig.getOption(SumarisConfigurationOption.LAUNCH_MODE.getKey());
730     }
731 
732     /**
733      * <p>isProduction.</p>
734      *
735      * @return true if production mode.
736      */
737     public boolean isProduction() {
738        return LaunchModeEnum.production.name().equalsIgnoreCase(getLaunchMode());
739     }
740 
741     public int getDefaultQualityFlagId() {
742         return applicationConfig.getOptionAsInt(SumarisConfigurationOption.DEFAULT_QUALITY_FLAG.getKey());
743     }
744 
745     public int getSequenceIncrementValue() {
746         return applicationConfig.getOptionAsInt(SumarisConfigurationOption.SEQUENCE_INCREMENT.getKey());
747     }
748 
749     public int getSequenceStartWithValue() {
750         return applicationConfig.getOptionAsInt(SumarisConfigurationOption.SEQUENCE_START_WITH.getKey());
751     }
752 
753     public String getSequenceSuffix() {
754         return applicationConfig.getOption(SumarisConfigurationOption.SEQUENCE_SUFFIX.getKey());
755     }
756 
757     public String getCsvSeparator() {
758         return applicationConfig.getOption(SumarisConfigurationOption.CSV_SEPARATOR.getKey());
759     }
760 
761     public boolean isInitStatisticalRectangles() {
762         return applicationConfig.getOptionAsBoolean(SumarisConfigurationOption.INIT_STATISTICAL_RECTANGLES.getKey());
763     }
764 
765     public boolean isPreserveHistoricalMeasurements() {
766         return applicationConfig.getOptionAsBoolean(SumarisConfigurationOption.PRESERVE_HISTORICAL_MEASUREMENTS.getKey());
767     }
768 
769     /* -- protected methods -- */
770 
771     /**
772      * <p>getOptionAsURL.</p>
773      *
774      * @param key a {@link String} object.
775      * @return a {@link URL} object.
776      */
777     protected URL getOptionAsURL(String key) {
778         String urlString = applicationConfig.getOption(key);
779 
780         // Could be empty (e.g. demo deployment)
781         if (StringUtils.isBlank(urlString)) {
782             return null;
783         }
784 
785         // correct end of the url string
786         if (!urlString.endsWith("/")) {
787             int schemeIndex = urlString.indexOf("://");
788             int firstSlashIndex = urlString.indexOf('/', schemeIndex + 3);
789             boolean addSlash = false;
790             if (firstSlashIndex == -1) {
791                 addSlash = true;
792             }
793             else {
794                 int lastSlashIndex = urlString.lastIndexOf('/');
795                 if (lastSlashIndex > firstSlashIndex) {
796                     addSlash = urlString.indexOf('.', lastSlashIndex) == -1;
797                 }
798             }
799 
800             if (addSlash) {
801                 urlString += '/';
802             }
803         }
804 
805         URL url = null;
806         try {
807             url = new URL(urlString);
808         } catch (MalformedURLException ex) {
809             log.error(ex.getLocalizedMessage(), ex);
810         }
811 
812         return url;
813     }
814 
815     public String getColumnDefaultValue(String tableName, String columnName) {
816         Preconditions.checkArgument(StringUtils.isNotBlank(tableName));
817         Preconditions.checkArgument(StringUtils.isNotBlank(columnName));
818 
819         String defaultvalue = applicationConfig.getOption("sumaris." + tableName.toUpperCase() + "." + columnName.toUpperCase() + ".defaultValue");
820         return defaultvalue;
821     }
822 }