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;
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_bundle = CfbBundle.getInst(m_locale);
64 m_config = new Config();
66 m_configFile = new File("config.properties");
71 m_removeSchema = false;
75 Options createOptions() {
76 Options opt = new Options();
78 opt.addOption("c", "config", true, "Properties configuration file");
79 opt.addOption("d", "dbname", true, "DB name");
80 opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
81 opt.addOption("f", "fbp", true, "FindBugsProject file");
82 opt.addOption("h", "host", true, "DB hostname");
83 opt.addOption("j", "project", true, "proJect name");
84 opt.addOption("n", "number", true, "Build number (version)");
85 opt.addOption("o", "outfile", true, "Output report filename");
86 opt.addOption("p", "pass", true, "DB password");
87 opt.addOption("t", "port", true, "DB port");
88 opt.addOption("u", "user", true, "DB username");
93 boolean parseArgs(PrintWriter pw, String[] args) {
94 Options opt = createOptions();
97 CommandLine line = new GnuParser().parse(opt, args);
98 if (line.hasOption("c")) {
99 m_configFile = new File(line.getOptionValue("c"));
101 if (line.hasOption("d")) {
102 m_config.setDbName(line.getOptionValue("d"));
104 if (line.hasOption("f")) {
105 m_fbp = new File(line.getOptionValue("f"));
107 if (line.hasOption("h")) {
108 m_config.setDbHost(line.getOptionValue("h"));
110 if (line.hasOption("j")) {
111 m_projName = line.getOptionValue("j");
113 if (line.hasOption("n")) {
114 m_buildNum = line.getOptionValue("n");
116 if (line.hasOption("o")) {
117 m_output = new File(line.getOptionValue("o"));
119 if (line.hasOption("p")) {
120 m_config.setDbPass(line.getOptionValue("p"));
122 m_removeSchema = line.hasOption("drop-tables");
123 if (line.hasOption("t")) {
124 m_config.setDbPort(Integer.parseInt(line.getOptionValue("t")));
126 if (line.hasOption("u")) {
127 m_config.setDbUser(line.getOptionValue("u"));
130 catch (ParseException exc) {
138 void usage(PrintWriter pw, Options opt) {
139 HelpFormatter help = new HelpFormatter();
140 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
143 String trans(String key) {
144 return m_bundle.get(key);
147 String getenv(String varName) {
148 // This is a separate function so that we can override it at unit test time
149 return System.getenv(varName);
152 String getProperty(String propName) {
153 // This is a separate function so that we can override it at unit test time
154 return System.getProperty(propName);
157 File getFindBugsDir() {
158 return (null != m_fbDir) ? m_fbDir : new File(".");
162 String findBugsDir = getenv("FINDBUGS_HOME");
163 if (null != findBugsDir) {
164 m_fbDir = new File(findBugsDir);
166 findBugsDir = getProperty("findbugs.home");
167 if (null != findBugsDir) {
168 m_fbDir = new File(findBugsDir);
172 void readConfig() throws IOException {
173 if (null != m_configFile) {
174 m_config.readFile(m_configFile);
178 void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException, TypeMismatchException {
179 initArgs(); // read environment and system properties
180 if ( ! parseArgs(pw, args) ) {
185 File findBugsDir = getFindBugsDir();
186 File workDir = new File(".");
187 MessageMap messageMap = new MessageMap();
188 messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
190 try (Connection con = m_driver.connect(
191 m_config.getDbHost(), m_config.getDbPort(),
192 m_config.getDbName(),
193 m_config.getDbUser(), m_config.getDbPass())
196 m_schema.setMessageMap(messageMap);
198 if (m_removeSchema) {
202 m_schema.ensureDbInitialized(con);
203 messageMap.loadIds(con, m_driver);
205 catch (SQLException exc) {
206 reportUnableToConnect(pw, exc);
210 Analyzer analyzer = new Analyzer(messageMap);
211 Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_projName, m_buildNum);
212 if (null == analysis) {
213 pw.println(trans(CfbBundle.ANALYSIS_FAILED));
218 Connection con = m_driver.connect(
219 m_config.getDbHost(), m_config.getDbPort(),
220 m_config.getDbName(),
221 m_config.getDbUser(), m_config.getDbPass()
225 DbStore store = new DbStore(con, m_driver, messageMap.getColl());
228 Analysis prior = store.getPrior(analysis);
229 Delta delta = new Delta(prior, analysis);
231 HtmlReport report = new HtmlReport(m_bundle, messageMap.getColl(), delta);
232 if (null != m_output) {
233 report.write(m_output);
236 Notifier notifier = new Notifier(m_bundle, m_config);
237 notifier.sendEmailIfNeeded(pw, report);
239 catch (StoreException exc) {
240 exc.printStackTrace(pw);
243 catch (SQLException exc) {
244 reportUnableToConnect(pw, exc);
249 private void reportUnableToConnect(PrintWriter pw, SQLException exc) {
250 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
251 String cannotConnect = MessageFormat.format(cannotConnectFormat,
252 m_config.getDbHost(),
253 ""+m_config.getDbPort(),
254 m_config.getDbName(),
257 exc.printStackTrace(pw);
258 SQLException next = exc.getNextException();
259 while (null != next) {
260 next.printStackTrace(pw);
261 next = next.getNextException();
263 pw.println(cannotConnect);
266 public static void main(String[] args) {
267 CFB cfb = new CFB(Locale.getDefault());
269 try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()))) {
270 cfb.doMain(pw, args);
272 } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {
273 exc.printStackTrace();