4 import java.io.IOException;
5 import java.io.PrintWriter;
6 import java.sql.Connection;
7 import java.sql.SQLException;
8 import java.text.MessageFormat;
9 import java.util.Locale;
11 import net.jaekl.cfb.analyze.Analysis;
12 import net.jaekl.cfb.analyze.Analyzer;
13 import net.jaekl.cfb.db.CfbSchema;
14 import net.jaekl.cfb.db.driver.DbDriver;
15 import net.jaekl.cfb.db.driver.PostgresqlDriver;
17 import org.apache.commons.cli.CommandLine;
18 import org.apache.commons.cli.GnuParser;
19 import org.apache.commons.cli.HelpFormatter;
20 import org.apache.commons.cli.Options;
21 import org.apache.commons.cli.ParseException;
29 // Command-line parameters
30 String m_dbName; // db name
31 File m_fbp; // FindBugsProject file
32 File m_fbDir; // Directory where FindBugs is installed
33 String m_host; // db host
34 int m_port; // db port
35 String m_user; // db user
36 String m_pass; // db password
39 m_driver = new PostgresqlDriver();
40 m_schema = new CfbSchema(m_driver);
42 m_bundle = CfbBundle.getInst(m_locale);
53 Options createOptions() {
54 Options opt = new Options();
56 opt.addOption("d", "dbname", true, "DB name");
57 opt.addOption("f", "fbp", true, "FindBugsProject file");
58 opt.addOption("h", "host", true, "DB hostname");
59 opt.addOption("p", "pass", true, "DB password");
60 opt.addOption("t", "port", true, "DB port");
61 opt.addOption("u", "user", true, "DB username");
66 boolean parseArgs(PrintWriter pw, String[] args) {
67 Options opt = createOptions();
70 CommandLine line = new GnuParser().parse(opt, args);
71 if (line.hasOption("d")) {
72 m_dbName = line.getOptionValue("d");
74 if (line.hasOption("f")) {
75 m_fbp = new File(line.getOptionValue("f"));
77 if (line.hasOption("h")) {
78 m_host = line.getOptionValue("h");
80 if (line.hasOption("p")) {
81 m_pass = line.getOptionValue("p");
83 if (line.hasOption("t")) {
84 m_port = Integer.parseInt(line.getOptionValue("t"));
86 if (line.hasOption("u")) {
87 m_user = line.getOptionValue("u");
90 catch (ParseException exc) {
98 void usage(PrintWriter pw, Options opt) {
99 HelpFormatter help = new HelpFormatter();
100 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
103 String trans(String key) {
104 return m_bundle.get(key);
107 String getenv(String varName) {
108 // This is a separate function so that we can override it at unit test time
109 return System.getenv(varName);
112 String getProperty(String propName) {
113 // This is a separate function so that we can override it at unit test time
114 return System.getProperty(propName);
118 String findBugsDir = getenv("FINDBUGS_HOME");
119 if (null != findBugsDir) {
120 m_fbDir = new File(findBugsDir);
122 findBugsDir = getProperty("findbugs.home");
123 if (null != findBugsDir) {
124 m_fbDir = new File(findBugsDir);
128 void doMain(PrintWriter pw, String[] args) throws SQLException, IOException {
129 initArgs(); // read environment and system properties
130 if ( ! parseArgs(pw, args) ) {
134 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
135 m_schema.ensureDbInitialized(con);
137 catch (SQLException exc) {
138 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
139 String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
140 exc.printStackTrace(pw);
141 pw.println(cannotConnect);
145 File findBugsDir = (null != m_fbDir) ? m_fbDir : new File(".");
146 File workDir = new File(".");
147 Analyzer analyzer = new Analyzer(findBugsDir);
148 Analysis analysis = analyzer.analyze(pw, workDir, m_fbp);
149 if (null != analysis) {
154 public static void main(String[] args) {
155 CFB cfb = new CFB(Locale.getDefault());
157 try (PrintWriter pw = new PrintWriter(System.out)){
158 cfb.doMain(pw, args);
160 } catch (SQLException | IOException exc) {
161 exc.printStackTrace();