update link to upper-constraints.txt
[dmaap/messagerouter/messageservice.git] / src / main / java / org / onap / dmaap / filemonitor / ServicePropertyService.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 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  *        http://www.apache.org/licenses/LICENSE-2.0
11 *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *
21  *******************************************************************************/
22  package org.onap.dmaap.filemonitor;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
32 import javax.annotation.PostConstruct;
33
34 /**
35  * ServicePropertyService class
36  * @author rajashree.khare
37  *
38  */
39 public class ServicePropertyService {
40         private boolean loadOnStartup;
41         private ServicePropertiesListener fileChangedListener;
42         private ServicePropertiesMap filePropertiesMap;
43         private String ssfFileMonitorPollingInterval;
44         private String ssfFileMonitorThreadpoolSize;
45         private List<File> fileList;
46         private static final String FILE_CHANGE_LISTENER_LOC = System
47                         .getProperty("AJSC_CONF_HOME") + "/etc";
48         private static final String USER_CONFIG_FILE = "service-file-monitor.properties";
49
50         private static final EELFLogger logger = EELFManager.getInstance().getLogger(ServicePropertyService.class);
51
52         // do not remove the postConstruct annotation, init method will not be
53         // called after constructor
54         /**
55          * Init method
56          * @throws Exception ex
57          */
58         @PostConstruct
59         public void init()  {
60
61                 try {
62                         getFileList(FILE_CHANGE_LISTENER_LOC);
63
64                 } catch (Exception ex) {
65                         logger.error("Error creating property map ", ex);
66                 }
67
68         }
69
70         private void getFileList(String dirName) throws IOException {
71                 File directory = new File(dirName);
72
73                 if (fileList == null)
74                         fileList = new ArrayList<>();
75
76                 // get all the files that are ".json" or ".properties", from a directory
77                 // & it's sub-directories
78                 File[] fList = directory.listFiles();
79
80                 for (File file : fList) {
81                         // read service property files from the configuration file
82                         if (file.isFile() && file.getPath().endsWith(USER_CONFIG_FILE)) {
83                                 try(FileInputStream fis = new FileInputStream(file)) {
84
85                                         Properties prop = new Properties();
86                                         prop.load(fis);
87
88                                         for (String filePath : prop.stringPropertyNames()) {
89                                                 fileList.add(new File(prop.getProperty(filePath)));
90                                         }
91                                 } catch (Exception ioe) {
92                                         logger.error("Error reading the file stream ", ioe);
93                                 }
94                         } else if (file.isDirectory()) {
95                                 getFileList(file.getPath());
96                         }
97                 }
98
99         }
100
101         public void setLoadOnStartup(boolean loadOnStartup) {
102                 this.loadOnStartup = loadOnStartup;
103         }
104
105         public void setSsfFileMonitorPollingInterval(
106                         String ssfFileMonitorPollingInterval) {
107                 this.ssfFileMonitorPollingInterval = ssfFileMonitorPollingInterval;
108         }
109
110         public void setSsfFileMonitorThreadpoolSize(
111                         String ssfFileMonitorThreadpoolSize) {
112                 this.ssfFileMonitorThreadpoolSize = ssfFileMonitorThreadpoolSize;
113         }
114
115         public boolean isLoadOnStartup() {
116                 return loadOnStartup;
117         }
118
119         public String getSsfFileMonitorPollingInterval() {
120                 return ssfFileMonitorPollingInterval;
121         }
122
123         public String getSsfFileMonitorThreadpoolSize() {
124                 return ssfFileMonitorThreadpoolSize;
125         }
126
127         public ServicePropertiesListener getFileChangedListener() {
128                 return fileChangedListener;
129         }
130
131         public void setFileChangedListener(
132                         ServicePropertiesListener fileChangedListener) {
133                 this.fileChangedListener = fileChangedListener;
134         }
135
136         public ServicePropertiesMap getFilePropertiesMap() {
137                 return filePropertiesMap;
138         }
139
140         public void setFilePropertiesMap(ServicePropertiesMap filePropertiesMap) {
141                 this.filePropertiesMap = filePropertiesMap;
142         }
143 }