diff options
Diffstat (limited to 'integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java')
-rw-r--r-- | integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java | 154 |
1 files changed, 76 insertions, 78 deletions
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 d50c7059..02e4c70b 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,14 +2,14 @@ * ============LICENSE_START======================================================= * Integrity Audit * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -21,6 +21,7 @@ package org.onap.policy.common.ia; import java.util.Properties; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.ia.IntegrityAuditProperties.NodeTypeEnum; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; @@ -46,18 +47,18 @@ public class IntegrityAudit { /* * This is the audit period in milliseconds. For example, if it had a value of 3600000, the * audit can only run once per hour. If it has a value of 6000, it can run once per minute. - * + * * Values: integrityAuditPeriodSeconds < 0 (negative number) indicates the audit is off * integrityAuditPeriodSeconds == 0 indicates the audit is to run continuously * integrityAuditPeriodSeconds > 0 indicates the audit is to run at most once during the * indicated period - * + * */ private int integrityAuditPeriodSeconds; /** * IntegrityAudit constructor. - * + * * @param resourceName the resource name * @param persistenceUnit the persistence unit * @param properties the properties @@ -118,100 +119,97 @@ public class IntegrityAudit { public static boolean parmsAreBad(String resourceName, String persistenceUnit, Properties properties, StringBuilder badparams) { - boolean parmsAreBad = false; - - if (resourceName == null || resourceName.isEmpty()) { - badparams.append("resourceName "); - parmsAreBad = true; - } - - if (persistenceUnit == null || persistenceUnit.isEmpty()) { - badparams.append("persistenceUnit "); - parmsAreBad = true; - } + boolean parmsAreBad = checkEmpty(badparams, "resourceName", resourceName); + parmsAreBad = checkEmpty(badparams, "persistenceUnit", persistenceUnit) || parmsAreBad; if (properties == null || properties.isEmpty()) { badparams.append("properties "); parmsAreBad = true; } else { - String dbDriver = properties.getProperty(IntegrityAuditProperties.DB_DRIVER); - if (dbDriver == null || dbDriver.isEmpty()) { - badparams.append("dbDriver "); - parmsAreBad = true; - } + parmsAreBad = checkProperties(properties, badparams) || parmsAreBad; + } // End else + logger.debug("parmsAreBad: exit:" + "\nresourceName: " + resourceName + "\npersistenceUnit: " + persistenceUnit + + "\nproperties: " + properties); - String dbUrl = properties.getProperty(IntegrityAuditProperties.DB_URL); - if (dbUrl == null || dbUrl.isEmpty()) { - badparams.append("dbUrl "); - parmsAreBad = true; - } + return parmsAreBad; + } - String dbUser = properties.getProperty(IntegrityAuditProperties.DB_USER); - if (dbUser == null || dbUser.isEmpty()) { - badparams.append("dbUser "); - parmsAreBad = true; - } + private static boolean checkEmpty(StringBuilder builder, String name, String value) { + if (StringUtils.isEmpty(value)) { + builder.append(name); + builder.append(' '); + return true; - String dbPwd = properties.getProperty(IntegrityAuditProperties.DB_PWD); - if (dbPwd == null) { // may be empty - badparams.append("dbPwd "); - parmsAreBad = true; - } + } else { + return false; + } + } - String siteName = properties.getProperty(IntegrityAuditProperties.SITE_NAME); - if (siteName == null || siteName.isEmpty()) { - badparams.append("siteName "); - parmsAreBad = true; - } + private static boolean checkProperties(Properties properties, StringBuilder badparams) { + boolean parmsAreBad = + checkEmpty(badparams, "dbDriver", properties.getProperty(IntegrityAuditProperties.DB_DRIVER)); + parmsAreBad = checkEmpty(badparams, "dbUrl", properties.getProperty(IntegrityAuditProperties.DB_URL)) + || parmsAreBad; + parmsAreBad = checkEmpty(badparams, "dbUser", properties.getProperty(IntegrityAuditProperties.DB_USER)) + || parmsAreBad; + parmsAreBad = checkEmpty(badparams, "dbPwd", properties.getProperty(IntegrityAuditProperties.DB_PWD)) + || parmsAreBad; + parmsAreBad = checkEmpty(badparams, "siteName", properties.getProperty(IntegrityAuditProperties.SITE_NAME)) + || parmsAreBad; + parmsAreBad = checkNodeType(properties, badparams) || parmsAreBad; + + return checkAuditPeriod(properties, badparams) || parmsAreBad; + } - String nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE); - if (nodeType == null || nodeType.isEmpty()) { - badparams.append("nodeType "); - parmsAreBad = true; - } else { - nodeType = nodeType.trim(); - if (!isNodeTypeEnum(nodeType)) { - String nodetypes = "nodeType must be one of["; - for (NodeTypeEnum n : NodeTypeEnum.values()) { - nodetypes = nodetypes.concat(n.toString() + " "); - } - badparams.append(nodetypes + "] "); - parmsAreBad = true; - } - } - // IntegrityAuditProperties.AUDIT_PERIOD_SECONDS and - // IntegrityAuditProperties.AUDIT_PERIOD_MILLISECONDS are allowed to be null - if (properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null) { - try { - Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); - } catch (NumberFormatException nfe) { - badparams.append(", auditPeriodSeconds=" - + properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); - parmsAreBad = true; + private static boolean checkNodeType(Properties properties, StringBuilder badparams) { + String nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE); + if (nodeType == null || nodeType.isEmpty()) { + badparams.append("nodeType "); + return true; + } else { + nodeType = nodeType.trim(); + if (!isNodeTypeEnum(nodeType)) { + String nodetypes = "nodeType must be one of["; + for (NodeTypeEnum n : NodeTypeEnum.values()) { + nodetypes = nodetypes.concat(n.toString() + " "); } + badparams.append(nodetypes + "] "); + return true; } - } // End else - logger.debug("parmsAreBad: exit:" + "\nresourceName: " + resourceName + "\npersistenceUnit: " + persistenceUnit - + "\nproperties: " + properties); + } + return false; + } - return parmsAreBad; + private static boolean checkAuditPeriod(Properties properties, StringBuilder badparams) { + // IntegrityAuditProperties.AUDIT_PERIOD_SECONDS and + // IntegrityAuditProperties.AUDIT_PERIOD_MILLISECONDS are allowed to be null + if (properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null) { + try { + Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); + } catch (NumberFormatException nfe) { + badparams.append(", auditPeriodSeconds=" + + properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); + return true; + } + } + return false; } /** * Starts the audit thread. - * + * * @throws IntegrityAuditException if an error occurs */ public void startAuditThread() throws IntegrityAuditException { logger.info("startAuditThread: Entering"); if (integrityAuditPeriodSeconds >= 0) { - this.auditThread = makeAuditThread(this.resourceName, this.persistenceUnit, + this.auditThread = makeAuditThread(this.resourceName, this.persistenceUnit, this.properties, integrityAuditPeriodSeconds); - logger.info("startAuditThread: Audit started and will run every " + integrityAuditPeriodSeconds + logger.info("startAuditThread: Audit started and will run every " + integrityAuditPeriodSeconds + " seconds"); this.auditThread.start(); - + } else { logger.info("startAuditThread: Suppressing integrity audit, integrityAuditPeriodSeconds=" + integrityAuditPeriodSeconds); @@ -255,7 +253,7 @@ public class IntegrityAudit { /** * Waits a bit for the AuditThread to complete. Used by JUnit tests. - * + * * @param twaitms wait time, in milliseconds * @return {@code true} if the thread stopped within the given time, {@code false} otherwise * @throws InterruptedException if the thread is interrupted @@ -272,7 +270,7 @@ public class IntegrityAudit { /** * Return if audit thread. - * + * * @return {@code true} if an audit thread exists, {@code false} otherwise */ protected boolean haveAuditThread() { @@ -281,12 +279,12 @@ public class IntegrityAudit { /** * Creates an audit thread. May be overridden by junit tests. - * + * * @param resourceName2 the resource name * @param persistenceUnit2 the persistence unit * @param properties2 properties * @param integrityAuditPeriodSeconds2 - * + * * @return a new audit thread * @throws IntegrityAuditException audit exception */ |