View Javadoc
1   package fr.ifremer.quadrige3.core.action;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 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 Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  
28  import fr.ifremer.quadrige3.core.config.QuadrigeConfiguration;
29  import fr.ifremer.quadrige3.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.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.nuiton.config.ApplicationConfigHelper;
36  import org.nuiton.config.ApplicationConfigProvider;
37  import org.nuiton.config.ConfigActionDef;
38  import org.nuiton.i18n.I18n;
39  
40  import java.io.File;
41  import java.io.IOException;
42  import java.util.Set;
43  
44  /**
45   * <p>ActionUtils class.</p>
46   */
47  public class ActionUtils {
48      /* Logger */
49      private static final Log log = LogFactory.getLog(ActionUtils.class);
50  
51      /**
52       * <p>Constructor for ActionUtils.</p>
53       */
54      protected ActionUtils() {
55          // helper class
56      }
57  
58      /**
59       * <p>logConnectionProperties.</p>
60       */
61      public static void logConnectionProperties() {
62          if (!log.isInfoEnabled()) {
63              return;
64          }
65          QuadrigeConfiguration config = QuadrigeConfiguration.getInstance();
66          boolean isFileDatabase = Daos.isHsqlFileDatabase(config.getJdbcURL());
67          if (isFileDatabase) {
68              log.info(String.format(" Database directory: %s", config.getDbDirectory()));
69          }
70          log.info(String.format(" JDBC Driver: %s", config.getJdbcDriver()));
71          log.info(String.format(" JDBC URL: %s", config.getJdbcURL()));
72          log.info(String.format(" JDBC Username: %s", config.getJdbcUsername()));
73          String jdbcCatalog = config.getJdbcCatalog();
74          if (StringUtils.isNotBlank(jdbcCatalog)) {
75              log.info(String.format(" JDBC Catalog: %s", jdbcCatalog));
76          }
77          String jdbcSchema = config.getJdbcSchema();
78          if (StringUtils.isNotBlank(jdbcSchema)) {
79              log.info(String.format(" JDBC Schema: %s", jdbcSchema));
80          }
81      }
82  
83      /**
84       * Check output file or directory validity
85       *
86       * @param isDirectory a boolean.
87       * @param actionClass a {@link java.lang.Class} object.
88       * @return a {@link java.io.File} object.
89       */
90      public static File checkAndGetOutputFile(boolean isDirectory, Class<?> actionClass) {
91          QuadrigeConfiguration config = QuadrigeConfiguration.getInstance();
92          
93          File output = config.getLiquibaseOutputFile();
94          if (output == null) {
95              log.error(I18n.t("quadrige3.action.noOutput.error", "--output [...]", getActionAlias(actionClass)));
96              System.exit(-1);
97          }
98          
99          if (output.exists()) {
100             if (isDirectory) {
101                 if (!output.isDirectory()) {
102                     log.error(I18n.t("quadrige3.action.outputNotADirectory.error", output.getPath()));
103                     System.exit(-1);
104                 }
105                 else if (ArrayUtils.isNotEmpty(output.listFiles())) {
106                     // Could be force, so delete the directory
107                     if (config.isForceLiquibaseOutputFile()) {
108                         log.info(I18n.t("quadrige3.action.deleteOutputDirectory", output.getPath()));
109                         try {
110                             FileUtils.deleteDirectory(output);
111                         } catch (IOException e) {
112                             log.error(e.getMessage());
113                             System.exit(-1);
114                         }
115                     }
116                     else {
117                         log.error(I18n.t("quadrige3.action.outputNotEmptyDirectory.error", output.getPath()));
118                         System.exit(-1);
119                     }
120                 }
121             }
122             else {
123                 log.error(I18n.t("quadrige3.action.outputNotAFile.error", output.getPath()));
124                 System.exit(-1);
125             }
126         }
127         
128         return output;
129     }
130     
131     /**
132      * <p>getActionAlias.</p>
133      *
134      * @param clazz a {@link java.lang.Class} object.
135      * @return a {@link java.lang.String} object.
136      */
137     public static String getActionAlias(Class<?> clazz) {
138         ConfigActionDef actionDefFound = null;
139         
140         // Retrieve the configActionDef for the given class
141         Set<ApplicationConfigProvider> providers =
142                 ApplicationConfigHelper.getProviders(null,
143                         null,
144                         null,
145                         true);
146         String classname = clazz.getName();
147         for(ApplicationConfigProvider provider: providers) {
148             ConfigActionDef[] actionDefs = provider.getActions();
149             if (ArrayUtils.isNotEmpty(actionDefs)) {
150                 for (ConfigActionDef actionDef:actionDefs) {
151                     if (actionDef.getAction() != null
152                             && actionDef.getAction().startsWith(classname)) {
153                         actionDefFound = actionDef;
154                         break;
155                     }
156                 }
157                 if (actionDefFound != null) {
158                     break;
159                 }
160             }
161         }
162         
163         // If a config action def exists, return the first alias
164         if (actionDefFound != null) {
165             String[] alias = actionDefFound.getAliases();
166             if (ArrayUtils.isNotEmpty(alias)) {
167                 return alias[0];
168             }
169         }
170         return I18n.t("quadrige3.action.current");
171     }
172 }