Store db parameters in Config object, not in CFB object.
[cfb.git] / prod / net / jaekl / cfb / Config.java
1 package net.jaekl.cfb;
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.ArrayList;
8 import java.util.Arrays;
9 import java.util.List;
10 import java.util.Properties;
11
12 import net.jaekl.qd.util.FileIO;
13
14 import org.apache.commons.cli.CommandLine;
15 import org.apache.commons.cli.GnuParser;
16 import org.apache.commons.cli.Options;
17 import org.apache.commons.cli.ParseException;
18
19 public class Config {
20         private static final String DB_HOST = "db.host";
21         private static final String DB_NAME = "db.name";
22         private static final String DB_PASS = "db.pass";
23         private static final String DB_PORT = "db.port";
24         private static final String DB_USER = "db.user";
25         private static final String FINDBUGS_HOME = "FindBugsHome";
26         private static final String MAIL_FROM = "mail.from";
27         private static final String MAIL_SMTP_HOST = "mail.smtp.host";
28         private static final String NOTIFY = "notify";
29         
30         File m_configProperties;
31
32         String m_dbName; // db name
33         File m_fbp;             // FindBugsProject file
34         File m_fbDir;   // Directory where FindBugs is installed
35         String m_host;  // db host
36         int m_port;             // db port
37         String m_user;  // db user
38         String m_pass;  // db password
39         String m_buildNum; // build number (version)
40         boolean m_removeSchema; // purge DB schema
41         File m_output;  // File to which we should write our output (report)
42         
43         String       m_mailFrom;
44         String       m_mailSmtpHost;
45         List<String> m_notify;
46         
47         Options m_options;
48         
49         public Config() {
50                 m_dbName = "CFB";
51                 m_fbp    = null;
52                 m_fbDir  = null;
53                 m_host = "localhost";
54                 m_port = 5432;
55                 m_pass = "";
56                 m_user = "user";
57                 m_buildNum = null;
58                 m_removeSchema = false;
59                 m_output = null;
60                 
61                 m_mailFrom = "findbugs@localhost";
62                 m_mailSmtpHost = "localhost";
63                 m_notify = new ArrayList<String>();
64                 
65                 m_options = createOptions();
66         }
67         
68         public String getMailFrom() { return m_mailFrom; }
69         public String getMailSmtpHost() { return m_mailSmtpHost; }
70         public ArrayList<String> getNotify() { return new ArrayList<String>(m_notify); }
71         
72         public String getDbHost() { return m_host; }
73         public int    getDbPort() { return m_port; }
74         public String getDbName() { return m_dbName; }
75         public String getDbUser() { return m_user; }
76         public String getDbPass() { return m_pass; }
77         
78         public void setDbHost(String value) { m_host = value; }
79         public void setDbPort(int value)    { m_port = value; }
80         public void setDbName(String value) { m_dbName = value; }
81         public void setDbUser(String value) { m_user = value; }
82         public void setDbPass(String value) { m_pass = value; }
83         
84         public void readFile(File configProperties) throws IOException
85         {
86                 Properties props = new Properties();
87                 InputStream is = null;
88                 
89                 try {
90                         is = FileIO.getInst().openInput(configProperties);
91                         props.load(is);
92                 }
93                 finally {
94                         if (null != is) {
95                                 is.close();
96                         }
97                 }
98                 
99                 if (props.containsKey(DB_HOST)) {
100                         m_host = props.getProperty(DB_HOST);
101                 }
102                 if (props.containsKey(DB_NAME)) {
103                         m_dbName = props.getProperty(DB_NAME);
104                 }
105                 if (props.containsKey(DB_PASS)) {
106                         m_pass = props.getProperty(DB_PASS);
107                 }
108                 if (props.containsKey(DB_PORT)) {
109                         m_port = Integer.parseInt(props.getProperty(DB_PORT));
110                 }
111                 if (props.containsKey(DB_USER)) {
112                         m_user = props.getProperty(DB_USER);
113                 }
114                 if (props.containsKey(FINDBUGS_HOME)) {
115                         m_fbDir = new File(props.getProperty(FINDBUGS_HOME));
116                 }
117                 if (props.containsKey(MAIL_FROM)) {
118                         m_mailFrom = props.getProperty(MAIL_FROM);
119                 }
120                 if (props.containsKey(MAIL_SMTP_HOST)) {
121                         m_mailSmtpHost = props.getProperty(MAIL_SMTP_HOST);
122                 }
123                 if (props.containsKey(NOTIFY)) {
124                         String[] addresses = props.getProperty(NOTIFY).split(",");
125                         m_notify = Arrays.asList(addresses);
126                 }
127         }
128         
129         public Options createOptions() {
130                 Options opt = new Options();
131                 
132                 opt.addOption("c",  "config",      true,  "Properties configuration file");
133                 opt.addOption("d",  "dbname",      true,  "DB name");
134                 opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
135                 opt.addOption("f",  "fbp",         true,  "FindBugsProject file");
136                 opt.addOption("h",  "host",        true,  "DB hostname");
137                 opt.addOption("n",  "number",      true,  "Build number (version)");
138                 opt.addOption("o",  "outfile",     true,  "Output report filename");
139                 opt.addOption("p",  "pass",        true,  "DB password");
140                 opt.addOption("t",  "port",        true,  "DB port");
141                 opt.addOption("u",  "user",        true,  "DB username");
142                 
143                 return opt;
144         }
145         
146         public void parseArgs(PrintWriter pw, String[] args) throws ParseException
147         {
148                 assert(null != m_options);
149                 
150                 CommandLine line = new GnuParser().parse(m_options, args);
151                 if (line.hasOption("c")) {
152                         m_configProperties = new File(line.getOptionValue("c"));
153                 }
154                 if (line.hasOption("d")) {
155                         m_dbName = line.getOptionValue("d");
156                 }
157                 if (line.hasOption("f")) {
158                         m_fbp = new File(line.getOptionValue("f"));
159                 }
160                 if (line.hasOption("h")) {
161                         m_host = line.getOptionValue("h");
162                 }
163                 if (line.hasOption("n")) {
164                         m_buildNum = line.getOptionValue("n");
165                 }
166                 if (line.hasOption("o")) {
167                         m_output = new File(line.getOptionValue("o"));
168                 }
169                 if (line.hasOption("p")) {
170                         m_pass = line.getOptionValue("p");
171                 }
172                 m_removeSchema = line.hasOption("drop-tables");
173                 if (line.hasOption("t")) {
174                         m_port = Integer.parseInt(line.getOptionValue("t"));
175                 }
176                 if (line.hasOption("u")) {
177                         m_user = line.getOptionValue("u");
178                 }
179         }
180 }