From d2b10d0697934a82868878c6027962e5cb5d1882 Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Wed, 25 Nov 2020 12:33:58 +0100 Subject: Improve code quality - YamlToObjectConverter Issue-ID: SDC-3389 Signed-off-by: Zebek Bogumil Change-Id: I2819a423b375065b067ed9f0d147737ac07a7ba4 --- .../org/onap/sdc/utils/YamlToObjectConverter.java | 73 +++++++--------------- 1 file changed, 24 insertions(+), 49 deletions(-) diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/YamlToObjectConverter.java b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/YamlToObjectConverter.java index add82bc..ab0a528 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/utils/YamlToObjectConverter.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/utils/YamlToObjectConverter.java @@ -22,11 +22,11 @@ package org.onap.sdc.utils; import java.beans.IntrospectionException; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; import java.util.HashMap; +import java.util.Map; import org.onap.sdc.utils.heat.HeatConfiguration; import org.slf4j.Logger; @@ -42,15 +42,17 @@ public class YamlToObjectConverter { private static Logger log = LoggerFactory .getLogger(YamlToObjectConverter.class.getName()); - private static HashMap yamls = new HashMap(); + private static final Map YAMLS = new HashMap<>(); - private static Yaml defaultYaml = new Yaml(); + private static final Yaml DEFAULT_YAML = new Yaml(); static { + YAMLS.put(HeatConfiguration.class.getName(), provideYamlForHeatConfiguration()); + } + private static Yaml provideYamlForHeatConfiguration() { org.yaml.snakeyaml.constructor.Constructor heatConstructor = new org.yaml.snakeyaml.constructor.Constructor(HeatConfiguration.class); TypeDescription heatDescription = new TypeDescription(HeatConfiguration.class); - //heatDescription.putListPropertyType("parameters", HeatParameterConfiguration.class); heatConstructor.addTypeDescription(heatDescription); PropertyUtils propertyUtils = new PropertyUtils() { @Override @@ -66,85 +68,49 @@ public class YamlToObjectConverter { propertyUtils.setSkipMissingProperties(true); heatConstructor.setPropertyUtils(propertyUtils); - Yaml yaml = new Yaml(heatConstructor); - - yamls.put(HeatConfiguration.class.getName(), yaml); - - } - - private static Yaml getYamlByClassName(Class className) { - - Yaml yaml = yamls.get(className.getName()); - if (yaml == null) { - yaml = defaultYaml; - } - - return yaml; + return new Yaml(heatConstructor); } public T convert(String dirPath, Class className, String configFileName) { - T config = null; try { - String fullFileName = dirPath + File.separator + configFileName; - config = convert(fullFileName, className); - } catch (Exception e) { - log.error("Failed to convert yaml file " + configFileName - + " to object.", e); + log.error("Failed to convert yaml file {} to object.", configFileName, e); } return config; } public T convert(String fullFileName, Class className) { - T config = null; Yaml yaml = getYamlByClassName(className); - InputStream in = null; try { - File f = new File(fullFileName); if (!f.exists()) { - log.warn("The file " + fullFileName - + " cannot be found. Ignore reading configuration."); - return null; - } - in = Files.newInputStream(Paths.get(fullFileName)); - - config = yaml.loadAs(in, className); - - // System.out.println(config.toString()); - } catch (Exception e) { - log.error("Failed to convert yaml file " + fullFileName - + " to object.", e); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.warn("The file {} cannot be found. Ignore reading configuration.", fullFileName); + } else { + try (InputStream in = Files.newInputStream(Paths.get(fullFileName))) { + config = yaml.loadAs(in, className); } } + } catch (Exception e) { + log.error("Failed to convert yaml file {} to object.", fullFileName, e); } return config; } public T convertFromString(String yamlContents, Class className) { - T config = null; - Yaml yaml = getYamlByClassName(className); - try { + Yaml yaml = getYamlByClassName(className); config = yaml.loadAs(yamlContents, className); } catch (Exception e) { log.error("Failed to convert YAML {} to object.", yamlContents, e); @@ -152,4 +118,13 @@ public class YamlToObjectConverter { return config; } + + private static synchronized Yaml getYamlByClassName(Class className) { + Yaml yaml = YAMLS.get(className.getName()); + if (yaml == null) { + yaml = DEFAULT_YAML; + } + + return yaml; + } } -- cgit 1.2.3-korg