| .. | .. |
|---|
| 15 | 15 | import org.apache.commons.cli.PosixParser; |
|---|
| 16 | 16 | import org.apache.logging.log4j.LogManager; |
|---|
| 17 | 17 | import org.apache.logging.log4j.Logger; |
|---|
| 18 | | -import org.codehaus.jackson.map.DeserializerFactory.Config; |
|---|
| 19 | 18 | |
|---|
| 20 | 19 | /** |
|---|
| 21 | 20 | * Main class when SeCuris client is used from command line. |
|---|
| .. | .. |
|---|
| 44 | 43 | |
|---|
| 45 | 44 | private static final Logger LOG = LogManager.getLogger(License.class); |
|---|
| 46 | 45 | |
|---|
| 47 | | - /** |
|---|
| 48 | | - * Aplication entry point when it used as CLI |
|---|
| 49 | | - * |
|---|
| 50 | | - * @param args |
|---|
| 51 | | - * @throws URISyntaxException |
|---|
| 52 | | - */ |
|---|
| 53 | | - public static void main(String[] args) throws URISyntaxException { |
|---|
| 46 | + /** |
|---|
| 47 | + * Aplication entry point when it used as CLI |
|---|
| 48 | + * |
|---|
| 49 | + * @param args |
|---|
| 50 | + * @throws URISyntaxException |
|---|
| 51 | + */ |
|---|
| 52 | + public static void main(String[] args) throws URISyntaxException { |
|---|
| 54 | 53 | |
|---|
| 55 | | - LOG.debug("SeCuris client tool init "); |
|---|
| 54 | + LOG.debug("SeCuris client tool init "); |
|---|
| 56 | 55 | |
|---|
| 57 | | - checkConfigFile(); |
|---|
| 58 | | - CommandLine cmd = getCommandLine(args); |
|---|
| 56 | + checkConfigFile(); |
|---|
| 57 | + CommandLine cmd = getCommandLine(args); |
|---|
| 59 | 58 | |
|---|
| 60 | | - try { |
|---|
| 61 | | - if (cmd.hasOption('g')) { |
|---|
| 62 | | - String filename = cmd.getOptionValue("rfile"); |
|---|
| 63 | | - if (filename == null) |
|---|
| 64 | | - filename = "./license.req"; |
|---|
| 65 | | - File file = new File(filename); |
|---|
| 66 | | - LicenseManager.getInstance().createRequestFile(file); |
|---|
| 67 | | - LOG.info("Request file {} generated OK", file.getAbsolutePath()); |
|---|
| 68 | | - System.exit(0); |
|---|
| 69 | | - } |
|---|
| 59 | + try { |
|---|
| 60 | + if (cmd.hasOption('g')) { |
|---|
| 61 | + String filename = cmd.getOptionValue("rfile"); |
|---|
| 62 | + if (filename == null) |
|---|
| 63 | + filename = "./license.req"; |
|---|
| 64 | + File file = new File(filename); |
|---|
| 65 | + LicenseManager.getInstance().createRequestFile(file); |
|---|
| 66 | + LOG.info("Request file {} generated OK", file.getAbsolutePath()); |
|---|
| 67 | + System.exit(0); |
|---|
| 68 | + } |
|---|
| 70 | 69 | |
|---|
| 71 | | - if (cmd.hasOption('l')) { |
|---|
| 72 | | - String filename = cmd.getOptionValue("validate"); |
|---|
| 73 | | - if (filename == null) |
|---|
| 74 | | - filename = "./license.lic"; |
|---|
| 75 | | - File file = new File(filename); |
|---|
| 76 | | - if (!file.exists()) { |
|---|
| 77 | | - throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath()); |
|---|
| 78 | | - } |
|---|
| 79 | | - try { |
|---|
| 80 | | - LicenseManager.getInstance().validateLicense(file); |
|---|
| 81 | | - LOG.info("License file {} is valid", file.getAbsolutePath()); |
|---|
| 82 | | - } catch (SeCurisException e) { |
|---|
| 83 | | - LOG.info("License file {} is NOT valid", file.getAbsolutePath()); |
|---|
| 84 | | - LOG.info("Reason: {}", e.toString()); |
|---|
| 85 | | - } |
|---|
| 70 | + if (cmd.hasOption('l')) { |
|---|
| 71 | + String filename = cmd.getOptionValue("validate"); |
|---|
| 72 | + if (filename == null) |
|---|
| 73 | + filename = "./license.lic"; |
|---|
| 74 | + File file = new File(filename); |
|---|
| 75 | + if (!file.exists()) { |
|---|
| 76 | + throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath()); |
|---|
| 77 | + } |
|---|
| 78 | + try { |
|---|
| 79 | + LicenseManager.getInstance().validateLicense(file); |
|---|
| 80 | + LOG.info("License file {} is valid", file.getAbsolutePath()); |
|---|
| 81 | + } catch (SeCurisException e) { |
|---|
| 82 | + LOG.info("License file {} is NOT valid", file.getAbsolutePath()); |
|---|
| 83 | + LOG.info("Reason: {}", e.toString()); |
|---|
| 84 | + } |
|---|
| 86 | 85 | |
|---|
| 87 | | - System.exit(0); |
|---|
| 88 | | - } |
|---|
| 86 | + System.exit(0); |
|---|
| 87 | + } |
|---|
| 89 | 88 | |
|---|
| 90 | | - if (cmd.hasOption('c')) { |
|---|
| 91 | | - SignedLicenseBean lic = LicenseManager.getInstance().requestLicense(); |
|---|
| 92 | | - String filename = cmd.getOptionValue("c"); |
|---|
| 89 | + if (cmd.hasOption('c')) { |
|---|
| 90 | + SignedLicenseBean lic = LicenseManager.getInstance().requestLicense(); |
|---|
| 91 | + String filename = cmd.getOptionValue("c"); |
|---|
| 93 | 92 | if (filename == null) |
|---|
| 94 | 93 | filename = "./license.lic"; |
|---|
| 95 | 94 | File file = new File(filename); |
|---|
| 96 | 95 | LicenseManager.getInstance().save(lic, file); |
|---|
| 97 | 96 | LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath()); |
|---|
| 98 | 97 | System.exit(0); |
|---|
| 99 | | - } |
|---|
| 98 | + } |
|---|
| 100 | 99 | |
|---|
| 101 | | - if (cmd.hasOption('t')) { |
|---|
| 100 | + if (cmd.hasOption('t')) { |
|---|
| 102 | 101 | LicenseManager.getInstance().testServer(); |
|---|
| 103 | 102 | LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL)); |
|---|
| 104 | 103 | |
|---|
| 105 | 104 | System.exit(0); |
|---|
| 106 | | - } |
|---|
| 105 | + } |
|---|
| 107 | 106 | |
|---|
| 108 | | - if (cmd.hasOption('r')) { |
|---|
| 109 | | - String licFilename = cmd.getOptionValue("renew"); |
|---|
| 110 | | - checkMandatoryParameter(licFilename, "renew"); |
|---|
| 107 | + if (cmd.hasOption('r')) { |
|---|
| 108 | + String licFilename = cmd.getOptionValue("renew"); |
|---|
| 109 | + checkMandatoryParameter(licFilename, "renew"); |
|---|
| 111 | 110 | File file = new File(licFilename); |
|---|
| 112 | 111 | if (!file.exists()) { |
|---|
| 113 | 112 | throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath()); |
|---|
| .. | .. |
|---|
| 118 | 117 | LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath()); |
|---|
| 119 | 118 | LicenseManager.getInstance().save(newLic, file); |
|---|
| 120 | 119 | LOG.info("New license file saved as: {}", file.getAbsolutePath()); |
|---|
| 121 | | - System.exit(0); |
|---|
| 122 | | - } |
|---|
| 120 | + System.exit(0); |
|---|
| 121 | + } |
|---|
| 123 | 122 | |
|---|
| 124 | | - } catch (SeCurisException e) { |
|---|
| 125 | | - LOG.error("The command generated an error: {}", e.toString()); |
|---|
| 126 | | - } |
|---|
| 127 | | - } |
|---|
| 123 | + } catch (SeCurisException e) { |
|---|
| 124 | + LOG.error("The command generated an error: {}", e.toString()); |
|---|
| 125 | + } |
|---|
| 126 | + } |
|---|
| 128 | 127 | |
|---|
| 129 | | - private static void checkMandatoryParameter(String value, String param) { |
|---|
| 130 | | - if (value == null) { |
|---|
| 131 | | - LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param); |
|---|
| 132 | | - System.exit(-5); |
|---|
| 133 | | - } |
|---|
| 134 | | - } |
|---|
| 128 | + private static void checkMandatoryParameter(String value, String param) { |
|---|
| 129 | + if (value == null) { |
|---|
| 130 | + LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param); |
|---|
| 131 | + System.exit(-5); |
|---|
| 132 | + } |
|---|
| 133 | + } |
|---|
| 135 | 134 | |
|---|
| 136 | | - /** |
|---|
| 137 | | - * Checks that config file exists and contains mandatory parameters |
|---|
| 138 | | - */ |
|---|
| 139 | | - private static void checkConfigFile() { |
|---|
| 140 | | - String appCode = Params.get(Params.KEYS.APPLICATION_CODE); |
|---|
| 141 | | - if (appCode == null) { |
|---|
| 142 | | - LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.APPLICATION_CODE); |
|---|
| 143 | | - System.exit(-3); |
|---|
| 144 | | - } |
|---|
| 145 | | - String customerCode = Params.get(Params.KEYS.CUSTOMER_CODE); |
|---|
| 146 | | - if (customerCode == null) { |
|---|
| 147 | | - LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE); |
|---|
| 148 | | - System.exit(-4); |
|---|
| 149 | | - } |
|---|
| 150 | | - } |
|---|
| 135 | + /** |
|---|
| 136 | + * Checks that config file exists and contains mandatory parameters |
|---|
| 137 | + */ |
|---|
| 138 | + private static void checkConfigFile() { |
|---|
| 139 | + if (Params.get(Params.KEYS.APPLICATION_CODE) == null) { |
|---|
| 140 | + LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.APPLICATION_CODE); |
|---|
| 141 | + System.exit(-3); |
|---|
| 142 | + } |
|---|
| 143 | + if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) { |
|---|
| 144 | + LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE); |
|---|
| 145 | + System.exit(-4); |
|---|
| 146 | + } |
|---|
| 147 | + if (Params.get(Params.KEYS.PACK_CODE) == null) { |
|---|
| 148 | + LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE); |
|---|
| 149 | + System.exit(-6); |
|---|
| 150 | + } |
|---|
| 151 | + } |
|---|
| 151 | 152 | |
|---|
| 152 | | - private static CommandLine getCommandLine(String[] args) { |
|---|
| 153 | | - Options ops = prepareOptionCLI(); |
|---|
| 154 | | - if (args.length == 0) { |
|---|
| 155 | | - printHelp(ops); |
|---|
| 156 | | - } |
|---|
| 157 | | - CommandLineParser parser = new PosixParser(); |
|---|
| 158 | | - CommandLine cmd = null; |
|---|
| 159 | | - try { |
|---|
| 160 | | - cmd = parser.parse(ops, args); |
|---|
| 161 | | - } catch (ParseException e) { |
|---|
| 162 | | - printHelp(ops); |
|---|
| 163 | | - } |
|---|
| 153 | + private static CommandLine getCommandLine(String[] args) { |
|---|
| 154 | + Options ops = prepareOptionCLI(); |
|---|
| 155 | + if (args.length == 0) { |
|---|
| 156 | + printHelp(ops); |
|---|
| 157 | + } |
|---|
| 158 | + CommandLineParser parser = new PosixParser(); |
|---|
| 159 | + CommandLine cmd = null; |
|---|
| 160 | + try { |
|---|
| 161 | + cmd = parser.parse(ops, args); |
|---|
| 162 | + } catch (ParseException e) { |
|---|
| 163 | + printHelp(ops); |
|---|
| 164 | + } |
|---|
| 164 | 165 | |
|---|
| 165 | | - if (cmd.hasOption('h')) { |
|---|
| 166 | | - printHelp(ops); |
|---|
| 167 | | - } |
|---|
| 166 | + if (cmd.hasOption('h')) { |
|---|
| 167 | + printHelp(ops); |
|---|
| 168 | + } |
|---|
| 168 | 169 | |
|---|
| 169 | | - return cmd; |
|---|
| 170 | | - } |
|---|
| 170 | + return cmd; |
|---|
| 171 | + } |
|---|
| 171 | 172 | |
|---|
| 172 | | - private static void printHelp(Options ops) { |
|---|
| 173 | | - HelpFormatter formatter = new HelpFormatter(); |
|---|
| 174 | | - formatter.printHelp("securis-client", ops, true); |
|---|
| 175 | | - System.exit(-1); |
|---|
| 176 | | - } |
|---|
| 173 | + private static void printHelp(Options ops) { |
|---|
| 174 | + HelpFormatter formatter = new HelpFormatter(); |
|---|
| 175 | + formatter.printHelp("securis-client", ops, true); |
|---|
| 176 | + System.exit(-1); |
|---|
| 177 | + } |
|---|
| 177 | 178 | |
|---|
| 178 | | - @SuppressWarnings("static-access") |
|---|
| 179 | | - private static Options prepareOptionCLI() { |
|---|
| 180 | | - Options options = new Options(); |
|---|
| 179 | + @SuppressWarnings("static-access") |
|---|
| 180 | + private static Options prepareOptionCLI() { |
|---|
| 181 | + Options options = new Options(); |
|---|
| 181 | 182 | |
|---|
| 182 | | - options.addOption("h", "help", false, "Show help."); |
|---|
| 183 | | - options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile").withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r')); |
|---|
| 184 | | - // options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s')); |
|---|
| 185 | | - options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true).create('l')); |
|---|
| 183 | + options.addOption("h", "help", false, "Show help."); |
|---|
| 184 | + options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile") |
|---|
| 185 | + .withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r')); |
|---|
| 186 | + // options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s')); |
|---|
| 187 | + options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true) |
|---|
| 188 | + .create('l')); |
|---|
| 186 | 189 | |
|---|
| 187 | | - options.addOption("g", "gen_request", false, "Generate request file. If --rfile parameter is missing then It is generated in current directory."); |
|---|
| 188 | | - options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.").hasArg(true).hasOptionalArg().create('c')); |
|---|
| 189 | | - options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. "); |
|---|
| 190 | | - options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.").hasArg(true).create('r')); |
|---|
| 190 | + options.addOption("g", "gen_request", false, |
|---|
| 191 | + "Generate request file. If --rfile parameter is missing then It is generated in current directory."); |
|---|
| 192 | + options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.") |
|---|
| 193 | + .hasArg(true).hasOptionalArg().create('c')); |
|---|
| 194 | + options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. "); |
|---|
| 195 | + options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.") |
|---|
| 196 | + .hasArg(true).create('r')); |
|---|
| 191 | 197 | |
|---|
| 192 | | - return options; |
|---|
| 193 | | - } |
|---|
| 198 | + return options; |
|---|
| 199 | + } |
|---|
| 194 | 200 | } |
|---|