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 String m_host; // db host
33 int m_port; // db port
34 String m_user; // db user
35 String m_pass; // db password
38 m_driver = new PostgresqlDriver();
39 m_schema = new CfbSchema(m_driver);
41 m_bundle = CfbBundle.getInst(m_locale);
51 Options createOptions() {
52 Options opt = new Options();
54 opt.addOption("d", "dbname", true, "DB name");
55 opt.addOption("f", "fbp", true, "FindBugsProject file");
56 opt.addOption("h", "host", true, "DB hostname");
57 opt.addOption("p", "pass", true, "DB password");
58 opt.addOption("t", "port", true, "DB port");
59 opt.addOption("u", "user", true, "DB username");
64 boolean parseArgs(PrintWriter pw, String[] args) {
65 Options opt = createOptions();
68 CommandLine line = new GnuParser().parse(opt, args);
69 if (line.hasOption("d")) {
70 m_dbName = line.getOptionValue("d");
72 if (line.hasOption("f")) {
73 m_fbp = new File(line.getOptionValue("f"));
75 if (line.hasOption("h")) {
76 m_host = line.getOptionValue("h");
78 if (line.hasOption("p")) {
79 m_pass = line.getOptionValue("p");
81 if (line.hasOption("t")) {
82 m_port = Integer.parseInt(line.getOptionValue("t"));
84 if (line.hasOption("u")) {
85 m_user = line.getOptionValue("u");
88 catch (ParseException exc) {
96 void usage(PrintWriter pw, Options opt) {
97 HelpFormatter help = new HelpFormatter();
98 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
101 String trans(String key) {
102 return m_bundle.get(key);
105 void doMain(PrintWriter pw, String[] args) throws SQLException, IOException {
106 if ( ! parseArgs(pw, args) ) {
110 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
111 m_schema.ensureDbInitialized(con);
113 catch (SQLException exc) {
114 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
115 String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
116 exc.printStackTrace(pw);
117 pw.println(cannotConnect);
121 File findBugsDir = new File(".");
122 File workDir = new File(".");
123 Analyzer analyzer = new Analyzer(findBugsDir);
124 Analysis analysis = analyzer.analyze(workDir, m_fbp);
127 public static void main(String[] args) {
128 CFB cfb = new CFB(Locale.getDefault());
130 try (PrintWriter pw = new PrintWriter(System.out)){
131 cfb.doMain(pw, args);
132 } catch (SQLException | IOException exc) {
133 exc.printStackTrace();