9052734734708eb4b14854fd161dbac3d71860ab
[squelch.git] / src / main / java / net / jaekl / squelch / Args.java
1 package net.jaekl.squelch;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.PrintWriter;
7 import java.util.Properties;
8
9 import net.jaekl.squelch.util.FileUtil;
10
11 import org.apache.commons.cli.CommandLine;
12 import org.apache.commons.cli.DefaultParser;
13 import org.apache.commons.cli.HelpFormatter;
14 import org.apache.commons.cli.Options;
15 import org.apache.commons.cli.ParseException;
16
17 public class Args {
18         private static final String DB_URL = "db.url";
19         private static final String DB_PASSWORD = "db.password";
20         private static final String DB_USER = "db.user";
21         private static final String DEFAULT_CONFIG_FILENAME = "config.properties";
22         
23         private String m_className;     // name of the main class that launches this program
24         private boolean m_help;
25         private String m_url;
26         private String m_pass;
27         private String m_user;
28         
29         public Args(String className) {
30                 m_className = className;
31                 m_help = false;
32                 m_url = "";
33                 m_pass = "";
34                 m_user = "";
35         }
36         
37         public boolean isHelp() { return m_help; }
38         public String getUrl() { return m_url; }
39         public String getPass() { return m_pass; }
40         public String getUser() { return m_user; }
41         
42         public Options createOptions() 
43         {
44                 Options opt = new Options();
45
46                 opt.addOption("c",  "config",      true,  "Config file");
47                 opt.addOption("h",  "help",        false, "Show usage information");
48                 opt.addOption("r",  "url",         true,  "DB JDBC URL");
49                 opt.addOption("p",  "password",    true,  "DB password");
50                 opt.addOption("u",  "user",        true,  "DB username");
51                 
52                 return opt;
53         }
54
55         public boolean parseArgs(PrintWriter pw, String[] args) throws IOException 
56         {
57                 Options opt = createOptions();
58                 try {
59                         CommandLine line = new DefaultParser().parse(opt, args);
60         
61                         String configFilename = (line.hasOption("c")) ? (line.getOptionValue("c")) : (DEFAULT_CONFIG_FILENAME);
62                         File configFile = FileUtil.getInst().newFile(configFilename);
63                         if (configFile.canRead()) {
64                                 readConfigFile(configFile);
65                         }
66                         
67                         m_help   = line.hasOption("h");
68
69                         if (line.hasOption("p")) {
70                                 m_pass = line.getOptionValue("p");
71                         }
72                         if (line.hasOption("r")) {
73                                 m_url = line.getOptionValue("r");
74                         }
75                         if (line.hasOption("u")) {
76                                 m_user = line.getOptionValue("u");
77                         }
78                 }
79                 catch (ParseException exc) {
80                         usage(pw, opt);
81                         return false;
82                 }
83                 
84                 if (m_help) {
85                         usage(pw, opt);
86                 }
87                 
88                 return true;
89         }
90         
91         void readConfigFile(File configFile) throws IOException
92         {
93                 Properties props = new Properties();            
94                 try (InputStream is = FileUtil.getInst().openStream(configFile)) {
95                         props.load(is);
96                 }
97                 
98                 m_url = props.getProperty(DB_URL, "");
99                 m_pass = props.getProperty(DB_PASSWORD, "");
100                 m_user = props.getProperty(DB_USER, "");
101         }
102         
103         // Note that this leverages commons-cli's HelpFormatter to 
104         // generate the usage message.  It will always be in English.
105         // If we want to localize that, we'd need to recode this,
106         // and also translate the parameter descriptions in 
107         // createOptions().
108         void usage(PrintWriter pw, Options opt) 
109         {
110                 HelpFormatter help = new HelpFormatter();
111                 help.printHelp(pw, 80, m_className, "", opt, 0, 0, "", true);
112         }
113 }