Add ability to execute FindBugs
[cfb.git] / prod / net / jaekl / cfb / CFB.java
1 package net.jaekl.cfb;
2
3 import java.io.File;
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;
10
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;
16
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;
22
23 public class CFB {
24         DbDriver m_driver;
25         CfbSchema m_schema;
26         CfbBundle m_bundle;     
27         Locale m_locale;
28         
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
36         
37         CFB(Locale locale) {
38                 m_driver = new PostgresqlDriver();
39                 m_schema = new CfbSchema(m_driver);
40                 m_locale = locale;
41                 m_bundle = CfbBundle.getInst(m_locale);
42                 
43                 m_dbName = "CFB";
44                 m_fbp  = null;
45                 m_host = "localhost";
46                 m_port = 5432;
47                 m_pass = "";
48                 m_user = "user";
49         }
50         
51         Options createOptions() {
52                 Options opt = new Options();
53                 
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");
60                 
61                 return opt;
62         }
63         
64         boolean parseArgs(PrintWriter pw, String[] args) {
65                 Options opt = createOptions();
66                 
67                 try {
68                         CommandLine line = new GnuParser().parse(opt, args);
69                         if (line.hasOption("d")) {
70                                 m_dbName = line.getOptionValue("d");
71                         }
72                         if (line.hasOption("f")) {
73                                 m_fbp = new File(line.getOptionValue("f"));
74                         }
75                         if (line.hasOption("h")) {
76                                 m_host = line.getOptionValue("h");
77                         }
78                         if (line.hasOption("p")) {
79                                 m_pass = line.getOptionValue("p");
80                         }
81                         if (line.hasOption("t")) {
82                                 m_port = Integer.parseInt(line.getOptionValue("t"));
83                         }
84                         if (line.hasOption("u")) {
85                                 m_user = line.getOptionValue("u");
86                         }
87                 } 
88                 catch (ParseException exc) {
89                         usage(pw, opt);
90                         return false;
91                 }
92                 
93                 return true;
94         }
95         
96         void usage(PrintWriter pw, Options opt) {
97                 HelpFormatter help = new HelpFormatter();
98                 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
99         }
100         
101         String trans(String key) {
102                 return m_bundle.get(key);
103         }
104         
105         void doMain(PrintWriter pw, String[] args) throws SQLException, IOException {
106                 if ( ! parseArgs(pw, args) ) {
107                         return;
108                 }
109                 
110                 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
111                         m_schema.ensureDbInitialized(con);                      
112                 }
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);
118                         return;
119                 }
120                 
121                 File findBugsDir = new File(".");
122                 File workDir = new File(".");
123                 Analyzer analyzer = new Analyzer(findBugsDir);
124                 Analysis analysis = analyzer.analyze(workDir, m_fbp);
125         }
126         
127         public static void main(String[] args) {
128                 CFB cfb = new CFB(Locale.getDefault());
129                 
130                 try (PrintWriter pw = new PrintWriter(System.out)){
131                         cfb.doMain(pw, args);
132                 } catch (SQLException | IOException exc) {
133                         exc.printStackTrace();
134                 }
135         }
136
137 }