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);
64 m_config = new Config();
66 m_configFile = new File("config.properties");
71 m_removeSchema = false;
75 CfbBundle getBundle() {
76 CfbBundle bundle = m_bundle;
78 synchronized(CFB.class) {
79 if (null == m_bundle) {
80 m_bundle = bundle = CfbBundle.getInst(m_locale);
87 Options createOptions() {
88 Options opt = new Options();
90 opt.addOption("c", "config", true, "Properties configuration file");
91 opt.addOption("d", "dbname", true, "DB name");
92 opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
93 opt.addOption("f", "fbp", true, "FindBugsProject file");
94 opt.addOption("h", "host", true, "DB hostname");
95 opt.addOption("j", "project", true, "proJect name");
96 opt.addOption("n", "number", true, "Build number (version)");
97 opt.addOption("o", "outfile", true, "Output report filename");
98 opt.addOption("p", "pass", true, "DB password");
99 opt.addOption("t", "port", true, "DB port");
100 opt.addOption("u", "user", true, "DB username");
105 boolean parseArgs(PrintWriter pw, String[] args) {
106 Options opt = createOptions();
109 CommandLine line = new GnuParser().parse(opt, args);
110 if (line.hasOption("c")) {
111 m_configFile = new File(line.getOptionValue("c"));
113 if (line.hasOption("d")) {
114 m_config.setDbName(line.getOptionValue("d"));
116 if (line.hasOption("f")) {
117 m_fbp = new File(line.getOptionValue("f"));
119 if (line.hasOption("h")) {
120 m_config.setDbHost(line.getOptionValue("h"));
122 if (line.hasOption("j")) {
123 m_projName = line.getOptionValue("j");
125 if (line.hasOption("n")) {
126 m_buildNum = line.getOptionValue("n");
128 if (line.hasOption("o")) {
129 m_output = new File(line.getOptionValue("o"));
131 if (line.hasOption("p")) {
132 m_config.setDbPass(line.getOptionValue("p"));
134 m_removeSchema = line.hasOption("drop-tables");
135 if (line.hasOption("t")) {
136 m_config.setDbPort(Integer.parseInt(line.getOptionValue("t")));
138 if (line.hasOption("u")) {
139 m_config.setDbUser(line.getOptionValue("u"));
142 catch (ParseException exc) {
150 void usage(PrintWriter pw, Options opt) {
151 HelpFormatter help = new HelpFormatter();
152 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
155 String trans(String key) {
156 return getBundle().get(key);
159 String getenv(String varName) {
160 // This is a separate function so that we can override it at unit test time
161 return System.getenv(varName);
164 String getProperty(String propName) {
165 // This is a separate function so that we can override it at unit test time
166 return System.getProperty(propName);
169 File getFindBugsDir() {
170 return (null != m_fbDir) ? m_fbDir : new File(".");
174 String findBugsDir = getenv("FINDBUGS_HOME");
175 if (null != findBugsDir) {
176 m_fbDir = new File(findBugsDir);
178 findBugsDir = getProperty("findbugs.home");
179 if (null != findBugsDir) {
180 m_fbDir = new File(findBugsDir);
184 void readConfig() throws IOException {
185 if (null != m_configFile) {
186 m_config.readFile(m_configFile);
190 void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException, TypeMismatchException {
191 initArgs(); // read environment and system properties
192 if ( ! parseArgs(pw, args) ) {
197 File findBugsDir = getFindBugsDir();
198 File workDir = new File(".");
199 MessageMap messageMap = new MessageMap();
200 messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
202 if (!ensureDbInitialized(pw, messageMap)) {
206 Analyzer analyzer = new Analyzer(messageMap);
207 Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_projName, m_buildNum);
208 if (null == analysis) {
209 pw.println(trans(CfbBundle.ANALYSIS_FAILED));
213 storeAndReport(pw, messageMap, analysis);
216 boolean ensureDbInitialized(PrintWriter pw, MessageMap messageMap)
217 throws TypeMismatchException
219 try (Connection con = m_driver.connect(
220 m_config.getDbHost(), m_config.getDbPort(),
221 m_config.getDbName(),
222 m_config.getDbUser(), m_config.getDbPass())
225 m_schema.setMessageMap(messageMap);
227 if (m_removeSchema) {
229 return false; // do not continue execution
231 m_schema.ensureDbInitialized(con);
232 messageMap.loadIds(con, m_driver);
234 catch (SQLException exc) {
235 reportUnableToConnect(pw, exc);
236 return false; // do not continue execution
239 return true; // all OK; continue execution
242 void storeAndReport(PrintWriter pw, MessageMap messageMap, Analysis analysis)
243 throws TypeMismatchException, IOException
246 Connection con = m_driver.connect(
247 m_config.getDbHost(), m_config.getDbPort(),
248 m_config.getDbName(),
249 m_config.getDbUser(), m_config.getDbPass()
253 DbStore store = new DbStore(con, m_driver, messageMap.getColl());
256 Analysis prior = store.getPrior(analysis);
257 Delta delta = new Delta(prior, analysis);
259 HtmlReport report = new HtmlReport(getBundle(), messageMap.getColl(), delta);
260 if (null != m_output) {
261 report.write(m_output);
264 Notifier notifier = new Notifier(getBundle(), m_config);
265 notifier.sendEmailIfNeeded(pw, report);
267 catch (StoreException exc) {
268 exc.printStackTrace(pw);
270 catch (SQLException exc) {
271 reportUnableToConnect(pw, exc);
275 private void reportUnableToConnect(PrintWriter pw, SQLException exc) {
276 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
277 String cannotConnect = MessageFormat.format(cannotConnectFormat,
278 m_config.getDbHost(),
279 ""+m_config.getDbPort(),
280 m_config.getDbName(),
283 exc.printStackTrace(pw);
284 SQLException next = exc.getNextException();
285 while (null != next) {
286 next.printStackTrace(pw);
287 next = next.getNextException();
289 pw.println(cannotConnect);
292 public static void main(String[] args) {
293 CFB cfb = new CFB(Locale.getDefault());
295 try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()))) {
296 cfb.doMain(pw, args);
298 } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {
299 exc.printStackTrace();