View Javadoc
1   package net.sumaris.core.action;
2   
3   /*-
4    * #%L
5    * Sumaris3 Core :: Sumaris3 Core Shared
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  
28  import net.sumaris.core.config.SumarisConfiguration;
29  import net.sumaris.core.dao.technical.Daos;
30  import org.apache.commons.io.FileUtils;
31  import org.apache.commons.lang3.ArrayUtils;
32  import org.apache.commons.lang3.StringUtils;
33  import org.nuiton.config.ApplicationConfigHelper;
34  import org.nuiton.config.ApplicationConfigProvider;
35  import org.nuiton.config.ConfigActionDef;
36  import org.nuiton.i18n.I18n;
37  import org.slf4j.LoggerFactory;
38  
39  import java.io.File;
40  import java.io.IOException;
41  import java.util.Set;
42  
43  /**
44   * <p>ActionUtils class.</p>
45   */
46  public class ActionUtils {
47      /* Logger */
48      private static final org.slf4j.Logger log = LoggerFactory.getLogger(ActionUtils.class);
49  
50      /**
51       * <p>Constructor for ActionUtils.</p>
52       */
53      protected ActionUtils() {
54          // helper class
55      }
56  
57      /**
58       * <p>logConnectionProperties.</p>
59       */
60      public static void logConnectionProperties() {
61          if (!log.isInfoEnabled()) {
62              return;
63          }
64          SumarisConfiguration config = SumarisConfiguration.getInstance();
65          boolean isFileDatabase = Daos.isFileDatabase(config.getJdbcURL());
66          if (isFileDatabase) {
67              log.info(String.format(" Database directory: %s", config.getDbDirectory()));
68          }
69          log.info(String.format(" JDBC Driver: %s", config.getJdbcDriver()));
70          log.info(String.format(" JDBC URL: %s", config.getJdbcURL()));
71          log.info(String.format(" JDBC Username: %s", config.getJdbcUsername()));
72          String jdbcCatalog = config.getJdbcCatalog();
73          if (StringUtils.isNotBlank(jdbcCatalog)) {
74              log.info(String.format(" JDBC Catalog: %s", jdbcCatalog));
75          }
76          String jdbcSchema = config.getJdbcSchema();
77          if (StringUtils.isNotBlank(jdbcSchema)) {
78              log.info(String.format(" JDBC Schema: %s", jdbcSchema));
79          }
80      }
81  
82      /**
83       * Check output file or directory validity
84       *
85       * @param isDirectory a boolean.
86       * @param actionClass a {@link Class} object.
87       * @return a {@link File} object.
88       */
89      public static File checkAndGetOutputFile(boolean isDirectory, Class<?> actionClass) {
90          SumarisConfiguration config = SumarisConfiguration.getInstance();
91          
92          File output = config.getLiquibaseOutputFile();
93          if (output == null) {
94              log.error(I18n.t("sumaris.action.noOutput.error", "--output [...]", getActionAlias(actionClass)));
95              System.exit(-1);
96          }
97          
98          if (output.exists()) {
99              if (isDirectory) {
100                 if (!output.isDirectory()) {
101                     log.error(I18n.t("sumaris.action.outputNotADirectory.error", output.getPath()));
102                     System.exit(-1);
103                 }
104                 else if (ArrayUtils.isNotEmpty(output.listFiles())) {
105                     // Could be force, so delete the directory
106                     if (config.isForceLiquibaseOutputFile() || !config.isProduction()) {
107                         log.info(I18n.t("sumaris.action.deleteOutputDirectory", output.getPath()));
108                         try {
109                             FileUtils.deleteDirectory(output);
110                         } catch (IOException e) {
111                             log.error(e.getMessage());
112                             System.exit(-1);
113                         }
114                     }
115                     else {
116                         log.error(I18n.t("sumaris.action.outputNotEmptyDirectory.error", output.getPath()));
117                         System.exit(-1);
118                     }
119                 }
120             }
121             else {
122                 // Could be force, so delete the directory
123                 if (config.isForceLiquibaseOutputFile() || !config.isProduction()) {
124                     log.info(I18n.t("sumaris.action.deleteOutputFile", output.getPath()));
125                     try {
126                         FileUtils.forceDelete(output);
127                     } catch (IOException e) {
128                         log.error(e.getMessage());
129                         System.exit(-1);
130                     }
131                 }
132                 else {
133                     log.error(I18n.t("sumaris.action.outputNotAFile.error", output.getPath()));
134                     System.exit(-1);
135                 }
136             }
137         }
138         
139         return output;
140     }
141     
142     /**
143      * <p>getActionAlias.</p>
144      *
145      * @param clazz a {@link Class} object.
146      * @return a {@link String} object.
147      */
148     public static String getActionAlias(Class<?> clazz) {
149         ConfigActionDef actionDefFound = null;
150         
151         // Retrieve the configActionDef for the given class
152         Set<ApplicationConfigProvider> providers =
153                 ApplicationConfigHelper.getProviders(null,
154                         null,
155                         null,
156                         true);
157         String classname = clazz.getName();
158         for(ApplicationConfigProvider provider: providers) {
159             ConfigActionDef[] actionDefs = provider.getActions();
160             if (ArrayUtils.isNotEmpty(actionDefs)) {
161                 for (ConfigActionDef actionDef:actionDefs) {
162                     if (actionDef.getAction() != null
163                             && actionDef.getAction().startsWith(classname)) {
164                         actionDefFound = actionDef;
165                         break;
166                     }
167                 }
168                 if (actionDefFound != null) {
169                     break;
170                 }
171             }
172         }
173         
174         // If a config action def exists, return the first alias
175         if (actionDefFound != null) {
176             String[] alias = actionDefFound.getAliases();
177             if (ArrayUtils.isNotEmpty(alias)) {
178                 return alias[0];
179             }
180         }
181         return I18n.t("sumaris.action.current");
182     }
183 
184     public static boolean checkValidConnection() {
185         SumarisConfiguration config = SumarisConfiguration.getInstance();
186 
187         ActionUtils.logConnectionProperties();
188 
189         boolean isValidConnection = Daos.isValidConnectionProperties(config.getJdbcDriver(),
190                 config.getJdbcURL(),
191                 config.getJdbcUsername(),
192                 config.getJdbcPassword());
193 
194         if (!isValidConnection) {
195             log.warn("Connection error: invalid connection.");
196             return false;
197         }
198 
199         return true;
200     }
201 }