Restructure database code.
[cfb.git] / prod / net / jaekl / cfb / CFB.java
1 package net.jaekl.cfb;
2
3 // Comparative FindBugs
4 // 
5 // Tool to compare successive runs of FindBugs, 
6 // flagging the change from one run to the next.
7 // 
8 // Copyright (C) 2015 Christian Jaekl
9
10 import java.io.File;
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;
18
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;
27
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;
34
35 public class CFB {
36         DbDriver m_driver;
37         CfbSchema m_schema;
38         CfbBundle m_bundle;     
39         Locale m_locale;
40         
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)
50         
51         CFB(Locale locale) {
52                 m_driver = new PostgresqlDriver();
53                 m_schema = new CfbSchema(m_driver);
54                 m_locale = locale;
55                 m_bundle = CfbBundle.getInst(m_locale);
56                 
57                 m_dbName = "CFB";
58                 m_fbp    = null;
59                 m_fbDir  = null;
60                 m_host = "localhost";
61                 m_port = 5432;
62                 m_pass = "";
63                 m_user = "user";
64                 m_buildNum = null;
65         }
66         
67         Options createOptions() {
68                 Options opt = new Options();
69                 
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");
77                 
78                 return opt;
79         }
80         
81         boolean parseArgs(PrintWriter pw, String[] args) {
82                 Options opt = createOptions();
83                 
84                 try {
85                         CommandLine line = new GnuParser().parse(opt, args);
86                         if (line.hasOption("d")) {
87                                 m_dbName = line.getOptionValue("d");
88                         }
89                         if (line.hasOption("f")) {
90                                 m_fbp = new File(line.getOptionValue("f"));
91                         }
92                         if (line.hasOption("h")) {
93                                 m_host = line.getOptionValue("h");
94                         }
95                         if (line.hasOption("n")) {
96                                 m_buildNum = line.getOptionValue("n");
97                         }
98                         if (line.hasOption("p")) {
99                                 m_pass = line.getOptionValue("p");
100                         }
101                         if (line.hasOption("t")) {
102                                 m_port = Integer.parseInt(line.getOptionValue("t"));
103                         }
104                         if (line.hasOption("u")) {
105                                 m_user = line.getOptionValue("u");
106                         }
107                 } 
108                 catch (ParseException exc) {
109                         usage(pw, opt);
110                         return false;
111                 }
112                 
113                 return true;
114         }
115         
116         void usage(PrintWriter pw, Options opt) {
117                 HelpFormatter help = new HelpFormatter();
118                 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
119         }
120         
121         String trans(String key) {
122                 return m_bundle.get(key);
123         }
124         
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);
128         }
129         
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);
133         }
134         
135         File getFindBugsDir() {
136                 return (null != m_fbDir) ? m_fbDir : new File(".");
137         }
138         
139         void initArgs() {
140                 String findBugsDir = getenv("FINDBUGS_HOME");
141                 if (null != findBugsDir) {
142                         m_fbDir = new File(findBugsDir);
143                 }
144                 findBugsDir = getProperty("findbugs.home");
145                 if (null != findBugsDir) {
146                         m_fbDir = new File(findBugsDir);
147                 }
148         } 
149         
150         void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException {
151                 initArgs();     // read environment and system properties
152                 if ( ! parseArgs(pw, args) ) {
153                         return;
154                 }
155
156                 File findBugsDir = getFindBugsDir();
157                 File workDir = new File(".");
158                 MessageMap messageMap = new MessageMap();
159                 messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
160                 
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);      
164                 }
165                 catch (SQLException exc) {
166                         reportUnableToConnect(pw, exc);
167                         return;
168                 }
169                 
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));
174                         return;
175                 }
176                 
177                 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
178                         DbStore store = new DbStore(con);
179                         
180                         store.put(analysis);
181                 }
182                 catch (SQLException exc) {
183                         reportUnableToConnect(pw, exc);
184                         return;
185                 }
186         }
187
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);
193         }
194         
195         public static void main(String[] args) {
196                 CFB cfb = new CFB(Locale.getDefault());
197                 
198                 try (PrintWriter pw = new PrintWriter(System.out)){
199                         cfb.doMain(pw, args);
200                         pw.flush();
201                 } catch (SQLException | IOException | XmlParseException | SAXException exc) {
202                         exc.printStackTrace();
203                 }
204         }
205
206 }