Add the concept of "Project Name" to the RUNS table in the database.
[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.OutputStreamWriter;
13 import java.io.PrintWriter;
14 import java.nio.charset.Charset;
15 import java.sql.Connection;
16 import java.sql.SQLException;
17 import java.text.MessageFormat;
18 import java.util.Locale;
19 import java.util.Locale.Category;
20
21 import net.jaekl.cfb.analyze.Analysis;
22 import net.jaekl.cfb.analyze.Analyzer;
23 import net.jaekl.cfb.analyze.Delta;
24 import net.jaekl.cfb.analyze.HtmlReport;
25 import net.jaekl.cfb.analyze.MessageMap;
26 import net.jaekl.cfb.analyze.Notifier;
27 import net.jaekl.cfb.db.CfbSchema;
28 import net.jaekl.cfb.db.TypeMismatchException;
29 import net.jaekl.cfb.db.driver.DbDriver;
30 import net.jaekl.cfb.db.driver.PostgresqlDriver;
31 import net.jaekl.cfb.store.DbStore;
32 import net.jaekl.qd.xml.XmlParseException;
33
34 import org.apache.commons.cli.CommandLine;
35 import org.apache.commons.cli.GnuParser;
36 import org.apache.commons.cli.HelpFormatter;
37 import org.apache.commons.cli.Options;
38 import org.apache.commons.cli.ParseException;
39 import org.xml.sax.SAXException;
40
41 public class CFB {
42         DbDriver m_driver;
43         CfbSchema m_schema;
44         CfbBundle m_bundle;     
45         Locale m_locale;
46         
47         Config m_config;
48         
49         // Command-line parameters
50         File m_configFile;
51         String m_dbName; // db name
52         File m_fbp;             // FindBugsProject file
53         File m_fbDir;   // Directory where FindBugs is installed
54         String m_host;  // db host
55         int m_port;             // db port
56         String m_user;  // db user
57         String m_pass;  // db password
58         String m_projName; // project (module) name
59         String m_buildNum; // build number (version)
60         boolean m_removeSchema; // purge DB schema
61         File m_output;  // File to which we should write our output (report)
62         
63         CFB(Locale locale) {
64                 m_driver = new PostgresqlDriver();
65                 m_schema = new CfbSchema(m_driver);
66                 m_locale = locale;
67                 m_bundle = CfbBundle.getInst(m_locale);
68                 m_config = new Config();
69                 
70                 m_configFile = new File("config.properties");
71                 m_dbName = "CFB";
72                 m_fbp    = null;
73                 m_fbDir  = null;
74                 m_host = "localhost";
75                 m_port = 5432;
76                 m_pass = "";
77                 m_user = "user";
78                 m_projName = null;
79                 m_buildNum = null;
80                 m_removeSchema = false;
81                 m_output = null;
82         }
83         
84         Options createOptions() {
85                 Options opt = new Options();
86                 
87                 opt.addOption("c",  "config",      true,  "Properties configuration file");
88                 opt.addOption("d",  "dbname",      true,  "DB name");
89                 opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
90                 opt.addOption("f",  "fbp",         true,  "FindBugsProject file");
91                 opt.addOption("h",  "host",        true,  "DB hostname");
92                 opt.addOption("j",  "project",     true,  "proJect name");
93                 opt.addOption("n",  "number",      true,  "Build number (version)");
94                 opt.addOption("o",  "outfile",     true,  "Output report filename");
95                 opt.addOption("p",  "pass",        true,  "DB password");
96                 opt.addOption("t",  "port",        true,  "DB port");
97                 opt.addOption("u",  "user",        true,  "DB username");
98                 
99                 return opt;
100         }
101         
102         boolean parseArgs(PrintWriter pw, String[] args) {
103                 Options opt = createOptions();
104                 
105                 try {
106                         CommandLine line = new GnuParser().parse(opt, args);
107                         if (line.hasOption("c")) {
108                                 m_configFile = new File(line.getOptionValue("c"));
109                         }
110                         if (line.hasOption("d")) {
111                                 m_dbName = line.getOptionValue("d");
112                         }
113                         if (line.hasOption("f")) {
114                                 m_fbp = new File(line.getOptionValue("f"));
115                         }
116                         if (line.hasOption("h")) {
117                                 m_host = line.getOptionValue("h");
118                         }
119                         if (line.hasOption("j")) {
120                                 m_projName = line.getOptionValue("j");
121                         }
122                         if (line.hasOption("n")) {
123                                 m_buildNum = line.getOptionValue("n");
124                         }
125                         if (line.hasOption("o")) {
126                                 m_output = new File(line.getOptionValue("o"));
127                         }
128                         if (line.hasOption("p")) {
129                                 m_pass = line.getOptionValue("p");
130                         }
131                         m_removeSchema = line.hasOption("drop-tables");
132                         if (line.hasOption("t")) {
133                                 m_port = Integer.parseInt(line.getOptionValue("t"));
134                         }
135                         if (line.hasOption("u")) {
136                                 m_user = line.getOptionValue("u");
137                         }
138                 } 
139                 catch (ParseException exc) {
140                         usage(pw, opt);
141                         return false;
142                 }
143                 
144                 return true;
145         }
146         
147         void usage(PrintWriter pw, Options opt) {
148                 HelpFormatter help = new HelpFormatter();
149                 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
150         }
151         
152         String trans(String key) {
153                 return m_bundle.get(key);
154         }
155         
156         String getenv(String varName) {
157                 // This is a separate function so that we can override it at unit test time
158                 return System.getenv(varName);
159         }
160         
161         String getProperty(String propName) {
162                 // This is a separate function so that we can override it at unit test time
163                 return System.getProperty(propName);
164         }
165         
166         File getFindBugsDir() {
167                 return (null != m_fbDir) ? m_fbDir : new File(".");
168         }
169         
170         void initArgs() {
171                 String findBugsDir = getenv("FINDBUGS_HOME");
172                 if (null != findBugsDir) {
173                         m_fbDir = new File(findBugsDir);
174                 }
175                 findBugsDir = getProperty("findbugs.home");
176                 if (null != findBugsDir) {
177                         m_fbDir = new File(findBugsDir);
178                 }
179         }
180         
181         void readConfig() throws IOException {
182                 if (null != m_configFile) {
183                         m_config.readFile(m_configFile);
184                 }
185         }
186         
187         void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException, TypeMismatchException {
188                 initArgs();     // read environment and system properties
189                 if ( ! parseArgs(pw, args) ) {
190                         return;
191                 }
192                 readConfig();
193
194                 File findBugsDir = getFindBugsDir();
195                 File workDir = new File(".");
196                 MessageMap messageMap = new MessageMap();
197                 messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
198                 
199                 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
200                         m_schema.setMessageMap(messageMap);
201                         
202                         if (m_removeSchema) {
203                                 m_schema.purge(con);
204                                 return;
205                         }
206                         m_schema.ensureDbInitialized(con);
207                         messageMap.loadIds(con, m_driver);
208                 }
209                 catch (SQLException exc) {
210                         reportUnableToConnect(pw, exc);
211                         return;
212                 }
213                 
214                 Analyzer analyzer = new Analyzer(messageMap);
215                 Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_projName, m_buildNum);
216                 if (null == analysis) {
217                         pw.println(trans(CfbBundle.ANALYSIS_FAILED));
218                         return;
219                 }
220                 
221                 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
222                         DbStore store = new DbStore(con, m_driver, messageMap.getColl());
223                         
224                         store.put(analysis);
225                         Analysis prior = store.getPrior(analysis);
226                         Delta delta = new Delta(prior, analysis);
227
228                         HtmlReport report = new HtmlReport(m_bundle, messageMap.getColl(), delta);
229                         if (null != m_output) {
230                                 report.write(m_output);
231                         }
232                         
233                         Notifier notifier = new Notifier(m_bundle, m_config);
234                         notifier.sendEmailIfNeeded(pw, report);
235                 }
236                 catch (SQLException exc) {
237                         reportUnableToConnect(pw, exc);
238                         return;
239                 }
240         }
241
242         private void reportUnableToConnect(PrintWriter pw, SQLException exc) {
243                 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
244                 String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
245                 exc.printStackTrace(pw);
246                 SQLException next = exc.getNextException();
247                 while (null != next) {
248                         next.printStackTrace(pw);
249                         next = next.getNextException();
250                 }
251                 pw.println(cannotConnect);
252         }
253         
254         public static void main(String[] args) {
255                 CFB cfb = new CFB(Locale.getDefault());
256                 
257                 try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()))) {
258                         cfb.doMain(pw, args);
259                         pw.flush();
260                 } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {
261                         exc.printStackTrace();
262                 }
263         }
264
265 }