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 /utils | |
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 'utils')
15 files changed, 101 insertions, 98 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/cmd/CommandLineArgumentsHandler.java b/utils/src/main/java/org/onap/policy/common/utils/cmd/CommandLineArgumentsHandler.java index 37a9047b..b9ba3fdd 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/cmd/CommandLineArgumentsHandler.java +++ b/utils/src/main/java/org/onap/policy/common/utils/cmd/CommandLineArgumentsHandler.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 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. @@ -23,7 +24,6 @@ package org.onap.policy.common.utils.cmd; import java.io.PrintWriter; import java.io.StringWriter; import java.net.URISyntaxException; -import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; @@ -184,9 +184,9 @@ public class CommandLineArgumentsHandler { * @return the help string */ public String help() { - final HelpFormatter helpFormatter = new HelpFormatter(); - final StringWriter stringWriter = new StringWriter(); - final PrintWriter printWriter = new PrintWriter(stringWriter); + final var helpFormatter = new HelpFormatter(); + final var stringWriter = new StringWriter(); + final var printWriter = new PrintWriter(stringWriter); final String cmdLineSyntax = this.helpClassName + " [options...]"; helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, cmdLineSyntax, "options", options, 0, 0, ""); @@ -243,13 +243,13 @@ public class CommandLineArgumentsHandler { } // The file name refers to a resource on the local file system - final URL fileUrl = ResourceUtils.getUrl4Resource(fileName); + final var fileUrl = ResourceUtils.getUrl4Resource(fileName); if (fileUrl == null) { throw new CommandLineException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist"); } try { - Path path = Path.of(fileUrl.toURI()); + var path = Path.of(fileUrl.toURI()); if (!Files.isRegularFile(path)) { throw new CommandLineException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file"); } diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java index 3036d353..daacf479 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 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. @@ -55,12 +55,12 @@ public class PropertyCoder { * @return a class T object */ public <T> T decode(String json, String keyProperty, Class<T> clazz) { - JsonElement jsonElement = GSON.fromJson(json, JsonElement.class); + var jsonElement = GSON.fromJson(json, JsonElement.class); return new MyDecoder(jsonElement, keyProperty).decrypt(jsonElement, clazz); } public <T> T decode(Reader reader, String keyProperty, Class<T> clazz) { - JsonElement jsonElement = GSON.fromJson(reader, JsonElement.class); + var jsonElement = GSON.fromJson(reader, JsonElement.class); return new MyDecoder(jsonElement, keyProperty).decrypt(jsonElement, clazz); } @@ -71,9 +71,9 @@ public class PropertyCoder { if (!jsonElement.isJsonObject()) { return; } - JsonObject jsonObject = jsonElement.getAsJsonObject(); + var jsonObject = jsonElement.getAsJsonObject(); // Use keyProperty from input to retrieve secretKey - String secretKey = jsonObject.get(keyProperty).getAsString(); + var secretKey = jsonObject.get(keyProperty).getAsString(); if (!StringUtils.isBlank(secretKey)) { crypto = new CryptoUtils(secretKey); } @@ -97,7 +97,7 @@ public class PropertyCoder { if (!jsonElement.getAsJsonPrimitive().isString()) { return jsonElement; } - String value = jsonElement.getAsString(); + var value = jsonElement.getAsString(); if (!value.startsWith("enc:")) { return jsonElement; } @@ -111,7 +111,7 @@ public class PropertyCoder { if (crypto == null) { return jsonArray; } - JsonArray newArray = new JsonArray(); + var newArray = new JsonArray(); for (JsonElement element: jsonArray) { newArray.add(decrypt(element)); } @@ -122,14 +122,14 @@ public class PropertyCoder { if (crypto == null) { return jsonObject; } - JsonObject newObject = new JsonObject(); + var newObject = new JsonObject(); Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet(); for (Map.Entry<String, JsonElement> entry : entrySet) { String key = entry.getKey(); - JsonElement jsonElement = decrypt(entry.getValue()); + var jsonElement = decrypt(entry.getValue()); newObject.add(key, jsonElement); } return newObject; } } -}
\ No newline at end of file +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java index 7f5e3b85..0d84f85c 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 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. @@ -157,7 +157,7 @@ public class StandardCoder implements Coder { @Override public void encode(OutputStream target, Object object) throws CoderException { try { - Writer wtr = makeWriter(target); + var wtr = makeWriter(target); toJson(wtr, object); // flush, but don't close @@ -170,7 +170,7 @@ public class StandardCoder implements Coder { @Override public void encode(File target, Object object) throws CoderException { - try (Writer wtr = makeWriter(target)) { + try (var wtr = makeWriter(target)) { toJson(wtr, object); // no need to flush or close here @@ -212,7 +212,7 @@ public class StandardCoder implements Coder { @Override public <T> T decode(File source, Class<T> clazz) throws CoderException { - try (Reader input = makeReader(source)) { + try (var input = makeReader(source)) { return fromJson(input, clazz); } catch (RuntimeException | IOException e) { diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java index 5d682638..f6c3ca43 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 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. @@ -20,7 +20,6 @@ package org.onap.policy.common.utils.coder; -import com.google.gson.JsonArray; import com.google.gson.JsonElement; import java.io.Serializable; @@ -130,7 +129,7 @@ public class StandardCoderObject implements Serializable { return null; } - JsonArray array = element.getAsJsonArray(); + var array = element.getAsJsonArray(); if (index >= array.size()) { return null; diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java index 647a6155..4deeba14 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java @@ -1,6 +1,6 @@ /*-- * ============LICENSE_START======================================================= - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020-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. @@ -72,7 +72,7 @@ public class StandardValCoder extends StandardCoder { @Override protected String toJson(@NonNull Object object) { - StringWriter output = new StringWriter(); + var output = new StringWriter(); toJson(output, object); return output.toString(); } @@ -89,7 +89,7 @@ public class StandardValCoder extends StandardCoder { @Override protected <T> T fromJson(String json, Class<T> clazz) { - StringReader reader = new StringReader(json); + var reader = new StringReader(json); return convertFromDouble(clazz, gson.fromJson(validatorApi.createJsonReader(validator, reader), clazz)); } diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java b/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java index 7d61e63f..b694dd4f 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 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. @@ -86,7 +86,7 @@ public class YamlJsonTranslator { * @return YAML representing the original object */ public String toYaml(Object object) { - StringWriter output = new StringWriter(); + var output = new StringWriter(); toYaml(output, object); return output.toString(); } @@ -98,8 +98,8 @@ public class YamlJsonTranslator { * @param object POJO to be translated */ public void toYaml(Writer target, Object object) { - DumperOptions dumper = new DumperOptions(); - Serializer serializer = new Serializer(new Emitter(target, dumper), new Resolver(), dumper, null); + var dumper = new DumperOptions(); + var serializer = new Serializer(new Emitter(target, dumper), new Resolver(), dumper, null); try { serializer.open(); @@ -140,7 +140,7 @@ public class YamlJsonTranslator { * @return a POJO representing the YAML read from the reader */ public <T> T fromYaml(Reader source, Class<T> clazz) { - Node node = new Yaml().compose(source); + var node = new Yaml().compose(source); return fromJson(makeJson(node), clazz); } @@ -281,7 +281,7 @@ public class YamlJsonTranslator { protected JsonArray makeJsonArray(SequenceNode node) { List<Node> nodes = node.getValue(); - JsonArray array = new JsonArray(nodes.size()); + var array = new JsonArray(nodes.size()); nodes.forEach(subnode -> array.add(makeJson(subnode))); return array; @@ -294,10 +294,10 @@ public class YamlJsonTranslator { * @return a gson element corresponding to the node */ protected JsonObject makeJsonObject(MappingNode node) { - JsonObject obj = new JsonObject(); + var obj = new JsonObject(); for (NodeTuple tuple : node.getValue()) { - Node key = tuple.getKeyNode(); + var key = tuple.getKeyNode(); String skey = ((ScalarNode) key).getValue(); obj.add(skey, makeJson(tuple.getValueNode())); @@ -314,7 +314,7 @@ public class YamlJsonTranslator { */ protected JsonElement makeJsonPrim(ScalarNode node) { try { - Tag tag = node.getTag(); + var tag = node.getTag(); if (tag == Tag.INT) { return new JsonPrimitive(Long.valueOf(node.getValue())); diff --git a/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java b/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java index a2fb5a8b..a0a83905 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java +++ b/utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * 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. @@ -113,7 +113,7 @@ public class NetworkUtil { */ public static boolean isTcpPortOpen(String host, int port, int retries, long interval) throws InterruptedException { - int retry = 0; + var retry = 0; while (retry < retries) { /* * As with the server socket, this is only used to see if the port is open, diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/BeanConfigurator.java b/utils/src/main/java/org/onap/policy/common/utils/properties/BeanConfigurator.java index 2ef91911..f8c52091 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/properties/BeanConfigurator.java +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/BeanConfigurator.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP - Common Modules * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 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. @@ -20,6 +20,7 @@ package org.onap.policy.common.utils.properties; +import com.google.re2j.Pattern; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -45,6 +46,7 @@ import org.onap.policy.common.utils.properties.exception.PropertyMissingExceptio * <i>accept</i> includes the "empty" option. */ public class BeanConfigurator { + private static final Pattern COMMA_PAT = Pattern.compile(","); /** * The "empty" option that may appear within the {@link Property}'s <i>accept</i> @@ -414,7 +416,7 @@ public class BeanConfigurator { * @return {@code true} if the <i>accept</i> attribute includes "empty" */ protected boolean isEmptyOk(Property prop) { - for (String option : prop.accept().split(",")) { + for (String option : COMMA_PAT.split(prop.accept())) { if (ACCEPT_EMPTY.equals(option)) { return true; } @@ -495,7 +497,7 @@ public class BeanConfigurator { * @throws PropertyAccessException if a "get" method cannot be identified */ private Method getGetter(Field field, Property prop) throws PropertyAccessException { - String capnm = StringUtils.capitalize(field.getName()); + var capnm = StringUtils.capitalize(field.getName()); try { return getGetter(field, "get" + capnm); diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java b/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java index 996f1b87..07346927 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/PropertyObjectUtils.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 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. @@ -24,7 +24,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.regex.Matcher; import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,6 +35,7 @@ public class PropertyObjectUtils { public static final Logger logger = LoggerFactory.getLogger(PropertyObjectUtils.class); private static final Pattern NAME_PAT = Pattern.compile("\\[(\\d{1,3})\\]$"); + private static final Pattern DOT_PAT = Pattern.compile("[.]"); private PropertyObjectUtils() { // do nothing @@ -58,7 +58,7 @@ public class PropertyObjectUtils { for (String name : properties.stringPropertyNames()) { if (name.startsWith(dottedPrefix)) { - String[] components = name.substring(pfxlen).split("[.]"); + String[] components = DOT_PAT.split(name.substring(pfxlen)); setProperty(map, components, properties.getProperty(name)); } } @@ -79,13 +79,13 @@ public class PropertyObjectUtils { final int lastComp = names.length - 1; // process all but the final component - for (int comp = 0; comp < lastComp; ++comp) { + for (var comp = 0; comp < lastComp; ++comp) { node = getNode(node, names[comp]); } // process the final component String name = names[lastComp]; - Matcher matcher = NAME_PAT.matcher(name); + var matcher = NAME_PAT.matcher(name); if (!matcher.find()) { // no subscript @@ -95,7 +95,7 @@ public class PropertyObjectUtils { // subscripted List<Object> array = getArray(node, name.substring(0, matcher.start())); - int index = Integer.parseInt(matcher.group(1)); + var index = Integer.parseInt(matcher.group(1)); expand(array, index); array.set(index, value); } @@ -109,7 +109,7 @@ public class PropertyObjectUtils { */ @SuppressWarnings("unchecked") private static Map<String, Object> getNode(Map<String, Object> map, String name) { - Matcher matcher = NAME_PAT.matcher(name); + var matcher = NAME_PAT.matcher(name); if (!matcher.find()) { // no subscript @@ -118,7 +118,7 @@ public class PropertyObjectUtils { // subscripted List<Object> array = getArray(map, name.substring(0, matcher.start())); - int index = Integer.parseInt(matcher.group(1)); + var index = Integer.parseInt(matcher.group(1)); expand(array, index); Object item = array.get(index); diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/SpecProperties.java b/utils/src/main/java/org/onap/policy/common/utils/properties/SpecProperties.java index ec7157d3..f711a058 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/properties/SpecProperties.java +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/SpecProperties.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018, 2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018, 2020-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. @@ -89,7 +89,7 @@ public class SpecProperties extends Properties { return super.getProperty(key); } - String suffix = key.substring(prefix.length()); + var suffix = key.substring(prefix.length()); String val = super.getProperty(specPrefix + suffix); if (val != null) { diff --git a/utils/src/main/java/org/onap/policy/common/utils/properties/exception/PropertyException.java b/utils/src/main/java/org/onap/policy/common/utils/properties/exception/PropertyException.java index 44edd428..d6895847 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/properties/exception/PropertyException.java +++ b/utils/src/main/java/org/onap/policy/common/utils/properties/exception/PropertyException.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * ONAP Policy Engine - Common Modules * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018, 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. * 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. @@ -38,7 +38,7 @@ public class PropertyException extends Exception { /** * Constructor. - * + * * @param propName name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} */ @@ -51,7 +51,7 @@ public class PropertyException extends Exception { /** * Constructor. - * + * * @param propnm name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} * @param message error message @@ -65,7 +65,7 @@ public class PropertyException extends Exception { /** * Constructor. - * + * * @param propnm name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} * @param cause cause of the exception @@ -79,7 +79,7 @@ public class PropertyException extends Exception { /** * Constructor. - * + * * @param propnm name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} * @param message error message @@ -94,7 +94,7 @@ public class PropertyException extends Exception { /** * Get the property name. - * + * * @return name of the property for which the exception was thrown, or {@code null} if * no name was provided */ @@ -104,7 +104,7 @@ public class PropertyException extends Exception { /** * Get the field name. - * + * * @return name of the field for which the exception was thrown, or {@code null} if no * field was provided */ @@ -114,7 +114,7 @@ public class PropertyException extends Exception { /** * Make the message. - * + * * @param propnm name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} * @param message error message, never {@code null} @@ -126,13 +126,13 @@ public class PropertyException extends Exception { /** * Make the message. - * + * * @param propnm name of the property causing the exception, or {@code null} * @param fieldName name of the field causing the exception, or {@code null} * @return an error message composed of the two items */ private static String makeMessage(String propnm, String fieldName) { - StringBuilder bldr = new StringBuilder(50); + var bldr = new StringBuilder(50); if (propnm == null) { bldr.append("property exception"); diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java index 2acc67ac..b648fccd 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2020 Nordix Foundation. - * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020-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. @@ -33,6 +33,7 @@ import java.util.Set; import java.util.TreeSet; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,6 +45,8 @@ public abstract class ResourceUtils { // Get a reference to the logger private static final Logger LOGGER = LoggerFactory.getLogger(ResourceUtils.class); + private static final Pattern SLASH_PAT = Pattern.compile("/"); + // The length of byte buffers used to read resources into strings private static final int BYTE_BUFFER_LENGH = 1024; @@ -66,7 +69,7 @@ public abstract class ResourceUtils { */ public static URL getUrl4Resource(final String resourceName) { // Check the local fine system first - final URL urlToResource = getLocalFile(resourceName); + final var urlToResource = getLocalFile(resourceName); // Check if this is a local file if (urlToResource != null) { @@ -92,8 +95,8 @@ public abstract class ResourceUtils { } // Read the stream contents in to an output stream - final ByteArrayOutputStream resourceOutputStreamBuffer = new ByteArrayOutputStream(); - final byte[] resourceBuffer = new byte[BYTE_BUFFER_LENGH]; + final var resourceOutputStreamBuffer = new ByteArrayOutputStream(); + final var resourceBuffer = new byte[BYTE_BUFFER_LENGH]; int length; try { while ((length = resourceStream.read(resourceBuffer)) != -1) { @@ -116,7 +119,7 @@ public abstract class ResourceUtils { */ public static InputStream getResourceAsStream(final String resourceName) { // Find a URL to the resource first - final URL urlToResource = getUrl4Resource(resourceName); + final var urlToResource = getUrl4Resource(resourceName); // Check if the resource exists if (urlToResource == null) { @@ -143,11 +146,11 @@ public abstract class ResourceUtils { */ public static URL getUrlResource(final String resourceName) { try { - final ClassLoader classLoader = ResourceUtils.class.getClassLoader(); + final var classLoader = ResourceUtils.class.getClassLoader(); - final String[] fileParts = resourceName.split("/"); + final String[] fileParts = SLASH_PAT.split(resourceName); // Read the resource - URL url = classLoader.getResource(resourceName); + var url = classLoader.getResource(resourceName); // Check if the resource is defined if (url != null) { @@ -178,8 +181,8 @@ public abstract class ResourceUtils { public static URL getLocalFile(final String resourceName) { try { // Input might already be in URL format - final URL ret = new URL(resourceName); - final File f = new File(ret.toURI()); + final var ret = new URL(resourceName); + final var f = new File(ret.toURI()); if (f.exists()) { return ret; } @@ -188,10 +191,10 @@ public abstract class ResourceUtils { } try { - final File f = new File(resourceName); + final var f = new File(resourceName); // Check if the file exists if (f.exists()) { - final URL urlret = f.toURI().toURL(); + final var urlret = f.toURI().toURL(); LOGGER.debug("resource \"{}\" was found on the local file system", f.toURI().toURL()); return urlret; } else { @@ -215,7 +218,7 @@ public abstract class ResourceUtils { return null; } - URL modelFileUrl = getUrl4Resource(resource); + var modelFileUrl = getUrl4Resource(resource); if (modelFileUrl != null) { return modelFileUrl.getPath(); } else { @@ -231,7 +234,7 @@ public abstract class ResourceUtils { */ public static Set<String> getDirectoryContents(final String resourceDirectoryName) { // Find the location of the resource, is it in a Jar or on the local file system? - URL directoryUrl = ResourceUtils.getUrl4Resource(resourceDirectoryName); + var directoryUrl = ResourceUtils.getUrl4Resource(resourceDirectoryName); if (directoryUrl == null) { LOGGER.debug("resource \"{}\" was not found", resourceDirectoryName); @@ -259,7 +262,7 @@ public abstract class ResourceUtils { */ public static Set<String> getDirectoryContentsLocal(final URL localResourceDirectoryUrl, final String resourceDirectoryName) { - File localDirectory = new File(localResourceDirectoryUrl.getFile()); + var localDirectory = new File(localResourceDirectoryUrl.getFile()); if (!localDirectory.isDirectory()) { LOGGER.debug("resource \"{}\" is not a directory", resourceDirectoryName); @@ -290,12 +293,12 @@ public abstract class ResourceUtils { final String resourceDirectoryName) { String dirNameWithSlash = resourceDirectoryName + "/"; int minLength = dirNameWithSlash.length() + 1; - File jarResourceDirectory = new File(jarResourceDirectoryUrl.getPath()); + var jarResourceDirectory = new File(jarResourceDirectoryUrl.getPath()); String jarFileName = jarResourceDirectory.getParent().replaceFirst("^file:", "").replaceFirst("!.*$", ""); Set<String> localDirectorySet = new TreeSet<>(); - try (JarFile jarFile = new JarFile(jarFileName)) { + try (var jarFile = new JarFile(jarFileName)) { Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java index 2810c7be..6039e083 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020-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. @@ -50,7 +50,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static String getTextFileAsString(final String textFilePath) throws IOException { - final File textFile = new File(textFilePath); + final var textFile = new File(textFilePath); return Files.readString(textFile.toPath()); } @@ -62,7 +62,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static void putStringAsTextFile(final String outString, final String textFilePath) throws IOException { - final File textFile = new File(textFilePath); + final var textFile = new File(textFilePath); if (!textFile.getParentFile().exists()) { textFile.getParentFile().mkdirs(); } @@ -100,9 +100,9 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static String getReaderAsString(final Reader textReader) throws IOException { - final StringBuilder builder = new StringBuilder(); + final var builder = new StringBuilder(); int charsRead = -1; - final char[] chars = new char[READER_CHAR_BUFFER_SIZE_4096]; + final var chars = new char[READER_CHAR_BUFFER_SIZE_4096]; do { charsRead = textReader.read(chars); if (charsRead > 0) { diff --git a/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java b/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java index 50e85d9e..3de6df39 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 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. @@ -106,7 +106,7 @@ public class CryptoUtils implements CryptoCoder { * @return The encrypted String */ public static String encrypt(String value, String secretKey) { - SecretKeySpec keySpec = readSecretKeySpec(secretKey); + var keySpec = readSecretKeySpec(secretKey); return encryptValue(value, keySpec); } @@ -119,10 +119,10 @@ public class CryptoUtils implements CryptoCoder { return value; } try { - Cipher cipher = Cipher.getInstance(ALGORITHM_DETAILS); - byte[] iv = new byte[IV_BLOCK_SIZE_IN_BYTES]; + var cipher = Cipher.getInstance(ALGORITHM_DETAILS); + var iv = new byte[IV_BLOCK_SIZE_IN_BYTES]; RANDOM.nextBytes(iv); - GCMParameterSpec ivspec = new GCMParameterSpec(TAG_SIZE_IN_BITS, iv); + var ivspec = new GCMParameterSpec(TAG_SIZE_IN_BITS, iv); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivspec); return "enc:" + DatatypeConverter.printBase64Binary( @@ -157,7 +157,7 @@ public class CryptoUtils implements CryptoCoder { * @return The String decrypted if string begin with 'enc:' */ public static String decrypt(String value, String secretKey) { - SecretKeySpec keySpec = readSecretKeySpec(secretKey); + var keySpec = readSecretKeySpec(secretKey); if (keySpec != null) { return decryptValue(value, keySpec); } else { @@ -173,11 +173,11 @@ public class CryptoUtils implements CryptoCoder { throw new IllegalArgumentException("Invalid size on input value"); } try { - String pureValue = value.substring(4); + var pureValue = value.substring(4); byte[] encryptedValue = DatatypeConverter.parseBase64Binary(pureValue); - Cipher cipher = Cipher.getInstance(ALGORITHM_DETAILS); - GCMParameterSpec ivspec = new GCMParameterSpec(TAG_SIZE_IN_BITS, + var cipher = Cipher.getInstance(ALGORITHM_DETAILS); + var ivspec = new GCMParameterSpec(TAG_SIZE_IN_BITS, ArrayUtils.subarray(encryptedValue, 0, IV_BLOCK_SIZE_IN_BYTES)); byte[] realData = ArrayUtils.subarray(encryptedValue, IV_BLOCK_SIZE_IN_BYTES, encryptedValue.length); diff --git a/utils/src/main/java/org/onap/policy/common/utils/validation/Version.java b/utils/src/main/java/org/onap/policy/common/utils/validation/Version.java index 527113dd..46e006bd 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/validation/Version.java +++ b/utils/src/main/java/org/onap/policy/common/utils/validation/Version.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP COMMON * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,7 +21,6 @@ package org.onap.policy.common.utils.validation; -import com.google.re2j.Matcher; import com.google.re2j.Pattern; import lombok.Data; import lombok.NoArgsConstructor; @@ -56,7 +55,7 @@ public class Version implements Comparable<Version> { * @param versionString the version string */ public Version(@NonNull final String versionString) { - Version newVersion = makeVersion("String", "constructor", versionString); + var newVersion = makeVersion("String", "constructor", versionString); if (newVersion != null) { this.major = newVersion.major; @@ -79,7 +78,7 @@ public class Version implements Comparable<Version> { * that does not match the major.minor.patch form) */ public static Version makeVersion(String type, String name, String versionText) { - Matcher matcher = VERSION_PAT.matcher(versionText); + var matcher = VERSION_PAT.matcher(versionText); if (!matcher.matches()) { logger.info("invalid version for {} {}: {}", type, name, versionText); return null; @@ -113,7 +112,7 @@ public class Version implements Comparable<Version> { @Override public int compareTo(Version other) { - int result = Integer.compare(major, other.major); + var result = Integer.compare(major, other.major); if (result != 0) { return result; } |