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