rsanchez
2015-09-22 7d8713e88c7872b195f7e4d02ac4812536ae196b
src/main/java/net/curisit/securis/License.java
....@@ -41,163 +41,166 @@
4141 */
4242 public class License {
4343
44
- private static final Logger LOG = LogManager.getLogger(License.class);
44
+ private static final Logger LOG = LogManager.getLogger(License.class);
4545
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 {
5353
54
- LOG.debug("SeCuris client tool init ");
54
+ LOG.debug("SeCuris client tool init ");
5555
56
- checkConfigFile();
57
- CommandLine cmd = getCommandLine(args);
56
+ checkConfigFile();
57
+ CommandLine cmd = getCommandLine(args);
5858
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
+ }
6969
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
+ }
8585
86
- System.exit(0);
87
- }
86
+ System.exit(0);
87
+ }
8888
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
- }
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
+ }
101101
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));
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));
105105
106
- System.exit(0);
107
- }
106
+ System.exit(0);
107
+ }
108108
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
- }
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
+ }
124124
125
- } catch (SeCurisException e) {
126
- LOG.error("The command generated an error: {}", e.toString());
127
- }
128
- }
125
+ } catch (SeCurisException e) {
126
+ LOG.error("The command generated an error: {}", e.toString());
127
+ }
128
+ }
129129
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
- }
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
+ }
136136
137
- /**
138
- * Checks that config file exists and contains mandatory parameters
139
- */
140
- private static void checkConfigFile() {
141
- if (Params.get(Params.KEYS.LIC_TYPE_CODE) == null && Params.get(Params.KEYS.APPLICATION_CODE) == null) {
142
- LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.LIC_TYPE_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
- }
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) {
154142
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
- }
143
+ if (Params.get(Params.KEYS.LIC_TYPE_CODE) == null) {
144
+ LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.LIC_TYPE_CODE);
145
+ System.exit(-3);
146
+ }
147
+ if (Params.get(Params.KEYS.CUSTOMER_CODE) == null) {
148
+ LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.CUSTOMER_CODE);
149
+ System.exit(-4);
150
+ }
151
+ if (Params.get(Params.KEYS.PACK_CODE) == null) {
152
+ LOG.error("Manadatory parameter {} is not set in config file", Params.KEYS.PACK_CODE);
153
+ System.exit(-6);
154
+ }
155
+ } // else: The license will be got using activationCode
156
+ }
167157
168
- if (cmd.hasOption('h')) {
169
- printHelp(ops);
170
- }
158
+ private static CommandLine getCommandLine(String[] args) {
159
+ Options ops = prepareOptionCLI();
160
+ if (args.length == 0) {
161
+ printHelp(ops);
162
+ }
163
+ CommandLineParser parser = new PosixParser();
164
+ CommandLine cmd = null;
165
+ try {
166
+ cmd = parser.parse(ops, args);
167
+ } catch (ParseException e) {
168
+ printHelp(ops);
169
+ }
171170
172
- return cmd;
173
- }
171
+ if (cmd.hasOption('h')) {
172
+ printHelp(ops);
173
+ }
174174
175
- private static void printHelp(Options ops) {
176
- HelpFormatter formatter = new HelpFormatter();
177
- formatter.printHelp("securis-client", ops, true);
178
- System.exit(-1);
179
- }
175
+ return cmd;
176
+ }
180177
181
- @SuppressWarnings("static-access")
182
- private static Options prepareOptionCLI() {
183
- Options options = new Options();
178
+ private static void printHelp(Options ops) {
179
+ HelpFormatter formatter = new HelpFormatter();
180
+ formatter.printHelp("securis-client", ops, true);
181
+ System.exit(-1);
182
+ }
184183
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'));
184
+ @SuppressWarnings("static-access")
185
+ private static Options prepareOptionCLI() {
186
+ Options options = new Options();
191187
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'));
188
+ options.addOption("h", "help", false, "Show help.");
189
+ options.addOption(OptionBuilder.withArgName("req_file").withLongOpt("rfile")
190
+ .withDescription("Set request file for its generation or for license requesting.").hasArg(true).create('r'));
191
+ // options.addOption(OptionBuilder.withArgName("url_license_server").withLongOpt("server").withDescription("License server url.").hasArg(true).create('s'));
192
+ options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true)
193
+ .create('l'));
199194
200
- return options;
201
- }
195
+ options.addOption("g", "gen_request", false,
196
+ "Generate request file. If --rfile parameter is missing then It is generated in current directory.");
197
+ options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.")
198
+ .hasArg(true).hasOptionalArg().create('c'));
199
+ options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");
200
+ options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.")
201
+ .hasArg(true).create('r'));
202
+
203
+ return options;
204
+ }
202205
203206 }