Further unit tests: CFB and Delta
[cfb.git] / prod / net / jaekl / cfb / CFB.java
index 639a73198088ffcf7e9335286dbeb4932512ad0a..c97a1c8f7b4e74196f528b80d11bb16dfdd22884 100644 (file)
@@ -42,7 +42,7 @@ import org.xml.sax.SAXException;
 public class CFB {
        DbDriver m_driver;
        CfbSchema m_schema;
-       CfbBundle m_bundle;     
+       volatile static CfbBundle m_bundle = null;      
        Locale m_locale;
        
        Config m_config;
@@ -60,7 +60,7 @@ public class CFB {
                m_driver = new PostgresqlDriver();
                m_schema = new CfbSchema(m_driver);
                m_locale = locale;
-               m_bundle = CfbBundle.getInst(m_locale);
+               m_bundle = null;
                m_config = new Config();
                
                m_configFile = new File("config.properties");
@@ -72,6 +72,18 @@ public class CFB {
                m_output = null;
        }
        
+       CfbBundle getBundle() {
+               CfbBundle bundle = m_bundle;
+               if (null == bundle) {
+                       synchronized(CFB.class) {
+                               if (null == m_bundle) {
+                                       m_bundle = bundle = CfbBundle.getInst(m_locale);
+                               }
+                       }
+               }
+               return bundle;
+       }
+       
        Options createOptions() {
                Options opt = new Options();
                
@@ -141,7 +153,7 @@ public class CFB {
        }
        
        String trans(String key) {
-               return m_bundle.get(key);
+               return getBundle().get(key);
        }
        
        String getenv(String varName) {
@@ -187,6 +199,23 @@ public class CFB {
                MessageMap messageMap = new MessageMap();
                messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
                
+               if (!ensureDbInitialized(pw, messageMap)) {
+                       return;
+               }
+               
+               Analyzer analyzer = new Analyzer(messageMap);
+               Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_projName, m_buildNum);
+               if (null == analysis) {
+                       pw.println(trans(CfbBundle.ANALYSIS_FAILED));
+                       return;
+               }
+               
+               storeAndReport(pw, messageMap, analysis);
+       }
+
+       boolean ensureDbInitialized(PrintWriter pw, MessageMap messageMap)
+                       throws TypeMismatchException 
+       {
                try (Connection con = m_driver.connect(
                                        m_config.getDbHost(), m_config.getDbPort(), 
                                        m_config.getDbName(), 
@@ -197,23 +226,22 @@ public class CFB {
                        
                        if (m_removeSchema) {
                                m_schema.purge(con);
-                               return;
+                               return false;   // do not continue execution
                        }
                        m_schema.ensureDbInitialized(con);
                        messageMap.loadIds(con, m_driver);
                }
                catch (SQLException exc) {
                        reportUnableToConnect(pw, exc);
-                       return;
-               }
-               
-               Analyzer analyzer = new Analyzer(messageMap);
-               Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_projName, m_buildNum);
-               if (null == analysis) {
-                       pw.println(trans(CfbBundle.ANALYSIS_FAILED));
-                       return;
+                       return false;   // do not continue execution
                }
                
+               return true;    // all OK; continue execution
+       }
+
+       void storeAndReport(PrintWriter pw, MessageMap messageMap, Analysis analysis) 
+                       throws TypeMismatchException, IOException 
+       {
                try (
                                Connection con = m_driver.connect(
                                                m_config.getDbHost(), m_config.getDbPort(), 
@@ -228,21 +256,19 @@ public class CFB {
                        Analysis prior = store.getPrior(analysis);
                        Delta delta = new Delta(prior, analysis);
 
-                       HtmlReport report = new HtmlReport(m_bundle, messageMap.getColl(), delta);
+                       HtmlReport report = new HtmlReport(getBundle(), messageMap.getColl(), delta);
                        if (null != m_output) {
                                report.write(m_output);
                        }
                        
-                       Notifier notifier = new Notifier(m_bundle, m_config);
+                       Notifier notifier = new Notifier(getBundle(), m_config);
                        notifier.sendEmailIfNeeded(pw, report);
                }
                catch (StoreException exc) {
                        exc.printStackTrace(pw);
-                       return;
                }
                catch (SQLException exc) {
                        reportUnableToConnect(pw, exc);
-                       return;
                }
        }