fixing warnings from checkstyle in common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / impl / FSConfigurationSource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  */
20
21 package org.openecomp.sdc.common.impl;
22
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;
27
28 /**
29  * Read configuration from file system
30  *
31  * @author esofer
32  */
33 public class FSConfigurationSource implements ConfigurationSource {
34
35     private YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
36
37     private ConfigFileChangeListener changeListener = null;
38     private String appConfigDir = null;
39
40     public FSConfigurationSource(ConfigFileChangeListener changeListener, String appConfigDir) {
41         super();
42         this.changeListener = changeListener;
43         this.appConfigDir = appConfigDir;
44     }
45
46     /*
47      * get and watch configuration changes. The file name we looking for is the lower case of the class name separated by "-".
48      *
49      * (non-Javadoc)
50      *
51      * @see org.openecomp.sdc.common.api.ConfigurationSource#getAndWatchConfiguration (java.lang.Class, org.openecomp.sdc.common.api.ConfigurationListener)
52      */
53     public <T> T getAndWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
54
55         String configFileName = calculateFileName(className);
56
57         T object = yamlToObjectConverter.convert(this.appConfigDir, className, configFileName);
58
59         if (configurationListener != null && changeListener != null) {
60             if (object != null) {
61                 changeListener.register(configFileName, configurationListener);
62             }
63         }
64
65         return object;
66     }
67
68     public <T> void addWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
69
70         String configFileName = calculateFileName(className);
71
72         if (configurationListener != null) {
73             changeListener.register(configFileName, configurationListener);
74         }
75
76     }
77
78     /**
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 .
80      *
81      * @param className
82      * @return file name based on the class name
83      */
84     private static <T> String calculateFileName(Class<T> className) {
85
86         String[] words = className.getSimpleName().split("(?=\\p{Upper})");
87
88         StringBuilder builder = new StringBuilder();
89
90         // There cannot be a null value returned from "split" - words != null is
91         // redundant
92         // if (words != null) {
93         boolean isFirst = true;
94         for (int i = 0; i < words.length; i++) {
95
96             String word = words[i];
97             if (word != null && !word.isEmpty()) {
98                 if (!isFirst) {
99                     builder.append("-");
100                 } else {
101                     isFirst = false;
102                 }
103                 builder.append(words[i].toLowerCase());
104             }
105         }
106         return builder.toString() + Constants.YAML_SUFFIX;
107
108         /*
109          * } else { return className.getSimpleName().toLowerCase() + Constants.YAML_SUFFIX; }
110          */
111
112     }
113
114 }