1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.sw4j.tool.barcode.random;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.CommandLineParser;
25 import org.apache.commons.cli.DefaultParser;
26 import org.apache.commons.cli.Option;
27 import org.apache.commons.cli.Options;
28 import org.apache.commons.cli.ParseException;
29 import org.sw4j.tool.barcode.random.codedata.FileCodeData;
30 import org.sw4j.tool.barcode.random.config.Config;
31 import org.sw4j.tool.barcode.random.generator.CodeGenerator;
32
33
34
35
36
37
38 public class Main {
39
40
41
42
43
44
45 private static final int EXIT_NO_APP_HOME = -3;
46
47
48
49
50
51
52 private final Logger logger = Logger.getLogger(Main.class.getName());
53
54
55
56
57
58
59 public Main() {
60 }
61
62
63
64
65
66
67
68
69
70
71 public static void main(final String... args) throws IOException {
72 new Main().run(args);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public void run(final String... args) throws IOException {
93 String appHome = System.getProperty("app.home");
94 if (appHome == null) {
95 System.err.println("app.home not set");
96 System.exit(EXIT_NO_APP_HOME);
97 }
98
99 CommandLine cl = parseCommandLine(args);
100
101 File configFile;
102 if (cl.hasOption("c")) {
103 configFile = new File(cl.getOptionValue("c"));
104 } else {
105 String configFileName = "etc/random.yaml";
106 configFile = new File(appHome, configFileName);
107 }
108 Config config = Config.readYamlConfig(configFile);
109 CodeGenerator generator;
110 if (config.getPredefined() == null) {
111 generator = new CodeGenerator(config.getRandom(), new FileCodeData(config));
112 } else {
113 generator = new CodeGenerator(config.getPredefined(), new FileCodeData(config));
114 }
115
116 generator.createCodes();
117
118 System.out.println("End");
119 }
120
121
122
123
124
125
126
127
128
129 private CommandLine parseCommandLine(final String... args) {
130 Options clo = new Options();
131
132 clo.addOption(Option
133 .builder("c")
134 .longOpt("config")
135 .hasArg(true)
136 .required(false)
137 .argName("config-file")
138 .desc("The config file to use.")
139 .build());
140
141 CommandLineParser clp = new DefaultParser();
142 CommandLine cl = new CommandLine.Builder().build();
143 try {
144 cl = clp.parse(clo, args, true);
145 } catch (ParseException pex) {
146 logger.log(Level.WARNING, "Problems while parsing the command line", pex);
147 }
148
149 return cl;
150 }
151
152 }