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 * ================================================================================
22 package org.openecomp.sdc.common.impl;
24 import java.util.Arrays;
25 import java.util.stream.Collectors;
26 import lombok.AllArgsConstructor;
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.api.exception.LoadConfigurationException;
31 import org.openecomp.sdc.common.util.YamlToObjectConverter;
32 import org.openecomp.sdc.exception.YamlConversionException;
35 * Read configuration from file system
40 public class FSConfigurationSource implements ConfigurationSource {
42 private final ConfigFileChangeListener changeListener;
43 private final String appConfigDir;
46 * 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
50 * @return file name based on the class name
52 static <T> String calculateFileName(Class<T> className) {
53 String[] words = className.getSimpleName().split("(?=\\p{Upper})");
54 return Arrays.stream(words).map(String::toLowerCase)
55 .collect(Collectors.collectingAndThen(Collectors.joining("-"), str -> str + Constants.YAML_SUFFIX));
59 * get and watch configuration changes. The file name we looking for is the lower case of the class name separated by "-".
63 * @see org.openecomp.sdc.common.api.ConfigurationSource#getAndWatchConfiguration (java.lang.Class, org.openecomp.sdc.common.api.ConfigurationListener)
65 public <T> T getAndWatchConfiguration(final Class<T> className, final ConfigurationListener configurationListener) {
66 final String configFileName = calculateFileName(className);
69 object = (new YamlToObjectConverter()).convert(this.appConfigDir, className, configFileName);
70 } catch (final YamlConversionException e) {
71 final String errorMsg = String.format("Could not load '%s' in '%s' for class '%s'", configFileName, appConfigDir, className);
72 throw new LoadConfigurationException(errorMsg, e);
74 if (configurationListener != null && changeListener != null) {
75 changeListener.register(configFileName, configurationListener);
80 public <T> void addWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
81 if (configurationListener != null && changeListener != null) {
82 changeListener.register(calculateFileName(className), configurationListener);