001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.git;
029
030import org.opencms.main.CmsSystemInfo;
031import org.opencms.main.OpenCms;
032
033import java.io.File;
034import java.io.FileInputStream;
035import java.io.IOException;
036import java.util.ArrayList;
037import java.util.Arrays;
038import java.util.Collection;
039import java.util.Map.Entry;
040import java.util.Properties;
041
042/** Access to a single git configuration file. */
043public class CmsGitConfiguration {
044
045    /** The system module export path. */
046    private static final String SYSTEM_MODULE_EXPORTPATH = OpenCms.getSystemInfo().getPackagesRfsPath()
047        + CmsSystemInfo.FOLDER_MODULES;
048
049    /** The variable under which the default commit message is set. */
050    private static final String DEFAULT_COMMIT_MESSAGE = "COMMIT_MESSAGE";
051    /** The variable under which the default commit mode is set. */
052    private static final String DEFAULT_COMMIT_MODE = "GIT_COMMIT";
053    /** The variable under which the default copy and unzip behavior is set. */
054    private static final String DEFAULT_COPY_AND_UNZIP = "COPY_AND_UNZIP";
055    /** The variable under which the default exclude libs option is set. */
056    private static final String DEFAULT_EXCLUDE_LIBS = "DEFAULT_EXCLUDE_LIBS";
057    /** The variable under which the export mode is set. */
058    private static final String DEFAULT_EXPORT_MODE = "MODULE_EXPORT_MODE";
059    /** The variable under which the default git user email is set. */
060    private static final String DEFAULT_GIT_USER_EMAIL = "GIT_USER_EMAIL";
061    /** The variable under which the default git user name is set. */
062    private static final String DEFAULT_GIT_USER_NAME = "GIT_USER_NAME";
063    /** The variable under which the default commit mode is set. */
064    private static final String DEFAULT_IGNORE_UNCLEAN = "GIT_IGNORE_UNCLEAN";
065    /** The variable under wich the default module export path is set. */
066    private static final String DEFAULT_MODULE_EXPORT_PATH = "MODULE_EXPORT_FOLDER";
067    /** The variable under which the default modules are listed in the configuration file. */
068    private static final String DEFAULT_MODULES_TO_EXPORT = "DEFAULT_MODULES_TO_EXPORT";
069    /** The variable under which the default pull mode is set for pulling after the commit. */
070    private static final String DEFAULT_PULL_MODE_AFTER = "GIT_PULL_AFTER";
071    /** The variable under which the default pull mode is set for pulling before any other action. */
072    private static final String DEFAULT_PULL_MODE_BEFORE = "GIT_PULL_BEFORE";
073    /** The variable under which the default push mode is set. */
074    private static final String DEFAULT_PUSH_MODE = "GIT_PUSH";
075    /** The variable under which the repository path is set. */
076    private static final String DEFAULT_REPOSITORY_PATH = "REPOSITORY_HOME";
077    /** The variable under which the resources sub-folder for the modules is configured. */
078    private static final String DEFAULT_MODULE_RESOURCES_SUBFOLDER = "MODULE_RESOURCES_SUBFOLDER";
079    /** The variable under which the module path of the repository is configured. */
080    private static final String DEFAULT_MODULE_PATH = "MODULE_PATH";
081
082    /** The commit message as configured. */
083    private String m_defaultCommitMessage = "Autocommit of exported modules.";
084    /** The default commit mode. */
085    private boolean m_defaultCommitMode;
086    /** The default copy and unzip mode. */
087    private boolean m_defaultCopyAndUnzip;
088    /** Flag, indicating if the lib/ folders of the modules should be removed before the commit. */
089    private boolean m_defaultExcludeLibs;
090    /** The export mode as configured - is ignored if the system export folder is chosen. */
091    private int m_defaultExportMode;
092    /** The git user email as configured. */
093    private String m_defaultGitUserEmail;
094    /** The git user name as configured. */
095    private String m_defaultGitUserName;
096    /** Flag, indicating if execution of the script should go on for an unclean repository. */
097    private boolean m_defaultIgnoreUnclean;
098    /** The default module export path as configured. */
099    private String m_defaultModuleExportPath = "";
100    /** The pull mode as configured. */
101    private boolean m_defaultPullModeAfter;
102    /** The pull mode as configured. */
103    private boolean m_defaultPullModeBefore;
104    /** The push mode as configured. */
105    private boolean m_defaultPushMode;
106    /** Collection of the modules to export and check in, as configured in the config file. */
107    private Collection<String> m_configuredModules = new ArrayList<String>();
108    /** The path to the folder containing the modules. */
109    private String m_modulesPath;
110    /** The configured resources sub folder of a module. */
111    private String m_resourcesSubfolder;
112    /** The repository path as configured. */
113    private String m_repositoryPath = "";
114
115    /** Flag, indicating if the config-file is missing. */
116    private boolean m_isValid;
117
118    /** The configuration file. */
119    private File m_configFile;
120
121    /**
122     * Default constructor to wrap a configuration file.
123     * @param configFile the configuration file.
124     */
125    CmsGitConfiguration(final File configFile) {
126        m_configFile = configFile;
127        readConfigFile();
128    }
129
130    /**
131     * Returns the wrapped configuration file.
132     * @return the wrapped configuration file.
133     */
134    public File getConfigurationFile() {
135
136        return m_configFile;
137    }
138
139    /** Returns the modules specified in the config file.
140     * @return the modules specified in the config file.
141     */
142    public Collection<String> getConfiguredModules() {
143
144        return m_configuredModules;
145    }
146
147    /** Returns a flag, indicating if automatic commit is enabled by default.
148     * @return a flag, indicating if automatic commit is enabled by default.
149     */
150    public boolean getDefaultAutoCommit() {
151
152        return m_defaultCommitMode;
153    }
154
155    /** Returns a flag, indicating if automatic pulling after the commit is enabled by default.
156     * @return a flag, indicating if automatic pulling after the commit is enabled by default.
157     */
158    public boolean getDefaultAutoPullAfter() {
159
160        return m_defaultPullModeAfter;
161    }
162
163    /** Returns a flag, indicating if automatic pulling first is enabled by default.
164     * @return a flag, indicating if automatic pulling first is enabled by default.
165     */
166    public boolean getDefaultAutoPullBefore() {
167
168        return m_defaultPullModeBefore;
169    }
170
171    /** Returns a flag, indicating if automatic pushing is enabled by default.
172     * @return a flag, indicating if automatic pushing is enabled by default.
173     */
174    public boolean getDefaultAutoPush() {
175
176        return m_defaultPushMode;
177    }
178
179    /** Returns the default commit message.
180     * @return the default commit message.
181     */
182    public String getDefaultCommitMessage() {
183
184        return m_defaultCommitMessage;
185    }
186
187    /** Returns the default copy-and-unzip flag.
188     * @return the default copy-and-unzip flag.
189     */
190    public boolean getDefaultCopyAndUnzip() {
191
192        return m_defaultCopyAndUnzip;
193    }
194
195    /** Returns the default exclude libs flag.
196     * @return the default exclude libs flag.
197     */
198    public boolean getDefaultExcludeLibs() {
199
200        return m_defaultExcludeLibs;
201    }
202
203    /** Returns the configured default git user email, or <code>null</code> if the email is by default not adjusted at all.
204     * @return the configured default git user email, or <code>null</code> if the email is by default not adjusted at all.
205     */
206    public String getDefaultGitUserEmail() {
207
208        return m_defaultGitUserEmail;
209    }
210
211    /** Returns the configured default git user name, or <code>null</code> if the name is by default not adjusted at all.
212     * @return the configured default git user name, or <code>null</code> if the name is by default not adjusted at all.
213     */
214    public String getDefaultGitUserName() {
215
216        return m_defaultGitUserName;
217    }
218
219    /** Returns the default ignore-unclean flag.
220     * @return the default ignore-unclean flag.
221     */
222    public boolean getDefaultIngoreUnclean() {
223
224        return m_defaultIgnoreUnclean;
225    }
226
227    /** Returns the export mode.
228     * @return the export mode.
229     */
230    public int getExportMode() {
231
232        return m_defaultModuleExportPath.isEmpty() ? 1 : m_defaultExportMode;
233    }
234
235    /**
236     * Returns the path to the config file that is wrapped.
237     * @return the path to the config file that is wrapped.
238     */
239    public String getFilePath() {
240
241        if (null != m_configFile) {
242            return m_configFile.getAbsolutePath();
243        }
244        return null;
245    }
246
247    /** Returns the RFS path where module .zip-files are read before check in.
248     * @return the RFS path where module .zip-files are read before check in.
249     */
250    public String getModuleExportPath() {
251
252        return m_defaultModuleExportPath.isEmpty() ? SYSTEM_MODULE_EXPORTPATH : m_defaultModuleExportPath;
253    }
254
255    /**
256     * Returns the path to where modules are stored in the repository.
257     * @return the path to where modules are stored in the repository.
258     */
259    public String getModulesPath() {
260
261        return m_modulesPath;
262    }
263
264    /** Returns the name of the configuration.
265     * @return the name of the configuration.
266     */
267    public String getName() {
268
269        if ((null != m_configFile) && (null != m_configFile.getName())) {
270            return m_configFile.getName();
271        }
272        return "<invalid configuration>";
273    }
274
275    /** Returns the path to the home directory of the repository where the modules are checked in.
276     * @return the path to the home directory of the repository where the modules are checked in.
277     */
278    public String getRepositoryPath() {
279
280        return m_repositoryPath;
281    }
282
283    /**
284     * Returns the subfolder where module resources are placed inside the modules main folder in the repository.
285     * @return the subfolder where module resources are placed inside the modules main folder in the repository.
286     */
287    public String getResourcesSubFolder() {
288
289        return m_resourcesSubfolder;
290    }
291
292    /** Returns a flag, indicating if the config file could be read.
293     * @return a flag, indicating if the config file could be read.
294     */
295    public boolean isValid() {
296
297        return m_isValid;
298    }
299
300    /** Helper to read a line from the config file.
301     * @param propValue the property value
302     * @return the value of the variable set at this line.
303     */
304    private String getValueFromProp(final String propValue) {
305
306        String value = propValue;
307        // remove quotes
308        value = value.trim();
309        if ((value.startsWith("\"") && value.endsWith("\"")) || (value.startsWith("'") && value.endsWith("'"))) {
310            value = value.substring(1, value.length() - 1);
311        }
312        return value;
313    }
314
315    /** Reads the config file and stores configured values in member variables. */
316    private void readConfigFile() {
317
318        m_isValid = false;
319        if ((null != m_configFile) && m_configFile.exists()) {
320            if (m_configFile.canRead()) {
321                Properties props = new Properties();
322                try (FileInputStream input = new FileInputStream(m_configFile)) {
323                    props.load(input);
324                    for (Entry<Object, Object> entry : props.entrySet()) {
325                        String key = (String)entry.getKey();
326                        String propValue = (String)entry.getValue();
327                        if (key.equals(DEFAULT_MODULES_TO_EXPORT)) {
328                            String value = getValueFromProp(propValue).trim();
329                            if (value.contains(",")) {
330                                m_configuredModules = Arrays.asList(value.split(" *, *"));
331                            } else {
332                                m_configuredModules = Arrays.asList(value.split(" +"));
333                            }
334                        }
335
336                        if (key.equals(DEFAULT_MODULE_PATH)) {
337                            m_modulesPath = getValueFromProp(propValue).trim();
338                        }
339
340                        if (key.equals(DEFAULT_MODULE_RESOURCES_SUBFOLDER)) {
341                            m_resourcesSubfolder = getValueFromProp(propValue).trim();
342                        }
343
344                        if (key.equals(DEFAULT_PULL_MODE_BEFORE)) {
345                            String value = getValueFromProp(propValue);
346                            value = value.trim();
347                            if (value.equals("1")) {
348                                m_defaultPullModeBefore = true;
349                            }
350                        }
351                        if (key.equals(DEFAULT_PULL_MODE_AFTER)) {
352                            String value = getValueFromProp(propValue);
353                            value = value.trim();
354                            if (value.equals("1")) {
355                                m_defaultPullModeAfter = true;
356                            }
357                        }
358                        if (key.equals(DEFAULT_PUSH_MODE)) {
359                            String value = getValueFromProp(propValue);
360                            value = value.trim();
361                            if (value.equals("1")) {
362                                m_defaultPushMode = true;
363                            }
364                        }
365                        if (key.equals(DEFAULT_MODULE_EXPORT_PATH)) {
366                            String value = getValueFromProp(propValue);
367                            m_defaultModuleExportPath = value.trim();
368                        }
369                        if (key.equals(DEFAULT_REPOSITORY_PATH)) {
370                            String value = getValueFromProp(propValue);
371                            m_repositoryPath = value.trim();
372                        }
373                        if (key.equals(DEFAULT_EXPORT_MODE)) {
374                            String value = getValueFromProp(propValue);
375                            m_defaultExportMode = (value.trim().equals("1")) ? 1 : 0;
376                        }
377                        if (key.equals(DEFAULT_COMMIT_MESSAGE)) {
378                            String value = getValueFromProp(propValue);
379                            m_defaultCommitMessage = value;
380                        }
381                        if (key.equals(DEFAULT_GIT_USER_NAME)) {
382                            String value = getValueFromProp(propValue);
383                            m_defaultGitUserName = value;
384                        }
385                        if (key.equals(DEFAULT_GIT_USER_EMAIL)) {
386                            String value = getValueFromProp(propValue);
387                            m_defaultGitUserEmail = value;
388                        }
389                        if (key.equals(DEFAULT_EXCLUDE_LIBS)) {
390                            String value = getValueFromProp(propValue);
391                            m_defaultExcludeLibs = (value.trim().equals("1")) ? true : false;
392                        }
393                        if (key.equals(DEFAULT_COMMIT_MODE)) {
394                            String value = getValueFromProp(propValue);
395                            m_defaultCommitMode = (value.trim().equals("1")) ? true : false;
396                        }
397                        if (key.equals(DEFAULT_IGNORE_UNCLEAN)) {
398                            String value = getValueFromProp(propValue);
399                            m_defaultIgnoreUnclean = (value.trim().equals("1")) ? true : false;
400                        }
401                        if (key.equals(DEFAULT_COPY_AND_UNZIP)) {
402                            String value = getValueFromProp(propValue);
403                            m_defaultCopyAndUnzip = (value.trim().equals("1")) ? true : false;
404                        }
405                    }
406                    m_isValid = true;
407                } catch (IOException e) {
408                    e.printStackTrace();
409                }
410            }
411        }
412    }
413
414}