Minor fine-tuning of findbugs command execution
[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         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
37         
38         CFB(Locale locale) {
39                 m_driver = new PostgresqlDriver();
40                 m_schema = new CfbSchema(m_driver);
41                 m_locale = locale;
42                 m_bundle = CfbBundle.getInst(m_locale);
43                 
44                 m_dbName = "CFB";
45                 m_fbp    = null;
46                 m_fbDir  = null;
47                 m_host = "localhost";
48                 m_port = 5432;
49                 m_pass = "";
50                 m_user = "user";
51         }
52         
53         Options createOptions() {
54                 Options opt = new Options();
55                 
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");
62                 
63                 return opt;
64         }
65         
66         boolean parseArgs(PrintWriter pw, String[] args) {
67                 Options opt = createOptions();
68                 
69                 try {
70                         CommandLine line = new GnuParser().parse(opt, args);
71                         if (line.hasOption("d")) {
72                                 m_dbName = line.getOptionValue("d");
73                         }
74                         if (line.hasOption("f")) {
75                                 m_fbp = new File(line.getOptionValue("f"));
76                         }
77                         if (line.hasOption("h")) {
78                                 m_host = line.getOptionValue("h");
79                         }
80                         if (line.hasOption("p")) {
81                                 m_pass = line.getOptionValue("p");
82                         }
83                         if (line.hasOption("t")) {
84                                 m_port = Integer.parseInt(line.getOptionValue("t"));
85                         }
86                         if (line.hasOption("u")) {
87                                 m_user = line.getOptionValue("u");
88                         }
89                 } 
90                 catch (ParseException exc) {
91                         usage(pw, opt);
92                         return false;
93                 }
94                 
95                 return true;
96         }
97         
98         void usage(PrintWriter pw, Options opt) {
99                 HelpFormatter help = new HelpFormatter();
100                 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
101         }
102         
103         String trans(String key) {
104                 return m_bundle.get(key);
105         }
106         
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);
110         }
111         
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);
115         }
116         
117         void initArgs() {
118                 String findBugsDir = getenv("FINDBUGS_HOME");
119                 if (null != findBugsDir) {
120                         m_fbDir = new File(findBugsDir);
121                 }
122                 findBugsDir = getProperty("findbugs.home");
123                 if (null != findBugsDir) {
124                         m_fbDir = new File(findBugsDir);
125                 }
126         }
127         
128         void doMain(PrintWriter pw, String[] args) throws SQLException, IOException {
129                 initArgs();     // read environment and system properties
130                 if ( ! parseArgs(pw, args) ) {
131                         return;
132                 }
133                 
134                 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
135                         m_schema.ensureDbInitialized(con);                      
136                 }
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);
142                         return;
143                 }
144                 
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) {
150                         // TODO
151                 }
152         }
153         
154         public static void main(String[] args) {
155                 CFB cfb = new CFB(Locale.getDefault());
156                 
157                 try (PrintWriter pw = new PrintWriter(System.out)){
158                         cfb.doMain(pw, args);
159                         pw.flush();
160                 } catch (SQLException | IOException exc) {
161                         exc.printStackTrace();
162                 }
163         }
164
165 }