DM_DEFAULT_ENCODING: Be explicit that we want the system default encoding.
[cfb.git] / prod / net / jaekl / cfb / CFB.java
index 94ef08f8a996cecf7ce681574b95bc3fd1ad62f9..c088ca8d818aa3fa6574302e54129b275f51eb84 100644 (file)
@@ -9,7 +9,9 @@ package net.jaekl.cfb;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.nio.charset.Charset;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.text.MessageFormat;
@@ -21,6 +23,7 @@ import net.jaekl.cfb.analyze.Analyzer;
 import net.jaekl.cfb.analyze.Delta;
 import net.jaekl.cfb.analyze.HtmlReport;
 import net.jaekl.cfb.analyze.MessageMap;
+import net.jaekl.cfb.analyze.Notifier;
 import net.jaekl.cfb.db.CfbSchema;
 import net.jaekl.cfb.db.TypeMismatchException;
 import net.jaekl.cfb.db.driver.DbDriver;
@@ -41,7 +44,10 @@ public class CFB {
        CfbBundle m_bundle;     
        Locale m_locale;
        
+       Config m_config;
+       
        // Command-line parameters
+       File m_configFile;
        String m_dbName; // db name
        File m_fbp;             // FindBugsProject file
        File m_fbDir;   // Directory where FindBugs is installed
@@ -58,7 +64,9 @@ public class CFB {
                m_schema = new CfbSchema(m_driver);
                m_locale = locale;
                m_bundle = CfbBundle.getInst(m_locale);
+               m_config = new Config();
                
+               m_configFile = new File("config.properties");
                m_dbName = "CFB";
                m_fbp    = null;
                m_fbDir  = null;
@@ -74,6 +82,7 @@ public class CFB {
        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");
@@ -92,6 +101,9 @@ public class CFB {
                
                try {
                        CommandLine line = new GnuParser().parse(opt, args);
+                       if (line.hasOption("c")) {
+                               m_configFile = new File(line.getOptionValue("c"));
+                       }
                        if (line.hasOption("d")) {
                                m_dbName = line.getOptionValue("d");
                        }
@@ -158,13 +170,20 @@ public class CFB {
                if (null != findBugsDir) {
                        m_fbDir = new File(findBugsDir);
                }
-       } 
+       }
+       
+       void readConfig() throws IOException {
+               if (null != m_configFile) {
+                       m_config.readFile(m_configFile);
+               }
+       }
        
        void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException, TypeMismatchException {
                initArgs();     // read environment and system properties
                if ( ! parseArgs(pw, args) ) {
                        return;
                }
+               readConfig();
 
                File findBugsDir = getFindBugsDir();
                File workDir = new File(".");
@@ -200,8 +219,13 @@ public class CFB {
                        Analysis prior = store.getPrior(analysis);
                        Delta delta = new Delta(prior, analysis);
 
-                       HtmlReport report = new HtmlReport(m_bundle, messageMap.getColl());
-                       report.write(m_output, delta);
+                       HtmlReport report = new HtmlReport(m_bundle, messageMap.getColl(), delta);
+                       if (null != m_output) {
+                               report.write(m_output);
+                       }
+                       
+                       Notifier notifier = new Notifier(m_bundle, m_config);
+                       notifier.sendEmailIfNeeded(pw, report);
                }
                catch (SQLException exc) {
                        reportUnableToConnect(pw, exc);
@@ -224,7 +248,7 @@ public class CFB {
        public static void main(String[] args) {
                CFB cfb = new CFB(Locale.getDefault());
                
-               try (PrintWriter pw = new PrintWriter(System.out)){
+               try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()))) {
                        cfb.doMain(pw, args);
                        pw.flush();
                } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {