re base code
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / impl / ExternalConfiguration.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.apache.commons.jci.monitor.FilesystemAlterationMonitor;
24 import org.openecomp.sdc.common.api.ConfigurationSource;
25
26 import java.io.File;
27
28 /**
29  * 
30  * Save the
31  * 
32  * @author esofer
33  *
34  */
35 public class ExternalConfiguration {
36
37         private static String appName;
38         private static String appVersion;
39         private static String configDir;
40         private static ConfigurationSource configurationSource;
41
42         private static FilesystemAlterationMonitor fam = null;
43
44         private static ConfigFileChangeListener changeListener = new ConfigFileChangeListener();
45
46         private static boolean enableReconfigure = true;
47
48         public static String getAppName() {
49                 return appName;
50         }
51
52         public static void setAppName(String appName) {
53                 ExternalConfiguration.appName = appName;
54         }
55
56         public static String getAppVersion() {
57                 return appVersion;
58         }
59
60         public static void setAppVersion(String appVersion) {
61                 ExternalConfiguration.appVersion = appVersion;
62         }
63
64         public static String getConfigDir() {
65                 return configDir;
66         }
67
68         public static void setConfigDir(String configDir) {
69                 ExternalConfiguration.configDir = configDir;
70         }
71
72         public static ConfigurationSource getConfigurationSource() {
73                 return configurationSource;
74         }
75
76         public static void setConfigurationSource(ConfigurationSource configurationSource) {
77                 ExternalConfiguration.configurationSource = configurationSource;
78         }
79
80         public static ConfigFileChangeListener getChangeListener() {
81                 return changeListener;
82         }
83
84         public static void listenForChanges() {
85
86                 String watchingDir = configDir + File.separator + appName;
87                 if (enableReconfigure) {
88                         if (fam == null) {
89                                 fam = new FilesystemAlterationMonitor();
90                                 fam.setInterval(1000);
91                                 fam.addListener(new File(watchingDir), changeListener);
92                                 fam.start();
93                         }
94                 }
95         }
96
97         public static void stopListenForFileChanges() {
98                 if (enableReconfigure) {
99                         if (fam != null) {
100                                 fam.stop();
101                                 fam = null;
102                         }
103                 }
104         }
105
106 }