Remove unused Neo4jErrorsConfiguration
[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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.common.impl;
23
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;
33
34 /**
35  * Read configuration from file system
36  *
37  * @author esofer
38  */
39 @AllArgsConstructor
40 public class FSConfigurationSource implements ConfigurationSource {
41
42     private final ConfigFileChangeListener changeListener;
43     private final String appConfigDir;
44
45     /**
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
47      * be my-class.yaml .
48      *
49      * @param className
50      * @return file name based on the class name
51      */
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));
56     }
57
58     /*
59      * get and watch configuration changes. The file name we looking for is the lower case of the class name separated by "-".
60      *
61      * (non-Javadoc)
62      *
63      * @see org.openecomp.sdc.common.api.ConfigurationSource#getAndWatchConfiguration (java.lang.Class, org.openecomp.sdc.common.api.ConfigurationListener)
64      */
65     public <T> T getAndWatchConfiguration(final Class<T> className, final ConfigurationListener configurationListener) {
66         final String configFileName = calculateFileName(className);
67         T object;
68         try {
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);
73         }
74         if (configurationListener != null && changeListener != null) {
75             changeListener.register(configFileName, configurationListener);
76         }
77         return object;
78     }
79
80     public <T> void addWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
81         if (configurationListener != null && changeListener != null) {
82             changeListener.register(calculateFileName(className), configurationListener);
83         }
84     }
85 }