View Javadoc
1   package net.sumaris.server.config;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Sumaris server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
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  import net.sumaris.core.config.SumarisConfiguration;
28  import net.sumaris.core.config.SumarisConfigurationOption;
29  import net.sumaris.core.exception.SumarisTechnicalException;
30  import org.apache.commons.io.FileUtils;
31  import org.apache.commons.lang3.StringUtils;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.nuiton.config.ApplicationConfig;
35  import org.nuiton.i18n.I18n;
36  import org.nuiton.i18n.init.DefaultI18nInitializer;
37  import org.nuiton.i18n.init.UserI18nInitializer;
38  
39  import javax.naming.InitialContext;
40  import javax.naming.NamingException;
41  import java.io.File;
42  import java.io.IOException;
43  import java.util.Locale;
44  import java.util.TimeZone;
45  
46  /**
47   * <p>SumarisServerConfiguration class.</p>
48   *
49   */
50  public class SumarisServerConfiguration extends SumarisConfiguration {
51  
52      public static final String CONFIG_FILE_NAME = "application.properties";
53  
54      private static final String CONFIG_FILE_ENV_PROPERTY = "spring.config.location";
55  
56      private static final String CONFIG_FILE_JNDI_NAME = "java:comp/env/" + CONFIG_FILE_NAME;
57  
58  
59      /* Logger */
60      private static final Logger log = LoggerFactory.getLogger(SumarisServerConfiguration.class);
61  
62      private static SumarisServerConfiguration instance;
63  
64      /**
65       * <p>initDefault.</p>
66       */
67      public static void initDefault() {
68          instance = new SumarisServerConfiguration(getWebConfigFile(), args);
69          setInstance(instance);
70      }
71  
72      /**
73       * <p>Getter for the field <code>instance</code>.</p>
74       *
75       * @return a {@link net.sumaris.server.config.SumarisServerConfiguration} object.
76       */
77      public static SumarisServerConfiguration getInstance() {
78          if (instance == null) {
79              initDefault();
80          }
81          return instance;
82      }
83  
84      /**
85       * <p>Constructor for SumarisServerConfiguration.</p>
86       *
87       * @param applicationConfig a {@link org.nuiton.config.ApplicationConfig} object.
88       */
89      public SumarisServerConfiguration(ApplicationConfig applicationConfig) {
90          super(applicationConfig);
91      }
92  
93      /**
94       * <p>Constructor for SumarisServerConfiguration.</p>
95       *
96       * @param file a {@link String} object.
97       * @param args a {@link String} object.
98       */
99      public SumarisServerConfiguration(String file, String... args) {
100         super(file, args);
101 
102         // Init i18n
103         try {
104             initI18n();
105         } catch (IOException e) {
106             throw new SumarisTechnicalException("i18n initialization failed", e);
107         }
108 
109         // Init directories
110         try {
111             initDirectories();
112         } catch (IOException e) {
113             throw new SumarisTechnicalException("Directories initialization failed", e);
114         }
115 
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     protected void overrideExternalModulesDefaultOptions(ApplicationConfig applicationConfig) {
121         super.overrideExternalModulesDefaultOptions(applicationConfig);
122     }
123 
124 
125     /**
126      * <p>getVersionAsString.</p>
127      *
128      * @return a {@link String} object.
129      */
130     public String getVersionAsString() {
131         return applicationConfig.getOption(SumarisConfigurationOption.VERSION.getKey());
132     }
133 
134     /**
135      * <p>getServerPort.</p>
136      *
137      * @return a {@link Integer} object.
138      */
139     public Integer getServerPort() {
140         return applicationConfig.getOptionAsInt(SumarisServerConfigurationOption.SERVER_PORT.getKey());
141     }
142 
143     /**
144      * <p>getServerAddress.</p>
145      *
146      * @return a {@link String} object.
147      */
148     public String getServerUrl() {
149         return applicationConfig.getOption(SumarisServerConfigurationOption.SERVER_URL.getKey());
150     }
151 
152     /**
153      * <p>getDownloadDirectory</p>
154      * @return a {@link File} object.
155      */
156     public File getDownloadDirectory() {
157         return applicationConfig.getOptionAsFile(SumarisServerConfigurationOption.DOWNLOAD_DIRECTORY.getKey());
158     }
159 
160     /**
161      * <p>getServerAddress.</p>
162      *
163      * @return a {@link String} object.
164      */
165     public String getRegistrationConfirmUrlPattern() {
166         return applicationConfig.getOption(SumarisServerConfigurationOption.REGISTRATION_CONFIRM_URL.getKey());
167     }
168 
169     /**
170      * <p>getAdminMail.</p>
171      *
172      * @return a {@link String} object, the admin email.
173      */
174     public String getAdminMail() {
175         return applicationConfig.getOption(SumarisServerConfigurationOption.ADMIN_MAIL.getKey());
176     }
177 
178     /**
179      * <p>get mail from address.</p>
180      *
181      * @return a {@link String} object, the 'from' address to use for mail .
182      */
183     public String getMailFrom() {
184         return applicationConfig.getOption(SumarisServerConfigurationOption.MAIL_FROM.getKey());
185     }
186 
187     /**
188      * <p>get keypair salt.</p>
189      *
190      * @return a {@link String} object, the 'salt' for the server keypair generation.
191      */
192     public String getKeypairSalt() {
193         return applicationConfig.getOption(SumarisServerConfigurationOption.KEYPAIR_SALT.getKey());
194     }
195 
196     /**
197      * <p>get keypair password.</p>
198      *
199      * @return a {@link String} object, the 'password' for the server keypair generation.
200      */
201     public String getKeypairPassword() {
202         return applicationConfig.getOption(SumarisServerConfigurationOption.KEYPAIR_PASSWORD.getKey());
203     }
204 
205     /**
206      * <p>get auth challenge life time (in seconds).</p>
207      *
208      * @return a {@link Integer}
209      */
210     public int getAuthChallengeLifeTime() {
211         return applicationConfig.getOptionAsInt(SumarisServerConfigurationOption.AUTH_CHALLENGE_LIFE_TIME.getKey());
212     }
213 
214     /**
215      * <p>get auth session duration (in seconds).</p>
216      *
217      * @return a {@link Integer}
218      */
219     public int getAuthTokenLifeTimeInSeconds() {
220         return applicationConfig.getOptionAsInt(SumarisServerConfigurationOption.AUTH_TOKEN_LIFE_TIME.getKey());
221     }
222 
223     /* -- Internal methods -- */
224 
225     /**
226      * <p>getWebConfigFile.</p>
227      *
228      * @return a {@link String} object.
229      */
230     protected static String getWebConfigFile() {
231         // Could override config file id (useful for dev)
232         String configFile = CONFIG_FILE_NAME;
233         if (System.getProperty(CONFIG_FILE_ENV_PROPERTY) != null) {
234             configFile = System.getProperty(CONFIG_FILE_ENV_PROPERTY);
235             configFile = configFile.replaceAll("\\\\", "/");
236         }
237         else {
238             try {
239                 InitialContext ic = new InitialContext();
240                 String jndiPathToConfFile = (String) ic.lookup(CONFIG_FILE_JNDI_NAME);
241                 if (StringUtils.isNotBlank(jndiPathToConfFile)) {
242                     configFile = jndiPathToConfFile;
243                 }
244             } catch (NamingException e) {
245                 log.debug(String.format("Error while reading JNDI initial context. Skip configuration path override, from context [%s]", CONFIG_FILE_JNDI_NAME));
246             }
247         }
248 
249         return configFile;
250     }
251 
252     /**
253      * <p>initI18n.</p>
254      *
255      * @throws IOException if any.
256      */
257     protected void initI18n() throws IOException {
258 
259         // --------------------------------------------------------------------//
260         // initConfig i18n
261         // --------------------------------------------------------------------//
262         File i18nDirectory = new File(getDataDirectory(), "i18n");
263         if (i18nDirectory.exists()) {
264             // clean i18n cache
265             FileUtils.cleanDirectory(i18nDirectory);
266         }
267 
268         FileUtils.forceMkdir(i18nDirectory);
269 
270         if (log.isDebugEnabled()) {
271             log.debug("I18N directory: " + i18nDirectory);
272         }
273 
274         Locale i18nLocale = getI18nLocale();
275 
276         I18n.init(new UserI18nInitializer(
277             i18nDirectory, new DefaultI18nInitializer(getI18nBundleName())),
278             i18nLocale);
279         if (log.isInfoEnabled()) {
280             log.info(I18n.t("sumaris.server.init.i18n",
281                     i18nLocale, i18nDirectory));
282         }
283     }
284 
285     /**
286      * <p>initDirectories.</p>
287      *
288      * @throws IOException if any.
289      */
290     protected void initDirectories() throws IOException {
291 
292         // log the data directory used
293         log.info(I18n.t("sumaris.server.init.data.directory", getDataDirectory()));
294 
295         // Data directory
296         FileUtils.forceMkdir(getDataDirectory());
297 
298         // DB attachment directory
299         FileUtils.forceMkdir(getDbAttachmentDirectory());
300 
301         // DB backup directory
302         FileUtils.forceMkdir(getDbBackupDirectory());
303 
304         // Download directory
305         FileUtils.forceMkdir(getDownloadDirectory());
306 
307         // temp directory
308         File tempDirectory = getTempDirectory();
309         if (tempDirectory.exists()) {
310             // clean temp files
311             FileUtils.cleanDirectory(tempDirectory);
312         }
313 
314 
315     }
316 
317     /**
318      * <p>getI18nBundleName.</p>
319      *
320      * @return a {@link String} object.
321      */
322     protected static String getI18nBundleName() {
323         return "sumaris-core-server-i18n";
324     }
325 
326     /**
327      * Initialization default timezone, from configuration (mantis #34754)
328      */
329     @Override
330     protected void initTimeZone() {
331 
332         String timeZone = applicationConfig.getOption(SumarisConfigurationOption.TIMEZONE.getKey());
333         if (StringUtils.isNotBlank(timeZone)) {
334             log.info(String.format("Using timezone [%s]", timeZone));
335             TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
336             System.setProperty("user.timezone", timeZone);
337         } else {
338             log.info(String.format("Using default timezone [%s]", System.getProperty("user.timezone")));
339         }
340 
341         String dbTimeZone = applicationConfig.getOption(SumarisConfigurationOption.DB_TIMEZONE.getKey());
342         if (StringUtils.isNotBlank(dbTimeZone)) {
343             log.info(String.format("Using timezone [%s] for database", dbTimeZone));
344         } else {
345             log.info(String.format("Using default timezone [%s] for database", System.getProperty("user.timezone")));
346         }
347     }
348 
349 }