sonar majorissue for ServicePropertyService.java
[dmaap/messagerouter/messageservice.git] / src / main / java / com / att / nsa / 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 com.att.nsa.dmaap.filemonitor;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.lang.reflect.Method;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Properties;
31
32 import javax.annotation.PostConstruct;
33
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37
38 /**
39  * ServicePropertyService class
40  * @author author
41  *
42  */
43 public class ServicePropertyService {
44         private boolean loadOnStartup;
45         private ServicePropertiesListener fileChangedListener;
46         private ServicePropertiesMap filePropertiesMap;
47         private String ssfFileMonitorPollingInterval;
48         private String ssfFileMonitorThreadpoolSize;
49         private List<File> fileList;
50         private static final String FILE_CHANGE_LISTENER_LOC = System
51                         .getProperty("AJSC_CONF_HOME") + "/etc";
52         private static final String USER_CONFIG_FILE = "service-file-monitor.properties";
53
54         private static final EELFLogger logger = EELFManager.getInstance().getLogger(ServicePropertyService.class);
55
56         // do not remove the postConstruct annotation, init method will not be
57         // called after constructor
58         /**
59          * Init method
60          * @throws Exception ex
61          */
62         @PostConstruct
63         public void init() throws Exception {
64
65                 try {
66                         getFileList(FILE_CHANGE_LISTENER_LOC);
67
68 //                      for (File file : fileList) {
69 //                                      FileChangedListener fileChangedListener = this.fileChangedListener;
70 //                                      Object filePropertiesMap = this.filePropertiesMap;
71 //                                      Method m = filePropertiesMap.getClass().getMethod(
72 //                                                      "refresh", File.class);
73 //                                      m.invoke(filePropertiesMap, file);
74 //                                      FileMonitor fm = FileMonitor.getInstance();
75 //                                      fm.addFileChangedListener(file, fileChangedListener,
76 //                                                      loadOnStartup);
77 //                              
78 //                      }
79                 } catch (Exception ex) {
80                         logger.error("Error creating property map ", ex);
81                 }
82
83         }
84
85         private void getFileList(String dirName) throws IOException {
86                 File directory = new File(dirName);
87                 FileInputStream fis = null;
88
89                 if (fileList == null)
90                         fileList = new ArrayList<>();
91
92                 // get all the files that are ".json" or ".properties", from a directory
93                 // & it's sub-directories
94                 File[] fList = directory.listFiles();
95
96                 for (File file : fList) {
97                         // read service property files from the configuration file
98                         if (file.isFile() && file.getPath().endsWith(USER_CONFIG_FILE)) {
99                                 try {
100                                         fis = new FileInputStream(file);
101                                         Properties prop = new Properties();
102                                         prop.load(fis);
103
104                                         for (String filePath : prop.stringPropertyNames()) {
105                                                 fileList.add(new File(prop.getProperty(filePath)));
106                                         }
107                                 } catch (Exception ioe) {
108                                         logger.error("Error reading the file stream ", ioe);
109                                 } finally {
110                                         if (fis != null) {
111                                                 fis.close();
112                                         }
113                                 }
114                         } else if (file.isDirectory()) {
115                                 getFileList(file.getPath());
116                         }
117                 }
118
119         }
120
121         public void setLoadOnStartup(boolean loadOnStartup) {
122                 this.loadOnStartup = loadOnStartup;
123         }
124
125         public void setSsfFileMonitorPollingInterval(
126                         String ssfFileMonitorPollingInterval) {
127                 this.ssfFileMonitorPollingInterval = ssfFileMonitorPollingInterval;
128         }
129
130         public void setSsfFileMonitorThreadpoolSize(
131                         String ssfFileMonitorThreadpoolSize) {
132                 this.ssfFileMonitorThreadpoolSize = ssfFileMonitorThreadpoolSize;
133         }
134
135         public boolean isLoadOnStartup() {
136                 return loadOnStartup;
137         }
138
139         public String getSsfFileMonitorPollingInterval() {
140                 return ssfFileMonitorPollingInterval;
141         }
142
143         public String getSsfFileMonitorThreadpoolSize() {
144                 return ssfFileMonitorThreadpoolSize;
145         }
146
147         public ServicePropertiesListener getFileChangedListener() {
148                 return fileChangedListener;
149         }
150
151         public void setFileChangedListener(
152                         ServicePropertiesListener fileChangedListener) {
153                 this.fileChangedListener = fileChangedListener;
154         }
155
156         public ServicePropertiesMap getFilePropertiesMap() {
157                 return filePropertiesMap;
158         }
159
160         public void setFilePropertiesMap(ServicePropertiesMap filePropertiesMap) {
161                 this.filePropertiesMap = filePropertiesMap;
162         }
163 }