2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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=========================================================
19 * Modifications copyright (c) 2019 Nokia
20 * ================================================================================
23 package org.openecomp.sdc.common.impl;
25 import java.util.Arrays;
26 import java.util.stream.Collectors;
27 import org.openecomp.sdc.common.api.ConfigurationListener;
28 import org.openecomp.sdc.common.api.ConfigurationSource;
29 import org.openecomp.sdc.common.api.Constants;
30 import org.openecomp.sdc.common.util.YamlToObjectConverter;
33 * Read configuration from file system
38 public class FSConfigurationSource implements ConfigurationSource {
40 private final YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
42 private final ConfigFileChangeListener changeListener;
43 private final String appConfigDir;
45 public FSConfigurationSource(ConfigFileChangeListener changeListener, String appConfigDir) {
47 this.changeListener = changeListener;
48 this.appConfigDir = appConfigDir;
52 * get and watch configuration changes. The file name we looking for is the lower case of the class name separated by "-".
56 * @see org.openecomp.sdc.common.api.ConfigurationSource#getAndWatchConfiguration (java.lang.Class, org.openecomp.sdc.common.api.ConfigurationListener)
58 public <T> T getAndWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
60 String configFileName = calculateFileName(className);
62 T object = yamlToObjectConverter.convert(this.appConfigDir, className, configFileName);
64 if (configurationListener != null && changeListener != null) {
66 changeListener.register(configFileName, configurationListener);
73 public <T> void addWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
75 String configFileName = calculateFileName(className);
77 if (configurationListener != null) {
78 changeListener.register(configFileName, configurationListener);
84 * convert camel case string to list of words separated by "-" where each word is in lower case format. For example, MyClass will be calculated to be my-class.yaml .
87 * @return file name based on the class name
89 static <T> String calculateFileName(Class<T> className) {
90 String[] words = className.getSimpleName().split("(?=\\p{Upper})");
92 return Arrays.stream(words)
93 .map(String::toLowerCase)
94 .collect(Collectors.collectingAndThen(
95 Collectors.joining("-"),
96 str -> str + Constants.YAML_SUFFIX)