3 // Comparative FindBugs
5 // Tool to compare successive runs of FindBugs,
6 // flagging the change from one run to the next.
8 // Copyright (C) 2015 Christian Jaekl
11 import java.io.IOException;
12 import java.io.OutputStreamWriter;
13 import java.io.PrintWriter;
14 import java.nio.charset.Charset;
15 import java.sql.Connection;
16 import java.sql.SQLException;
17 import java.text.MessageFormat;
18 import java.util.Locale;
19 import java.util.Locale.Category;
21 import net.jaekl.cfb.analyze.Analysis;
22 import net.jaekl.cfb.analyze.Analyzer;
23 import net.jaekl.cfb.analyze.Delta;
24 import net.jaekl.cfb.analyze.HtmlReport;
25 import net.jaekl.cfb.analyze.MessageMap;
26 import net.jaekl.cfb.analyze.Notifier;
27 import net.jaekl.cfb.db.CfbSchema;
28 import net.jaekl.cfb.db.TypeMismatchException;
29 import net.jaekl.cfb.db.driver.DbDriver;
30 import net.jaekl.cfb.db.driver.PostgresqlDriver;
31 import net.jaekl.cfb.store.DbStore;
32 import net.jaekl.qd.xml.XmlParseException;
34 import org.apache.commons.cli.CommandLine;
35 import org.apache.commons.cli.GnuParser;
36 import org.apache.commons.cli.HelpFormatter;
37 import org.apache.commons.cli.Options;
38 import org.apache.commons.cli.ParseException;
39 import org.xml.sax.SAXException;
49 // Command-line parameters
51 String m_dbName; // db name
52 File m_fbp; // FindBugsProject file
53 File m_fbDir; // Directory where FindBugs is installed
54 String m_host; // db host
55 int m_port; // db port
56 String m_user; // db user
57 String m_pass; // db password
58 String m_projName; // project (module) name
59 String m_buildNum; // build number (version)
60 boolean m_removeSchema; // purge DB schema
61 File m_output; // File to which we should write our output (report)
64 m_driver = new PostgresqlDriver();
65 m_schema = new CfbSchema(m_driver);
67 m_bundle = CfbBundle.getInst(m_locale);
68 m_config = new Config();
70 m_configFile = new File("config.properties");
80 m_removeSchema = false;
84 Options createOptions() {
85 Options opt = new Options();
87 opt.addOption("c", "config", true, "Properties configuration file");
88 opt.addOption("d", "dbname", true, "DB name");
89 opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
90 opt.addOption("f", "fbp", true, "FindBugsProject file");
91 opt.addOption("h", "host", true, "DB hostname");
92 opt.addOption("j", "project", true, "proJect name");
93 opt.addOption("n", "number", true, "Build number (version)");
94 opt.addOption("o", "outfile", true, "Output report filename");
95 opt.addOption("p", "pass", true, "DB password");
96 opt.addOption("t", "port", true, "DB port");
97 opt.addOption("u", "user", true, "DB username");
102 boolean parseArgs(PrintWriter pw, String[] args) {
103 Options opt = createOptions();
106 CommandLine line = new GnuParser().parse(opt, args);
107 if (line.hasOption("c")) {
108 m_configFile = new File(line.getOptionValue("c"));
110 if (line.hasOption("d")) {
111 m_dbName = line.getOptionValue("d");
113 if (line.hasOption("f")) {
114 m_fbp = new File(line.getOptionValue("f"));
116 if (line.hasOption("h")) {
117 m_host = line.getOptionValue("h");
119 if (line.hasOption("j")) {
120 m_projName = line.getOptionValue("j");
122 if (line.hasOption("n")) {
123 m_buildNum = line.getOptionValue("n");
125 if (line.hasOption("o")) {
126 m_output = new File(line.getOptionValue("o"));
128 if (line.hasOption("p")) {
129 m_pass = line.getOptionValue("p");
131 m_removeSchema = line.hasOption("drop-tables");
132 if (line.hasOption("t")) {
133 m_port = Integer.parseInt(line.getOptionValue("t"));
135 if (line.hasOption("u")) {
136 m_user = line.getOptionValue("u");
139 catch (ParseException exc) {
147 void usage(PrintWriter pw, Options opt) {
148 HelpFormatter help = new HelpFormatter();
149 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
152 String trans(String key) {
153 return m_bundle.get(key);
156 String getenv(String varName) {
157 // This is a separate function so that we can override it at unit test time
158 return System.getenv(varName);
161 String getProperty(String propName) {
162 // This is a separate function so that we can override it at unit test time
163 return System.getProperty(propName);
166 File getFindBugsDir() {
167 return (null != m_fbDir) ? m_fbDir : new File(".");
171 String findBugsDir = getenv("FINDBUGS_HOME");
172 if (null != findBugsDir) {
173 m_fbDir = new File(findBugsDir);
175 findBugsDir = getProperty("findbugs.home");
176 if (null != findBugsDir) {
177 m_fbDir = new File(findBugsDir);
181 void readConfig() throws IOException {
182 if (null != m_configFile) {
183 m_config.readFile(m_configFile);
187 void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException, TypeMismatchException {
188 initArgs(); // read environment and system properties
189 if ( ! parseArgs(pw, args) ) {
194 File findBugsDir = getFindBugsDir();
195 File workDir = new File(".");
196 MessageMap messageMap = new MessageMap();
197 messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
199 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
200 m_schema.setMessageMap(messageMap);
202 if (m_removeSchema) {
206 m_schema.ensureDbInitialized(con);
207 messageMap.loadIds(con, m_driver);
209 catch (SQLException exc) {
210 reportUnableToConnect(pw, exc);
214 Analyzer analyzer = new Analyzer(messageMap);
215 Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_projName, m_buildNum);
216 if (null == analysis) {
217 pw.println(trans(CfbBundle.ANALYSIS_FAILED));
221 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
222 DbStore store = new DbStore(con, m_driver, messageMap.getColl());
225 Analysis prior = store.getPrior(analysis);
226 Delta delta = new Delta(prior, analysis);
228 HtmlReport report = new HtmlReport(m_bundle, messageMap.getColl(), delta);
229 if (null != m_output) {
230 report.write(m_output);
233 Notifier notifier = new Notifier(m_bundle, m_config);
234 notifier.sendEmailIfNeeded(pw, report);
236 catch (SQLException exc) {
237 reportUnableToConnect(pw, exc);
242 private void reportUnableToConnect(PrintWriter pw, SQLException exc) {
243 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
244 String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
245 exc.printStackTrace(pw);
246 SQLException next = exc.getNextException();
247 while (null != next) {
248 next.printStackTrace(pw);
249 next = next.getNextException();
251 pw.println(cannotConnect);
254 public static void main(String[] args) {
255 CFB cfb = new CFB(Locale.getDefault());
257 try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()))) {
258 cfb.doMain(pw, args);
260 } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {
261 exc.printStackTrace();