2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.config;
26 import com.google.common.base.Splitter;
28 import com.google.gson.JsonElement;
29 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.List;
34 import org.apache.commons.io.IOUtils;
35 import org.onap.clamp.clds.util.JsonUtils;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.core.env.Environment;
39 import org.springframework.stereotype.Component;
42 * Holds Clamp properties and add some functionalities.
45 public class ClampProperties {
48 private ApplicationContext appContext;
50 private Environment env;
51 public static final String CONFIG_PREFIX = "clamp.config.";
58 * @return The string with the value
60 public String getStringValue(String key) {
61 return env.getProperty(CONFIG_PREFIX + key);
65 * get property value for a combo key (key1 + "." + key2). If not found just use
71 * The second key after a dot
72 * @return The string with the value
74 public String getStringValue(String key1, String key2) {
75 String value = getStringValue(key1 + "." + key2);
76 if (value == null || value.length() == 0) {
77 value = getStringValue(key1);
83 * Return json as objects that can be updated. The value obtained from the
84 * clds-reference file will be used as a filename.
87 * The key that will be used to access the clds-reference file
90 * In case of issues with the JSON parser
92 public JsonElement getJsonTemplate(String key) throws IOException {
93 String fileReference = getStringValue(key);
94 return (fileReference != null)
95 ? JsonUtils.GSON.fromJson(getFileContentFromPath(fileReference), JsonElement.class)
100 * Return json as objects that can be updated. First try with combo key (key1 +
101 * "." + key2), otherwise default to just key1. The value obtained from the
102 * clds-reference file will be used as a filename.
107 * The second key after a dot
109 * @throws IOException
110 * In case of issues with the JSON parser
112 public JsonElement getJsonTemplate(String key1, String key2) throws IOException {
113 String fileReference = getStringValue(key1, key2);
114 return (fileReference != null)
115 ? JsonUtils.GSON.fromJson(getFileContentFromPath(fileReference), JsonElement.class)
120 * Return the file content. The value obtained from the clds-reference file will
121 * be used as a filename.
124 * The key that will be used to access the clds-reference file
125 * @return File content in String
126 * @throws IOException
127 * In case of issues with the JSON parser
129 public String getFileContent(String key) throws IOException {
130 String fileReference = getStringValue(key);
131 return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
135 * Return the file content. First try with combo key (key1 + "." + key2),
136 * otherwise default to just key1. The value obtained from the clds-reference
137 * file will be used as a filename.
142 * The second key after a dot
143 * @return File content in String
144 * @throws IOException
145 * In case of issues with the JSON parser
147 public String getFileContent(String key1, String key2) throws IOException {
148 String fileReference = getStringValue(key1, key2);
149 return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
152 private String getFileContentFromPath(String filepath) throws IOException {
153 URL url = appContext.getResource(filepath).getURL();
154 return IOUtils.toString(url, StandardCharsets.UTF_8);
158 * Returns the list of strings split with separator.
163 * property value separator
164 * @return List of Strings split with a separator
166 public List<String> getStringList(String key, String separator) {
167 return Splitter.on(separator).trimResults().omitEmptyStrings()
168 .splitToList(env.getProperty(CONFIG_PREFIX + key));