001/*
002 * #%L
003 * Netarchivesuite - deploy
004 * %%
005 * Copyright (C) 2005 - 2018 The Royal Danish Library, 
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.deploy;
024
025import java.io.File;
026import java.io.IOException;
027import java.nio.charset.Charset;
028
029
030import org.apache.commons.cli.CommandLine;
031import org.apache.commons.cli.CommandLineParser;
032import org.apache.commons.cli.Option;
033import org.apache.commons.cli.Options;
034import org.apache.commons.cli.ParseException;
035import org.apache.commons.cli.PosixParser;
036
037import dk.netarkivet.common.utils.Settings;
038
039// JDK8-import
040//import java.util.Optional;
041
042
043/**
044 * The application that is run to generate install and start/stop scripts for all physical locations, machines and
045 * applications.
046 * <p>
047 * The actual deployment has to be done from an Linux/Unix machine, and this application should therefore not be run on
048 * Windows.
049 */
050public final class DeployApplication {
051
052    static {
053        Settings.addDefaultClasspathSettings(Constants.BUILD_COMPLETE_SETTINGS_FILE_PATH);
054    }
055
056    /** The configuration for this deploy. */
057    private static DeployConfiguration deployConfig;
058    /** Argument parameter. */
059    private static ArgumentParameters ap = new ArgumentParameters();
060    /** The deploy-config file. */
061    private static File deployConfigFile;
062    /** The NetarchiveSuite file. */
063    private static File netarchiveSuiteFile;
064    /** The security policy file. */
065    private static File secPolicyFile;
066    /** SLF4J xml configuration file. */
067    private static File slf4jConfigFile;
068    /** The database file. */
069    private static File dbFile;
070    /** The arguments for resetting tempDir. */
071    private static boolean resetDirectory;
072    /** The archive database file. */
073    private static File arcDbFile;
074    /** The folder with the external libraries to be deployed. */
075    private static File externalJarFolder;
076    
077    //private static Optional<File> defaultBundlerZip;
078    private static File defaultBundlerZip;
079    
080    /** The logo png file. */
081    private static File logoFile;
082    /** The menulogo png file. */
083    private static File menulogoFile;
084    
085    /**
086     * Private constructor to disallow instantiation of this class.
087     */
088    private DeployApplication() {
089    }
090
091    /**
092     * Run deploy.
093     *
094     * @param args The Command-line arguments in no particular order:
095     *
096     * -C The deploy configuration file (ends with .xml). -Z The NetarchiveSuite file to be unpacked (ends with .zip).
097     * -S The security policy file (ends with .policy). -L The logging property file (ends with .prop). -O [OPTIONAL]
098     * The output directory -D [OPTIONAL] The harvest definition database -T [OPTIONAL] The test arguments
099     * (httpportoffset, port, environmentName, mailReceiver) -R [OPTIONAL] For resetting the tempDir (takes arguments
100     * 'y' or 'yes') -E [OPTIONAL] Evaluating the deployConfig file (arguments: 'y' or 'yes') -A [OPTIONAL] For archive
101     * database. -J [OPTIONAL] For deploying with external jar files. Must be the total path to the directory containing
102     * jar-files. -l [OPTIONAL] for logo png file. -m [OPTIONAL] for menulogo png file 
103     * These external files will be placed on every machine, and they have to manually be put into the
104     * classpath, where they should be used.
105     */
106    public static void main(String[] args) {
107        try {
108            // Make sure the arguments can be parsed.
109            if (!ap.parseParameters(args)) {
110                System.err.print(Constants.MSG_ERROR_PARSE_ARGUMENTS);
111                System.out.println(ap.listArguments());
112                System.exit(1);
113            }
114
115            // Check arguments
116            if (ap.getCommandLine().getOptions().length < Constants.ARGUMENTS_REQUIRED) {
117                System.err.print(Constants.MSG_ERROR_NOT_ENOUGH_ARGUMENTS);
118                System.out.println();
119                System.out.println("Use DeployApplication with following arguments:");
120                System.out.println(ap.listArguments());
121                System.out.println("outputdir defaults to ./environmentName (set in config file)");
122                System.exit(1);
123            }
124            // test if more arguments than options is given
125            if (args.length > ap.getOptions().getOptions().size()) {
126                System.err.print(Constants.MSG_ERROR_TOO_MANY_ARGUMENTS);
127                System.out.println();
128                System.out.println("Maximum " + ap.getOptions().getOptions().size() + "arguments.");
129                System.exit(1);
130            }
131
132            // Retrieving the configuration filename
133            String deployConfigFileName = ap.getCommandLine().getOptionValue(Constants.ARG_CONFIG_FILE);
134            // Retrieving the NetarchiveSuite filename
135            String netarchiveSuiteFileName = ap.getCommandLine().getOptionValue(Constants.ARG_NETARCHIVE_SUITE_FILE);
136            // Retrieving the security policy filename
137            String secPolicyFileName = ap.getCommandLine().getOptionValue(Constants.ARG_SECURITY_FILE);
138            // Retrieving the SLF4J xml filename
139            String slf4jConfigFileName = ap.getCommandLine().getOptionValue(Constants.ARG_SLF4J_CONFIG_FILE);
140            // Retrieving the output directory name
141            String outputDir = ap.getCommandLine().getOptionValue(Constants.ARG_OUTPUT_DIRECTORY);
142            // Retrieving the database filename
143            String databaseFileName = ap.getCommandLine().getOptionValue(Constants.ARG_DATABASE_FILE);
144            // Retrieving the test arguments
145            String testArguments = ap.getCommandLine().getOptionValue(Constants.ARG_TEST);
146            // Retrieving the reset argument
147            String resetArgument = ap.getCommandLine().getOptionValue(Constants.ARG_RESET);
148            // Retrieving the evaluate argument
149            String evaluateArgument = ap.getCommandLine().getOptionValue(Constants.ARG_EVALUATE);
150            // Retrieve the archive database filename.
151            String arcDbFileName = ap.getCommandLine().getOptionValue(Constants.ARG_ARC_DB);
152            // Retrieves the jar-folder name.
153            String jarFolderName = ap.getCommandLine().getOptionValue(Constants.ARG_JAR_FOLDER);
154            // Retrieves the logo filename.
155            String logoFilename = ap.getCommandLine().getOptionValue(Constants.ARG_LOGO);
156            // Retrieves the menulogo filename.
157            String menulogoFilename = ap.getCommandLine().getOptionValue(Constants.ARG_MENULOGO);
158            
159            // Retrieves the source encoding.
160            // If not specified get system default
161            String sourceEncoding = ap.getCommandLine().getOptionValue(Constants.ARG_SOURCE_ENCODING);
162            String msgTail = "";
163            if (sourceEncoding == null || sourceEncoding.isEmpty()) {
164                sourceEncoding = Charset.defaultCharset().name();
165                msgTail = " (defaulted)";
166            }
167            System.out.println("Will read source files using encoding '" + sourceEncoding + "'" + msgTail);
168
169            // check deployConfigFileName and retrieve the corresponding file
170            initConfigFile(deployConfigFileName);
171
172            // check netarchiveSuiteFileName and retrieve the corresponding file
173            initNetarchiveSuiteFile(netarchiveSuiteFileName);
174
175            // check secPolicyFileName and retrieve the corresponding file
176            initSecPolicyFile(secPolicyFileName);
177
178            initSLF4JXmlFile(slf4jConfigFileName);
179
180            // check database
181            initDatabase(databaseFileName);
182
183            // check and apply the test arguments
184            initTestArguments(testArguments);
185
186            // check reset arguments.
187            initReset(resetArgument);
188
189            // evaluates the config file
190            initEvaluate(evaluateArgument, sourceEncoding);
191
192            // check the archive database
193            initArchiveDatabase(arcDbFileName);
194
195            // check the external jar-files library folder.
196            initJarFolder(jarFolderName);
197
198            //initBundlerZip(Optional.ofNullable(
199            //        ap.getCommandLine().getOptionValue(Constants.ARG_DEFAULT_BUNDLER_ZIP)));
200            initBundlerZip(ap.getCommandLine().getOptionValue(Constants.ARG_DEFAULT_BUNDLER_ZIP));
201            
202            // check the logo files
203            initLogos(logoFilename, menulogoFilename);
204            
205            // Make the configuration based on the input data
206            deployConfig = new DeployConfiguration(deployConfigFile, netarchiveSuiteFile, secPolicyFile,
207                    slf4jConfigFile, outputDir, dbFile, arcDbFile, resetDirectory, externalJarFolder, sourceEncoding,
208                    defaultBundlerZip, logoFile, menulogoFile);
209
210            // Write the scripts, directories and everything
211            deployConfig.write();
212        } catch (SecurityException e) {
213            // This problem should only occur in tests -> thus not err message.
214            System.out.println("SECURITY ERROR: ");
215            e.printStackTrace();
216        } catch (Throwable e) {
217            System.err.println("DEPLOY APPLICATION ERROR: ");
218            e.printStackTrace();
219        }
220    }
221
222    /**
223     * Checks the configuration file argument and retrieves the file.
224     *
225     * @param deployConfigFileName The configuration file argument.
226     */
227    private static void initConfigFile(String deployConfigFileName) {
228        // check whether deploy-config file name is given as argument
229        if (deployConfigFileName == null) {
230            System.err.print(Constants.MSG_ERROR_NO_CONFIG_FILE_ARG);
231            System.out.println();
232            System.exit(1);
233        }
234        // check whether deploy-config file has correct extensions
235        if (!deployConfigFileName.endsWith(Constants.EXTENSION_XML_FILES)) {
236            System.err.print(Constants.MSG_ERROR_CONFIG_EXTENSION);
237            System.out.println();
238            System.exit(1);
239        }
240        // get the file
241        deployConfigFile = new File(deployConfigFileName);
242        // check whether the deploy-config file exists.
243        if (!deployConfigFile.exists()) {
244            System.err.print(Constants.MSG_ERROR_NO_CONFIG_FILE_FOUND);
245            System.out.println();
246            System.exit(1);
247        }
248    }
249
250    /**
251     * Checks the NetarchiveSuite file argument and retrieves the file.
252     *
253     * @param netarchiveSuiteFileName The NetarchiveSuite argument.
254     */
255    private static void initNetarchiveSuiteFile(String netarchiveSuiteFileName) {
256        // check whether NetarchiveSuite file name is given as argument
257        if (netarchiveSuiteFileName == null) {
258            System.err.print(Constants.MSG_ERROR_NO_NETARCHIVESUITE_FILE_ARG);
259            System.out.println();
260            System.exit(1);
261        }
262        // check whether the NetarchiveSuite file has correct extensions
263        if (!netarchiveSuiteFileName.endsWith(Constants.EXTENSION_ZIP_FILES)) {
264            System.err.print(Constants.MSG_ERROR_NETARCHIVESUITE_EXTENSION);
265            System.out.println();
266            System.exit(1);
267        }
268        // get the file
269        netarchiveSuiteFile = new File(netarchiveSuiteFileName);
270        // check whether the NetarchiveSuite file exists.
271        if (!netarchiveSuiteFile.isFile()) {
272            System.err.print(Constants.MSG_ERROR_NO_NETARCHIVESUITE_FILE_FOUND);
273            System.out.println();
274            System.out.println("Couldn't find file: " + netarchiveSuiteFile.getAbsolutePath());
275            System.exit(1);
276        }
277    }
278
279    /**
280     * Checks the security policy file argument and retrieves the file.
281     *
282     * @param secPolicyFileName The security policy argument.
283     */
284    private static void initSecPolicyFile(String secPolicyFileName) {
285        // check whether security policy file name is given as argument
286        if (secPolicyFileName == null) {
287            System.err.print(Constants.MSG_ERROR_NO_SECURITY_FILE_ARG);
288            System.out.println();
289            System.exit(1);
290        }
291        // check whether security policy file has correct extensions
292        if (!secPolicyFileName.endsWith(Constants.EXTENSION_POLICY_FILES)) {
293            System.err.print(Constants.MSG_ERROR_SECURITY_EXTENSION);
294            System.out.println();
295            System.exit(1);
296        }
297        // get the file
298        secPolicyFile = new File(secPolicyFileName);
299        // check whether the security policy file exists.
300        if (!secPolicyFile.exists()) {
301            System.err.print(Constants.MSG_ERROR_NO_SECURITY_FILE_FOUND);
302            System.out.println();
303            System.out.println("Couldn't find file: " + secPolicyFile.getAbsolutePath());
304            System.exit(1);
305        }
306    }
307
308    /**
309     * Checks the SLF4J config file argument and retrieves the file.
310     *
311     * @param slf4jXmlFileName The SLF4J config argument.
312     */
313    private static void initSLF4JXmlFile(String slf4jXmlFileName) {
314        // check whether SLF4J config file name is given as argument
315        if (slf4jXmlFileName == null) {
316            System.err.print(Constants.MSG_ERROR_NO_SLF4J_CONFIG_FILE_ARG);
317            System.out.println();
318            System.exit(1);
319        }
320        // check whether the SLF4J xml file has correct extensions
321        if (!slf4jXmlFileName.endsWith(Constants.EXTENSION_XML_FILES)) {
322            System.err.print(Constants.MSG_ERROR_SLF4J_CONFIG_EXTENSION);
323            System.out.println();
324            System.exit(1);
325        }
326        // get the file
327        slf4jConfigFile = new File(slf4jXmlFileName);
328        // check whether the SLF4J xml file exists.
329        if (!slf4jConfigFile.exists()) {
330            System.err.print(Constants.MSG_ERROR_NO_SLF4J_CONFIG_FILE_FOUND);
331            System.out.println();
332            System.out.println("Couldn't find file: " + slf4jConfigFile.getAbsolutePath());
333            System.exit(1);
334        }
335    }
336
337    /**
338     * Checks the database argument (if any) for extension and existence.
339     *
340     * @param databaseFileName The name of the database file.
341     */
342    private static void initDatabase(String databaseFileName) {
343        dbFile = null;
344        // check the extension on the database, if it is given as argument
345        if (databaseFileName != null) {
346            if (!databaseFileName.endsWith(Constants.EXTENSION_JAR_FILES)
347                    && !databaseFileName.endsWith(Constants.EXTENSION_ZIP_FILES)) {
348                System.err.print(Constants.MSG_ERROR_DATABASE_EXTENSION);
349                System.out.println();
350                System.exit(1);
351            }
352
353            // get the file
354            dbFile = new File(databaseFileName);
355            // check whether the database file exists.
356            if (!dbFile.isFile()) {
357                System.err.print(Constants.MSG_ERROR_NO_DATABASE_FILE_FOUND);
358                System.out.println();
359                System.out.println("Couldn't find file: " + dbFile.getAbsolutePath());
360                System.exit(1);
361            }
362        }
363    }
364
365    /**
366     * Checks the arguments for resetting the directory. Only the arguments 'y' or 'yes' resets the database directory.
367     * Default is 'no'.
368     * <p>
369     * If another argument than 'y', 'yes', 'n' or 'no' is given, an warning is given.
370     *
371     * @param resetArgument The argument for resetting given.
372     */
373    private static void initReset(String resetArgument) {
374        if (resetArgument != null) {
375            if (resetArgument.equalsIgnoreCase(Constants.YES_SHORT)
376                    || resetArgument.equalsIgnoreCase(Constants.YES_LONG)) {
377                // if positive argument, then set to true.
378                resetDirectory = true;
379            } else if (resetArgument.equalsIgnoreCase(Constants.NO_SHORT)
380                    || resetArgument.equalsIgnoreCase(Constants.NO_LONG)) {
381                // if negative argument, then set to false.
382                resetDirectory = false;
383            } else {
384                // if wrong argument, notify and set to false.
385                System.err.println(Constants.MSG_ERROR_RESET_ARGUMENT);
386                resetDirectory = false;
387            }
388        } else {
389            // if no arguments, then
390            resetDirectory = false;
391        }
392    }
393
394    /**
395     * Checks the arguments for evaluating the config file. Only the arguments 'y' or 'yes' is accepted for evaluation.
396     * Anything else (including argument set to null) does not evaluate the deployConfigFile.
397     *
398     * @param evaluateArgument The argument for evaluation.
399     * @param encoding the encoding to use to read from the input file
400     */
401    public static void initEvaluate(String evaluateArgument, String encoding) {
402        // check if argument is given and it is acknowledgement ('y' or 'yes')
403        if ((evaluateArgument != null)
404                && (!evaluateArgument.isEmpty())
405                && (evaluateArgument.equalsIgnoreCase(Constants.YES_SHORT) || evaluateArgument
406                        .equalsIgnoreCase(Constants.YES_LONG))) {
407            // if yes, then evaluate config file
408            EvaluateConfigFile evf = new EvaluateConfigFile(deployConfigFile, encoding);
409            evf.evaluate();
410        }
411    }
412
413    /**
414     * Checks the argument for the archive database.
415     *
416     * @param arcDbFileName The path to the archive database.
417     */
418    public static void initArchiveDatabase(String arcDbFileName) {
419        arcDbFile = null;
420        // check the extension on the database, if it is given as argument
421        if (arcDbFileName != null) {
422            if (!arcDbFileName.endsWith(Constants.EXTENSION_JAR_FILES)
423                    && !arcDbFileName.endsWith(Constants.EXTENSION_ZIP_FILES)) {
424                System.err.print(Constants.MSG_ERROR_BPDB_EXTENSION);
425                System.out.println();
426                System.exit(1);
427            }
428
429            // get the file
430            arcDbFile = new File(arcDbFileName);
431            // check whether the database file exists.
432            if (!arcDbFile.isFile()) {
433                System.err.print(Constants.MSG_ERROR_NO_BPDB_FILE_FOUND);
434                System.out.println();
435                System.out.println("Couldn't find file: " + arcDbFile.getAbsolutePath());
436                System.exit(1);
437            }
438        }
439    }
440
441    /**
442     * Checks the argument for the external jar-folder.
443     *
444     * @param folderName The path to the folder. Global path.
445     */
446    public static void initJarFolder(String folderName) {
447        externalJarFolder = null;
448        if (folderName != null && !folderName.isEmpty()) {
449            externalJarFolder = new File(folderName);
450
451            if (!externalJarFolder.isDirectory()) {
452                System.err.print(Constants.MSG_ERROR_NO_JAR_FOLDER);
453                System.out.println();
454                System.out.println("Couldn't find directory: " + externalJarFolder.getAbsolutePath());
455                System.exit(1);
456            }
457        }
458    }
459
460    /**
461     * Checks if the default bundler zip file exists if defined.
462     *
463     * @param defaultBundlerZipName The path to the default bundler zip file to use.
464     */
465    public static void initBundlerZip(String defaultBundlerZipName) {
466        if (defaultBundlerZipName != null) {
467            defaultBundlerZip = new File(defaultBundlerZipName);
468            if (!defaultBundlerZip.exists()) {
469                System.err.print(Constants.MSG_ERROR_NO_BUNDLER_ZIP_FILE);
470                System.out.println();
471                System.out.println("Couldn't find the default bundler file: " + defaultBundlerZip.getAbsolutePath());
472                System.exit(1);
473            }
474        }
475    }
476
477    /**
478     * Checks if the logo files exists
479     *
480     * @param logoFilename The absolute path to the logo png file.
481     * @param menulogoFilename The absoluet path to the menu logo png file.
482     */
483    public static void initLogos(String logoFilename, String menulogoFilename) {
484        if (logoFilename != null) {
485                logoFile = new File(logoFilename);
486                if (!logoFile.exists()) {
487                        logoFile = null;
488                }
489        }
490        
491        if (menulogoFilename != null) {
492                menulogoFile = new File(menulogoFilename);
493                if (!menulogoFile.exists()) {
494                        menulogoFile = null;
495                }
496        }
497    }
498    
499    /**
500     * Applies the test arguments.
501     * <p>
502     * If the test arguments are given correctly, the configuration file is loaded and changed appropriately, then
503     * written to a test configuration file.
504     * <p>
505     * The new test configuration file has the same name as the original configuration file, except ".xml" is replaced
506     * by "_text.xml". Thus "path/config.xml" -> "path/config_test.xml".
507     *
508     * @param testArguments The test arguments.
509     */
510    private static void initTestArguments(String testArguments) {
511        // test if any test arguments (if none, don't apply, just stop).
512        if (testArguments == null || testArguments.isEmpty()) {
513            return;
514        }
515
516        String[] changes = testArguments.split(Constants.REGEX_COMMA_CHARACTER);
517        if (changes.length != Constants.TEST_ARGUMENTS_REQUIRED) {
518            System.err.print(Constants.MSG_ERROR_TEST_ARGUMENTS);
519            System.out.println();
520            System.out.println(changes.length + " arguments was given and " + Constants.TEST_ARGUMENTS_REQUIRED
521                    + " was expected.");
522            System.out.println("Received: " + testArguments);
523            System.exit(1);
524        }
525
526        try {
527            CreateTestInstance cti = new CreateTestInstance(deployConfigFile);
528
529            // apply the arguments
530            cti.applyTestArguments(changes[Constants.TEST_ARGUMENT_OFFSET_INDEX],
531                    changes[Constants.TEST_ARGUMENT_HTTP_INDEX],
532                    changes[Constants.TEST_ARGUMENT_ENVIRONMENT_NAME_INDEX],
533                    changes[Constants.TEST_ARGUMENT_MAIL_INDEX]);
534
535            // replace ".xml" with "_test.xml"
536            String tmp = deployConfigFile.getPath();
537            // split this into two ("path/config.xml" = {"path/config", ".xml"})
538            String[] configFile = tmp.split(Constants.REGEX_DOT_CHARACTER);
539            // take the first part and add test ending
540            // ("path/config" + "_test.xml" = "path/config_test.xml")
541            String nameOfNewConfig = configFile[0] + Constants.TEST_CONFIG_FILE_REPLACE_ENDING;
542
543            // create and use new config file.
544            cti.createConfigurationFile(nameOfNewConfig);
545            deployConfigFile = new File(nameOfNewConfig);
546        } catch (IOException e) {
547            System.out.println("Error in test arguments: " + e);
548            System.exit(1);
549        }
550    }
551
552    /**
553     * Handles the incoming arguments.
554     */
555    private static class ArgumentParameters {
556        /** Options object for parameters. */
557        private Options options = new Options();
558        /** Parser for parsing the command line arguments. */
559        private CommandLineParser parser = new PosixParser();
560        /** The command line. */
561        private CommandLine cmd;
562        /** Whether the options has an argument. */
563        private static final boolean HAS_ARG = true;
564
565        /**
566         * Initialise options by setting legal parameters for batch jobs.
567         */
568        ArgumentParameters() {
569            options.addOption(Constants.ARG_CONFIG_FILE, HAS_ARG, "Config file.");
570            options.addOption(Constants.ARG_NETARCHIVE_SUITE_FILE, HAS_ARG, "The NetarchiveSuite package file.");
571            options.addOption(Constants.ARG_SECURITY_FILE, HAS_ARG, "Security property file.");
572            options.addOption(Constants.ARG_SLF4J_CONFIG_FILE, HAS_ARG, "SLF4J config file.");
573            options.addOption(Constants.ARG_OUTPUT_DIRECTORY, HAS_ARG, "[OPTIONAL] output directory.");
574            options.addOption(Constants.ARG_DATABASE_FILE, HAS_ARG, "[OPTIONAL] Database file.");
575            options.addOption(Constants.ARG_TEST, HAS_ARG, "[OPTIONAL] Tests arguments (offset for http port,"
576                    + " http port, environment name, mail receiver).");
577            options.addOption(Constants.ARG_RESET, HAS_ARG, "[OPTIONAL] Reset temp directory ('y' or 'yes'"
578                    + "means reset, anything else means do not reset."
579                    + " Different from 'y', 'yes', 'n' or 'no' gives" + " an error message.");
580            options.addOption(Constants.ARG_EVALUATE, HAS_ARG, "[OPTIONAL] Evaluate the config file.");
581            options.addOption(Constants.ARG_ARC_DB, HAS_ARG, "[OPTIONAL] The archive database file");
582            options.addOption(Constants.ARG_JAR_FOLDER, HAS_ARG, "[OPTIONAL] Installing the external jar library "
583                    + "files within the given folder.");
584            options.addOption(Constants.ARG_SOURCE_ENCODING, HAS_ARG, "[OPTIONAL] Encoding to use for source files.");
585            options.addOption(Constants.ARG_DEFAULT_BUNDLER_ZIP, HAS_ARG, "[OPTIONAL] The bundled harvester to use. "
586                    + "If not provided here the bundler needs to be provided in the settings for each (H3) harvester");
587            options.addOption(null, Constants.ARG_LOGO, HAS_ARG, "[OPTIONAL] The Logo png file to use. "
588                    + "(this is only working for Linux)");
589            options.addOption(null, Constants.ARG_MENULOGO, HAS_ARG, "[OPTIONAL] The Menulogo png file to use. "
590                    + "(this is only working for Linux)");
591        }
592
593        /**
594         * Parsing the input arguments.
595         *
596         * @param args The input arguments.
597         * @return Whether it parsed correctly or not.
598         */
599        Boolean parseParameters(String[] args) {
600            try {
601                // parse the command line arguments
602                cmd = parser.parse(options, args);
603            } catch (ParseException exp) {
604                System.out.println("Parsing error: " + exp);
605                return false;
606            }
607            return true;
608        }
609
610        /**
611         * Get the list of possible arguments with their description.
612         *
613         * @return The list describing the possible arguments.
614         */
615        String listArguments() {
616            StringBuilder res = new StringBuilder();
617            res.append(Constants.NEWLINE);
618            res.append(Constants.INIT_ARGUMENTS_LIST);
619            // add options
620            for (Object o : options.getOptions()) {
621                Option op = (Option) o;
622                res.append(Constants.NEWLINE);
623                res.append(Constants.DASH);
624                res.append(op.getOpt());
625                res.append(Constants.SPACE);
626                res.append(op.getDescription());
627            }
628            return res.toString();
629        }
630
631        /**
632         * For retrieving the options.
633         *
634         * @return The options.
635         */
636        public Options getOptions() {
637            return options;
638        }
639
640        /**
641         * For retrieving the commandLine.
642         *
643         * @return The cmd.
644         */
645        public CommandLine getCommandLine() {
646            return cmd;
647        }
648    }
649
650}