aboutsummaryrefslogtreecommitdiffstats
path: root/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java
blob: 9412606d69d105ec5ee80fbb3f589496d5fe845f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
/*
 * ============LICENSE_START=======================================================
 * Integrity Monitor
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.common.im;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.onap.policy.common.im.jmx.ComponentAdmin;
import org.onap.policy.common.im.jmx.ComponentAdminMBean;
import org.onap.policy.common.im.jmx.JmxAgentConnection;
import org.onap.policy.common.im.jpa.ForwardProgressEntity;
import org.onap.policy.common.im.jpa.ResourceRegistrationEntity;
import org.onap.policy.common.im.jpa.StateManagementEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * IntegrityMonitor Main class for monitoring the integrity of a resource and managing its state.
 * State management follows the X.731 ITU standard.
 */
public class IntegrityMonitor {
    private static final Logger logger = LoggerFactory.getLogger(IntegrityMonitor.class.getName());

    // only allow one instance of IntegrityMonitor
    private static IntegrityMonitor instance = null;
    
    /*
     * Common strings
     */
    private static final String NULL_PROPERTY_STRING = " property is null";
    private static final String IGNORE_INVALID_PROPERTY_STRING = "Ignored invalid property: {}";
    private static final String PROPERTY_EXCEPTION_STRING = "IntegrityMonitor Property Exception: ";
    private static final String EXCEPTION_STRING = "IntegrityMonitor threw exception.";
    private static final String STATE_CHECK_STRING = "IntegrityMonitor.stateCheck(): "
            + "Failed to diableFail dependent resource = ";
    private static final String RESOURCE_STRING = "Resource ";
    private static final String LC_RESOURCE_STRING = "resource";
    
    /*
     * Query String
     */
    private static final String QUERY_STRING = "Select f from ForwardProgressEntity f where f.resourceName=:rn";
    
    // may be changed by junit tests
    private static Factory factory = new Factory();

    private static String resourceName = null;
    boolean alarmExists = false;

    /*
     * Error message that is written by the dependencyCheck() method. It is made available
     * externally through the evaluateSanity() method.
     */
    private String dependencyCheckErrorMsg = "";

    // The entity manager factory for JPA access
    private EntityManagerFactory emf;
    private EntityManager em;

    // Persistence Unit for JPA
    public static final String PERSISTENCE_UNIT = "operationalPU";

    public static final long CYCLE_INTERVAL_MILLIS = 1000L;

    private StateManagement stateManager = null;

    private FpManager fpManager = null;

    // The forward progress counter is incremented as the
    // process being monitored makes forward progress
    private int fpCounter = 0;
    private int lastFpCounter = 0;

    // elapsed time since last FP counter check
    private long elapsedTime = 0;

    // elapsed time since last test transaction check
    private long elapsedTestTransTime = 0;

    // elapsed time since last write Fpc check
    private long elapsedWriteFpcTime = 0;

    // last dependency health check time. Initialize so that the periodic check
    // starts after 60 seconds.
    // This allows time for dependents to come up.
    private long lastDependencyCheckTime = MonitorTime.getInstance().getMillis();

    // Time of the last state audit. It is initialized at the time of the IM
    // construction
    private Date lastStateAuditTime = MonitorTime.getInstance().getDate();

    // Interval between state audits in ms. We leave it turned off by default so
    // that it will only
    // be run on the nodes which we want doing the audit. In particular, we only
    // want it to run
    // on the droolspdps
    private static long stateAuditIntervalMs = 0L;

    // the number of cycles since 'fpCounter' was last changed
    private int missedCycles = 0;

    // forward progress monitoring interval
    private static long monitorIntervalMs = toMillis(IntegrityMonitorProperties.DEFAULT_MONITOR_INTERVAL);
    // The number of periods the counter fails to increment before an alarm is
    // raised.
    private static int failedCounterThreshold = IntegrityMonitorProperties.DEFAULT_FAILED_COUNTER_THRESHOLD;
    // test transaction interval
    private static long testTransIntervalMs = toMillis(IntegrityMonitorProperties.DEFAULT_TEST_INTERVAL);
    // write Fpc to DB interval
    private static long writeFpcIntervalMs = toMillis(IntegrityMonitorProperties.DEFAULT_WRITE_FPC_INTERVAL);
    // check the health of dependencies
    private static long checkDependencyIntervalMs =
                    toMillis(IntegrityMonitorProperties.DEFAULT_CHECK_DEPENDENCY_INTERVAL);

    // A lead subsystem will have dependency groups with resource names in the
    // properties file.
    // For non-lead subsystems, the dependency_group property will be absent.
    private static String[] depGroups = null;

    private static boolean isUnitTesting = false;

    // can turn on health checking of dependents via jmx test() call by setting
    // this property to true
    private static boolean testViaJmx = false;

    private static String jmxFqdn = null;

    // this is the max interval allowed without any forward progress
    // counter updates
    private static long maxFpcUpdateIntervalMs = toMillis(IntegrityMonitorProperties.DEFAULT_MAX_FPC_UPDATE_INTERVAL);

    // Node types
    private enum NodeType {
        PDP_XACML, PDP_DROOLS, PAP, PAP_ADMIN, LOGPARSER, BRMS_GATEWAY, ASTRA_GATEWAY, ELK_SERVER, PYPDP

    }

    private static String siteName;
    private static String nodeType;
    private Date refreshStateAuditLastRunDate;
    private static long refreshStateAuditIntervalMs = 600000; // run it once per 10 minutes

    // lock objects
    private final Object evaluateSanityLock = new Object();
    private final Object fpMonitorCycleLock = new Object();
    private final Object dependencyCheckLock = new Object();
    private final Object testTransactionLock = new Object();
    private final Object startTransactionLock = new Object();
    private final Object endTransactionLock = new Object();
    private final Object checkTestTransactionLock = new Object();
    private final Object checkWriteFpcLock = new Object();
    private static final Object getInstanceLock = new Object();
    private final Object refreshStateAuditLock = new Object();
    private final Object imFlushLock = new Object();

    private Map<String, String> allSeemsWellMap;
    private Map<String, String> allNotWellMap;

    /**
     * IntegrityMonitor constructor. It is invoked from the getInstance() method in this class or
     * from the constructor of a child or sub-class. A class can extend the IntegrityMonitor class
     * if there is a need to override any of the base methods (ex. subsystemTest()). Only one
     * instance is allowed to be created per resource name.
     * 
     * @param resourceName The resource name of the resource
     * @param properties a set of properties passed in from the resource
     * @throws IntegrityMonitorException if any errors are encountered in the constructor
     */
    protected IntegrityMonitor(String resourceName, Properties properties) throws IntegrityMonitorException {

        // singleton check since this constructor can be called from a child or
        // sub-class
        if (instance != null) {
            String msg = "IM object exists and only one instance allowed";
            logger.error("{}", msg);
            throw new IntegrityMonitorException("IntegrityMonitor constructor exception: " + msg);
        }
        instance = this;

        IntegrityMonitor.resourceName = resourceName;

        /*
         * Validate that the properties file contains all the needed properties. Throws an
         * IntegrityMonitorPropertiesException
         */
        validateProperties(properties);

        // construct jmx url
        String jmxUrl = getJmxUrl();

        //
        // Create the entity manager factory
        //
        emf = Persistence.createEntityManagerFactory(factory.getPersistenceUnit(), properties);
        //
        // Did it get created?
        //
        if (emf == null) {
            logger.error("Error creating IM entity manager factory with persistence unit: {}", 
                            factory.getPersistenceUnit());
            throw new IntegrityMonitorException("Unable to create IM Entity Manager Factory");
        }

        // add entry to forward progress and resource registration tables in DB

        // Start a transaction
        em = emf.createEntityManager();
        EntityTransaction et = em.getTransaction();

        et.begin();

        try {
            // if ForwardProgress entry exists for resourceName, update it. If
            // not found, create a new entry
            Query fquery = em.createQuery(QUERY_STRING);
            fquery.setParameter("rn", resourceName);

            @SuppressWarnings("rawtypes")
            List fpList = fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
            ForwardProgressEntity fpx = null;
            if (!fpList.isEmpty()) {
                // ignores multiple results
                fpx = (ForwardProgressEntity) fpList.get(0);
                // refresh the object from DB in case cached data was returned
                em.refresh(fpx);
                if (logger.isDebugEnabled()) {
                    logger.debug("Resource {} exists and will be updated - old fpc= {}, lastUpdated= {}", resourceName,
                            fpx.getFpcCount(), fpx.getLastUpdated());
                }
                fpx.setFpcCount(fpCounter);
            } else {
                // Create a forward progress object
                logger.debug("Adding resource {} to ForwardProgress table", resourceName);
                fpx = new ForwardProgressEntity();
            }
            // update/set columns in entry
            fpx.setResourceName(resourceName);
            em.persist(fpx);
            // flush to the DB
            synchronized (imFlushLock) {
                em.flush();
            }

            // if ResourceRegistration entry exists for resourceName, update it.
            // If not found, create a new entry
            Query rquery = em.createQuery("Select r from ResourceRegistrationEntity r where r.resourceName=:rn");
            rquery.setParameter("rn", resourceName);

            @SuppressWarnings("rawtypes")
            List rrList = rquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
            ResourceRegistrationEntity rrx = null;
            if (!rrList.isEmpty()) {
                // ignores multiple results
                rrx = (ResourceRegistrationEntity) rrList.get(0);
                // refresh the object from DB in case cached data was returned
                em.refresh(rrx);
                if (logger.isDebugEnabled()) {
                    logger.debug("Resource {} exists and will be updated - old url= {}, createdDate={}", resourceName,
                            rrx.getResourceUrl(), rrx.getCreatedDate());
                }
                rrx.setLastUpdated(MonitorTime.getInstance().getDate());
            } else {
                // register resource by adding entry to table in DB
                logger.debug("Adding resource {} to ResourceRegistration table", resourceName);
                rrx = new ResourceRegistrationEntity();
            }
            // update/set columns in entry
            rrx.setResourceName(resourceName);
            rrx.setResourceUrl(jmxUrl);
            rrx.setNodeType(nodeType);
            rrx.setSite(siteName);
            em.persist(rrx);
            // flush to the DB
            synchronized (imFlushLock) {
                et.commit();
            }

        } catch (Exception e) {
            logger.error("IntegrityMonitor constructor DB table update failed with exception: ", e);
            try {
                if (et.isActive()) {
                    synchronized (imFlushLock) {
                        et.rollback();
                    }
                }
            } catch (Exception e1) {
                logger.error("IntegrityMonitor constructor threw exception: ", e1);
            }
            throw e;
        }

        try {
            // create instance of StateMangement class and pass emf to it
            stateManager = new StateManagement(emf, resourceName);

            /**
             * Initialize the state and status attributes. This will maintain any Administrative
             * state value but will set the operational state = enabled, availability status = null,
             * standby status = null. The integrity monitor will set the operational state via the
             * FPManager and the owning application must set the standby status by calling
             * promote/demote on the StateManager.
             */
            stateManager.initializeState();

        } catch (StateManagementException e) {
            throw new IntegrityMonitorException(e);
        }

        // create management bean
        try {
            new ComponentAdmin(resourceName, this, stateManager);
        } catch (Exception e) {
            logger.error("ComponentAdmin constructor exception: {}", e.toString(), e);
        }

        fpManager = new FpManager();
        fpManager.start();

    }

    /**
     * Get an instance of IntegrityMonitor for a given resource name. It creates one if it does not
     * exist. Only one instance is allowed to be created per resource name.
     * 
     * @param resourceName The resource name of the resource
     * @param properties a set of properties passed in from the resource
     * @return The new instance of IntegrityMonitor
     * @throws IntegrityMonitorException if unable to create jmx url or the constructor returns an
     *         exception
     */
    public static IntegrityMonitor getInstance(String resourceName, Properties properties)
            throws IntegrityMonitorException {

        synchronized (getInstanceLock) {
            logger.debug("getInstance() called - resourceName= {}", resourceName);
            if (resourceName == null || resourceName.isEmpty() || properties == null) {
                logger.error("Error: getIntegrityMonitorInstance() called with invalid input");
                return null;
            }

            if (instance == null) {
                logger.debug("Creating new instance of IntegrityMonitor");
                instance = new IntegrityMonitor(resourceName, properties);
            }
            return instance;
        }
    }

    /**
     * Get the single instance.
     * 
     * @return the instance
     * @throws IntegrityMonitorException if no instance exists
     */
    public static IntegrityMonitor getInstance() throws IntegrityMonitorException {
        logger.debug("getInstance() called");
        if (instance == null) {
            String msg = "No IntegrityMonitor instance exists."
                    + " Please use the method IntegrityMonitor.getInstance(String resourceName, Properties properties)";
            throw new IntegrityMonitorPropertiesException(msg);
        } else {
            return instance;
        }
    }

    /**
     * This is a facility used by JUnit testing to destroy the IntegrityMonitor instance before
     * creating a new one. It waits a bit to allow the FPManager to fully exit.
     */
    public static void deleteInstance() throws IntegrityMonitorException {
        logger.debug("deleteInstance() called");
        synchronized (getInstanceLock) {
            if (isUnitTesting() && instance != null && instance.getFpManager() != null) {
                FpManager fpm = instance.getFpManager();

                // Stop the FPManager thread
                fpm.stopAndExit();

                try {
                    // Make sure it has exited
                    fpm.join(2000L);
                } catch (InterruptedException e) {
                    logger.error("deleteInstance: Interrupted while waiting for FPManaager to fully exit", e);
                    Thread.currentThread().interrupt();
                }

                if (fpm.isAlive()) {
                    logger.error("IntegrityMonitor.deleteInstance() Failed to kill FPManager thread");
                    throw new IntegrityMonitorException(
                            "IntegrityMonitor.deleteInstance() Failed to kill FPManager thread");
                }

                instance = null;
            }
        }
        logger.debug("deleteInstance() exit");
    }

    private FpManager getFpManager() {
        return fpManager;
    }

    private static String getJmxUrl() throws IntegrityMonitorException {

        // get the jmx remote port and construct the JMX URL
        Properties systemProps = System.getProperties();
        String jmxPort = systemProps.getProperty("com.sun.management.jmxremote.port");
        String jmxErrMsg;
        if (jmxPort == null) {
            jmxErrMsg = "System property com.sun.management.jmxremote.port for JMX remote port is not set";
            logger.error("{}", jmxErrMsg);
            throw new IntegrityMonitorException("getJmxUrl exception: " + jmxErrMsg);
        }

        int port = 0;
        try {
            port = Integer.parseInt(jmxPort);
        } catch (NumberFormatException e) {
            jmxErrMsg = "JMX remote port is not a valid integer value - " + jmxPort;
            logger.error("{}", jmxErrMsg);
            throw new IntegrityMonitorException("getJmxUrl exception: " + jmxErrMsg);
        }

        try {
            if (jmxFqdn == null) {
                // get FQDN of this host
                jmxFqdn = InetAddress.getLocalHost().getCanonicalHostName();
            }
        } catch (Exception e) {
            String msg = "getJmxUrl could not get hostname";
            logger.error("{}", msg, e);
            throw new IntegrityMonitorException("getJmxUrl Exception: " + msg);
        }
        if (jmxFqdn == null) {
            String msg = "getJmxUrl encountered null hostname";
            logger.error("{}", msg);
            throw new IntegrityMonitorException("getJmxUrl error: " + msg);
        }

        // assemble the jmx url
        String jmxUrl = "service:jmx:rmi:///jndi/rmi://" + jmxFqdn + ":" + port + "/jmxrmi";

        logger.debug("IntegerityMonitor - jmx url={}", jmxUrl);

        return jmxUrl;
    }

    /**
     * evaluateSanity() is designed to be called by an external entity to evealuate the sanity of
     * the node. It checks the operational and administrative states and the standby status. If the
     * operational state is disabled, it will include the dependencyCheckErrorMsg which includes
     * information about any dependency (node) which has failed.
     */
    public void evaluateSanity() throws IntegrityMonitorException {
        logger.debug("evaluateSanity called ....");
        synchronized (evaluateSanityLock) {

            String errorMsg = dependencyCheckErrorMsg;
            logger.debug("evaluateSanity dependencyCheckErrorMsg = {}", errorMsg);
            // check op state and throw exception if disabled
            if ((stateManager.getOpState() != null) && stateManager.getOpState().equals(StateManagement.DISABLED)) {
                String msg = RESOURCE_STRING + resourceName + " operation state is disabled. " + errorMsg;
                logger.debug("{}", msg);
                throw new IntegrityMonitorException(msg);
            }

            // check admin state and throw exception if locked
            if ((stateManager.getAdminState() != null) && stateManager.getAdminState().equals(StateManagement.LOCKED)) {
                String msg = RESOURCE_STRING + resourceName + " is administratively locked";
                logger.debug("{}", msg);
                throw new AdministrativeStateException("IntegrityMonitor Admin State Exception: " + msg);
            }
            // check standby state and throw exception if cold standby
            if ((stateManager.getStandbyStatus() != null)
                    && stateManager.getStandbyStatus().equals(StateManagement.COLD_STANDBY)) {
                String msg = RESOURCE_STRING + resourceName + " is cold standby";
                logger.debug("{}", msg);
                throw new StandbyStatusException("IntegrityMonitor Standby Status Exception: " + msg);
            }

        }

    }

    /**
     * This method checks the forward progress counter and the state of a dependency. If the
     * dependency is unavailable or failed, an error message is created which is checked when
     * evaluateSanity interface is called. If the error message is set then the evaluateSanity will
     * return an error.
     * 
     * @param dep the dependency
     */
    public String stateCheck(String dep) {
        logger.debug("checking state of dependent resource: {}", dep);
        String errorMsg = null;
        ForwardProgressEntity forwardProgressEntity = null;
        StateManagementEntity stateManagementEntity = null;

        // Start a transaction
        EntityTransaction et = em.getTransaction();
        et.begin();

        try {
            Query query = em.createQuery("Select p from ForwardProgressEntity p where p.resourceName=:resource");
            query.setParameter(LC_RESOURCE_STRING, dep);

            @SuppressWarnings("rawtypes")
            List fpList = query.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();

            if (!fpList.isEmpty()) {
                // exists
                forwardProgressEntity = (ForwardProgressEntity) fpList.get(0);
                // refresh the object from DB in case cached data was returned
                em.refresh(forwardProgressEntity);
                logger.debug("Found entry in ForwardProgressEntity table for dependent Resource={}", dep);
            } else {
                errorMsg = dep + ": resource not found in ForwardProgressEntity database table";
                logger.error("{}", errorMsg);
            }
            synchronized (imFlushLock) {
                et.commit();
            }
        } catch (Exception ex) {
            // log an error
            errorMsg = dep + ": ForwardProgressEntity DB operation failed with exception: ";
            logger.error("{}", errorMsg, ex);
            synchronized (imFlushLock) {
                if (et.isActive()) {
                    et.rollback();
                }
            }
        }

        if (errorMsg == null) {
            // Start a transaction
            et = em.getTransaction();
            et.begin();
            try {
                // query if StateManagement entry exists for dependent resource
                Query query = em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource");
                query.setParameter(LC_RESOURCE_STRING, dep);

                @SuppressWarnings("rawtypes")
                List smList = query.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
                if (!smList.isEmpty()) {
                    // exist
                    stateManagementEntity = (StateManagementEntity) smList.get(0);
                    // refresh the object from DB in case cached data was
                    // returned
                    em.refresh(stateManagementEntity);
                    logger.debug("Found entry in StateManagementEntity table for dependent Resource={}", dep);
                } else {
                    errorMsg = dep + ": resource not found in state management entity database table";
                    logger.error("{}", errorMsg);
                }

                synchronized (imFlushLock) {
                    et.commit();
                }
            } catch (Exception e) {
                // log an error
                errorMsg = dep + ": StateManagementEntity DB read failed with exception: ";
                logger.error("{}", errorMsg, e);
                synchronized (imFlushLock) {
                    if (et.isActive()) {
                        et.rollback();
                    }
                }
            }
        }

        // verify that the ForwardProgress is current (check last_updated)
        if (errorMsg == null) {
            if (forwardProgressEntity != null && stateManagementEntity != null) {
                Date date = MonitorTime.getInstance().getDate();
                long diffMs = date.getTime() - forwardProgressEntity.getLastUpdated().getTime();
                logger.debug("IntegrityMonitor.stateCheck(): diffMs = {}", diffMs);

                // Threshold for a stale entry
                long staleMs = maxFpcUpdateIntervalMs;
                logger.debug("IntegrityMonitor.stateCheck(): staleMs = {}", staleMs);

                if (diffMs > staleMs) {
                    // ForwardProgress is stale. Disable it
                    try {
                        if (!stateManagementEntity.getOpState().equals(StateManagement.DISABLED)) {
                            logger.debug("IntegrityMonitor.stateCheck(): Changing OpStat = disabled for {}", dep);
                            stateManager.disableFailed(dep);
                        }
                    } catch (Exception e) {
                        String msg = STATE_CHECK_STRING + dep
                                + "; " + e.getMessage();
                        logger.error("{}", msg, e);
                    }
                }
            } else {

                if (forwardProgressEntity == null) {
                    String msg = STATE_CHECK_STRING + dep
                            + "; " + " forwardProgressEntity == null.";
                    logger.error("{}", msg);
                }

                else {
                    String msg = STATE_CHECK_STRING + dep
                            + "; " + " stateManagementEntity == null.";
                    logger.error("{}", msg);
                }
            }
        }

        // check operation, admin and standby states of dependent resource
        if (errorMsg == null) {
            if (stateManagementEntity != null) {
                if ((stateManager.getAdminState() != null)
                        && stateManagementEntity.getAdminState().equals(StateManagement.LOCKED)) {
                    errorMsg = dep + ": resource is administratively locked";
                    logger.error("{}", errorMsg);
                } else if ((stateManager.getOpState() != null)
                        && stateManagementEntity.getOpState().equals(StateManagement.DISABLED)) {
                    errorMsg = dep + ": resource is operationally disabled";
                    logger.error("{}", errorMsg);
                } else if ((stateManager.getStandbyStatus() != null)
                        && stateManagementEntity.getStandbyStatus().equals(StateManagement.COLD_STANDBY)) {
                    errorMsg = dep + ": resource is cold standby";
                    logger.error("{}", errorMsg);
                }
            } else {
                errorMsg = dep + ": could not check standy state of resource. stateManagementEntity == null.";
                logger.error("{}", errorMsg);
            }
        }

        String returnMsg = "IntegrityMonitor.stateCheck(): returned error_msg: " + errorMsg;
        logger.debug("{}", returnMsg);
        return errorMsg;
    }

    private String fpCheck(String dep) {
        logger.debug("checking forward progress count of dependent resource: {}", dep);

        String errorMsg = null;

        // check FPC count - a changing FPC count indicates the resource JVM is
        // running

        // Start a transaction
        EntityTransaction et = em.getTransaction();
        et.begin();
        try {
            Query fquery = em.createQuery(QUERY_STRING);
            fquery.setParameter("rn", dep);

            @SuppressWarnings("rawtypes")
            List fpList = fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
            ForwardProgressEntity fpx;
            if (!fpList.isEmpty()) {
                // ignores multiple results
                fpx = (ForwardProgressEntity) fpList.get(0);
                // refresh the object from DB in case cached data was returned
                em.refresh(fpx);
                if (logger.isDebugEnabled()) {
                    logger.debug("Dependent resource {} - fpc= {}, lastUpdated={}", dep, fpx.getFpcCount(),
                            fpx.getLastUpdated());
                }
                long currTime = MonitorTime.getInstance().getMillis();
                // if dependent resource FPC has not been updated, consider it
                // an error
                if ((currTime - fpx.getLastUpdated().getTime()) > maxFpcUpdateIntervalMs) {
                    errorMsg = dep + ": FP count has not been updated in the last " + maxFpcUpdateIntervalMs + "ms";
                    logger.error("{}", errorMsg);
                    try {
                        // create instance of StateMangement class for dependent
                        StateManagement depStateManager = new StateManagement(emf, dep);
                        if (!depStateManager.getOpState().equals(StateManagement.DISABLED)) {
                            logger.debug("Forward progress not detected for dependent resource {}. Setting dependent's "
                                    + "state to disable failed.", dep);
                            depStateManager.disableFailed();
                        }
                    } catch (Exception e) {
                        // ignore errors
                        logger.error("Update dependent state failed with exception: ", e);
                    }
                }
            } else {
                // resource entry not found in FPC table
                errorMsg = dep + ": resource not found in ForwardProgressEntity table in the DB";
                logger.error("{}", errorMsg);
            }
            synchronized (imFlushLock) {
                et.commit();
            }
        } catch (Exception e) {
            // log an error and continue
            errorMsg = dep + ": ForwardProgressEntity DB read failed with exception: ";
            logger.error("{}", errorMsg, e);
            synchronized (imFlushLock) {
                if (et.isActive()) {
                    et.rollback();
                }
            }
        }

        return errorMsg;
    }

    /**
     * Get all forward progress entities.
     * 
     * @return list of all forward progress entities
     */
    public List<ForwardProgressEntity> getAllForwardProgressEntity() {
        logger.debug("getAllForwardProgressEntity: entry");
        ArrayList<ForwardProgressEntity> fpList = new ArrayList<>();
        // Start a transaction
        EntityTransaction et = em.getTransaction();
        et.begin();
        try {
            Query fquery = em.createQuery("Select e from ForwardProgressEntity e");
            @SuppressWarnings("rawtypes")
            List myList = fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
            synchronized (imFlushLock) {
                et.commit();
            }
            logger.debug("getAllForwardProgressEntity: myList.size(): {}", myList.size());
            if (!myList.isEmpty()) {
                for (int i = 0; i < myList.size(); i++) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("getAllForwardProgressEntity: myList.get({}).getResourceName(): {}", i,
                                ((ForwardProgressEntity) myList.get(i)).getResourceName());
                    }
                    fpList.add((ForwardProgressEntity) myList.get(i));
                }
            }
            synchronized (imFlushLock) {
                if (et.isActive()) {
                    et.commit();
                }
            }
        } catch (Exception e) {
            // log an error and continue
            String msg = "getAllForwardProgessEntity DB read failed with exception: ";
            logger.error("{}", msg, e);
            synchronized (imFlushLock) {
                if (et.isActive()) {
                    et.rollback();
                }
            }
        }
        return fpList;
    }

    private String jmxCheck(String dep) {
        logger.debug("checking health of dependent by calling test() via JMX on resource: {}", dep);

        String errorMsg = null;

        // get the JMX URL from the database
        String jmxUrl = null;
        // Start a transaction
        EntityTransaction et = em.getTransaction();
        et.begin();
        try {
            // query if ResourceRegistration entry exists for resourceName
            Query rquery = em.createQuery("Select r from ResourceRegistrationEntity r where r.resourceName=:rn");
            rquery.setParameter("rn", dep);

            @SuppressWarnings("rawtypes")
            List rrList = rquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
            ResourceRegistrationEntity rrx = null;

            if (!rrList.isEmpty()) {
                // ignores multiple results
                rrx = (ResourceRegistrationEntity) rrList.get(0);
                // refresh the object from DB in case cached data was returned
                em.refresh(rrx);
                jmxUrl = rrx.getResourceUrl();
                if (logger.isDebugEnabled()) {
                    logger.debug("Dependent Resource={}, url={}, createdDate={}", dep, jmxUrl, rrx.getCreatedDate());
                }
            } else {
                errorMsg = dep + ": resource not found in ResourceRegistrationEntity table in the DB";
                logger.error("{}", errorMsg);
            }

            synchronized (imFlushLock) {
                et.commit();
            }
        } catch (Exception e) {
            errorMsg = dep + ": ResourceRegistrationEntity DB read failed with exception: ";
            logger.error("{}", errorMsg, e);
            synchronized (imFlushLock) {
                if (et.isActive()) {
                    et.rollback();
                }
            }
        }

        if (jmxUrl != null) {
            JmxAgentConnection jmxAgentConnection = null;
            try {
                jmxAgentConnection = new JmxAgentConnection(jmxUrl);
                MBeanServerConnection mbeanServer = jmxAgentConnection.getMBeanConnection();
                ComponentAdminMBean admin =
                        JMX.newMXBeanProxy(mbeanServer, ComponentAdmin.getObjectName(dep), ComponentAdminMBean.class);

                // invoke the test method via the jmx proxy
                admin.test();
                logger.debug("Dependent resource {} sanity test passed", dep);
            } catch (Exception e) {
                errorMsg = dep + ": resource sanity test failed with exception: ";
                logger.error("{}", errorMsg, e);
            } finally {
                // close the JMX connector
                if (jmxAgentConnection != null) {
                    jmxAgentConnection.disconnect();
                }
            }
        }

        return errorMsg;
    }

    /**
     * Perform a dependency check.
     * 
     * @return an error message detailing any issues found
     */
    public String dependencyCheck() {
        logger.debug("dependencyCheck: entry");
        synchronized (dependencyCheckLock) {

            // Start with the error message empty
            String errorMsg = "";
            boolean dependencyFailure = false;

            /*
             * Before we check dependency groups we need to check subsystemTest.
             */
            try {
                // Test any subsystems that are not covered under the dependency
                // relationship
                subsystemTest();
            } catch (Exception e) {
                logger.error("IntegrityMonitor threw exception", e);
                dependencyFailure = true;
                // This indicates a subsystemTest failure
                try {
                    if (logger.isDebugEnabled()) {
                        logger.debug(
                                "{}: There has been a subsystemTest failure with error:{} Updating this resource's "
                                        + "state to disableDependency",
                                resourceName, e.getMessage());
                    }
                    // Capture the subsystemTest failure info
                    if (!errorMsg.isEmpty()) {
                        errorMsg = errorMsg.concat(",");
                    }
                    errorMsg = errorMsg.concat(resourceName + ": " + e.getMessage());
                    this.stateManager.disableDependency();
                } catch (Exception ex) {
                    logger.error(EXCEPTION_STRING, ex);
                    if (!errorMsg.isEmpty()) {
                        errorMsg = errorMsg.concat(",");
                    }
                    errorMsg = errorMsg.concat("\n" + resourceName
                            + ": Failed to disable dependency after subsystemTest failure due to: " + ex.getMessage());
                }
            }

            // Check the sanity of dependents for lead subcomponents
            if (depGroups != null && depGroups.length > 0) {
                // check state of resources in dependency groups
                for (String group : depGroups) {
                    group = group.trim();
                    if (group.isEmpty()) {
                        // ignore empty group
                        continue;
                    }
                    String[] dependencies = group.split(",");
                    if (logger.isDebugEnabled()) {
                        logger.debug("group dependencies = {}", Arrays.toString(dependencies));
                    }
                    int realDepCount = 0;
                    int failDepCount = 0;
                    for (String dep : dependencies) {
                        dep = dep.trim();
                        if (dep.isEmpty()) {
                            // ignore empty dependency
                            continue;
                        }
                        realDepCount++; // this is a valid dependency whose state is tracked
                        // if a resource is down, its FP count will not be incremented
                        String failMsg = fpCheck(dep);
                        if (failMsg == null) {
                            if (testViaJmx) {
                                failMsg = jmxCheck(dep);
                            } else {
                                failMsg = stateCheck(dep);
                            }
                        }
                        if (failMsg != null) {
                            failDepCount++;
                            if (!errorMsg.isEmpty()) {
                                errorMsg = errorMsg.concat(", ");
                            }
                            errorMsg = errorMsg.concat(failMsg);
                        }
                    } // end for (String dep : dependencies)

                    // if all dependencies in a group are failed, set this
                    // resource's state to disable dependency
                    if ((realDepCount > 0) && (failDepCount == realDepCount)) {
                        dependencyFailure = true;
                        try {
                            logger.debug("All dependents in group {} have failed their health check. Updating this "
                                    + "resource's state to disableDependency", group);
                            if (stateManager.getAvailStatus() == null || !((stateManager.getAvailStatus())
                                    .equals(StateManagement.DEPENDENCY)
                                    || (stateManager.getAvailStatus()).equals(StateManagement.DEPENDENCY_FAILED))) {
                                // Note: redundant calls are made by
                                // refreshStateAudit
                                this.stateManager.disableDependency();
                            }
                        } catch (Exception e) {
                            logger.error(EXCEPTION_STRING, e);
                            if (!errorMsg.isEmpty()) {
                                errorMsg = errorMsg.concat(",");
                            }
                            errorMsg = errorMsg.concat(resourceName + ": Failed to disable dependency");
                            break; // break out on failure and skip checking other groups
                        }
                    }
                    // check the next group

                } // end for (String group : depGroups)

                /*
                 * We have checked all the dependency groups. If all are ok and subsystemTest
                 * passed, dependencyFailure == false
                 */
                if (!dependencyFailure) {
                    try {
                        logger.debug(
                                "All dependency groups have at least one viable member. Updating this resource's state"
                                        + " to enableNoDependency");
                        if (stateManager.getAvailStatus() != null
                                && ((stateManager.getAvailStatus()).equals(StateManagement.DEPENDENCY)
                                        || (stateManager.getAvailStatus()).equals(StateManagement.DEPENDENCY_FAILED))) {
                            // Note: redundant calls are made by
                            // refreshStateAudit
                            this.stateManager.enableNoDependency();
                        }
                        // The refreshStateAudit will catch the case where it is disabled but
                        // availStatus != failed
                    } catch (Exception e) {
                        logger.error(EXCEPTION_STRING, e);
                        if (!errorMsg.isEmpty()) {
                            errorMsg = errorMsg.concat(",");
                        }
                        errorMsg = errorMsg.concat(resourceName + ": Failed to enable no dependency");
                    }
                }
            } else if (!dependencyFailure) {
                /*
                 * This is put here to clean up when no dependency group should exist, but one was
                 * erroneously added which caused the state to be disabled/dependency/coldstandby
                 * and later removed. We saw this happen in the lab, but is not very likely in a
                 * production environment...but you never know.
                 */
                try {
                    logger.debug("There are no dependents. Updating this resource's state to enableNoDependency");
                    if (stateManager.getAvailStatus() != null
                            && ((stateManager.getAvailStatus()).equals(StateManagement.DEPENDENCY)
                                    || (stateManager.getAvailStatus()).equals(StateManagement.DEPENDENCY_FAILED))) {
                        // Note: redundant calls are made by refreshStateAudit
                        this.stateManager.enableNoDependency();
                    }
                    // The refreshStateAudit will catch the case where it is disabled but
                    // availStatus != failed
                } catch (Exception e) {
                    logger.error(EXCEPTION_STRING, e);
                    if (!errorMsg.isEmpty()) {
                        errorMsg = errorMsg.concat(",");
                    }
                    errorMsg = errorMsg.concat(resourceName + ": Failed to enable no dependency");
                }
            }

            if (!errorMsg.isEmpty()) {
                logger.error("Sanity failure detected in a dependent resource: {}", errorMsg);

            }

            dependencyCheckErrorMsg = errorMsg;
            lastDependencyCheckTime = MonitorTime.getInstance().getMillis();
            logger.debug("dependencyCheck: exit");
            return errorMsg;
        }
    }

    /**
     * Execute a test transaction. It is called when the test transaction timer fires. It could be
     * overridden to provide additional test functionality. If overridden, the overriding method
     * must invoke startTransaction() and endTransaction() and check if the allNotWellMap is empty.
     */
    public void testTransaction() {
        synchronized (testTransactionLock) {
            logger.debug("testTransaction: entry");
            //
            // startTransaction() not required for testTransaction
            //

            // end transaction - increments local FP counter
            endTransaction();
        }
    }

    /**
     * Additional testing for subsystems that do not have a /test interface (for ex. 3rd party
     * processes like elk). This method would be overridden by the subsystem.
     */
    public void subsystemTest() throws IntegrityMonitorException {
        // Testing provided by subsystem
        logger.debug("IntegrityMonitor subsystemTest() OK");
    }

    /**
     * Checks admin state and resets transaction timer. Called by application at the start of a
     * transaction.
     * 
     * @throws AdministrativeStateException throws admin state exception if resource is locked
     * @throws StandbyStatusException if resource is in standby
     */
    public void startTransaction() throws AdministrativeStateException, StandbyStatusException {

        synchronized (startTransactionLock) {
            // check admin state and throw exception if locked
            if ((stateManager.getAdminState() != null) && stateManager.getAdminState().equals(StateManagement.LOCKED)) {
                String msg = RESOURCE_STRING + resourceName + " is administratively locked";

                throw new AdministrativeStateException("IntegrityMonitor Admin State Exception: " + msg);
            }
            // check standby state and throw exception if locked

            if ((stateManager.getStandbyStatus() != null)
                    && (stateManager.getStandbyStatus().equals(StateManagement.HOT_STANDBY)
                            || stateManager.getStandbyStatus().equals(StateManagement.COLD_STANDBY))) {
                String msg = RESOURCE_STRING + resourceName + " is standby";

                throw new StandbyStatusException("IntegrityMonitor Standby Status Exception: " + msg);
            }

            // reset transactionTimer so it will not fire
            elapsedTestTransTime = 0;
        }
    }

    /**
     * Increment the local forward progress counter. Called by application at the end of each
     * transaction (successful or not).
     */
    public void endTransaction() {
        synchronized (endTransactionLock) {
            if (getAllNotWellMap() != null) {
                if (!(getAllNotWellMap().isEmpty())) {
                    /*
                     * An entity has reported that it is not well. We must not allow the the forward
                     * progress counter to advance.
                     */
                    String msg = "allNotWellMap:";
                    for (Entry<String, String> entry : allNotWellMap.entrySet()) {
                        msg = msg.concat("\nkey = " + entry.getKey() + " msg = " + entry.getValue());
                    }
                    logger.error("endTransaction: allNotWellMap is NOT EMPTY.  Not advancing forward"
                            + "progress counter. \n{}\n", msg);
                    return;
                } else {
                    if (logger.isDebugEnabled() && getAllSeemsWellMap() != null && !(getAllSeemsWellMap().isEmpty())) {
                        String msg = "allSeemsWellMap:";
                        for (Entry<String, String> entry : allSeemsWellMap.entrySet()) {
                            msg = msg.concat("\nkey = " + entry.getKey() + " msg = " + entry.getValue());
                        }
                        logger.debug(
                                "endTransaction: allNotWellMap IS EMPTY and allSeemsWellMap is NOT EMPTY.  "
                                        + "Advancing forward progress counter. \n{}\n", msg);
                    }
                }
            }
            // increment local FPC
            fpCounter++;
        }
    }

    // update FP count in DB with local FP count
    private void writeFpc() throws IntegrityMonitorException {

        // Start a transaction
        EntityTransaction et = em.getTransaction();

        if (!et.isActive()) {
            et.begin();
        }

        try {
            // query if ForwardProgress entry exists for resourceName
            Query fquery = em.createQuery(QUERY_STRING);
            fquery.setParameter("rn", resourceName);

            @SuppressWarnings("rawtypes")
            List fpList = fquery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
            ForwardProgressEntity fpx;
            if (!fpList.isEmpty()) {
                // ignores multiple results
                fpx = (ForwardProgressEntity) fpList.get(0);
                // refresh the object from DB in case cached data was returned
                em.refresh(fpx);
                if (logger.isDebugEnabled()) {
                    logger.debug("Updating FP entry: Resource={}, fpcCount={}, lastUpdated={}, new fpcCount={}",
                            resourceName, fpx.getFpcCount(), fpx.getLastUpdated(), fpCounter);
                }
                fpx.setFpcCount(fpCounter);
                em.persist(fpx);
                // flush to the DB and commit
                synchronized (imFlushLock) {
                    et.commit();
                }
            } else {
                // Error - FP entry does not exist
                String msg = "FP entry not found in database for resource " + resourceName;
                throw new IntegrityMonitorException(msg);
            }
        } catch (Exception e) {
            try {
                synchronized (imFlushLock) {
                    if (et.isActive()) {
                        et.rollback();
                    }
                }
            } catch (Exception e1) {
                logger.error(EXCEPTION_STRING, e1);
            }
            logger.error("writeFpc DB table commit failed with exception: {}", e);
            throw e;
        }
    }

    // retrieve state manager reference
    public final StateManagement getStateManager() {
        return this.stateManager;
    }

    /**
     * Read and validate properties.
     * 
     * @throws IntegrityMonitorPropertiesException if a property is invalid
     */
    private static void validateProperties(Properties prop) throws IntegrityMonitorPropertiesException {

        if (prop.getProperty(IntegrityMonitorProperties.DB_DRIVER) == null) {
            String msg = IntegrityMonitorProperties.DB_DRIVER + NULL_PROPERTY_STRING;
            logger.error("{}", msg);
            throw new IntegrityMonitorPropertiesException(PROPERTY_EXCEPTION_STRING + msg);
        }

        if (prop.getProperty(IntegrityMonitorProperties.DB_URL) == null) {
            String msg = IntegrityMonitorProperties.DB_URL + NULL_PROPERTY_STRING;
            logger.error("{}", msg);
            throw new IntegrityMonitorPropertiesException(PROPERTY_EXCEPTION_STRING + msg);
        }

        if (prop.getProperty(IntegrityMonitorProperties.DB_USER) == null) {
            String msg = IntegrityMonitorProperties.DB_USER + NULL_PROPERTY_STRING;
            logger.error("{}", msg);
            throw new IntegrityMonitorPropertiesException(PROPERTY_EXCEPTION_STRING + msg);
        }

        if (prop.getProperty(IntegrityMonitorProperties.DB_PWD) == null) {
            String msg = IntegrityMonitorProperties.DB_PWD + NULL_PROPERTY_STRING;
            logger.error("{}", msg);
            throw new IntegrityMonitorPropertiesException(PROPERTY_EXCEPTION_STRING + msg);
        }

        if (prop.getProperty(IntegrityMonitorProperties.FP_MONITOR_INTERVAL) != null) {
            try {
                monitorIntervalMs = toMillis(
                        Integer.parseInt(prop.getProperty(IntegrityMonitorProperties.FP_MONITOR_INTERVAL).trim()));
            } catch (NumberFormatException e) {
                logger.warn(IGNORE_INVALID_PROPERTY_STRING, IntegrityMonitorProperties.FP_MONITOR_INTERVAL, e);
            }
        }

        if (prop.getProperty(IntegrityMonitorProperties.FAILED_COUNTER_THRESHOLD) != null) {
            try {
                failedCounterThreshold =
                        Integer.parseInt(prop.getProperty(IntegrityMonitorProperties.FAILED_COUNTER_THRESHOLD).trim());
            } catch (NumberFormatException e) {
                logger.warn(IGNORE_INVALID_PROPERTY_STRING, IntegrityMonitorProperties.FAILED_COUNTER_THRESHOLD, e);
            }
        }

        if (prop.getProperty(IntegrityMonitorProperties.TEST_TRANS_INTERVAL) != null) {
            try {
                testTransIntervalMs = toMillis(
                        Integer.parseInt(prop.getProperty(IntegrityMonitorProperties.TEST_TRANS_INTERVAL).trim()));
            } catch (NumberFormatException e) {
                logger.warn(IGNORE_INVALID_PROPERTY_STRING, IntegrityMonitorProperties.TEST_TRANS_INTERVAL, e);
            }
        }

        if (prop.getProperty(IntegrityMonitorProperties.WRITE_FPC_INTERVAL) != null) {
            try {
                writeFpcIntervalMs = toMillis(
                        Integer.parseInt(prop.getProperty(IntegrityMonitorProperties.WRITE_FPC_INTERVAL).trim()));
            } catch (NumberFormatException e) {
                logger.warn(IGNORE_INVALID_PROPERTY_STRING, IntegrityMonitorProperties.WRITE_FPC_INTERVAL, e);
            }
        }

        if (prop.getProperty(IntegrityMonitorProperties.CHECK_DEPENDENCY_INTERVAL) != null) {
            try {
                checkDependencyIntervalMs = toMillis(Integer
                        .parseInt(prop.getProperty(IntegrityMonitorProperties.CHECK_DEPENDENCY_INTERVAL).trim()));
            } catch (NumberFormatException e) {
                logger.warn(IGNORE_INVALID_PROPERTY_STRING, IntegrityMonitorProperties.CHECK_DEPENDENCY_INTERVAL, e);
            }
        }

        // dependency_groups are a semi-colon separated list of groups
        // each group is a comma separated list of resource names
        // For ex. dependency_groups = site_1.pap_1,site_1.pap_2 ; site_1.pdp_1,
        // site_1.pdp_2
        if (prop.getProperty(IntegrityMonitorProperties.DEPENDENCY_GROUPS) != null) {
            try {
                depGroups = prop.getProperty(IntegrityMonitorProperties.DEPENDENCY_GROUPS).split(";");
                if (logger.isDebugEnabled()) {
                    logger.debug("dependency groups property = {}", Arrays.toString(depGroups));
                }
            } catch (Exception e) {
                logger.warn(IGNORE_INVALID_PROPERTY_STRING, IntegrityMonitorProperties.DEPENDENCY_GROUPS, e);
            }
        }

        siteName = prop.getProperty(IntegrityMonitorProperties.SITE_NAME);
        if (siteName == null) {
            String msg = IntegrityMonitorProperties.SITE_NAME + NULL_PROPERTY_STRING;
            logger.error("{}", msg);
            throw new IntegrityMonitorPropertiesException(PROPERTY_EXCEPTION_STRING + msg);
        } else {
            siteName = siteName.trim();
        }

        nodeType = prop.getProperty(IntegrityMonitorProperties.NODE_TYPE);
        if (nodeType == null) {
            String msg = IntegrityMonitorProperties.NODE_TYPE + NULL_PROPERTY_STRING;
            logger.error("{}", msg);
            throw new IntegrityMonitorPropertiesException(PROPERTY_EXCEPTION_STRING + msg);
        } else {
            nodeType = nodeType.trim();
            if (!isNodeTypeEnum(nodeType)) {
                String msg = IntegrityMonitorProperties.NODE_TYPE + " property " + nodeType + " is invalid";
                logger.error("{}", msg);
                throw new IntegrityMonitorPropertiesException(PROPERTY_EXCEPTION_STRING + msg);
            }
        }

        if (prop.getProperty(IntegrityMonitorProperties.TEST_VIA_JMX) != null) {
            String jmxTest = prop.getProperty(IntegrityMonitorProperties.TEST_VIA_JMX).trim();
            testViaJmx = Boolean.parseBoolean(jmxTest);
        }

        if (prop.getProperty(IntegrityMonitorProperties.JMX_FQDN) != null) {
            jmxFqdn = prop.getProperty(IntegrityMonitorProperties.JMX_FQDN).trim();
            if (jmxFqdn.isEmpty()) {
                jmxFqdn = null;
            }
        }

        if (prop.getProperty(IntegrityMonitorProperties.MAX_FPC_UPDATE_INTERVAL) != null) {
            try {
                maxFpcUpdateIntervalMs = toMillis(
                        Integer.parseInt(prop.getProperty(IntegrityMonitorProperties.MAX_FPC_UPDATE_INTERVAL).trim()));
            } catch (NumberFormatException e) {
                logger.warn(IGNORE_INVALID_PROPERTY_STRING, IntegrityMonitorProperties.MAX_FPC_UPDATE_INTERVAL, e);
            }
        }

        if (prop.getProperty(IntegrityMonitorProperties.STATE_AUDIT_INTERVAL_MS) != null) {
            try {
                stateAuditIntervalMs =
                        Long.parseLong(prop.getProperty(IntegrityMonitorProperties.STATE_AUDIT_INTERVAL_MS));
            } catch (NumberFormatException e) {
                logger.warn(IGNORE_INVALID_PROPERTY_STRING, IntegrityMonitorProperties.STATE_AUDIT_INTERVAL_MS, e);
            }
        }

        if (prop.getProperty(IntegrityMonitorProperties.REFRESH_STATE_AUDIT_INTERVAL_MS) != null) {
            try {
                refreshStateAuditIntervalMs =
                        Long.parseLong(prop.getProperty(IntegrityMonitorProperties.REFRESH_STATE_AUDIT_INTERVAL_MS));
            } catch (NumberFormatException e) {
                logger.warn(IGNORE_INVALID_PROPERTY_STRING, IntegrityMonitorProperties.REFRESH_STATE_AUDIT_INTERVAL_MS,
                        e);
            }
        }

        logger.debug("IntegrityMonitor.validateProperties(): Property values \nmaxFpcUpdateIntervalMs = {}\n",
                maxFpcUpdateIntervalMs);
    }

    /**
     * Update properties.
     * 
     * @param newprop the new properties
     */
    public static void updateProperties(Properties newprop) {
        if (isUnitTesting()) {
            try {
                validateProperties(newprop);
            } catch (IntegrityMonitorPropertiesException e) {
                logger.error(EXCEPTION_STRING, e);
            }
        } else {
            logger.debug("Update integrity monitor properties not allowed");
        }
    }

    private static boolean isNodeTypeEnum(String nodeType) {
        String upper = nodeType.toUpperCase();
        for (NodeType n : NodeType.values()) {
            if (n.toString().equals(upper)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Look for "Forward Progress" -- if the 'FPMonitor' is stalled for too long, the operational
     * state is changed to 'Disabled', and an alarm is set. The state is restored when forward
     * progress continues.
     */
    private void fpMonitorCycle() {
        logger.debug("fpMonitorCycle(): entry");
        synchronized (fpMonitorCycleLock) {
            // monitoring interval checks
            if (monitorIntervalMs <= 0) {
                logger.debug("fpMonitorCycle(): disabled");
                elapsedTime = 0;
                return; // monitoring is disabled
            }

            elapsedTime = elapsedTime + CYCLE_INTERVAL_MILLIS;
            if (elapsedTime < monitorIntervalMs) {
                return; // monitoring interval not reached
            }

            elapsedTime = 0; // reset elapsed time

            try {
                if (fpCounter == lastFpCounter) {
                    // no forward progress
                    missedCycles += 1;
                    if (missedCycles >= failedCounterThreshold && !alarmExists) {
                        logger.debug("Forward progress not detected for resource {}. Setting state to disable failed.",
                                resourceName);
                        if (!(stateManager.getOpState()).equals(StateManagement.DISABLED)) {
                            // Note: The refreshStateAudit will make redundant
                            // calls
                            stateManager.disableFailed();
                        }
                        // The refreshStateAudit will catch the case where opStat = disabled and
                        // availState ! failed/dependency.failed
                        alarmExists = true;
                    }
                } else {
                    // forward progress has occurred
                    lastFpCounter = fpCounter;
                    missedCycles = 0;
                    // set op state to enabled
                    logger.debug("Forward progress detected for resource {}. Setting state to enable not failed.",
                            resourceName);
                    if (!(stateManager.getOpState()).equals(StateManagement.ENABLED)) {
                        // Note: The refreshStateAudit will make redundant calls
                        stateManager.enableNotFailed();
                    }
                    // The refreshStateAudit will catch the case where opState=enabled and
                    // availStatus != null
                    alarmExists = false;
                }
            } catch (Exception e) {
                // log error
                logger.error("FP Monitor encountered error. ", e);
            }
        }
        logger.debug("fpMonitorCycle(): exit");
    }

    /**
     * Look for "Forward Progress" on other nodes. If they are not making forward progress, check
     * their operational state. If it is not disabled, then disable them.
     */
    private void stateAudit() {
        logger.debug("IntegrityMonitor.stateAudit(): entry");
        if (stateAuditIntervalMs <= 0) {
            logger.debug("IntegrityMonitor.stateAudit(): disabled");
            return; // stateAudit is disabled
        }

        // Only run from nodes that are operational
        if (stateManager.getOpState().equals(StateManagement.DISABLED)) {
            logger.debug("IntegrityMonitor.stateAudit(): DISABLED. returning");
            return;
        }
        if (stateManager.getAdminState().equals(StateManagement.LOCKED)) {
            logger.debug("IntegrityMonitor.stateAudit(): LOCKED. returning");
            return;
        }
        if (!stateManager.getStandbyStatus().equals(StateManagement.NULL_VALUE)
            && stateManager.getStandbyStatus() != null
            && !stateManager.getStandbyStatus().equals(StateManagement.PROVIDING_SERVICE)) {
            logger.debug("IntegrityMonitor.stateAudit(): NOT PROVIDING_SERVICE. returning");
            return;
        }

        Date date = MonitorTime.getInstance().getDate();
        long timeSinceLastStateAudit = date.getTime() - lastStateAuditTime.getTime();
        if (timeSinceLastStateAudit < stateAuditIntervalMs) {
            logger.debug("IntegrityMonitor.stateAudit(): Not time to run. returning");
            return;
        }

        executeStateAudit();

        lastStateAuditTime = date;

        logger.debug("IntegrityMonitor.stateAudit(): exit");
    }

    /**
     * Execute state audit.
     */
    public void executeStateAudit() {
        logger.debug("IntegrityMonitor.executeStateAudit(): entry");
        Date date = MonitorTime.getInstance().getDate();

        // Get all entries in the forwardprogressentity table
        List<ForwardProgressEntity> fpList = getAllForwardProgressEntity();

        // Check if each forwardprogressentity entry is current
        for (ForwardProgressEntity fpe : fpList) {
            // If the this is my ForwardProgressEntity, continue
            if (fpe.getResourceName().equals(IntegrityMonitor.resourceName)) {
                continue;
            }
            // Make sure you are not getting a cached version
            em.refresh(fpe);
            long diffMs = date.getTime() - fpe.getLastUpdated().getTime();
            if (logger.isDebugEnabled()) {
                logger.debug("IntegrityMonitor.executeStateAudit(): resource = {}, diffMs = {}", fpe.getResourceName(),
                        diffMs);
            }

            // Threshold for a stale entry
            long staleMs = maxFpcUpdateIntervalMs;
            if (logger.isDebugEnabled()) {
                logger.debug("IntegrityMonitor.executeStateAudit(): resource = {}, staleMs = {}", fpe.getResourceName(),
                        staleMs);
            }

            if (diffMs > staleMs) {
                // ForwardProgress is stale. Disable it
                // Start a transaction
                logger.debug("IntegrityMonitor.executeStateAudit(): resource = {}, FPC is stale. Disabling it", 
                        fpe.getResourceName());
                EntityTransaction et = em.getTransaction();
                et.begin();
                StateManagementEntity sme = null;
                try {
                    // query if StateManagement entry exists for fpe resource
                    Query query =
                            em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource");
                    query.setParameter(LC_RESOURCE_STRING, fpe.getResourceName());

                    @SuppressWarnings("rawtypes")
                    List smList =
                            query.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
                    if (!smList.isEmpty()) {
                        // exists
                        sme = (StateManagementEntity) smList.get(0);
                        // refresh the object from DB in case cached data was
                        // returned
                        em.refresh(sme);
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "IntegrityMonitor.executeStateAudit(): Found entry in StateManagementEntity table "
                                            + "for Resource={}",
                                    sme.getResourceName());
                        }
                    } else {
                        String msg = "IntegrityMonitor.executeStateAudit(): " + fpe.getResourceName()
                                + ": resource not found in state management entity database table";
                        logger.error("{}", msg);
                    }
                    synchronized (imFlushLock) {
                        et.commit();
                    }
                } catch (Exception e) {
                    // log an error
                    logger.error("IntegrityMonitor.executeStateAudit(): {}: StateManagementEntity DB read failed with "
                            + "exception: ", fpe.getResourceName(), e);
                    synchronized (imFlushLock) {
                        if (et.isActive()) {
                            et.rollback();
                        }
                    }
                }

                if (sme != null && !sme.getOpState().equals(StateManagement.DISABLED)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("IntegrityMonitor.executeStateAudit(): Changing OpStat = disabled for {}",
                                sme.getResourceName());
                    }
                    try {
                        stateManager.disableFailed(sme.getResourceName());
                    } catch (Exception e) {
                        String msg = "IntegrityMonitor.executeStateAudit(): Failed to disable " + sme.getResourceName();
                        logger.error("{}", msg, e);
                    }
                }
            } // end if(diffMs > staleMs)
        } // end for(ForwardProgressEntity fpe : fpList)
        logger.debug("IntegrityMonitor.executeStateAudit(): exit");
    }

    /**
     * Execute a test transaction when test transaction interval has elapsed.
     */
    private void checkTestTransaction() {
        logger.debug("checkTestTransaction(): entry");
        synchronized (checkTestTransactionLock) {

            // test transaction timer checks
            if (testTransIntervalMs <= 0) {
                logger.debug("checkTestTransaction(): disabled");
                elapsedTestTransTime = 0;
                return; // test transaction is disabled
            }

            elapsedTestTransTime = elapsedTestTransTime + CYCLE_INTERVAL_MILLIS;
            if (elapsedTestTransTime < testTransIntervalMs) {
                return; // test transaction interval not reached
            }

            elapsedTestTransTime = 0; // reset elapsed time

            // execute test transaction
            testTransaction();
        }
        logger.debug("checkTestTransaction(): exit");
    }

    /**
     * Updates Fpc counter in database when write Fpc interval has elapsed.
     */
    private void checkWriteFpc() {
        logger.debug("checkWriteFpc(): entry");
        synchronized (checkWriteFpcLock) {

            // test transaction timer checks
            if (writeFpcIntervalMs <= 0) {
                logger.debug("checkWriteFpc(): disabled");
                elapsedWriteFpcTime = 0;
                return; // write Fpc is disabled
            }

            elapsedWriteFpcTime = elapsedWriteFpcTime + CYCLE_INTERVAL_MILLIS;
            if (elapsedWriteFpcTime < writeFpcIntervalMs) {
                return; // write Fpc interval not reached
            }

            elapsedWriteFpcTime = 0; // reset elapsed time

            // write Fpc to database
            try {
                writeFpc();
            } catch (Exception e) {
                logger.error(EXCEPTION_STRING, e);
            }
        }
        logger.debug("checkWriteFpc(): exit");
    }

    /**
     * Execute a dependency health check periodically which also updates this resource's state.
     */
    private void checkDependentHealth() {
        logger.debug("checkDependentHealth: entry");
        if (checkDependencyIntervalMs <= 0) {
            logger.debug("checkDependentHealth: disabled");
            return; // dependency monitoring is disabled
        }

        long currTime = MonitorTime.getInstance().getMillis();
        logger.debug("checkDependentHealth currTime - lastDependencyCheckTime = {}",
                currTime - lastDependencyCheckTime);
        if ((currTime - lastDependencyCheckTime) > checkDependencyIntervalMs) {
            // execute dependency check and update this resource's state

            dependencyCheck();
        }
        logger.debug("checkDependentHealth: exit");
    }

    /*
     * This is a simple refresh audit which is periodically run to assure that the states and status
     * attributes are aligned and notifications are sent to any listeners. It is possible for
     * state/status to get out of synch and notified systems to be out of synch due to database
     * corruption (manual or otherwise) or because a node became isolated.
     * 
     * When the operation (lock/unlock) is called, it will cause a re-evaluation of the state and
     * send a notification to all registered observers.
     */
    private void refreshStateAudit() {
        logger.debug("refreshStateAudit(): entry");
        if (refreshStateAuditIntervalMs <= 0) {
            // The audit is disabled
            logger.debug("refreshStateAudit(): disabled");
            return;
        }
        executeRefreshStateAudit();
        logger.debug("refreshStateAudit(): exit");
    }

    /**
     * Execute refresh state audit.
     */
    public void executeRefreshStateAudit() {
        logger.debug("executeRefreshStateAudit(): entry");
        synchronized (refreshStateAuditLock) {
            logger.debug("refreshStateAudit: entry");
            Date now = MonitorTime.getInstance().getDate();
            long nowMs = now.getTime();
            long lastTimeMs = refreshStateAuditLastRunDate.getTime();
            logger.debug("refreshStateAudit: ms since last run = {}", nowMs - lastTimeMs);

            if ((nowMs - lastTimeMs) > refreshStateAuditIntervalMs) {
                String adminState = stateManager.getAdminState();
                logger.debug("refreshStateAudit: adminState = {}", adminState);
                if (adminState.equals(StateManagement.LOCKED)) {
                    try {
                        logger.debug("refreshStateAudit: calling lock()");
                        stateManager.lock();
                    } catch (Exception e) {
                        logger.error("refreshStateAudit: caught unexpected exception from stateManager.lock(): ", e);
                    }
                } else { // unlocked
                    try {
                        logger.debug("refreshStateAudit: calling unlock()");
                        stateManager.unlock();
                    } catch (Exception e) {
                        logger.error("refreshStateAudit: caught unexpected exception from stateManager.unlock(): ", e);
                    }
                }
                refreshStateAuditLastRunDate = MonitorTime.getInstance().getDate();
                logger.debug("refreshStateAudit: exit");
            }
        }
        logger.debug("executeRefreshStateAudit(): exit");
    }

    /**
     * The following nested class periodically performs the forward progress check, checks
     * dependencies, does a refresh state audit and runs the stateAudit.
     */
    class FpManager extends Thread {
        
        private volatile boolean stopRequested = false;

        // Constructor - start FP manager thread
        FpManager() {
            // set now as the last time the refreshStateAudit ran
            IntegrityMonitor.this.refreshStateAuditLastRunDate = MonitorTime.getInstance().getDate();
        }

        @Override
        public void run() {
            logger.debug("FPManager thread running");

            try {
                factory.runStarted();

                while (!stopRequested) {
                    MonitorTime.getInstance().sleep(CYCLE_INTERVAL_MILLIS);
                    
                    IntegrityMonitor.this.runOnce();
                    factory.monitorCompleted();
                }

            } catch (InterruptedException e) {
                logger.debug(EXCEPTION_STRING, e);
                Thread.currentThread().interrupt();
            }
        }

        public void stopAndExit() {
            stopRequested = true;
            this.interrupt();
        }
    }

    private void runOnce() {
        try {
            logger.debug("FPManager calling fpMonitorCycle()");
            // check forward progress timer
            fpMonitorCycle();

            logger.debug("FPManager calling checkTestTransaction()");
            // check test transaction timer
            checkTestTransaction();

            logger.debug("FPManager calling checkWriteFpc()");
            // check write Fpc timer
            checkWriteFpc();

            logger.debug("FPManager calling checkDependentHealth()");
            // check dependency health
            checkDependentHealth();

            logger.debug("FPManager calling refreshStateAudit()");
            // check if it is time to run the refreshStateAudit
            refreshStateAudit();

            logger.debug("FPManager calling stateAudit()");
            // check if it is time to run the stateAudit
            stateAudit();

        } catch (Exception e) {
            logger.error("Ignore FPManager thread processing timer(s) exception: ", e);
        }
    }

    /**
     * Set all seems well or not well for the specified key.
     * 
     * @param key the key
     * @param asw <code>true</code> if all seems well for the key, <code>false</code> if all seems
     *        not well for the key
     * @param msg message to add for the key
     * @throws AllSeemsWellException if an error occurs
     */
    public void allSeemsWell(String key, Boolean asw, String msg)
            throws AllSeemsWellException {

        logger.debug("allSeemsWell entry: key = {}, asw = {}, msg = {}", key, asw, msg);
        if (key == null || key.isEmpty()) {
            logger.error("allSeemsWell: 'key' has no visible content");
            throw new IllegalArgumentException("allSeemsWell: 'key' has no visible content");
        }
        if (asw == null) {
            logger.error("allSeemsWell: 'asw' is null");
            throw new IllegalArgumentException("allSeemsWell: 'asw' is null");
        }
        if (msg == null || msg.isEmpty()) {
            logger.error("allSeemsWell: 'msg' has no visible content");
            throw new IllegalArgumentException("allSeemsWell: 'msg' has no visible content");
        }

        if (allSeemsWellMap == null) {
            allSeemsWellMap = new HashMap<>();
        }

        if (allNotWellMap == null) {
            allNotWellMap = new HashMap<>();
        }

        if (asw) {
            logger.info("allSeemsWell: ALL SEEMS WELL: key = {}, msg = {}", key, msg);
            try {
                allSeemsWellMap.put(key, msg);
            } catch (Exception e) {
                String exceptMsg =
                        "allSeemsWell: encountered an exception with allSeemsWellMap.put(" + key + "," + msg + ")";
                logger.error(exceptMsg);
                throw new AllSeemsWellException(exceptMsg, e);
            }

            try {
                allNotWellMap.remove(key);
            } catch (Exception e) {
                String exceptMsg = "allSeemsWell: encountered an exception with allNotWellMap.delete(" + key + ")";
                logger.error(exceptMsg);
                throw new AllSeemsWellException(exceptMsg, e);
            }

        } else {
            logger.error("allSeemsWell: ALL NOT WELL: key = {}, msg = {}", key, msg);
            try {
                allSeemsWellMap.remove(key);
            } catch (Exception e) {
                String exceptMsg = "allSeemsWell: encountered an exception with allSeemsWellMap.remove(" + key + ")";
                logger.error(exceptMsg);
                throw new AllSeemsWellException(exceptMsg, e);
            }

            try {
                allNotWellMap.put(key, msg);
            } catch (Exception e) {
                String exceptMsg = "allSeemsWell: encountered an exception with allNotWellMap.put(" + key + msg + ")";
                logger.error(exceptMsg);
                throw new AllSeemsWellException(exceptMsg, e);
            }
        }

        if (logger.isDebugEnabled()) {
            for (Entry<String, String> entry : allSeemsWellMap.entrySet()) {
                logger.debug("allSeemsWellMap: key = {}  msg = {}", entry.getKey(), entry.getValue());
            }
            for (Entry<String, String> entry : allNotWellMap.entrySet()) {
                logger.debug("allNotWellMap: key = {}  msg = {}", entry.getKey(), entry.getValue());
            }
            logger.debug("allSeemsWell exit");
        }
    }
    
    /**
     * Converts the given value to milliseconds using the current {@link #propertyUnits}.
     * 
     * @param value value to be converted, or -1
     * @return the value, in milliseconds, or -1
     */
    private static long toMillis(long value) {
        return (value < 0 ? -1 : value * 1000L);
    }

    public Map<String, String> getAllSeemsWellMap() {
        return allSeemsWellMap;
    }

    public Map<String, String> getAllNotWellMap() {
        return allNotWellMap;
    }

    /**
     * Wrapper used to access various objects. Overridden by junit tests.
     */
    public static class Factory {

        /**
         * Indicates that the {@link FpManager#run()} method has started. This method
         * simply returns.
         * 
         * @throws InterruptedException can be interrupted
         */
        public void runStarted() throws InterruptedException {
            // does nothing
        }

        /**
         * Indicates that a monitor activity has completed. This method simply returns.
         * 
         * @throws InterruptedException can be interrupted
         */
        public void monitorCompleted() throws InterruptedException {
            // does nothing
        }

        /**
         * Get persistence unit.
         * 
         * @return the persistence unit to be used
         */
        public String getPersistenceUnit() {
            return PERSISTENCE_UNIT;
        }
    }

    /*
     * The remaining methods are used by JUnit tests.
     */

    public static boolean isUnitTesting() {
        return isUnitTesting;
    }

    public static void setUnitTesting(boolean isUnitTesting) {
        IntegrityMonitor.isUnitTesting = isUnitTesting;
    }
}