View Javadoc
1   /*
2    * Copyright (C) 2019 sw4j.org
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   * This is the main class of the random barcode generator.
35   *
36   * @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
37   */
38  public class Main {
39  
40      /**
41       * <p>
42       * Exit code of the application when the {@code app.home} system property is not set.
43       * </p>
44       */
45      private static final int EXIT_NO_APP_HOME = -3;
46  
47      /**
48       * <p>
49       * The logger of this class.
50       * </p>
51       */
52      private final Logger logger = Logger.getLogger(Main.class.getName());
53  
54      /**
55       * <p>
56       * The sole constructor of this class.
57       * </p>
58       */
59      public Main() {
60      }
61  
62      /**
63       * <p>
64       * The main method to start the application.
65       * </p>
66       *
67       * @TODO handle the IOException internally
68       * @param args the command line arguments.
69       * @throws IOException forwarded exception.
70       */
71      public static void main(final String... args) throws IOException {
72          new Main().run(args);
73      }
74  
75      /**
76       * <p>
77       * Run the application with the given command line arguments.
78       * </p>
79       * <p>
80       * The application uses the system property {@code app.home} to determine the home folder of the application and
81       * resolve the default configuration file relative to this folder.
82       * </p>
83       * <p>
84       * This method needs the system property {@code app.home} to be set. If the system property is not set then the
85       * application is terminated with exit code {@code -3}.
86       * </p>
87       *
88       * @TODO handle the IOException internally
89       * @param args the command line arguments.
90       * @throws IOException forwarded exception
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      * <p>
123      * Parses the command line and returns the parsed command line.
124      * </p>
125      *
126      * @param args the command line arguments.
127      * @return the parsed command line.
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 }