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;
9 import net.jaekl.cfb.db.CfbSchema;
10 import net.jaekl.cfb.db.driver.DbDriver;
11 import net.jaekl.cfb.db.driver.PostgresqlDriver;
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;
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
33 m_driver = new PostgresqlDriver();
34 m_schema = new CfbSchema(m_driver);
36 m_bundle = CfbBundle.getInst(m_locale);
45 Options createOptions() {
46 Options opt = new Options();
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");
57 boolean parseArgs(PrintWriter pw, String[] args) {
58 Options opt = createOptions();
61 CommandLine line = new GnuParser().parse(opt, args);
62 if (line.hasOption("d")) {
63 m_dbName = line.getOptionValue("d");
65 if (line.hasOption("h")) {
66 m_host = line.getOptionValue("h");
68 if (line.hasOption("p")) {
69 m_pass = line.getOptionValue("p");
71 if (line.hasOption("t")) {
72 m_port = Integer.parseInt(line.getOptionValue("t"));
74 if (line.hasOption("u")) {
75 m_user = line.getOptionValue("u");
78 catch (ParseException exc) {
86 void usage(PrintWriter pw, Options opt) {
87 HelpFormatter help = new HelpFormatter();
88 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
91 String trans(String key) {
92 return m_bundle.get(key);
95 void doMain(PrintWriter pw, String[] args) throws SQLException {
96 if ( ! parseArgs(pw, args) ) {
100 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
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);
107 m_schema.ensureDbInitialized(con);
111 public static void main(String[] args) {
112 CFB cfb = new CFB(Locale.getDefault());
114 try (PrintWriter pw = new PrintWriter(System.out)){
115 cfb.doMain(pw, args);
116 } catch (SQLException exc) {
117 exc.printStackTrace();