diff options
author | Jim Hahn <jrh3@att.com> | 2021-05-06 11:28:53 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-05-06 15:26:24 -0400 |
commit | 1d0aaaa5b31719c1718700bb0d1a99c413fd513c (patch) | |
tree | bd2687f1d38f3e3c4e53a68695c8c91c6866468e /integrity-audit/src/main/java | |
parent | 3b00f1c32b89282dcbb74d3d3645e263f005319e (diff) |
Fix sonars in policy-common
Fixed sonars:
- use "var" instead of actual type name
- re-interrupt threads
- use rej2 split() instead of String split()
Issue-ID: POLICY-3285
Change-Id: I82261e0b8a53ee5c5264556fbf5cec37454f014e
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'integrity-audit/src/main/java')
5 files changed, 56 insertions, 51 deletions
diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java index 32246c86..03fbdb57 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -22,7 +22,6 @@ package org.onap.policy.common.ia; import java.util.Collections; import java.util.Comparator; -import java.util.Date; import java.util.List; import java.util.Properties; import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; @@ -149,10 +148,12 @@ public class AuditThread extends Thread { */ runUntilInterrupted(); + } catch (InterruptedException e) { + handleAuditLoopException(e); + Thread.currentThread().interrupt(); + } catch (Exception e) { - String msg = "AuditThread.run: Could not start audit loop. Exception thrown; message=" + e.getMessage(); - logger.error(MessageCodes.EXCEPTION_ERROR, e, msg); - integrityAudit.setThreadInitialized(false); + handleAuditLoopException(e); } dbDao.destroy(); @@ -160,16 +161,22 @@ public class AuditThread extends Thread { logger.info("AuditThread.run: Exiting"); } + private void handleAuditLoopException(Exception exception) { + String msg = "AuditThread.run: Could not start audit loop. Exception thrown; message=" + exception.getMessage(); + logger.error(MessageCodes.EXCEPTION_ERROR, exception, msg); + integrityAudit.setThreadInitialized(false); + } + private void runUntilInterrupted() throws InterruptedException { - boolean auditCompleted = false; + var auditCompleted = false; - DbAudit dbAudit = new DbAudit(dbDao); + var dbAudit = new DbAudit(dbDao); IntegrityAuditEntity entityCurrentlyDesignated; IntegrityAuditEntity thisEntity; integrityAudit.setThreadInitialized(true); // An exception will set it to false - boolean interrupted = false; + var interrupted = false; while (!interrupted) { try { @@ -210,6 +217,7 @@ public class AuditThread extends Thread { if (isInterruptedException(e)) { String msg = "AuditThread.run loop - Exception thrown: " + e.getMessage() + "; Stopping."; logger.error(MessageCodes.EXCEPTION_ERROR, e, msg); + Thread.currentThread().interrupt(); interrupted = true; } else { @@ -347,10 +355,10 @@ public class AuditThread extends Thread { IntegrityAuditEntity thisEntity = null; - int designatedEntityIndex = -1; - int entityIndex = 0; - int priorCandidateIndex = -1; - int subsequentCandidateIndex = -1; + var designatedEntityIndex = -1; + var entityIndex = 0; + var priorCandidateIndex = -1; + var subsequentCandidateIndex = -1; for (IntegrityAuditEntity integrityAuditEntity : integrityAuditEntityList) { @@ -561,7 +569,7 @@ public class AuditThread extends Thread { */ List<IntegrityAuditEntity> integrityAuditEntityList = dbDao.getIntegrityAuditEntities(this.persistenceUnit, this.nodeType); - int listSize = integrityAuditEntityList.size(); + var listSize = integrityAuditEntityList.size(); if (logger.isDebugEnabled()) { logger.debug("getIntegrityAuditEntityList: Got " + listSize + " IntegrityAuditEntity records"); } @@ -634,10 +642,10 @@ public class AuditThread extends Thread { + integrityAuditEntity.getLastUpdated()); } - boolean stale = false; + var stale = false; - Date currentTime = AuditorTime.getInstance().getDate(); - Date lastUpdated = integrityAuditEntity.getLastUpdated(); + var currentTime = AuditorTime.getInstance().getDate(); + var lastUpdated = integrityAuditEntity.getLastUpdated(); /* * If lastUpdated is null, we assume that the audit never ran for that node. @@ -690,8 +698,8 @@ public class AuditThread extends Thread { long timeDifference; - Date currentTime = AuditorTime.getInstance().getDate(); - Date lastUpdated = thisEntity.getLastUpdated(); + var currentTime = AuditorTime.getInstance().getDate(); + var lastUpdated = thisEntity.getLastUpdated(); long lastUpdatedTime = lastUpdated.getTime(); timeDifference = currentTime.getTime() - lastUpdatedTime; diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java index f69173f8..c8ca41ca 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -123,7 +123,7 @@ public class DbAudit { * nodes. Since the audit is run in a round-robin, every instance will be compared against * every other instance. */ - IntegrityAuditEntity myIae = dbDao.getMyIntegrityAuditEntity(); + var myIae = dbDao.getMyIntegrityAuditEntity(); if (myIae == null) { @@ -267,7 +267,7 @@ public class DbAudit { * @return DAO properties for the given DB node */ private Properties getTheirDaoProperties(IntegrityAuditEntity iae) { - Properties theirProperties = new Properties(); + var theirProperties = new Properties(); theirProperties.put(IntegrityAuditProperties.DB_DRIVER, iae.getJdbcDriver()); theirProperties.put(IntegrityAuditProperties.DB_URL, iae.getJdbcUrl()); @@ -294,7 +294,7 @@ public class DbAudit { logger.debug("dbAudit: Second comparison; traversing classNameSet, size=" + classNameSet.size()); } - int errorCount = 0; + var errorCount = 0; for (String clazzName : classNameSet) { @@ -327,7 +327,7 @@ public class DbAudit { IntegrityAuditEntity myIae, String clazzName, Set<Object> keySet, Map<Object, Object> myEntries) throws IntegrityAuditException { - int errorCount = 0; + var errorCount = 0; for (IntegrityAuditEntity iae : iaeList) { if (iae.getId() == myIae.getId()) { if (logger.isDebugEnabled()) { @@ -363,7 +363,7 @@ public class DbAudit { return 0; } - StringBuilder keyBuilder = new StringBuilder(); + var keyBuilder = new StringBuilder(); for (Object key : misMatchedKeySet) { keyBuilder.append(key.toString()); keyBuilder.append(", "); 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 ff4d2976..27d035f2 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 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. @@ -31,14 +31,11 @@ import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; -import javax.persistence.PersistenceUnitUtil; import javax.persistence.Query; import javax.persistence.TypedQuery; -import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import javax.persistence.metamodel.ManagedType; -import javax.persistence.metamodel.Metamodel; import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; @@ -132,7 +129,7 @@ public class DbDao { */ private void validateProperties(String resourceName, String persistenceUnit, Properties properties) throws IntegrityAuditPropertiesException { - StringBuilder badparams = new StringBuilder(); + var badparams = new StringBuilder(); if (IntegrityAudit.parmsAreBad(resourceName, persistenceUnit, properties, badparams)) { String msg = "DbDao: Bad parameters: badparams" + badparams; throw new IntegrityAuditPropertiesException(msg); @@ -158,9 +155,9 @@ public class DbDao { public Map<Object, Object> getAllMyEntries(String className) { logger.debug("getAllMyEntries: Entering, className=" + className); HashMap<Object, Object> resultMap = new HashMap<>(); - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); try { - CriteriaBuilder cb = em.getCriteriaBuilder(); + var cb = em.getCriteriaBuilder(); CriteriaQuery<Object> cq = cb.createQuery(); Root<?> rootEntry = cq.from(Class.forName(className)); CriteriaQuery<Object> all = cq.select(rootEntry); @@ -168,7 +165,7 @@ public class DbDao { List<Object> objectList = allQuery.getResultList(); // Now create the map - PersistenceUnitUtil util = emf.getPersistenceUnitUtil(); + var util = emf.getPersistenceUnitUtil(); for (Object o : objectList) { Object key = util.getIdentifier(o); resultMap.put(key, o); @@ -192,7 +189,7 @@ public class DbDao { logger.debug("getAllMyEntries: Entering, className=" + className + ",\n keySet=" + keySet); HashMap<Object, Object> resultMap = new HashMap<>(); - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); try { Class<?> clazz = Class.forName(className); for (Object key : keySet) { @@ -221,17 +218,17 @@ public class DbDao { logger.debug("getAllEntries: Entering, persistenceUnit=" + persistenceUnit + ",\n className=" + className); HashMap<Object, Object> resultMap = new HashMap<>(); - EntityManagerFactory theEmf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = theEmf.createEntityManager(); + var theEmf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + var em = theEmf.createEntityManager(); try { - CriteriaBuilder cb = em.getCriteriaBuilder(); + var cb = em.getCriteriaBuilder(); CriteriaQuery<Object> cq = cb.createQuery(); Root<?> rootEntry = cq.from(Class.forName(className)); CriteriaQuery<Object> all = cq.select(rootEntry); TypedQuery<Object> allQuery = em.createQuery(all); List<Object> objectList = allQuery.getResultList(); - PersistenceUnitUtil util = theEmf.getPersistenceUnitUtil(); + var util = theEmf.getPersistenceUnitUtil(); for (Object o : objectList) { Object key = util.getIdentifier(o); resultMap.put(key, o); @@ -262,8 +259,8 @@ public class DbDao { Set<Object> keySet) { logger.debug("getAllEntries: Entering, persistenceUnit=" + persistenceUnit + ",\n properties= " + properties + ",\n className=" + className + ",\n keySet= " + keySet); - EntityManagerFactory theEmf = Persistence.createEntityManagerFactory(persistenceUnit, properties); - EntityManager em = theEmf.createEntityManager(); + var theEmf = Persistence.createEntityManagerFactory(persistenceUnit, properties); + var em = theEmf.createEntityManager(); HashMap<Object, Object> resultMap = new HashMap<>(); try { Class<?> clazz = Class.forName(className); @@ -296,7 +293,7 @@ public class DbDao { logger.debug("getIntegrityAuditEntities: Entering, persistenceUnit=" + persistenceUnit + ",\n nodeType= " + nodeType); try { - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); // Start a transaction EntityTransaction et = em.getTransaction(); @@ -304,7 +301,7 @@ public class DbDao { // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not // found, create a new entry - Query iaequery = em + var iaequery = em .createQuery("Select i from IntegrityAuditEntity i where i.persistenceUnit=:pu and i.nodeType=:nt"); iaequery.setParameter("pu", persistenceUnit); iaequery.setParameter("nt", nodeType); @@ -357,7 +354,7 @@ public class DbDao { */ public IntegrityAuditEntity getIntegrityAuditEntity(long id) throws DbDaoTransactionException { try { - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); // Start a transaction EntityTransaction et = em.getTransaction(); @@ -385,7 +382,7 @@ public class DbDao { public Set<String> getPersistenceClassNames() { logger.debug("DbDao: getPersistenceClassNames() entry"); HashSet<String> returnList = new HashSet<>(); - final Metamodel mm = emf.getMetamodel(); + final var mm = emf.getMetamodel(); logger.debug("\n" + persistenceUnit + " persistence unit classes:"); for (final ManagedType<?> managedType : mm.getManagedTypes()) { Class<?> clazz = managedType.getJavaType(); @@ -496,7 +493,7 @@ public class DbDao { BiConsumer<EntityManager, IntegrityAuditEntity> updater) throws DbDaoTransactionException { try { - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); // Start a transaction EntityTransaction et = em.getTransaction(); @@ -582,7 +579,7 @@ public class DbDao { throw new DbDaoTransactionException(msg); } - EntityManager em = emf.createEntityManager(); + var em = emf.createEntityManager(); // Start a transaction EntityTransaction et = em.getTransaction(); @@ -590,7 +587,7 @@ public class DbDao { // if IntegrityAuditEntity entry exists for resourceName and PU, update it. If not // found, create a new entry - Query iaequery = em.createQuery("Delete from IntegrityAuditEntity"); + var iaequery = em.createQuery("Delete from IntegrityAuditEntity"); int returnCode = iaequery.executeUpdate(); @@ -650,7 +647,7 @@ public class DbDao { /* * Define query */ - Query query = em.createQuery( + var query = em.createQuery( "Select i from IntegrityAuditEntity i where i.persistenceUnit=:pu and i.nodeType=:nt"); query.setParameter("pu", persistenceUnit); query.setParameter("nt", nodeType); @@ -703,7 +700,7 @@ public class DbDao { continue; } - IntegrityAuditEntity integrityAuditEntity = (IntegrityAuditEntity) o; + var integrityAuditEntity = (IntegrityAuditEntity) o; if (integrityAuditEntity.getResourceName().equals(resourceName)) { if (logger.isDebugEnabled()) { logger.debug("changeDesignated: Designating resourceName=" diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java index 41aea118..10900e3c 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019, 2021 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. @@ -68,7 +68,7 @@ public class IntegrityAudit { throws IntegrityAuditException { logger.info("Constructor: Entering and checking for nulls"); - StringBuilder parmList = new StringBuilder(); + var parmList = new StringBuilder(); if (parmsAreBad(resourceName, persistenceUnit, properties, parmList)) { logger.error("Constructor: Parms contain nulls; cannot run audit for resourceName=" + resourceName + ", persistenceUnit=" + persistenceUnit + ", bad parameters: " + parmList); diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/jpa/IntegrityAuditEntity.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/jpa/IntegrityAuditEntity.java index 121b28b4..1c8277c0 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/jpa/IntegrityAuditEntity.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/jpa/IntegrityAuditEntity.java @@ -100,7 +100,7 @@ public class IntegrityAuditEntity implements Serializable { */ @PrePersist public void prePersist() { - Date date = AuditorTime.getInstance().getDate(); + var date = AuditorTime.getInstance().getDate(); this.createdDate = date; this.lastUpdated = date; } |