Add email notifications.
[cfb.git] / prod / net / jaekl / cfb / Config.java
diff --git a/prod/net/jaekl/cfb/Config.java b/prod/net/jaekl/cfb/Config.java
new file mode 100644 (file)
index 0000000..8bb401d
--- /dev/null
@@ -0,0 +1,146 @@
+package net.jaekl.cfb;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.GnuParser;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+
+public class Config {
+       private static final String FINDBUGS_HOME = "FindBugsHome";
+       private static final String MAIL_FROM = "mail.from";
+       private static final String MAIL_SMTP_HOST = "mail.smtp.host";
+       private static final String NOTIFY = "notify";
+       
+       File m_configProperties;
+
+       String m_dbName; // db name
+       File m_fbp;             // FindBugsProject file
+       File m_fbDir;   // Directory where FindBugs is installed
+       String m_host;  // db host
+       int m_port;             // db port
+       String m_user;  // db user
+       String m_pass;  // db password
+       String m_buildNum; // build number (version)
+       boolean m_removeSchema; // purge DB schema
+       File m_output;  // File to which we should write our output (report)
+       
+       String       m_mailFrom;
+       String       m_mailSmtpHost;
+       List<String> m_notify;
+       
+       Options m_options;
+       
+       public Config() {
+               m_dbName = "CFB";
+               m_fbp    = null;
+               m_fbDir  = null;
+               m_host = "localhost";
+               m_port = 5432;
+               m_pass = "";
+               m_user = "user";
+               m_buildNum = null;
+               m_removeSchema = false;
+               m_output = null;
+               
+               m_mailFrom = "findbugs@localhost";
+               m_mailSmtpHost = "localhost";
+               m_notify = new ArrayList<String>();
+               
+               m_options = createOptions();
+       }
+       
+       public String getMailFrom() { return m_mailFrom; }
+       public String getMailSmtpHost() { return m_mailSmtpHost; }
+       public ArrayList<String> getNotify() { return new ArrayList<String>(m_notify); }
+       
+       public void readFile(File configProperties) throws IOException
+       {
+               Properties props = new Properties();
+               FileInputStream fis = null;
+               
+               try {
+                       fis = new FileInputStream(configProperties);
+                       props.load(fis);
+               }
+               finally {
+                       if (null != fis) {
+                               fis.close();
+                       }
+               }
+               
+               if (props.containsKey(FINDBUGS_HOME)) {
+                       m_fbDir = new File(props.getProperty(FINDBUGS_HOME));
+               }
+               if (props.containsKey(MAIL_FROM)) {
+                       m_mailFrom = props.getProperty(MAIL_FROM);
+               }
+               if (props.containsKey(MAIL_SMTP_HOST)) {
+                       m_mailSmtpHost = props.getProperty(MAIL_SMTP_HOST);
+               }
+               if (props.containsKey(NOTIFY)) {
+                       String[] addresses = props.getProperty(NOTIFY).split(",");
+                       m_notify = Arrays.asList(addresses);
+               }
+       }
+       
+       public Options createOptions() {
+               Options opt = new Options();
+               
+               opt.addOption("c",  "config",      true,  "Properties configuration file");
+               opt.addOption("d",  "dbname",      true,  "DB name");
+               opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
+               opt.addOption("f",  "fbp",         true,  "FindBugsProject file");
+               opt.addOption("h",  "host",        true,  "DB hostname");
+               opt.addOption("n",  "number",      true,  "Build number (version)");
+               opt.addOption("o",  "outfile",     true,  "Output report filename");
+               opt.addOption("p",  "pass",        true,  "DB password");
+               opt.addOption("t",  "port",        true,  "DB port");
+               opt.addOption("u",  "user",        true,  "DB username");
+               
+               return opt;
+       }
+       
+       public void parseArgs(PrintWriter pw, String[] args) throws ParseException
+       {
+               assert(null != m_options);
+               
+               CommandLine line = new GnuParser().parse(m_options, args);
+               if (line.hasOption("c")) {
+                       m_configProperties = new File(line.getOptionValue("c"));
+               }
+               if (line.hasOption("d")) {
+                       m_dbName = line.getOptionValue("d");
+               }
+               if (line.hasOption("f")) {
+                       m_fbp = new File(line.getOptionValue("f"));
+               }
+               if (line.hasOption("h")) {
+                       m_host = line.getOptionValue("h");
+               }
+               if (line.hasOption("n")) {
+                       m_buildNum = line.getOptionValue("n");
+               }
+               if (line.hasOption("o")) {
+                       m_output = new File(line.getOptionValue("o"));
+               }
+               if (line.hasOption("p")) {
+                       m_pass = line.getOptionValue("p");
+               }
+               m_removeSchema = line.hasOption("drop-tables");
+               if (line.hasOption("t")) {
+                       m_port = Integer.parseInt(line.getOptionValue("t"));
+               }
+               if (line.hasOption("u")) {
+                       m_user = line.getOptionValue("u");
+               }
+       }
+}