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 /common-logging/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 'common-logging/src/main/java')
7 files changed, 47 insertions, 43 deletions
diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingContext.java b/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingContext.java index 47c05041..e457f5f3 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingContext.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingContext.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * 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. @@ -143,7 +143,7 @@ public class OnapLoggingContext { * with key "TransactionElapsedTime". */ public void transactionEnded() { - Instant transactionEndTime = Instant.now(); + var transactionEndTime = Instant.now(); setTransactionEndTimestamp(transactionEndTime); setTransactionElapsedTime(transactionEndTime); } @@ -163,7 +163,7 @@ public class OnapLoggingContext { * "MetricElapsedTime". */ public void metricEnded() { - Instant metricEndTime = Instant.now(); + var metricEndTime = Instant.now(); setMetricEndTimestamp(metricEndTime); setMetricElapsedTime(metricEndTime); } @@ -449,7 +449,7 @@ public class OnapLoggingContext { * @param transactionStartTime transaction start time */ public void setTransactionBeginTimestamp(Instant transactionStartTime) { - SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT); + var sdf = new SimpleDateFormat(TIME_FORMAT); context.put(TRANSACTION_BEGIN_TIME_STAMP, sdf.format(Date.from(transactionStartTime))); } @@ -468,7 +468,7 @@ public class OnapLoggingContext { * @param transactionEndTime transaction end time */ public void setTransactionEndTimestamp(Instant transactionEndTime) { - SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT); + var sdf = new SimpleDateFormat(TIME_FORMAT); context.put(TRANSACTION_END_TIME_STAMP, sdf.format(Date.from(transactionEndTime))); } @@ -510,7 +510,7 @@ public class OnapLoggingContext { * @param metricStartTime metric start time */ public void setMetricBeginTimestamp(Instant metricStartTime) { - SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT); + var sdf = new SimpleDateFormat(TIME_FORMAT); context.put(METRIC_BEGIN_TIME_STAMP, sdf.format(Date.from(metricStartTime))); } @@ -529,7 +529,7 @@ public class OnapLoggingContext { * @param metricEndTime metric end time */ public void setMetricEndTimestamp(Instant metricEndTime) { - SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT); + var sdf = new SimpleDateFormat(TIME_FORMAT); context.put(METRIC_END_TIME_STAMP, sdf.format(Date.from(metricEndTime))); } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java b/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java index b7c93c6f..86eb4dd3 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/OnapLoggingUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * 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. @@ -20,10 +20,14 @@ package org.onap.policy.common.logging; +import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; public class OnapLoggingUtils { + private static final Pattern COMMA_PAT = Pattern.compile(","); + private static final Pattern CURLS_PAT = Pattern.compile("[{][}]"); + private OnapLoggingUtils() { // Private constructor to prevent subclassing } @@ -37,7 +41,7 @@ public class OnapLoggingUtils { */ public static OnapLoggingContext getLoggingContextForRequest(HttpServletRequest request, OnapLoggingContext baseContext) { - OnapLoggingContext requestContext = new OnapLoggingContext(baseContext); + var requestContext = new OnapLoggingContext(baseContext); if (request.getLocalAddr() != null) { // may be null in junit tests requestContext.setServerIpAddress(request.getLocalAddr()); } @@ -45,7 +49,7 @@ public class OnapLoggingUtils { // otherwise from remote address in the request String forwarded = request.getHeader("X-Forwarded-For"); if (forwarded != null && forwarded.trim().length() > 0) { - forwarded = forwarded.trim().split(",")[0]; + forwarded = COMMA_PAT.split(forwarded.trim())[0]; requestContext.setClientIpAddress(forwarded); } else if (request.getRemoteAddr() != null) { // may be null in junit tests requestContext.setClientIpAddress(request.getRemoteAddr()); @@ -72,8 +76,8 @@ public class OnapLoggingUtils { return format; } int index; - StringBuilder builder = new StringBuilder(); - String[] token = format.split("[{][}]"); + var builder = new StringBuilder(); + String[] token = CURLS_PAT.split(format); for (index = 0; index < arguments.length; index++) { if (index < token.length) { builder.append(token[index]); diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventData.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventData.java index 23be38ba..02403992 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventData.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventData.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * 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. @@ -62,8 +62,8 @@ public class EventData { @Override public int hashCode() { - final int prime = 31; - int result = 1; + final var prime = 31; + var result = 1; result = prime * result + ((requestId == null) ? 0 : requestId.hashCode()); return result; } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfoHandler.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfoHandler.java index 1a87de53..f5203683 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfoHandler.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/EventTrackInfoHandler.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * 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. @@ -49,7 +49,7 @@ public class EventTrackInfoHandler extends TimerTask { */ private void cleanUp() { - EventTrackInfo eventTrackInfo = PolicyLogger.getEventTracker(); + var eventTrackInfo = PolicyLogger.getEventTracker(); if (eventTrackInfo == null) { return; } diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java index 3f94483b..15659561 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/eelf/PolicyLogger.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * 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. @@ -301,8 +301,8 @@ public class PolicyLogger { setMdcHostInfo(); - Instant startTime = Instant.now(); - Instant endTime = Instant.now(); + var startTime = Instant.now(); + var endTime = Instant.now(); seTimeStamps(startTime, endTime); @@ -334,8 +334,8 @@ public class PolicyLogger { MDC.put(MDC_INSTANCE_UUID, ""); MDC.put(MDC_ALERT_SEVERITY, ""); - Instant startTime = Instant.now(); - Instant endTime = Instant.now(); + var startTime = Instant.now(); + var endTime = Instant.now(); seTimeStamps(startTime, endTime); @@ -348,7 +348,7 @@ public class PolicyLogger { } private static void seTimeStamps(Instant startTime, Instant endTime) { - SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); + var sdf = new SimpleDateFormat(DATE_FORMAT); String formatedTime = sdf.format(Date.from(startTime)); MDC.put(BEGIN_TIME_STAMP, formatedTime); @@ -874,7 +874,7 @@ public class PolicyLogger { if (eventTracker == null) { eventTracker = new EventTrackInfo(); } - EventData event = new EventData(); + var event = new EventData(); event.setRequestId(eventId); event.setStartTime(Instant.now()); eventTracker.storeEventData(event); @@ -1007,7 +1007,7 @@ public class PolicyLogger { return; } - EventData event = eventTracker.getEventDataByRequestId(eventId); + var event = eventTracker.getEventDataByRequestId(eventId); if (event != null) { Instant endTime = event.getEndTime(); @@ -1035,7 +1035,7 @@ public class PolicyLogger { return; } - EventData event = eventTracker.getEventDataByRequestId(eventId.toString()); + var event = eventTracker.getEventDataByRequestId(eventId.toString()); if (event != null) { Instant endTime = event.getEndTime(); @@ -1226,13 +1226,13 @@ public class PolicyLogger { * @param arguments the messages */ private static String getNormalizedStackTrace(Throwable throwable, String... arguments) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); + var sw = new StringWriter(); + var pw = new PrintWriter(sw); throwable.printStackTrace(pw); String newStValue = sw.toString().replace('|', '!').replace("\n", " - "); int curSize = arguments == null ? 0 : arguments.length; - StringBuilder newArgument = new StringBuilder(); - for (int i = 0; i < curSize; i++) { + var newArgument = new StringBuilder(); + for (var i = 0; i < curSize; i++) { newArgument.append(arguments[i]); newArgument.append(":"); } @@ -1246,7 +1246,7 @@ public class PolicyLogger { private static void startCleanUp() { if (!isEventTrackerRunning) { - EventTrackInfoHandler ttrcker = new EventTrackInfoHandler(); + var ttrcker = new EventTrackInfoHandler(); timer = new Timer(true); timer.scheduleAtFixedRate(ttrcker, timerDelayTime, checkInterval); debugLogger.info("EventTrackInfoHandler begins! : {}", new Date()); @@ -1279,7 +1279,7 @@ public class PolicyLogger { */ public static LoggerType init(Properties properties) { - Properties loggerProperties = getLoggerProperties(properties); + var loggerProperties = getLoggerProperties(properties); // fetch and verify definitions of some properties try { @@ -1321,7 +1321,7 @@ public class PolicyLogger { } private static int getIntProp(Properties properties, String propName, int defaultValue) { - final int propValue = Integer.parseInt(properties.getProperty(propName, String.valueOf(defaultValue))); + final var propValue = Integer.parseInt(properties.getProperty(propName, String.valueOf(defaultValue))); debugLogger.info("{} value: {}", propName, propValue); diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java index fadb2545..2dabc016 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/FlexLogger.java @@ -186,7 +186,7 @@ public class FlexLogger extends SecurityManager { * loads the logger properties. */ private static LoggerType initlogger() { - LoggerType loggerType = LoggerType.EELF; + var loggerType = LoggerType.EELF; Properties properties = null; try { @@ -196,7 +196,7 @@ public class FlexLogger extends SecurityManager { if (properties != null) { String overrideLogbackLevel = properties.getProperty("override.logback.level.setup"); displayMessage("FlexLogger:overrideLogbackLevel => " + overrideLogbackLevel); - String loggerTypeString = properties.getProperty("logger.type"); + var loggerTypeString = properties.getProperty("logger.type"); if ("EELF".equalsIgnoreCase(loggerTypeString) && "TRUE".equalsIgnoreCase(overrideLogbackLevel)) { displayMessage("FlexLogger: start listener."); properties = PropertyUtil.getProperties("config/policyLogger.properties", @@ -234,8 +234,8 @@ public class FlexLogger extends SecurityManager { String auditLevel = properties.getProperty("audit.level"); String errorLevel = properties.getProperty("error.level"); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+00:00"); - Instant startTime = Instant.now(); + var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+00:00"); + var startTime = Instant.now(); String formatedTime = sdf.format(Date.from(startTime)); displayMessage("FlexLogger.propertiesChanged : called at time : " + formatedTime); displayMessage("FlexLogger.propertiesChanged : debugLevel : " + debugLevel); diff --git a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java index b8241432..ccd6048c 100644 --- a/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java +++ b/common-logging/src/main/java/org/onap/policy/common/logging/flexlogger/PropertyUtil.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-Logging * ================================================================================ - * 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. @@ -64,9 +64,9 @@ public class PropertyUtil { */ public static Properties getProperties(File file) throws IOException { // create an InputStream (may throw a FileNotFoundException) - try (FileInputStream fis = new FileInputStream(file)) { + try (var fis = new FileInputStream(file)) { // create the properties instance - Properties rval = new Properties(); + var rval = new Properties(); // load properties (may throw an IOException) rval.load(fis); @@ -202,7 +202,7 @@ public class PropertyUtil { lastModified = timestamp; // Save old set, and initial set of changed properties. - Properties oldProperties = properties; + var oldProperties = properties; HashSet<String> changedProperties = new HashSet<>(oldProperties.stringPropertyNames()); // Fetch the list of listeners that we will potentially notify, @@ -238,7 +238,7 @@ public class PropertyUtil { for (final Listener notify : listeners) { // Copy 'properties' and 'changedProperties', so it doesn't // cause problems if the recipient makes changes. - final Properties tmpProperties = (Properties) (properties.clone()); + final var tmpProperties = (Properties) (properties.clone()); final HashSet<String> tmpChangedProperties = new HashSet<>(changedProperties); // Do the notification in a separate thread, so blocking @@ -276,7 +276,7 @@ public class PropertyUtil { // Convert the file to a canonical form in order to avoid the situation // where different names refer to the same file. - File tempFile = file.getCanonicalFile(); + var tempFile = file.getCanonicalFile(); // See if there is an existing registration. The 'synchronized' block // is needed to handle the case where a new listener is added at about |