| .. | .. |
|---|
| 41 | 41 | */ |
|---|
| 42 | 42 | public class License { |
|---|
| 43 | 43 | |
|---|
| 44 | | - private static final Logger LOG = LogManager.getLogger(License.class); |
|---|
| 44 | + private static final Logger LOG = LogManager.getLogger(License.class); |
|---|
| 45 | 45 | |
|---|
| 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 { |
|---|
| 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 { |
|---|
| 53 | 53 | |
|---|
| 54 | | - LOG.debug("SeCuris client tool init "); |
|---|
| 54 | + LOG.debug("SeCuris client tool init "); |
|---|
| 55 | 55 | |
|---|
| 56 | | - checkConfigFile(); |
|---|
| 57 | | - CommandLine cmd = getCommandLine(args); |
|---|
| 56 | + checkConfigFile(); |
|---|
| 57 | + CommandLine cmd = getCommandLine(args); |
|---|
| 58 | 58 | |
|---|
| 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 | | - } |
|---|
| 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 | + } |
|---|
| 69 | 69 | |
|---|
| 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 | | - } |
|---|
| 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 | + } |
|---|
| 85 | 85 | |
|---|
| 86 | | - System.exit(0); |
|---|
| 87 | | - } |
|---|
| 86 | + System.exit(0); |
|---|
| 87 | + } |
|---|
| 88 | 88 | |
|---|
| 89 | | - if (cmd.hasOption('c')) { |
|---|
| 90 | | - SignedLicenseBean lic = LicenseManager.getInstance().requestLicense(); |
|---|
| 91 | | - String filename = cmd.getOptionValue("c"); |
|---|
| 92 | | - if (filename == null) |
|---|
| 93 | | - filename = "./license.lic"; |
|---|
| 94 | | - File file = new File(filename); |
|---|
| 95 | | - LicenseManager.getInstance().save(lic, file); |
|---|
| 96 | | - LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath()); |
|---|
| 97 | | - System.exit(0); |
|---|
| 98 | | - } |
|---|
| 89 | + if (cmd.hasOption('c')) { |
|---|
| 90 | + // TODO: Change CLI to get name and email in license request |
|---|
| 91 | + SignedLicenseBean lic = LicenseManager.getInstance().requestLicense(null, null); |
|---|
| 92 | + String filename = cmd.getOptionValue("c"); |
|---|
| 93 | + if (filename == null) { |
|---|
| 94 | + filename = "./license.lic"; |
|---|
| 95 | + } |
|---|
| 96 | + File file = new File(filename); |
|---|
| 97 | + LicenseManager.getInstance().save(lic, file); |
|---|
| 98 | + LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath()); |
|---|
| 99 | + System.exit(0); |
|---|
| 100 | + } |
|---|
| 99 | 101 | |
|---|
| 100 | | - if (cmd.hasOption('t')) { |
|---|
| 101 | | - LicenseManager.getInstance().testServer(); |
|---|
| 102 | | - LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL)); |
|---|
| 102 | + if (cmd.hasOption('t')) { |
|---|
| 103 | + LicenseManager.getInstance().testServer(); |
|---|
| 104 | + LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL)); |
|---|
| 103 | 105 | |
|---|
| 104 | | - System.exit(0); |
|---|
| 105 | | - } |
|---|
| 106 | + System.exit(0); |
|---|
| 107 | + } |
|---|
| 106 | 108 | |
|---|
| 107 | | - if (cmd.hasOption('r')) { |
|---|
| 108 | | - String licFilename = cmd.getOptionValue("renew"); |
|---|
| 109 | | - checkMandatoryParameter(licFilename, "renew"); |
|---|
| 110 | | - File file = new File(licFilename); |
|---|
| 111 | | - if (!file.exists()) { |
|---|
| 112 | | - throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath()); |
|---|
| 113 | | - } |
|---|
| 114 | | - SignedLicenseBean newLic = LicenseManager.getInstance().renew(file); |
|---|
| 115 | | - File oldLicFile = new File(file.getAbsoluteFile() + ".old"); |
|---|
| 116 | | - file.renameTo(oldLicFile); |
|---|
| 117 | | - LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath()); |
|---|
| 118 | | - LicenseManager.getInstance().save(newLic, file); |
|---|
| 119 | | - LOG.info("New license file saved as: {}", file.getAbsolutePath()); |
|---|
| 120 | | - System.exit(0); |
|---|
| 121 | | - } |
|---|
| 109 | + if (cmd.hasOption('r')) { |
|---|
| 110 | + String licFilename = cmd.getOptionValue("renew"); |
|---|
| 111 | + checkMandatoryParameter(licFilename, "renew"); |
|---|
| 112 | + File file = new File(licFilename); |
|---|
| 113 | + if (!file.exists()) { |
|---|
| 114 | + throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath()); |
|---|
| 115 | + } |
|---|
| 116 | + SignedLicenseBean newLic = LicenseManager.getInstance().renew(file); |
|---|
| 117 | + File oldLicFile = new File(file.getAbsoluteFile() + ".old"); |
|---|
| 118 | + file.renameTo(oldLicFile); |
|---|
| 119 | + LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath()); |
|---|
| 120 | + LicenseManager.getInstance().save(newLic, file); |
|---|
| 121 | + LOG.info("New license file saved as: {}", file.getAbsolutePath()); |
|---|
| 122 | + System.exit(0); |
|---|
| 123 | + } |
|---|
| 122 | 124 | |
|---|
| 123 | | - } catch (SeCurisException e) { |
|---|
| 124 | | - LOG.error("The command generated an error: {}", e.toString()); |
|---|
| 125 | | - } |
|---|
| 126 | | - } |
|---|
| 125 | + } catch (SeCurisException e) { |
|---|
| 126 | + LOG.error("The command generated an error: {}", e.toString()); |
|---|
| 127 | + } |
|---|
| 128 | + } |
|---|
| 127 | 129 | |
|---|
| 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 | | - } |
|---|
| 130 | + private static void checkMandatoryParameter(String value, String param) { |
|---|
| 131 | + if (value == null) { |
|---|
| 132 | + LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param); |
|---|
| 133 | + System.exit(-5); |
|---|
| 134 | + } |
|---|
| 135 | + } |
|---|
| 134 | 136 | |
|---|
| 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 | | - } |
|---|
| 137 | + /** |
|---|
| 138 | + * Checks that config file exists and contains mandatory parameters |
|---|
| 139 | + */ |
|---|
| 140 | + private static void checkConfigFile() { |
|---|
| 141 | + if (Params.get(Params.KEYS.APPLICATION_CODE) == null) { |
|---|
| 142 | + LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.APPLICATION_CODE); |
|---|
| 143 | + System.exit(-3); |
|---|
| 144 | + } |
|---|
| 145 | + if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) { |
|---|
| 146 | + LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE); |
|---|
| 147 | + System.exit(-4); |
|---|
| 148 | + } |
|---|
| 149 | + if (Params.get(Params.KEYS.PACK_CODE) == null) { |
|---|
| 150 | + LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE); |
|---|
| 151 | + System.exit(-6); |
|---|
| 152 | + } |
|---|
| 153 | + } |
|---|
| 152 | 154 | |
|---|
| 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 | | - } |
|---|
| 155 | + private static CommandLine getCommandLine(String[] args) { |
|---|
| 156 | + Options ops = prepareOptionCLI(); |
|---|
| 157 | + if (args.length == 0) { |
|---|
| 158 | + printHelp(ops); |
|---|
| 159 | + } |
|---|
| 160 | + CommandLineParser parser = new PosixParser(); |
|---|
| 161 | + CommandLine cmd = null; |
|---|
| 162 | + try { |
|---|
| 163 | + cmd = parser.parse(ops, args); |
|---|
| 164 | + } catch (ParseException e) { |
|---|
| 165 | + printHelp(ops); |
|---|
| 166 | + } |
|---|
| 165 | 167 | |
|---|
| 166 | | - if (cmd.hasOption('h')) { |
|---|
| 167 | | - printHelp(ops); |
|---|
| 168 | | - } |
|---|
| 168 | + if (cmd.hasOption('h')) { |
|---|
| 169 | + printHelp(ops); |
|---|
| 170 | + } |
|---|
| 169 | 171 | |
|---|
| 170 | | - return cmd; |
|---|
| 171 | | - } |
|---|
| 172 | + return cmd; |
|---|
| 173 | + } |
|---|
| 172 | 174 | |
|---|
| 173 | | - private static void printHelp(Options ops) { |
|---|
| 174 | | - HelpFormatter formatter = new HelpFormatter(); |
|---|
| 175 | | - formatter.printHelp("securis-client", ops, true); |
|---|
| 176 | | - System.exit(-1); |
|---|
| 177 | | - } |
|---|
| 175 | + private static void printHelp(Options ops) { |
|---|
| 176 | + HelpFormatter formatter = new HelpFormatter(); |
|---|
| 177 | + formatter.printHelp("securis-client", ops, true); |
|---|
| 178 | + System.exit(-1); |
|---|
| 179 | + } |
|---|
| 178 | 180 | |
|---|
| 179 | | - @SuppressWarnings("static-access") |
|---|
| 180 | | - private static Options prepareOptionCLI() { |
|---|
| 181 | | - Options options = new Options(); |
|---|
| 181 | + @SuppressWarnings("static-access") |
|---|
| 182 | + private static Options prepareOptionCLI() { |
|---|
| 183 | + Options options = new Options(); |
|---|
| 182 | 184 | |
|---|
| 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')); |
|---|
| 185 | + options.addOption("h", "help", false, "Show help."); |
|---|
| 186 | + options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile") |
|---|
| 187 | + .withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r')); |
|---|
| 188 | + // options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s')); |
|---|
| 189 | + options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true) |
|---|
| 190 | + .create('l')); |
|---|
| 189 | 191 | |
|---|
| 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')); |
|---|
| 192 | + options.addOption("g", "gen_request", false, |
|---|
| 193 | + "Generate request file. If --rfile parameter is missing then It is generated in current directory."); |
|---|
| 194 | + options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.") |
|---|
| 195 | + .hasArg(true).hasOptionalArg().create('c')); |
|---|
| 196 | + options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. "); |
|---|
| 197 | + options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.") |
|---|
| 198 | + .hasArg(true).create('r')); |
|---|
| 197 | 199 | |
|---|
| 198 | | - return options; |
|---|
| 199 | | - } |
|---|
| 200 | + return options; |
|---|
| 201 | + } |
|---|
| 200 | 202 | |
|---|
| 201 | 203 | } |
|---|