Commits

dloy  committed e354b13

Clean up db-state handling

  • Participants
  • Parent commits f103db9

Comments (0)

Files changed (3)

File fixity-src/src/main/java/org/cdlib/mrt/fixity/service/FixityService.java

     public FixityEntriesState getFixityEntry(String urlS)
         throws TException
     {
+        if (fixityServiceProperties.isShutdown()) {
+            throw new TException.EXTERNAL_SERVICE_UNAVAILABLE("Fixity service shutdown");
+        }
         FixityEntry entry = new FixityEntry();
         entry.setUrl(urlS);
         entry.setStatus((FixityEntry.StatusType)null);
         throws TException
     {
         
+        if (fixityServiceProperties.isShutdown()) {
+            throw new TException.EXTERNAL_SERVICE_UNAVAILABLE("Fixity service shutdown");
+        }
         Connection connection = fixityServiceProperties.getConnection(true);
         SearchFixityEntries searchEntries = FixityActionAbs.getSearchFixityEntries(entry, connection, logger);
         FixityEntry[] entries = searchEntries.call();

File fixity-src/src/main/java/org/cdlib/mrt/fixity/service/FixityServiceProperties.java

             fixityState = new FixityState(fixityInfo);
             db = new FixityItemDB(logger, setupProp);
             serviceStateManager
-                    = FixityServiceStateManager.getFixityServiceStateManager(logger, fixityInfo, db);
+                    = FixityServiceStateManager.getFixityServiceStateManager(logger, fixityInfo);
 
             File adminDir = new File(fixityService, "admin");
             if (!adminDir.exists()) {
                     + " - fixityState.isRunFixity():" + fixityState.isRunFixity()
                     + " - fixityState.isFixityProcessing():" + fixityState.isFixityProcessing()
                     ); //!!!!
-            FixityServiceState state = serviceStateManager.getFixityServiceState(!isShutdown());
+            Connection connection = getConnection(true);
+            FixityServiceState state = serviceStateManager.getFixityServiceState(connection);
             if (fixityState.isRunFixity()) {
                 if (!fixityState.isFixityProcessing()) {
                     state.setStatus(FixityServiceState.StateStatus.unknown);
     public Connection getConnection(boolean autoCommit)
         throws TException
     {
+        if (db == null) return null;
         return db.getConnection(autoCommit);
     }
 

File fixity-src/src/main/java/org/cdlib/mrt/fixity/service/FixityServiceStateManager.java

 {
     private static final String NAME = "FixityServiceStateManager";
     private static final String MESSAGE = NAME + ": ";
+    private static final boolean DEBUG = false;
 
     
     protected File fixityInfo = null;
-    protected FixityItemDB db = null;
     protected LoggerInf logger = null;
 
     public static FixityServiceStateManager getFixityServiceStateManager(
-            LoggerInf logger, File fixityInfo, FixityItemDB db)
+            LoggerInf logger, File fixityInfo)
         throws TException
     {
-        return new FixityServiceStateManager(logger, fixityInfo, db);
+        return new FixityServiceStateManager(logger, fixityInfo);
     }
 
-    protected FixityServiceStateManager(LoggerInf logger, File fixityInfo, FixityItemDB db)
+    protected FixityServiceStateManager(LoggerInf logger, File fixityInfo)
         throws TException
     {
         try {
             this.logger = logger;
-            if (db == null) {
-                throw new TException.INVALID_OR_MISSING_PARM(MESSAGE + "db not supplied");
-            }
-
-            this.db = db;
             this.fixityInfo = fixityInfo;
             if (!fixityInfo.exists()) {
                 throw new TException.INVALID_OR_MISSING_PARM(MESSAGE + "fixity-info.txt does not exist:");
         }
     }
 
-    public FixityServiceState getFixityServiceState(boolean dbRunning)
+    public FixityServiceState getFixityServiceState(Connection connection)
         throws TException
     {
         try {
             Properties serviceProperties = new Properties();
             serviceProperties.load(fis);
             FixityServiceState state = new FixityServiceState(serviceProperties);
-            if (dbRunning) addDBContent(state);
+            if (DEBUG) {
+                boolean dbRunning = false;
+                if (connection != null) dbRunning = true;
+                System.out.println(MESSAGE + "getFixityServiceState"
+                    + " - dbRunning:" + dbRunning
+                    );
+            }
+            if (connection != null) addDBContent(connection, state);
             return state;
 
         } catch (Exception ex) {
             throw new TException(ex);
+        } finally {
+            if (connection != null) {
+                try {
+                    connection.close();
+                } catch (Exception ex) { }
+            }
         }
     }
 
-    public void addDBContent(FixityServiceState state)
+    public void addDBContent(Connection connection, FixityServiceState state)
         throws TException
     {
         try {
             InputStream fis = new FileInputStream(fixityInfo);
             Properties serviceProperties = new Properties();
             serviceProperties.load(fis);
-            Connection connection = db.getConnection(true);
             addDates(connection, state);
             addCounts(connection, state);
 
             FixityServiceState state)
         throws TException
     {
+        if (DEBUG) System.out.println(MESSAGE + "addDates");
         try {
             String sql = "select * "
                     + "from " + FixNames.ITEM_TABLE + " "
                 Long diffL =recentL - oldestL;
                 Double diffF= diffL.doubleValue();
                 diffF = diffF / (1000.0 * 60.0 * 60.0 * 24.0);
-                state.setElapsedTimeDays(diffF);
+                state.setElapsedTimeDays(diffF); 
+                if (DEBUG) System.out.println(MESSAGE + "addDates - elapsedTimeDays:" + state.getElapsedTimeDays());
             }
 
             return;
             FixityServiceState state)
         throws TException
     {
+        if (DEBUG) System.out.println(MESSAGE + "addCounts");
         try {
             String sql = "select count(itemkey) "
                     + "from " + FixNames.ITEM_TABLE + ";";
                     + "where status='system-unavailable'";
 
             state.setNumUnavailable(getNum(sql, "count(itemkey)", connection));
+            if (DEBUG) System.out.println(MESSAGE + "addCounts - TotalSize:" + state.getTotalSize());
             return;
 
         } catch (Exception ex) {
             }
             db = new FixityItemDB(logger, prop);
             FixityServiceStateManager manager = getFixityServiceStateManager
-                    (logger, fixityInfo, db);
-            FixityServiceState state = manager.getFixityServiceState(true);
+                    (logger, fixityInfo);
+            Connection connect = db.getConnection(true);
+            FixityServiceState state = manager.getFixityServiceState(connect);
             
             FormatterInf anvl = FormatterAbs.getANVLFormatter(logger);
             String format = formatIt(anvl, state);