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