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.PrintWriter;
13 import java.sql.Connection;
14 import java.sql.SQLException;
15 import java.text.MessageFormat;
16 import java.util.Locale;
17 import java.util.Locale.Category;
19 import net.jaekl.cfb.analyze.Analysis;
20 import net.jaekl.cfb.analyze.Analyzer;
21 import net.jaekl.cfb.analyze.MessageMap;
22 import net.jaekl.cfb.db.CfbSchema;
23 import net.jaekl.cfb.db.driver.DbDriver;
24 import net.jaekl.cfb.db.driver.PostgresqlDriver;
25 import net.jaekl.cfb.store.DbStore;
26 import net.jaekl.qd.xml.XmlParseException;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.GnuParser;
30 import org.apache.commons.cli.HelpFormatter;
31 import org.apache.commons.cli.Options;
32 import org.apache.commons.cli.ParseException;
33 import org.xml.sax.SAXException;
41 // Command-line parameters
42 String m_dbName; // db name
43 File m_fbp; // FindBugsProject file
44 File m_fbDir; // Directory where FindBugs is installed
45 String m_host; // db host
46 int m_port; // db port
47 String m_user; // db user
48 String m_pass; // db password
49 String m_buildNum; // build number (version)
52 m_driver = new PostgresqlDriver();
53 m_schema = new CfbSchema(m_driver);
55 m_bundle = CfbBundle.getInst(m_locale);
67 Options createOptions() {
68 Options opt = new Options();
70 opt.addOption("d", "dbname", true, "DB name");
71 opt.addOption("f", "fbp", true, "FindBugsProject file");
72 opt.addOption("h", "host", true, "DB hostname");
73 opt.addOption("n", "number", true, "Build number (version)");
74 opt.addOption("p", "pass", true, "DB password");
75 opt.addOption("t", "port", true, "DB port");
76 opt.addOption("u", "user", true, "DB username");
81 boolean parseArgs(PrintWriter pw, String[] args) {
82 Options opt = createOptions();
85 CommandLine line = new GnuParser().parse(opt, args);
86 if (line.hasOption("d")) {
87 m_dbName = line.getOptionValue("d");
89 if (line.hasOption("f")) {
90 m_fbp = new File(line.getOptionValue("f"));
92 if (line.hasOption("h")) {
93 m_host = line.getOptionValue("h");
95 if (line.hasOption("n")) {
96 m_buildNum = line.getOptionValue("n");
98 if (line.hasOption("p")) {
99 m_pass = line.getOptionValue("p");
101 if (line.hasOption("t")) {
102 m_port = Integer.parseInt(line.getOptionValue("t"));
104 if (line.hasOption("u")) {
105 m_user = line.getOptionValue("u");
108 catch (ParseException exc) {
116 void usage(PrintWriter pw, Options opt) {
117 HelpFormatter help = new HelpFormatter();
118 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
121 String trans(String key) {
122 return m_bundle.get(key);
125 String getenv(String varName) {
126 // This is a separate function so that we can override it at unit test time
127 return System.getenv(varName);
130 String getProperty(String propName) {
131 // This is a separate function so that we can override it at unit test time
132 return System.getProperty(propName);
135 File getFindBugsDir() {
136 return (null != m_fbDir) ? m_fbDir : new File(".");
140 String findBugsDir = getenv("FINDBUGS_HOME");
141 if (null != findBugsDir) {
142 m_fbDir = new File(findBugsDir);
144 findBugsDir = getProperty("findbugs.home");
145 if (null != findBugsDir) {
146 m_fbDir = new File(findBugsDir);
150 void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException {
151 initArgs(); // read environment and system properties
152 if ( ! parseArgs(pw, args) ) {
156 File findBugsDir = getFindBugsDir();
157 File workDir = new File(".");
158 MessageMap messageMap = new MessageMap();
159 messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
161 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
162 m_schema.setMessageMap(messageMap);
163 m_schema.ensureDbInitialized(con);
165 catch (SQLException exc) {
166 reportUnableToConnect(pw, exc);
170 Analyzer analyzer = new Analyzer(messageMap);
171 Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_buildNum);
172 if (null == analysis) {
173 pw.println(trans(CfbBundle.ANALYSIS_FAILED));
177 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
178 DbStore store = new DbStore(con);
182 catch (SQLException exc) {
183 reportUnableToConnect(pw, exc);
188 private void reportUnableToConnect(PrintWriter pw, SQLException exc) {
189 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
190 String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
191 exc.printStackTrace(pw);
192 pw.println(cannotConnect);
195 public static void main(String[] args) {
196 CFB cfb = new CFB(Locale.getDefault());
198 try (PrintWriter pw = new PrintWriter(System.out)){
199 cfb.doMain(pw, args);
201 } catch (SQLException | IOException | XmlParseException | SAXException exc) {
202 exc.printStackTrace();