Bring things to a state where the basic DB schema gets auto-created if it doesn't...
authorChris Jaekl <chris@ringo.jaekl.net>
Thu, 27 Aug 2015 13:26:44 +0000 (22:26 +0900)
committerChris Jaekl <chris@ringo.jaekl.net>
Thu, 27 Aug 2015 13:26:44 +0000 (22:26 +0900)
go.sh [new file with mode: 0755]
prod/cfb.properties [new file with mode: 0644]
prod/net/jaekl/cfb/CFB.java
prod/net/jaekl/cfb/CfbBundle.java [new file with mode: 0644]
prod/net/jaekl/cfb/db/CfbSchema.java
prod/net/jaekl/cfb/db/Schema.java
prod/net/jaekl/cfb/db/driver/DbDriver.java
prod/net/jaekl/qd/QDBundleFactory.java [new file with mode: 0644]
setcp.sh [new file with mode: 0644]

diff --git a/go.sh b/go.sh
new file mode 100755 (executable)
index 0000000..b546f3c
--- /dev/null
+++ b/go.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+CFB_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+echo Compiling...
+find "${CFB_ROOT}/prod" -name "*.java" | xargs javac -classpath ${CFB_ROOT}/prod:${CLASSPATH} -Xlint:deprecation
+cp -r ${CFB_ROOT}/prod/* ${CFB_ROOT}/bin/
+find "${CFB_ROOT}/prod" -name '*.class' -exec rm {} \;
+echo Launching...
+java -Djsse.enableSNIExtension=false net.jaekl.cfb.CFB $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15} ${16} ${17} ${18} ${19} ${20}
diff --git a/prod/cfb.properties b/prod/cfb.properties
new file mode 100644 (file)
index 0000000..a491bf0
--- /dev/null
@@ -0,0 +1 @@
+cannot.connect.to.db=Unable to connect to database {2} on {0}:{1} as user {3}.
index d864b6dc8681b9ae3de5d32b6603329385cc83ca..4d30011284f4d33e02a57a950c6e30cb74ebbcea 100644 (file)
@@ -3,6 +3,12 @@ package net.jaekl.cfb;
 import java.io.PrintWriter;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.io.PrintWriter;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.text.MessageFormat;
+import java.util.Locale;
+
+import net.jaekl.cfb.db.CfbSchema;
+import net.jaekl.cfb.db.driver.DbDriver;
+import net.jaekl.cfb.db.driver.PostgresqlDriver;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.GnuParser;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.GnuParser;
@@ -10,13 +16,11 @@ import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 
-import net.jaekl.cfb.db.CfbSchema;
-import net.jaekl.cfb.db.driver.DbDriver;
-import net.jaekl.cfb.db.driver.PostgresqlDriver;
-
 public class CFB {
        DbDriver m_driver;
        CfbSchema m_schema;
 public class CFB {
        DbDriver m_driver;
        CfbSchema m_schema;
+       CfbBundle m_bundle;     
+       Locale m_locale;
        
        // Command-line parameters
        String m_dbName; // db name
        
        // Command-line parameters
        String m_dbName; // db name
@@ -25,9 +29,11 @@ public class CFB {
        String m_user;  // db user
        String m_pass;  // db password
        
        String m_user;  // db user
        String m_pass;  // db password
        
-       CFB() {
+       CFB(Locale locale) {
                m_driver = new PostgresqlDriver();
                m_schema = new CfbSchema(m_driver);
                m_driver = new PostgresqlDriver();
                m_schema = new CfbSchema(m_driver);
+               m_locale = locale;
+               m_bundle = CfbBundle.getInst(m_locale);
                
                m_dbName = "CFB";
                m_host = "localhost";
                
                m_dbName = "CFB";
                m_host = "localhost";
@@ -82,6 +88,10 @@ public class CFB {
                help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
        }
        
                help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
        }
        
+       String trans(String key) {
+               return m_bundle.get(key);
+       }
+       
        void doMain(PrintWriter pw, String[] args) throws SQLException {
                if ( ! parseArgs(pw, args) ) {
                        return;
        void doMain(PrintWriter pw, String[] args) throws SQLException {
                if ( ! parseArgs(pw, args) ) {
                        return;
@@ -89,8 +99,9 @@ public class CFB {
                
                try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
                        if (null == con) {
                
                try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
                        if (null == con) {
-                               // TODO:  string table
-                               pw.println("FATAL:  Cannot connect to db.");
+                               String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
+                               String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, m_port, m_dbName, m_user);
+                               pw.println(cannotConnect);
                                return;
                        }
                        m_schema.ensureDbInitialized(con);                      
                                return;
                        }
                        m_schema.ensureDbInitialized(con);                      
@@ -98,7 +109,7 @@ public class CFB {
        }
        
        public static void main(String[] args) {
        }
        
        public static void main(String[] args) {
-               CFB cfb = new CFB();
+               CFB cfb = new CFB(Locale.getDefault());
                
                try (PrintWriter pw = new PrintWriter(System.out)){
                        cfb.doMain(pw, args);
                
                try (PrintWriter pw = new PrintWriter(System.out)){
                        cfb.doMain(pw, args);
diff --git a/prod/net/jaekl/cfb/CfbBundle.java b/prod/net/jaekl/cfb/CfbBundle.java
new file mode 100644 (file)
index 0000000..0384851
--- /dev/null
@@ -0,0 +1,50 @@
+package net.jaekl.cfb;
+
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+import java.util.concurrent.ConcurrentHashMap;
+
+import net.jaekl.qd.QDBundleFactory;
+
+public class CfbBundle {
+       public static final String CANNOT_CONNECT = "cannot.connect.to.db";
+       
+       final static String BUNDLE_NAME = "cfb";
+       
+       static ConcurrentHashMap<Locale, CfbBundle> m_bundleMap = new ConcurrentHashMap<Locale, CfbBundle>();
+       
+       ResourceBundle m_bundle;
+       
+       public static CfbBundle getInst(Locale locale) {
+               CfbBundle result = m_bundleMap.get(locale);
+               if (null == result) {
+                       synchronized(CfbBundle.class) {
+                               result = m_bundleMap.get(locale);
+                               if (null == result) {
+                                       result = new CfbBundle(locale); 
+                               }
+                               m_bundleMap.put(locale, result);
+                       }
+               }
+               return result;
+       }
+       
+       private CfbBundle(Locale locale) {
+               m_bundle = QDBundleFactory.getInst().getBundle(BUNDLE_NAME, locale); 
+       }
+       
+       public String get(String key) {
+               try {
+                       if (null != m_bundle) {
+                               return m_bundle.getString(key);
+                       }
+               }
+               catch (MissingResourceException exc) {
+                       // Make it clear that something has gone wrong.
+                       exc.printStackTrace();  
+                       // Fall through to the fallback behaviour below
+               }
+               return "[" + key + "]";
+       }
+}
index b298f5768c9f1f9cad6ac460c4bc69d9d1f78f21..52e067ba0b829f2a0026a0987e6175dec91f6f94 100644 (file)
@@ -45,11 +45,11 @@ public class CfbSchema extends Schema {
                },
                {
                        // Runs of FindBugs, normally one per build version
                },
                {
                        // Runs of FindBugs, normally one per build version
-                       { "RUNS " },
+                       { "RUNS" },
                        { "RUNID", INTEGER, -1, NOT_NULL },
                        { "VERSION", VARCHAR, 32, NULL },
                        { "RUNID", INTEGER, -1, NOT_NULL },
                        { "VERSION", VARCHAR, 32, NULL },
-                       { "START", TIMESTAMPTZ, -1, NOT_NULL },
-                       { "END", TIMESTAMPTZ, -1, NOT_NULL }
+                       { "STARTTIME", TIMESTAMPTZ, -1, NOT_NULL },
+                       { "ENDTIME", TIMESTAMPTZ, -1, NOT_NULL }
                }
        };
        
                }
        };
        
index bfcfdb8abf3bf98d866ad1290a4426573944aea3..eef7b8ea15aad2e8272ccfb4d50afbb3850f7262 100644 (file)
@@ -6,6 +6,7 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.Locale;
 
 import net.jaekl.cfb.db.driver.DbDriver;
 
 
 import net.jaekl.cfb.db.driver.DbDriver;
 
@@ -42,12 +43,13 @@ public class Schema {
                
                try (ResultSet rs = dbmd.getTables(null, null, null, new String[]{"TABLE"})) {
                        while (rs.next()) {
                
                try (ResultSet rs = dbmd.getTables(null, null, null, new String[]{"TABLE"})) {
                        while (rs.next()) {
-                               extantTables.add(rs.getString(3));
+                               extantTables.add(rs.getString(3).toUpperCase(Locale.CANADA));
                        }
                }
                
                for (Table table : m_tables) {
                        }
                }
                
                for (Table table : m_tables) {
-                       if ( ! extantTables.contains(table.getName()) ) {
+                       String name = table.getName().toUpperCase(Locale.CANADA);
+                       if ( ! extantTables.contains(name) ) {
                                // One or more tables missing
                                return false;
                        }
                                // One or more tables missing
                                return false;
                        }
index fa3886248522012a5b8069c9e0c600ab7c36a92f..9de7264d9d2ca30ebec495ca59ca8087d655d009 100644 (file)
@@ -26,6 +26,9 @@ public abstract class DbDriver {
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                        ps.executeUpdate();
                }
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                        ps.executeUpdate();
                }
+               catch (SQLException exc) {
+                       throw new SQLException("Failed to executeUpdate:  " + sql, exc);
+               }
                
                return true;
        }
                
                return true;
        }
diff --git a/prod/net/jaekl/qd/QDBundleFactory.java b/prod/net/jaekl/qd/QDBundleFactory.java
new file mode 100644 (file)
index 0000000..e53c878
--- /dev/null
@@ -0,0 +1,40 @@
+// Copyright (C) 2004, 2014 Christian Jaekl
+
+// Central spot from which to access ResourceBundles.
+// This made more sense with earlier versions of Java, where the specification did not 
+// guarantee that ResourceBundles would be cached.  Java 7 and later cache by default,
+// but it still seems prudent to centralize accesses to resources here so that we have 
+// control in case we want to implement our own cache, or override certain behaviours.
+// 
+// Note that we rely on the JVM's caching, to avoid unnecessary overhead.
+// See http://java2go.blogspot.ca/2010/03/dont-be-smart-never-implement-resource.html
+
+package net.jaekl.qd;
+
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+public class QDBundleFactory {
+       static volatile QDBundleFactory m_inst; // singleton instance
+       
+       private QDBundleFactory() {
+               // no-op
+       }
+       
+       public static QDBundleFactory getInst() {
+               QDBundleFactory result = m_inst;
+               if (null == result) {
+                       synchronized(QDBundleFactory.class) {
+                               if (null == m_inst) {
+                                       m_inst = new QDBundleFactory();
+                               }
+                               result = m_inst;
+                       }
+               }
+               return result;
+       }
+       
+       public ResourceBundle getBundle(String baseName, Locale locale) {
+               return ResourceBundle.getBundle(baseName, locale);
+       }
+}
diff --git a/setcp.sh b/setcp.sh
new file mode 100644 (file)
index 0000000..a27737a
--- /dev/null
+++ b/setcp.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+
+export CLASSPATH=${SCRIPT_DIR}/bin
+
+for x in ${SCRIPT_DIR}/lib/*.jar
+do
+    export CLASSPATH=${x}:${CLASSPATH}
+done
+
+export CLASSPATH=/usr/share/java/commons-cli.jar:${CLASSPATH}
+export CLASSPATH=/usr/share/java/junit4.jar:${CLASSPATH}
+export CLASSPATH=/usr/share/java/postgresql.jar:${CLASSPATH}
+#export CLASSPATH=${SCRIPT_DIR}/jcov/jcov.jar:${CLASSPATH}