Bring things to a state where the basic DB schema gets auto-created if it doesn't...
[cfb.git] / prod / net / jaekl / cfb / CFB.java
1 package net.jaekl.cfb;
2
3 import java.io.PrintWriter;
4 import java.sql.Connection;
5 import java.sql.SQLException;
6 import java.text.MessageFormat;
7 import java.util.Locale;
8
9 import net.jaekl.cfb.db.CfbSchema;
10 import net.jaekl.cfb.db.driver.DbDriver;
11 import net.jaekl.cfb.db.driver.PostgresqlDriver;
12
13 import org.apache.commons.cli.CommandLine;
14 import org.apache.commons.cli.GnuParser;
15 import org.apache.commons.cli.HelpFormatter;
16 import org.apache.commons.cli.Options;
17 import org.apache.commons.cli.ParseException;
18
19 public class CFB {
20         DbDriver m_driver;
21         CfbSchema m_schema;
22         CfbBundle m_bundle;     
23         Locale m_locale;
24         
25         // Command-line parameters
26         String m_dbName; // db name
27         String m_host;  // db host
28         int m_port;             // db port
29         String m_user;  // db user
30         String m_pass;  // db password
31         
32         CFB(Locale locale) {
33                 m_driver = new PostgresqlDriver();
34                 m_schema = new CfbSchema(m_driver);
35                 m_locale = locale;
36                 m_bundle = CfbBundle.getInst(m_locale);
37                 
38                 m_dbName = "CFB";
39                 m_host = "localhost";
40                 m_port = 5432;
41                 m_pass = "";
42                 m_user = "user";
43         }
44         
45         Options createOptions() {
46                 Options opt = new Options();
47                 
48                 opt.addOption("d", "dbname", true, "DB name");
49                 opt.addOption("h", "host", true, "DB hostname");
50                 opt.addOption("p", "pass", true, "DB password");
51                 opt.addOption("t", "port", true, "DB port");
52                 opt.addOption("u", "user", true, "DB username");
53                 
54                 return opt;
55         }
56         
57         boolean parseArgs(PrintWriter pw, String[] args) {
58                 Options opt = createOptions();
59                 
60                 try {
61                         CommandLine line = new GnuParser().parse(opt, args);
62                         if (line.hasOption("d")) {
63                                 m_dbName = line.getOptionValue("d");
64                         }
65                         if (line.hasOption("h")) {
66                                 m_host = line.getOptionValue("h");
67                         }
68                         if (line.hasOption("p")) {
69                                 m_pass = line.getOptionValue("p");
70                         }
71                         if (line.hasOption("t")) {
72                                 m_port = Integer.parseInt(line.getOptionValue("t"));
73                         }
74                         if (line.hasOption("u")) {
75                                 m_user = line.getOptionValue("u");
76                         }
77                 } 
78                 catch (ParseException exc) {
79                         usage(pw, opt);
80                         return false;
81                 }
82                 
83                 return true;
84         }
85         
86         void usage(PrintWriter pw, Options opt) {
87                 HelpFormatter help = new HelpFormatter();
88                 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
89         }
90         
91         String trans(String key) {
92                 return m_bundle.get(key);
93         }
94         
95         void doMain(PrintWriter pw, String[] args) throws SQLException {
96                 if ( ! parseArgs(pw, args) ) {
97                         return;
98                 }
99                 
100                 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
101                         if (null == con) {
102                                 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
103                                 String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, m_port, m_dbName, m_user);
104                                 pw.println(cannotConnect);
105                                 return;
106                         }
107                         m_schema.ensureDbInitialized(con);                      
108                 }
109         }
110         
111         public static void main(String[] args) {
112                 CFB cfb = new CFB(Locale.getDefault());
113                 
114                 try (PrintWriter pw = new PrintWriter(System.out)){
115                         cfb.doMain(pw, args);
116                 } catch (SQLException exc) {
117                         exc.printStackTrace();
118                 }
119         }
120
121 }