Fix sonar critical associated with logging
Addressing the following sonar critical:
Use a logger to log this exception
Either log or rethrow this exception
It does not hurt to simply do these as well as clean out
lines of code that dump to Sys.out via e.printStackTrace.
Issue-ID: POLICY-195
Change-Id: Ibc328094a0653b7baa04778298e4a69955105ea3
Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java
index 20132ae..b392b55 100644
--- a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java
+++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java
@@ -141,10 +141,7 @@
resultMap.put(key, o);
}
}catch(Exception e){
- String msg = "getAllEntries encountered exception: " + e;
- logger.error(msg);
- System.out.println(new Date());
- e.printStackTrace();
+ logger.error("getAllEntries encountered exception: ", e);
}
em.close();
logger.debug("getAllMyEntries: Exit, resultMap.keySet()=" + resultMap.keySet());
@@ -170,10 +167,7 @@
resultMap.put(key, entry);
}
}catch(Exception e){
- String msg = "getAllMyEntries encountered exception: " + e;
- logger.error(msg);
- System.out.println(new Date());
- e.printStackTrace();
+ logger.error("getAllMyEntries encountered exception: ", e);
}
em.close();
@@ -210,10 +204,7 @@
resultMap.put(key, o);
}
}catch(Exception e){
- String msg = "getAllEntries encountered exception:" + e;
- logger.error(msg);
- System.out.println(new Date());
- e.printStackTrace();
+ logger.error("getAllEntries encountered exception:", e);
}
em.close();
@@ -288,9 +279,7 @@
return iaeList;
}catch (Exception e){
String msg = "DbDAO: " + "getIntegrityAuditEntities() " + "ecountered a problem in execution: ";
- logger.error(msg + e);
- System.out.println(new Date());
- e.printStackTrace();
+ logger.error(msg, e);
throw new DbDaoTransactionException(e);
}
@@ -454,8 +443,6 @@
}catch (Exception e){
String msg = "DbDAO: " + "register() " + "encountered a problem in execution: ";
logger.error(msg + e);
- System.out.println(new Date());
- e.printStackTrace();
throw new DbDaoTransactionException(e);
}
@@ -513,8 +500,6 @@
}catch (Exception e){
String msg = "DbDAO: " + "setDesignated() " + "ecountered a problem in execution: ";
logger.error(msg + e);
- System.out.println(new Date());
- e.printStackTrace();
throw new DbDaoTransactionException(e);
}
@@ -566,8 +551,6 @@
}catch (Exception e){
String msg = "DbDAO: " + "setLastUpdated() " + "ecountered a problem in execution: ";
logger.error(msg + e);
- System.out.println(new Date());
- e.printStackTrace();
throw new DbDaoTransactionException(e);
}
@@ -609,8 +592,6 @@
}catch (Exception e){
String msg = "DbDAO: " + "deleteAllIntegrityAuditEntities() " + "encountered a problem in execution: ";
logger.error(msg + e);
- System.out.println(new Date());
- e.printStackTrace();
throw new DbDaoTransactionException(e);
}
@@ -714,8 +695,6 @@
String msg = "DbDAO: " + "changeDesignated() "
+ "caught LockTimeoutException, message=" + e.getMessage();
logger.error(msg + e);
- System.out.println(new Date());
- e.printStackTrace();
throw new DbDaoTransactionException(msg, e);
}
else {
@@ -723,8 +702,6 @@
+ "caught LockTimeoutException, message=" + e.getMessage()
+ ". Error rolling back transaction.";
logger.error(msg + e);
- System.out.println(new Date());
- e.printStackTrace();
throw new DbDaoTransactionException(msg, e);
}
} catch (Exception e) {
@@ -743,8 +720,6 @@
+ "caught LockTimeoutException, message=" + e.getMessage()
+ ". Error rolling back transaction.";
logger.error(msg + e);
- System.out.println(new Date());
- e.printStackTrace();
throw new DbDaoTransactionException(msg, e);
}
}
diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java
index ce04254..fef4b89 100644
--- a/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java
+++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java
@@ -549,8 +549,8 @@
} catch (Exception e) {
String msg = "IntegrityMonitor.stateCheck(): Failed to diableFail dependent resource = " + dep
+ "; " + e.getMessage();
- logger.debug(msg);
- logger.error(msg);
+ logger.debug(msg, e);
+ logger.error(msg, e);
}
}
}
@@ -844,6 +844,7 @@
this.stateManager.disableDependency();
}
} catch (Exception e) {
+ logger.error(e);
if (!error_msg.isEmpty()) {
error_msg = error_msg.concat(",");
}
@@ -867,6 +868,7 @@
this.stateManager.enableNoDependency();
} // The refreshStateAudit will catch the case where it is disabled but availStatus != failed
} catch (Exception e) {
+ logger.error(e);
if (!error_msg.isEmpty()) {
error_msg = error_msg.concat(",");
}
@@ -887,6 +889,7 @@
this.stateManager.enableNoDependency();
}// The refreshStateAudit will catch the case where it is disabled but availStatus != failed
} catch (Exception e) {
+ logger.error(e);
if (!error_msg.isEmpty()) {
error_msg = error_msg.concat(",");
}
@@ -913,6 +916,7 @@
error_msg = error_msg.concat(resourceName + ": " + e.getMessage());
this.stateManager.disableDependency();
} catch (Exception ex) {
+ logger.error(ex);
if (!error_msg.isEmpty()) {
error_msg = error_msg.concat(",");
}
@@ -943,7 +947,7 @@
try {
startTransaction();
} catch (AdministrativeStateException | StandbyStatusException e) {
- // ignore
+ logger.debug(e);
}
// TODO: add test functionality if needed
@@ -1049,7 +1053,7 @@
}
}
} catch (Exception e1) {
- // ignore
+ logger.debug(e1);
}
logger.error("writeFpc DB table commit failed with exception: " + e);
throw e;
@@ -1131,7 +1135,7 @@
dep_groups = prop.getProperty(IntegrityMonitorProperties.DEPENDENCY_GROUPS).split(";");
logger.info("dependency groups property = " + Arrays.toString(dep_groups));
} catch (Exception e) {
- logger.warn("Ignored invalid property: " + IntegrityMonitorProperties.DEPENDENCY_GROUPS);
+ logger.warn("Ignored invalid property: " + IntegrityMonitorProperties.DEPENDENCY_GROUPS, e);
}
}
@@ -1203,7 +1207,7 @@
try {
validateProperties(newprop);
} catch (IntegrityMonitorPropertiesException e) {
- // ignore
+ logger.debug(e);
}
}
else {
@@ -1355,9 +1359,9 @@
}
} catch (Exception e) {
// log an error
- String msg = "IntegrityMonitor.stateAudit(): " + fpe.getResourceName() + ": StateManagementEntity DB read failed with exception: " + e;
+ String msg = "IntegrityMonitor.stateAudit(): " + fpe.getResourceName() + ": StateManagementEntity DB read failed with exception: ";
+ logger.error(msg, e);
logger.debug(msg);
- logger.error(msg);
synchronized(IMFLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -1372,7 +1376,7 @@
} catch (Exception e) {
String msg = "IntegrityMonitor.stateAudit(): Failed to disable " + sme.getResourceName();
logger.debug(msg);
- logger.error(msg);
+ logger.error(msg, e);
}
}
}// end if(diffMs > staleMs)
@@ -1427,7 +1431,7 @@
try {
writeFpc();
} catch (Exception e) {
- // ignore
+ logger.debug(e);
}
}
}
@@ -1477,9 +1481,6 @@
stateManager.lock();
} catch (Exception e) {
logger.error("refreshStateAudit: caught unexpected exception from stateManager.lock(): " + e );
- System.out.println(new Date() + " refreshStateAudit: caught unexpected exception "
- + "from stateManager.lock()");
- e.printStackTrace();
}
}else{//unlocked
try {
@@ -1487,9 +1488,6 @@
stateManager.unlock();;
} catch (Exception e) {
logger.error("refreshStateAudit: caught unexpected exception from stateManager.unlock(): " + e );
- System.out.println(new Date() + " refreshStateAudit: caught unexpected exception "
- + "from stateManager.unlock()");
- e.printStackTrace();
}
}
refreshStateAuditLastRunDate = new Date();
@@ -1528,6 +1526,8 @@
Thread.sleep(CYCLE_INTERVAL_MILLIS);
} catch (InterruptedException e) {
// The 'sleep' call was interrupted
+ logger.debug(e);
+ Thread.currentThread().interrupt();
continue;
}
diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java
index d8e0f69..bcf910f 100644
--- a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java
+++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java
@@ -136,7 +136,6 @@
logger.debug("StateManagement: constructor end, resourceName: " + this.resourceName);
} catch(Exception ex) {
logger.error("StateManagement: constructor caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -182,7 +181,6 @@
logger.debug("StateManagement: initializeState() operation completed, resourceName = " + this.resourceName);
} catch(Exception ex) {
logger.error("StateManagement.initializeState() caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -230,7 +228,6 @@
logger.debug("StateManagement: lock() operation completed, resourceName = " + this.resourceName);
} catch(Exception ex) {
logger.error("StateManagement.lock() caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -277,7 +274,6 @@
logger.debug("StateManagement: unlock() operation completed, resourceName = " + this.resourceName);
} catch(Exception ex) {
logger.error("StateManagement.unlock() caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -325,7 +321,6 @@
logger.debug("StateManagement enableNotFailed() operation completed, resourceName = " + this.resourceName);
} catch(Exception ex) {
logger.error("StateManagement.enableNotFailed() caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -371,7 +366,6 @@
logger.debug("StateManagement: disableFailed() operation completed, resourceName = " + this.resourceName);
} catch(Exception ex) {
logger.error("StateManagement.disableFailed() caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -424,7 +418,6 @@
+ otherResourceName);
} catch(Exception ex) {
logger.error("StateManagement.disableFailed(otherResourceName) caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -471,7 +464,6 @@
logger.debug("StateManagement: disableDependency() operation completed, resourceName = " + this.resourceName);
} catch(Exception ex) {
logger.error("StateManagement.disableDependency() caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -519,7 +511,6 @@
logger.debug("StateManagement: enableNoDependency() operation completed, resourceName = " + this.resourceName);
} catch(Exception ex) {
logger.error("StateManagement.enableNoDependency() caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -569,7 +560,6 @@
notifyObservers(STANDBY_STATUS);
}catch(Exception ex){
logger.error("StateManagement.promote() caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -622,7 +612,6 @@
logger.debug("StateManagement: demote() operation completed, resourceName = " + this.resourceName);
} catch(Exception ex) {
logger.error("StateManagement.demote() caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -677,7 +666,6 @@
logger.debug("StateManagement: demote(otherResourceName) operation completed, resourceName = " + otherResourceName);
} catch(Exception ex) {
logger.error("StateManagement.demote(otherResourceName) caught unexpected exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -713,8 +701,7 @@
this.adminState = null;
}
} catch(Exception ex) {
- ex.printStackTrace();
- logger.error("StateManagement: getAdminState exception: " + ex.toString());
+ logger.error("StateManagement: getAdminState exception: " + ex);
}
return this.adminState;
@@ -745,8 +732,7 @@
this.opState = null;
}
} catch(Exception ex) {
- ex.printStackTrace();
- logger.error("StateManagement: getOpState exception: " + ex.toString());
+ logger.error("StateManagement: getOpState exception: " + ex);
}
return this.opState;
@@ -777,8 +763,7 @@
this.availStatus = null;
}
} catch(Exception ex) {
- ex.printStackTrace();
- logger.error("StateManagement: getAvailStatus exception: " + ex.toString());
+ logger.error("StateManagement: getAvailStatus exception: ", ex);
}
return this.availStatus;
@@ -809,8 +794,7 @@
this.standbyStatus = null;
}
} catch(Exception ex) {
- ex.printStackTrace();
- logger.error("StateManagement: getStandbyStatus exception: " + ex.toString());
+ logger.error("StateManagement: getStandbyStatus exception: " + ex);
}
return this.standbyStatus;
@@ -851,8 +835,7 @@
stateManagementEntity.setStandbyStatus(NULL_VALUE); // default
}
} catch(Exception ex) {
- ex.printStackTrace();
- logger.error("findStateManagementEntity exception: " + ex.toString());
+ logger.error("findStateManagementEntity exception: " + ex);
}
return stateManagementEntity;
}
@@ -903,8 +886,7 @@
}
} catch (Exception e) {
logger.error("getStandbyStatus: Caught Exception attempting to get statemanagemententity record, message='"
- + e.getMessage() + "'");
- e.printStackTrace();
+ + e.getMessage() + "'", e);
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
@@ -954,7 +936,6 @@
}
}catch(Exception ex){
logger.error("StateManagement.deleteAllStateManagementEntities() caught Exception: " + ex);
- ex.printStackTrace();
synchronized(FLUSHLOCK){
if(et.isActive()){
et.rollback();
diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateTransition.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateTransition.java
index a45a536..03f6f4e 100644
--- a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateTransition.java
+++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateTransition.java
@@ -55,6 +55,7 @@
setupStateTable();
} catch(Exception ex) {
+ logger.debug(ex);
throw new StateTransitionException("StateTransition Exception: " + ex.toString());
}
}
@@ -142,7 +143,7 @@
stateElement.displayStateElement();
} catch(Exception ex) {
- logger.error("String split exception: " + ex.toString());
+ logger.error("String split exception: " + ex);
}
} else {
@@ -152,6 +153,7 @@
throw new StateTransitionException(msg);
}
} catch (Exception ex) {
+ logger.debug(ex);
throw new StateTransitionException("Exception: " + ex.toString() + ", adminState=[" + adminState + "], opState=[" + opState + "], availStatus=[" +
availStatus + "], standbyStatus=[" + standbyStatus + "], actionName=[" + actionName + "]");
}
diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java
index 9b9bc48..091f2d5 100644
--- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java
+++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java
@@ -26,6 +26,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
+
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationListener;
@@ -34,10 +35,16 @@
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.Logger;
+
/**
* Class to create a JMX RMI connection to the JmxAgent.
*/
public final class JmxAgentConnection {
+
+ private static final Logger logger = FlexLogger.getLogger(JmxAgentConnection.class);
+
private static final String DEFAULT_HOST = "localhost";
private static final String DEFAULT_PORT = "9996";
@@ -117,7 +124,7 @@
*/
public void disconnect() {
if (connector != null) {
- try { connector.close(); } catch (IOException e) { }
+ try { connector.close(); } catch (IOException e) { logger.debug(e); }
}
}
}
diff --git a/site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java b/site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java
index df843f3..78be681 100644
--- a/site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java
+++ b/site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java
@@ -298,7 +298,7 @@
}
catch (Exception e)
{
- // ignore exception
+ System.err.println(e);
}
}
@@ -486,7 +486,7 @@
}
catch (Exception e)
{
- // ignore any errors here
+ System.err.println(e);
}
}
}