2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2019 TechMahindra
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.universalvesadapter.utils;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
27 import org.json.simple.JSONArray;
28 import org.json.simple.JSONObject;
29 import org.json.simple.parser.JSONParser;
30 import org.json.simple.parser.ParseException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.stereotype.Component;
37 public class CollectorConfigPropertyRetrival {
39 @Value("${defaultConfigFilelocation}")
40 public String defaultConfigFilelocation;
41 private static final Logger debugLogger = LoggerFactory.getLogger("debugLogger");
42 private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");
43 private static JSONArray array;
45 public static JSONArray collectorConfigArray(String configFile) {
47 JSONParser parser = new JSONParser();
49 File file = new File(ClassLoader.getSystemResource(configFile.trim()).getFile());
51 FileReader fileReader = new FileReader(file);
52 JSONObject obj = (JSONObject) parser.parse(fileReader);
53 JSONObject appobj = (JSONObject) obj.get("app_preferences");
54 array = (JSONArray) appobj.get("collectors");
56 debugLogger.info("Retrieved JsonArray from Collector Config File");
58 } catch (ParseException e) {
59 errorLogger.error("ParseException occured at position:", e.getPosition());
60 } catch (FileNotFoundException e) {
62 errorLogger.error("Collector Config File is not found..", e.getMessage());
63 } catch (IOException e) {
65 errorLogger.error("Error occured due to :", e.getMessage());
72 public static String[] getProperyArray(String properyName, String defaultConfigFilelocation) {
73 JSONArray jsonArray = collectorConfigArray(defaultConfigFilelocation);
75 String[] propertyArray = new String[jsonArray.size()];
77 for (int k = 0; k < jsonArray.size(); k++) {
79 JSONObject collJson = (JSONObject) jsonArray.get(k);
81 propertyArray[k] = (String) collJson.get(properyName);
83 debugLogger.info("returning " + properyName + " array from Collector Config");