X-Git-Url: http://jaekl.net/gitweb/?a=blobdiff_plain;f=prod%2Fnet%2Fjaekl%2Fcfb%2FCFB.java;h=924d33182d28c4824207b6fde0712d4362d6d6b7;hb=58107a0cbb49652e7772ce80fb73d2c027590eb1;hp=4d30011284f4d33e02a57a950c6e30cb74ebbcea;hpb=635b88acc18a17db30d8d281c50d2f54ee2a91d9;p=cfb.git diff --git a/prod/net/jaekl/cfb/CFB.java b/prod/net/jaekl/cfb/CFB.java index 4d30011..924d331 100644 --- a/prod/net/jaekl/cfb/CFB.java +++ b/prod/net/jaekl/cfb/CFB.java @@ -1,20 +1,36 @@ package net.jaekl.cfb; +// Comparative FindBugs +// +// Tool to compare successive runs of FindBugs, +// flagging the change from one run to the next. +// +// Copyright (C) 2015 Christian Jaekl + +import java.io.File; +import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import java.text.MessageFormat; import java.util.Locale; +import java.util.Locale.Category; +import net.jaekl.cfb.analyze.Analysis; +import net.jaekl.cfb.analyze.Analyzer; +import net.jaekl.cfb.analyze.MessageMap; import net.jaekl.cfb.db.CfbSchema; import net.jaekl.cfb.db.driver.DbDriver; import net.jaekl.cfb.db.driver.PostgresqlDriver; +import net.jaekl.cfb.store.DbStore; +import net.jaekl.qd.xml.XmlParseException; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; +import org.xml.sax.SAXException; public class CFB { DbDriver m_driver; @@ -24,10 +40,14 @@ public class CFB { // Command-line parameters 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 CFB(Locale locale) { m_driver = new PostgresqlDriver(); @@ -36,20 +56,27 @@ public class CFB { m_bundle = CfbBundle.getInst(m_locale); 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; } Options createOptions() { Options opt = new Options(); opt.addOption("d", "dbname", true, "DB name"); - opt.addOption("h", "host", true, "DB hostname"); - opt.addOption("p", "pass", true, "DB password"); - opt.addOption("t", "port", true, "DB port"); - opt.addOption("u", "user", true, "DB username"); + opt.addOption("f", "fbp", true, "FindBugsProject file"); + opt.addOption("h", "host", true, "DB hostname"); + opt.addOption("n", "number", true, "Build number (version)"); + opt.addOption("p", "pass", true, "DB password"); + opt.addOption("r", "remove", false, "Remove database schema (drop all data)"); + opt.addOption("t", "port", true, "DB port"); + opt.addOption("u", "user", true, "DB username"); return opt; } @@ -62,12 +89,19 @@ public class CFB { 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("p")) { m_pass = line.getOptionValue("p"); } + m_removeSchema = line.hasOption("r"); if (line.hasOption("t")) { m_port = Integer.parseInt(line.getOptionValue("t")); } @@ -92,20 +126,79 @@ public class CFB { return m_bundle.get(key); } - void doMain(PrintWriter pw, String[] args) throws SQLException { + String getenv(String varName) { + // This is a separate function so that we can override it at unit test time + return System.getenv(varName); + } + + String getProperty(String propName) { + // This is a separate function so that we can override it at unit test time + return System.getProperty(propName); + } + + File getFindBugsDir() { + return (null != m_fbDir) ? m_fbDir : new File("."); + } + + void initArgs() { + String findBugsDir = getenv("FINDBUGS_HOME"); + if (null != findBugsDir) { + m_fbDir = new File(findBugsDir); + } + findBugsDir = getProperty("findbugs.home"); + if (null != findBugsDir) { + m_fbDir = new File(findBugsDir); + } + } + + void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException { + initArgs(); // read environment and system properties if ( ! parseArgs(pw, args) ) { return; } + + File findBugsDir = getFindBugsDir(); + File workDir = new File("."); + MessageMap messageMap = new MessageMap(); + messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY)); try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) { - if (null == con) { - String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT); - String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, m_port, m_dbName, m_user); - pw.println(cannotConnect); + m_schema.setMessageMap(messageMap); + + if (m_removeSchema) { + m_schema.purge(con); return; } - m_schema.ensureDbInitialized(con); + m_schema.ensureDbInitialized(con); } + catch (SQLException exc) { + reportUnableToConnect(pw, exc); + return; + } + + Analyzer analyzer = new Analyzer(messageMap); + Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_buildNum); + if (null == analysis) { + pw.println(trans(CfbBundle.ANALYSIS_FAILED)); + return; + } + + try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) { + DbStore store = new DbStore(con); + + store.put(analysis); + } + catch (SQLException exc) { + reportUnableToConnect(pw, exc); + return; + } + } + + private void reportUnableToConnect(PrintWriter pw, SQLException exc) { + String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT); + String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user); + exc.printStackTrace(pw); + pw.println(cannotConnect); } public static void main(String[] args) { @@ -113,7 +206,8 @@ public class CFB { try (PrintWriter pw = new PrintWriter(System.out)){ cfb.doMain(pw, args); - } catch (SQLException exc) { + pw.flush(); + } catch (SQLException | IOException | XmlParseException | SAXException exc) { exc.printStackTrace(); } }