rsanchez
2014-09-29 464f9fc6047ff994728acf69f6f0d3971984b492
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package net.curisit.securis;
import java.io.File;
import java.net.URISyntaxException;
import net.curisit.securis.beans.SignedLicenseBean;
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;
import org.codehaus.jackson.map.DeserializerFactory.Config;
/**
 * Main class when SeCuris client is used from command line.
 * 
 * <pre>
 * usage: securis-client [-c] [-g] [-h] [-l <lic_file>] [-r <lic_file>] [-s
 *       <url_license_server>] [-t]
 * -c,--create                        Request a license file from server.
 *                                    --rfile and --server parameters are
 *                                    mandatory.
 * -g,--gen_request                   Generate request file. If --rfile
 *                                     parameter is missing then It is
 *                                     generated in current directory.
 *  -h,--help                          Show help.
 *  -l,--validate <lic_file>           Validate lic file.
 *  -r,--renew <lic_file>              Synchronize/renew the current license
 *                                     file. --server parameter is mandatory.
 *  -s,--server <url_license_server>   License server url.
 *  -t,--test_lc                       Test if License Server (LC) is
 * </pre>
 * 
 * @author roberto <roberto.sanchez@curisit.net>
 * 
 */
public class License {
    private static final Logger LOG = LogManager.getLogger(License.class);
   /**
    * Aplication entry point when it used as CLI
    * 
    * @param args
    * @throws URISyntaxException
    */
   public static void main(String[] args) throws URISyntaxException {
       LOG.debug("SeCuris client tool init ");
       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());
                   LOG.info("Reason: {}", e.toString());
               }
               System.exit(0);
           }
           if (cmd.hasOption('c')) {
               SignedLicenseBean lic = LicenseManager.getInstance().requestLicense();
               String filename = cmd.getOptionValue("lic_file");
                if (filename == null)
                    filename = "./license.lic";
                File file = new File(filename);
                LicenseManager.getInstance().save(lic, file);
                LOG.info("License file sucessfully saved in file: {}", file.getAbsolutePath());
                System.exit(0);
           }
           if (cmd.hasOption('t')) {
                LicenseManager.getInstance().testServer();
                LOG.info("Server is OK, url: {}", Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL));
                System.exit(0);
           }
           if (cmd.hasOption('r')) {
               String licFilename = cmd.getOptionValue("renew");
               checkMandatoryParameter(licFilename, "renew");
               LOG.warn("This command is not yet implemented");
               System.exit(0);
           }
       } catch (SeCurisException e) {
           LOG.error("The command generated an error: {}", e.toString());
       }
   }
   private static void checkMandatoryParameter(String value, String param) {
       if (value == null) {
           LOG.error("Parameter {} is mandatory. Use --help to get information about parameters", param);
           System.exit(-5);
       }
   }
   /**
    * 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(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file to server.").hasArg(true).hasOptionalArg().create('c'));
       options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");
       options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.").hasArg(true).create('r'));
       return options;
   }
}