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=========================================================
21 package org.openecomp.sdc.common.impl;
23 import org.openecomp.sdc.common.api.ConfigurationListener;
24 import org.openecomp.sdc.common.api.ConfigurationSource;
25 import org.openecomp.sdc.common.api.Constants;
26 import org.openecomp.sdc.common.util.YamlToObjectConverter;
29 * Read configuration from file system
33 public class FSConfigurationSource implements ConfigurationSource {
35 private YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
37 private ConfigFileChangeListener changeListener = null;
38 private String appConfigDir = null;
40 public FSConfigurationSource(ConfigFileChangeListener changeListener, String appConfigDir) {
42 this.changeListener = changeListener;
43 this.appConfigDir = appConfigDir;
47 * get and watch configuration changes. The file name we looking for is the lower case of the class name separated by "-".
51 * @see org.openecomp.sdc.common.api.ConfigurationSource#getAndWatchConfiguration (java.lang.Class, org.openecomp.sdc.common.api.ConfigurationListener)
53 public <T> T getAndWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
55 String configFileName = calculateFileName(className);
57 T object = yamlToObjectConverter.convert(this.appConfigDir, className, configFileName);
59 if (configurationListener != null && changeListener != null) {
61 changeListener.register(configFileName, configurationListener);
68 public <T> void addWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
70 String configFileName = calculateFileName(className);
72 if (configurationListener != null) {
73 changeListener.register(configFileName, configurationListener);
79 * 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 .
82 * @return file name based on the class name
84 private static <T> String calculateFileName(Class<T> className) {
86 String[] words = className.getSimpleName().split("(?=\\p{Upper})");
88 StringBuilder builder = new StringBuilder();
90 // There cannot be a null value returned from "split" - words != null is
92 // if (words != null) {
93 boolean isFirst = true;
94 for (int i = 0; i < words.length; i++) {
96 String word = words[i];
97 if (word != null && !word.isEmpty()) {
103 builder.append(words[i].toLowerCase());
106 return builder.toString() + Constants.YAML_SUFFIX;
109 * } else { return className.getSimpleName().toLowerCase() + Constants.YAML_SUFFIX; }