package net.curisit.securis; import java.io.File; import java.net.URISyntaxException; import java.util.Date; import net.curisit.securis.utils.Params; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class License { static { // BasicConfigurator.configure(new ConsoleAppender()); // PropertyConfigurator.configure("/log4j.xml"); // LogManager.getRootLogger(). } // private static final Logger log = LoggerFactory.getLogger(License.class); private static final Logger log = LogManager.getLogger(License.class); public static void main(String[] args) throws URISyntaxException { // Configuration conf = XMLConfigurationFactory.getInstance().getConfiguration("config", License.class.getResource("/log4j.xml").toURI()); log.info("Tool init {}", new Date()); checkConfigFile(); CommandLine cmd = getCommandLine(args); try { if (cmd.hasOption('g')) { String filename = cmd.getOptionValue("rfile"); if (filename == null) filename = "./license.req"; File file = new File(filename); LicenseManager.getInstance().createRequestFile(file); log.info("Request file {} generated OK", file.getAbsolutePath()); System.exit(0); } if (cmd.hasOption('l')) { String filename = cmd.getOptionValue("validate"); if (filename == null) filename = "./license.lic"; File file = new File(filename); try { LicenseManager.getInstance().validateLicense(file); log.info("License file {} is valid", file.getAbsolutePath()); } catch (SeCurisException e) { log.info("License file {} is NOT valid", file.getAbsolutePath()); } System.exit(0); } } catch (SeCurisException e) { log.error("The command generated an error: {}", e.toString()); } } /** * Checks that config file exists and contains mandatory parameters */ private static void checkConfigFile() { String appCode = Params.get(Params.KEYS.APPLICATION_CODE); if (appCode == null) { log.error("Manadatory parameter {} is not set in config file", Params.KEYS.APPLICATION_CODE); System.exit(-3); } String customerCode = Params.get(Params.KEYS.CUSTOMER_CODE); if (customerCode == null) { log.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE); System.exit(-4); } } private static CommandLine getCommandLine(String[] args) { Options ops = prepareOptionCLI(); if (args.length == 0) { printHelp(ops); } CommandLineParser parser = new PosixParser(); CommandLine cmd = null; try { cmd = parser.parse(ops, args); } catch (ParseException e) { printHelp(ops); } if (cmd.hasOption('h')) { printHelp(ops); } return cmd; } private static void printHelp(Options ops) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("securis-client", ops, true); System.exit(-1); } @SuppressWarnings("static-access") private static Options prepareOptionCLI() { Options options = new Options(); options.addOption("h", "help", false, "Show help."); options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile").withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r')); options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s')); options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true).create('l')); options.addOption("g", "gen_request", false, "Generate request file. If --rfile parameter is missing then It is generated in current directory."); options.addOption("c", "create", false, "Request a license file from server. --rfile and --server parameters are mandatory."); options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. --server parameter is mandatory."); options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("sync").withDescription("Synchronize/renew the current license file. --server parameter is mandatory.").hasArg(true).create('y')); return options; } }