1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package com.att.nsa.dmaap.filemonitor;
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;
32 import javax.annotation.PostConstruct;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
39 * ServicePropertyService class
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";
54 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ServicePropertyService.class);
56 // do not remove the postConstruct annotation, init method will not be
57 // called after constructor
60 * @throws Exception ex
63 public void init() throws Exception {
66 getFileList(FILE_CHANGE_LISTENER_LOC);
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,
79 } catch (Exception ex) {
80 logger.error("Error creating property map ", ex);
85 private void getFileList(String dirName) throws IOException {
86 File directory = new File(dirName);
87 FileInputStream fis = null;
90 fileList = new ArrayList<>();
92 // get all the files that are ".json" or ".properties", from a directory
93 // & it's sub-directories
94 File[] fList = directory.listFiles();
96 for (File file : fList) {
97 // read service property files from the configuration file
98 if (file.isFile() && file.getPath().endsWith(USER_CONFIG_FILE)) {
100 fis = new FileInputStream(file);
101 Properties prop = new Properties();
104 for (String filePath : prop.stringPropertyNames()) {
105 fileList.add(new File(prop.getProperty(filePath)));
107 } catch (Exception ioe) {
108 logger.error("Error reading the file stream ", ioe);
114 } else if (file.isDirectory()) {
115 getFileList(file.getPath());
121 public void setLoadOnStartup(boolean loadOnStartup) {
122 this.loadOnStartup = loadOnStartup;
125 public void setSsfFileMonitorPollingInterval(
126 String ssfFileMonitorPollingInterval) {
127 this.ssfFileMonitorPollingInterval = ssfFileMonitorPollingInterval;
130 public void setSsfFileMonitorThreadpoolSize(
131 String ssfFileMonitorThreadpoolSize) {
132 this.ssfFileMonitorThreadpoolSize = ssfFileMonitorThreadpoolSize;
135 public boolean isLoadOnStartup() {
136 return loadOnStartup;
139 public String getSsfFileMonitorPollingInterval() {
140 return ssfFileMonitorPollingInterval;
143 public String getSsfFileMonitorThreadpoolSize() {
144 return ssfFileMonitorThreadpoolSize;
147 public ServicePropertiesListener getFileChangedListener() {
148 return fileChangedListener;
151 public void setFileChangedListener(
152 ServicePropertiesListener fileChangedListener) {
153 this.fileChangedListener = fileChangedListener;
156 public ServicePropertiesMap getFilePropertiesMap() {
157 return filePropertiesMap;
160 public void setFilePropertiesMap(ServicePropertiesMap filePropertiesMap) {
161 this.filePropertiesMap = filePropertiesMap;