Main.java
/*
* Copyright (C) 2019 sw4j.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sw4j.tool.barcode.random;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.sw4j.tool.barcode.random.codedata.FileCodeData;
import org.sw4j.tool.barcode.random.config.Config;
import org.sw4j.tool.barcode.random.generator.CodeGenerator;
/**
* This is the main class of the random barcode generator.
*
* @author Uwe Plonus <u.plonus@gmail.com>
*/
public class Main {
/**
* <p>
* Exit code of the application when the {@code app.home} system property is not set.
* </p>
*/
private static final int EXIT_NO_APP_HOME = -3;
/**
* <p>
* The logger of this class.
* </p>
*/
private final Logger logger = Logger.getLogger(Main.class.getName());
/**
* <p>
* The sole constructor of this class.
* </p>
*/
public Main() {
}
/**
* <p>
* The main method to start the application.
* </p>
*
* @TODO handle the IOException internally
* @param args the command line arguments.
* @throws IOException forwarded exception.
*/
public static void main(final String... args) throws IOException {
new Main().run(args);
}
/**
* <p>
* Run the application with the given command line arguments.
* </p>
* <p>
* The application uses the system property {@code app.home} to determine the home folder of the application and
* resolve the default configuration file relative to this folder.
* </p>
* <p>
* This method needs the system property {@code app.home} to be set. If the system property is not set then the
* application is terminated with exit code {@code -3}.
* </p>
*
* @TODO handle the IOException internally
* @param args the command line arguments.
* @throws IOException forwarded exception
*/
public void run(final String... args) throws IOException {
String appHome = System.getProperty("app.home");
if (appHome == null) {
System.err.println("app.home not set");
System.exit(EXIT_NO_APP_HOME);
}
CommandLine cl = parseCommandLine(args);
File configFile;
if (cl.hasOption("c")) {
configFile = new File(cl.getOptionValue("c"));
} else {
String configFileName = "etc/random.yaml";
configFile = new File(appHome, configFileName);
}
Config config = Config.readYamlConfig(configFile);
CodeGenerator generator;
if (config.getPredefined() == null) {
generator = new CodeGenerator(config.getRandom(), new FileCodeData(config));
} else {
generator = new CodeGenerator(config.getPredefined(), new FileCodeData(config));
}
generator.createCodes();
System.out.println("End");
}
/**
* <p>
* Parses the command line and returns the parsed command line.
* </p>
*
* @param args the command line arguments.
* @return the parsed command line.
*/
private CommandLine parseCommandLine(final String... args) {
Options clo = new Options();
clo.addOption(Option
.builder("c")
.longOpt("config")
.hasArg(true)
.required(false)
.argName("config-file")
.desc("The config file to use.")
.build());
CommandLineParser clp = new DefaultParser();
CommandLine cl = new CommandLine.Builder().build();
try {
cl = clp.parse(clo, args, true);
} catch (ParseException pex) {
logger.log(Level.WARNING, "Problems while parsing the command line", pex);
}
return cl;
}
}