Fix sonar critical associated with logging 69/11969/3
authorPamela Dragosh <pdragosh@research.att.com>
Tue, 12 Sep 2017 14:28:35 +0000 (10:28 -0400)
committerPamela Dragosh <pdragosh@research.att.com>
Tue, 12 Sep 2017 16:46:46 +0000 (12:46 -0400)
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>
integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java
integrity-monitor/src/main/java/org/onap/policy/common/im/IntegrityMonitor.java
integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java
integrity-monitor/src/main/java/org/onap/policy/common/im/StateTransition.java
integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java
site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java

index 20132ae..b392b55 100644 (file)
@@ -141,10 +141,7 @@ public class DbDAO {
                                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 @@ public class DbDAO {
                                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 @@ public class DbDAO {
                                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 @@ public class DbDAO {
                        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 @@ public class DbDAO {
                }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 @@ public class DbDAO {
                }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 @@ public class DbDAO {
                }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 @@ public class DbDAO {
                }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 @@ public class DbDAO {
                                        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 @@ public class DbDAO {
                                                        + "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 @@ public class DbDAO {
                                                        + "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);    
                                }
                        }
index ce04254..fef4b89 100644 (file)
@@ -549,8 +549,8 @@ public class IntegrityMonitor {
                                        } 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 @@ public class IntegrityMonitor {
                                                                        this.stateManager.disableDependency();
                                                                }
                                                } catch (Exception e) {
+                                                       logger.error(e);
                                                        if (!error_msg.isEmpty()) {
                                                                error_msg = error_msg.concat(",");
                                                        }
@@ -867,6 +868,7 @@ public class IntegrityMonitor {
                                                        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 @@ public class IntegrityMonitor {
                                                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 @@ public class IntegrityMonitor {
                                        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 @@ public class IntegrityMonitor {
                        try {
                                startTransaction();
                        } catch (AdministrativeStateException | StandbyStatusException e) {
-                               // ignore
+                               logger.debug(e);
                        }
 
                        // TODO: add test functionality if needed
@@ -1049,7 +1053,7 @@ public class IntegrityMonitor {
                                }
                        }
                } catch (Exception e1) {
-                       // ignore
+                       logger.debug(e1);
                }
                logger.error("writeFpc DB table commit failed with exception: " + e);
                throw e;
@@ -1131,7 +1135,7 @@ public class IntegrityMonitor {
                                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 @@ public class IntegrityMonitor {
                        try {
                                validateProperties(newprop);
                        } catch (IntegrityMonitorPropertiesException e) {
-                               // ignore
+                               logger.debug(e);
                        }
                }
                else {
@@ -1355,9 +1359,9 @@ public class IntegrityMonitor {
                                        }
                                } 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 @@ public class IntegrityMonitor {
                                                } 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 @@ public class IntegrityMonitor {
                        try {
                                writeFpc();
                        } catch (Exception e) {
-                               // ignore
+                               logger.debug(e);
                        }
                }
        }
@@ -1477,9 +1481,6 @@ public class IntegrityMonitor {
                                                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 @@ public class IntegrityMonitor {
                                                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 @@ public class IntegrityMonitor {
                                        Thread.sleep(CYCLE_INTERVAL_MILLIS);
                                } catch (InterruptedException e) {
                                        // The 'sleep' call was interrupted
+                                       logger.debug(e);
+                                       Thread.currentThread().interrupt();
                                        continue;
                                }
                                
index d8e0f69..bcf910f 100644 (file)
@@ -136,7 +136,6 @@ public class StateManagement extends Observable {
         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 @@ public class StateManagement extends Observable {
                          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 @@ public class StateManagement extends Observable {
                          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 @@ public class StateManagement extends Observable {
                          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 @@ public class StateManagement extends Observable {
                          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 @@ public class StateManagement extends Observable {
                          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 @@ public class StateManagement extends Observable {
                                          + 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 @@ public class StateManagement extends Observable {
                          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 @@ public class StateManagement extends Observable {
                          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 @@ public class StateManagement extends Observable {
                          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 @@ public class StateManagement extends Observable {
                          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 @@ public class StateManagement extends Observable {
                          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 @@ public String getAdminState()
                  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 @@ public String getOpState()
                  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 @@ public String getOpState()
                  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 @@ public String getOpState()
                  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 @@ public String getOpState()
                  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 @@ public String getOpState()
                        }
                } 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 @@ public String getOpState()
                  }
          }catch(Exception ex){
                  logger.error("StateManagement.deleteAllStateManagementEntities() caught Exception: " + ex);
-                 ex.printStackTrace();
                  synchronized(FLUSHLOCK){
                          if(et.isActive()){
                                  et.rollback();
index a45a536..03f6f4e 100644 (file)
@@ -55,6 +55,7 @@ public class StateTransition {
  
                  setupStateTable();
       } catch(Exception ex) {
+         logger.debug(ex);
          throw new StateTransitionException("StateTransition Exception: " + ex.toString());
       } 
   }
@@ -142,7 +143,7 @@ public class StateTransition {
                     
                         stateElement.displayStateElement();
              } catch(Exception ex) {
-                    logger.error("String split exception: " + ex.toString());
+                    logger.error("String split exception: " + ex);
              }
  
                 } else {
@@ -152,6 +153,7 @@ public class StateTransition {
                     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 + "]");
         }
index 9b9bc48..091f2d5 100644 (file)
@@ -26,6 +26,7 @@ package org.onap.policy.common.im.jmx;
 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.JMXConnector;
 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 final class JmxAgentConnection {
         */
        public void disconnect() {
                if (connector != null) {
-                       try { connector.close(); } catch (IOException e) { }
+                       try { connector.close(); } catch (IOException e) { logger.debug(e); }
                }
        }
 }
index df843f3..78be681 100644 (file)
@@ -298,7 +298,7 @@ public class Main
                  }
                catch (Exception e)
                  {
-                       // ignore exception
+                       System.err.println(e);
                  }
          }
 
@@ -486,7 +486,7 @@ public class Main
                          }
                        catch (Exception e)
                          {
-                               // ignore any errors here
+                               System.err.println(e);
                          }
                  }
          }