9a0a1463ddda835b454e06723834470d446de1ed
[cfb.git] / prod / net / jaekl / cfb / Config.java
1 package net.jaekl.cfb;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.PrintWriter;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.List;
10 import java.util.Properties;
11
12 import net.jaekl.qd.util.FileIO;
13
14 import org.apache.commons.cli.CommandLine;
15 import org.apache.commons.cli.GnuParser;
16 import org.apache.commons.cli.Options;
17 import org.apache.commons.cli.ParseException;
18
19 public class Config {
20         private static final String FINDBUGS_HOME = "FindBugsHome";
21         private static final String MAIL_FROM = "mail.from";
22         private static final String MAIL_SMTP_HOST = "mail.smtp.host";
23         private static final String NOTIFY = "notify";
24         
25         File m_configProperties;
26
27         String m_dbName; // db name
28         File m_fbp;             // FindBugsProject file
29         File m_fbDir;   // Directory where FindBugs is installed
30         String m_host;  // db host
31         int m_port;             // db port
32         String m_user;  // db user
33         String m_pass;  // db password
34         String m_buildNum; // build number (version)
35         boolean m_removeSchema; // purge DB schema
36         File m_output;  // File to which we should write our output (report)
37         
38         String       m_mailFrom;
39         String       m_mailSmtpHost;
40         List<String> m_notify;
41         
42         Options m_options;
43         
44         public Config() {
45                 m_dbName = "CFB";
46                 m_fbp    = null;
47                 m_fbDir  = null;
48                 m_host = "localhost";
49                 m_port = 5432;
50                 m_pass = "";
51                 m_user = "user";
52                 m_buildNum = null;
53                 m_removeSchema = false;
54                 m_output = null;
55                 
56                 m_mailFrom = "findbugs@localhost";
57                 m_mailSmtpHost = "localhost";
58                 m_notify = new ArrayList<String>();
59                 
60                 m_options = createOptions();
61         }
62         
63         public String getMailFrom() { return m_mailFrom; }
64         public String getMailSmtpHost() { return m_mailSmtpHost; }
65         public ArrayList<String> getNotify() { return new ArrayList<String>(m_notify); }
66         
67         public void readFile(File configProperties) throws IOException
68         {
69                 Properties props = new Properties();
70                 InputStream is = null;
71                 
72                 try {
73                         is = FileIO.getInst().openInput(configProperties);
74                         props.load(is);
75                 }
76                 finally {
77                         if (null != is) {
78                                 is.close();
79                         }
80                 }
81                 
82                 if (props.containsKey(FINDBUGS_HOME)) {
83                         m_fbDir = new File(props.getProperty(FINDBUGS_HOME));
84                 }
85                 if (props.containsKey(MAIL_FROM)) {
86                         m_mailFrom = props.getProperty(MAIL_FROM);
87                 }
88                 if (props.containsKey(MAIL_SMTP_HOST)) {
89                         m_mailSmtpHost = props.getProperty(MAIL_SMTP_HOST);
90                 }
91                 if (props.containsKey(NOTIFY)) {
92                         String[] addresses = props.getProperty(NOTIFY).split(",");
93                         m_notify = Arrays.asList(addresses);
94                 }
95                 
96                 try (PrintWriter pw = new PrintWriter(System.out)) {
97                         dump(pw);                       
98                 }
99         }
100         
101         private void dump(PrintWriter pw)
102         {
103                 pw.println("CONFIG DUMP:");
104                 pw.println("db_name: " + m_dbName);
105                 pw.println("fbp:  " + m_fbp);
106                 pw.println("fbDir:  " + m_fbDir);
107                 pw.println("host:  " + m_host);
108                 pw.println("port:  " + m_port);
109                 pw.println("user:  " + m_user);
110                 pw.println("pass:  " + m_pass);
111                 pw.println("buildNum:  " + m_buildNum);
112                 pw.println("removeSchema:  " + m_removeSchema);
113                 pw.println("output:  " + m_output);
114                 pw.println("mailFrom:  " + m_mailFrom);
115                 pw.println("smtpHost:  " + m_mailSmtpHost);
116                 pw.println("notify:  " + m_notify.size() + " entries");
117                 for (String s : m_notify) {
118                         pw.println("= " + s);
119                 }
120         }
121         
122         public Options createOptions() {
123                 Options opt = new Options();
124                 
125                 opt.addOption("c",  "config",      true,  "Properties configuration file");
126                 opt.addOption("d",  "dbname",      true,  "DB name");
127                 opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
128                 opt.addOption("f",  "fbp",         true,  "FindBugsProject file");
129                 opt.addOption("h",  "host",        true,  "DB hostname");
130                 opt.addOption("n",  "number",      true,  "Build number (version)");
131                 opt.addOption("o",  "outfile",     true,  "Output report filename");
132                 opt.addOption("p",  "pass",        true,  "DB password");
133                 opt.addOption("t",  "port",        true,  "DB port");
134                 opt.addOption("u",  "user",        true,  "DB username");
135                 
136                 return opt;
137         }
138         
139         public void parseArgs(PrintWriter pw, String[] args) throws ParseException
140         {
141                 assert(null != m_options);
142                 
143                 CommandLine line = new GnuParser().parse(m_options, args);
144                 if (line.hasOption("c")) {
145                         m_configProperties = new File(line.getOptionValue("c"));
146                 }
147                 if (line.hasOption("d")) {
148                         m_dbName = line.getOptionValue("d");
149                 }
150                 if (line.hasOption("f")) {
151                         m_fbp = new File(line.getOptionValue("f"));
152                 }
153                 if (line.hasOption("h")) {
154                         m_host = line.getOptionValue("h");
155                 }
156                 if (line.hasOption("n")) {
157                         m_buildNum = line.getOptionValue("n");
158                 }
159                 if (line.hasOption("o")) {
160                         m_output = new File(line.getOptionValue("o"));
161                 }
162                 if (line.hasOption("p")) {
163                         m_pass = line.getOptionValue("p");
164                 }
165                 m_removeSchema = line.hasOption("drop-tables");
166                 if (line.hasOption("t")) {
167                         m_port = Integer.parseInt(line.getOptionValue("t"));
168                 }
169                 if (line.hasOption("u")) {
170                         m_user = line.getOptionValue("u");
171                 }
172         }
173 }