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.cfb.store.StoreException;
33 import net.jaekl.qd.xml.XmlParseException;
35 import org.apache.commons.cli.CommandLine;
36 import org.apache.commons.cli.GnuParser;
37 import org.apache.commons.cli.HelpFormatter;
38 import org.apache.commons.cli.Options;
39 import org.apache.commons.cli.ParseException;
40 import org.xml.sax.SAXException;
45 volatile static CfbBundle m_bundle = null;
50 // Command-line parameters
52 File m_fbp; // FindBugsProject file
53 File m_fbDir; // Directory where FindBugs is installed
54 String m_projName; // project (module) name
55 String m_buildNum; // build number (version)
56 boolean m_removeSchema; // purge DB schema
57 File m_output; // File to which we should write our output (report)
60 m_driver = new PostgresqlDriver();
61 m_schema = new CfbSchema(m_driver);
63 m_config = new Config();
65 m_configFile = new File("config.properties");
70 m_removeSchema = false;
74 static CfbBundle getBundle(Locale locale) {
75 CfbBundle bundle = m_bundle;
77 synchronized(CFB.class) {
78 if (null == m_bundle) {
79 m_bundle = bundle = CfbBundle.getInst(locale);
86 Options createOptions() {
87 Options opt = new Options();
89 opt.addOption("c", "config", true, "Properties configuration file");
90 opt.addOption("d", "dbname", true, "DB name");
91 opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
92 opt.addOption("f", "fbp", true, "FindBugsProject file");
93 opt.addOption("h", "host", true, "DB hostname");
94 opt.addOption("j", "project", true, "proJect name");
95 opt.addOption("n", "number", true, "Build number (version)");
96 opt.addOption("o", "outfile", true, "Output report filename");
97 opt.addOption("p", "pass", true, "DB password");
98 opt.addOption("t", "port", true, "DB port");
99 opt.addOption("u", "user", true, "DB username");
104 boolean parseArgs(PrintWriter pw, String[] args) {
105 Options opt = createOptions();
108 CommandLine line = new GnuParser().parse(opt, args);
109 if (line.hasOption("c")) {
110 m_configFile = new File(line.getOptionValue("c"));
112 if (line.hasOption("d")) {
113 m_config.setDbName(line.getOptionValue("d"));
115 if (line.hasOption("f")) {
116 m_fbp = new File(line.getOptionValue("f"));
118 if (line.hasOption("h")) {
119 m_config.setDbHost(line.getOptionValue("h"));
121 if (line.hasOption("j")) {
122 m_projName = line.getOptionValue("j");
124 if (line.hasOption("n")) {
125 m_buildNum = line.getOptionValue("n");
127 if (line.hasOption("o")) {
128 m_output = new File(line.getOptionValue("o"));
130 if (line.hasOption("p")) {
131 m_config.setDbPass(line.getOptionValue("p"));
133 m_removeSchema = line.hasOption("drop-tables");
134 if (line.hasOption("t")) {
135 m_config.setDbPort(Integer.parseInt(line.getOptionValue("t")));
137 if (line.hasOption("u")) {
138 m_config.setDbUser(line.getOptionValue("u"));
141 catch (ParseException exc) {
149 void usage(PrintWriter pw, Options opt) {
150 HelpFormatter help = new HelpFormatter();
151 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
154 String trans(String key) {
155 return getBundle(m_locale).get(key);
158 String getenv(String varName) {
159 // This is a separate function so that we can override it at unit test time
160 return System.getenv(varName);
163 String getProperty(String propName) {
164 // This is a separate function so that we can override it at unit test time
165 return System.getProperty(propName);
168 File getFindBugsDir() {
169 return (null != m_fbDir) ? m_fbDir : new File(".");
173 String findBugsDir = getenv("FINDBUGS_HOME");
174 if (null != findBugsDir) {
175 m_fbDir = new File(findBugsDir);
177 findBugsDir = getProperty("findbugs.home");
178 if (null != findBugsDir) {
179 m_fbDir = new File(findBugsDir);
183 void readConfig() throws IOException {
184 if (null != m_configFile) {
185 m_config.readFile(m_configFile);
189 void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException, TypeMismatchException {
190 initArgs(); // read environment and system properties
191 if ( ! parseArgs(pw, args) ) {
196 File findBugsDir = getFindBugsDir();
197 File workDir = new File(".");
198 MessageMap messageMap = new MessageMap();
199 messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
201 if (!ensureDbInitialized(pw, messageMap)) {
205 Analyzer analyzer = new Analyzer(messageMap);
206 Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_projName, m_buildNum);
207 if (null == analysis) {
208 pw.println(trans(CfbBundle.ANALYSIS_FAILED));
212 storeAndReport(pw, messageMap, analysis);
215 boolean ensureDbInitialized(PrintWriter pw, MessageMap messageMap)
216 throws TypeMismatchException
218 try (Connection con = m_driver.connect(
219 m_config.getDbHost(), m_config.getDbPort(),
220 m_config.getDbName(),
221 m_config.getDbUser(), m_config.getDbPass())
224 m_schema.setMessageMap(messageMap);
226 if (m_removeSchema) {
228 return false; // do not continue execution
230 m_schema.ensureDbInitialized(con);
231 messageMap.loadIds(con, m_driver);
233 catch (SQLException exc) {
234 reportUnableToConnect(pw, exc);
235 return false; // do not continue execution
238 return true; // all OK; continue execution
241 void storeAndReport(PrintWriter pw, MessageMap messageMap, Analysis analysis)
242 throws TypeMismatchException, IOException
245 Connection con = m_driver.connect(
246 m_config.getDbHost(), m_config.getDbPort(),
247 m_config.getDbName(),
248 m_config.getDbUser(), m_config.getDbPass()
252 DbStore store = new DbStore(con, m_driver, messageMap.getColl());
255 Analysis prior = store.getPrior(analysis);
256 Delta delta = new Delta(prior, analysis);
258 HtmlReport report = new HtmlReport(getBundle(m_locale), messageMap.getColl(), delta);
259 if (null != m_output) {
260 report.write(m_output);
263 Notifier notifier = new Notifier(getBundle(m_locale), m_config);
264 notifier.sendEmailIfNeeded(pw, report);
266 catch (StoreException exc) {
267 exc.printStackTrace(pw);
269 catch (SQLException exc) {
270 reportUnableToConnect(pw, exc);
274 private void reportUnableToConnect(PrintWriter pw, SQLException exc) {
275 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
276 String cannotConnect = MessageFormat.format(cannotConnectFormat,
277 m_config.getDbHost(),
278 ""+m_config.getDbPort(),
279 m_config.getDbName(),
282 exc.printStackTrace(pw);
283 SQLException next = exc.getNextException();
284 while (null != next) {
285 next.printStackTrace(pw);
286 next = next.getNextException();
288 pw.println(cannotConnect);
291 public static void main(String[] args) {
292 CFB cfb = new CFB(Locale.getDefault());
294 try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()))) {
295 cfb.doMain(pw, args);
297 } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {
298 exc.printStackTrace();