summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dmaap/filemonitor/ServicePropertyService.java
blob: fe25e5800ec549eab7f42cf90f3fc8ecee78e0d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  org.onap.dmaap
 *  ================================================================================
 *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
*  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ============LICENSE_END=========================================================
 *  
 *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
 *  
 *******************************************************************************/
 package org.onap.dmaap.filemonitor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.annotation.PostConstruct;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;


//import com.att.ssf.filemonitor.FileChangedListener;
//import com.att.ssf.filemonitor.FileMonitor;

/**
 * ServicePropertyService class
 * @author rajashree.khare
 *
 */
public class ServicePropertyService {
	private boolean loadOnStartup;
	private ServicePropertiesListener fileChangedListener;
	private ServicePropertiesMap filePropertiesMap;
	private String ssfFileMonitorPollingInterval;
	private String ssfFileMonitorThreadpoolSize;
	private List<File> fileList;
	private static final String FILE_CHANGE_LISTENER_LOC = System
			.getProperty("AJSC_CONF_HOME") + "/etc";
	private static final String USER_CONFIG_FILE = "service-file-monitor.properties";

	private static final EELFLogger logger = EELFManager.getInstance().getLogger(ServicePropertyService.class);

	// do not remove the postConstruct annotation, init method will not be
	// called after constructor
	/**
	 * Init method
	 * @throws Exception ex
	 */
	@PostConstruct
	public void init()  {

		try {
			getFileList(FILE_CHANGE_LISTENER_LOC);

			/*for (File file : fileList) {
					FileChangedListener fileChangedListener = this.fileChangedListener;
					Object filePropertiesMap = this.filePropertiesMap;
					Method m = filePropertiesMap.getClass().getMethod(
							"refresh", File.class);
					m.invoke(filePropertiesMap, file);
					FileMonitor fm = FileMonitor.getInstance();
					fm.addFileChangedListener(file, fileChangedListener,
							loadOnStartup);
				
			}*/
		} catch (Exception ex) {
			logger.error("Error creating property map ", ex);
		}

	}

	private void getFileList(String dirName) throws IOException {
		File directory = new File(dirName);
		FileInputStream fis = null;

		if (fileList == null)
			fileList = new ArrayList<File>();

		// get all the files that are ".json" or ".properties", from a directory
		// & it's sub-directories
		File[] fList = directory.listFiles();

		for (File file : fList) {
			// read service property files from the configuration file
			if (file.isFile() && file.getPath().endsWith(USER_CONFIG_FILE)) {
				try {
					fis = new FileInputStream(file);
					Properties prop = new Properties();
					prop.load(fis);

					for (String filePath : prop.stringPropertyNames()) {
						fileList.add(new File(prop.getProperty(filePath)));
					}
				} catch (Exception ioe) {
					logger.error("Error reading the file stream ", ioe);
				} finally {
					fis.close();
				}
			} else if (file.isDirectory()) {
				getFileList(file.getPath());
			}
		}

	}

	public void setLoadOnStartup(boolean loadOnStartup) {
		this.loadOnStartup = loadOnStartup;
	}

	public void setSsfFileMonitorPollingInterval(
			String ssfFileMonitorPollingInterval) {
		this.ssfFileMonitorPollingInterval = ssfFileMonitorPollingInterval;
	}

	public void setSsfFileMonitorThreadpoolSize(
			String ssfFileMonitorThreadpoolSize) {
		this.ssfFileMonitorThreadpoolSize = ssfFileMonitorThreadpoolSize;
	}

	public boolean isLoadOnStartup() {
		return loadOnStartup;
	}

	public String getSsfFileMonitorPollingInterval() {
		return ssfFileMonitorPollingInterval;
	}

	public String getSsfFileMonitorThreadpoolSize() {
		return ssfFileMonitorThreadpoolSize;
	}

	public ServicePropertiesListener getFileChangedListener() {
		return fileChangedListener;
	}

	public void setFileChangedListener(
			ServicePropertiesListener fileChangedListener) {
		this.fileChangedListener = fileChangedListener;
	}

	public ServicePropertiesMap getFilePropertiesMap() {
		return filePropertiesMap;
	}

	public void setFilePropertiesMap(ServicePropertiesMap filePropertiesMap) {
		this.filePropertiesMap = filePropertiesMap;
	}
}