Subversion Repositories XServices

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
102 brianR 1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
 
17
package net.brutex.xservices.util.cache;
18
 
19
import java.io.File;
20
import java.io.IOException;
21
import java.util.ArrayList;
22
import java.util.Enumeration;
23
import java.util.Iterator;
24
import java.util.List;
25
import java.util.concurrent.ExecutorService;
26
import java.util.regex.Matcher;
27
import java.util.regex.Pattern;
28
import javax.servlet.ServletContext;
29
import javax.servlet.ServletException;
30
import javax.servlet.http.HttpServlet;
31
import javax.ws.rs.core.Response;
32
import net.brutex.xservices.types.scm.ItemListType;
33
import net.brutex.xservices.types.scm.ItemType;
34
import net.brutex.xservices.types.scmfindings.FindingDetailsType;
35
import net.brutex.xservices.types.scmfindings.FindingType;
36
import net.brutex.xservices.types.scmfindings.FindingsListType;
37
import net.brutex.xservices.types.scmfindings.GroupMatchListType;
38
import net.brutex.xservices.types.scmfindings.ObjectFactory;
39
import net.brutex.xservices.ws.rs.CVSInfoImpl;
40
import org.apache.commons.configuration.ConfigurationException;
41
import org.apache.commons.configuration.PropertiesConfiguration;
42
import org.apache.jcs.JCS;
43
import org.apache.jcs.access.exception.CacheException;
44
import org.apache.log4j.Logger;
45
 
46
/**
47
 * @author Brian Rosenberger, bru(at)brutex.de
48
 *
49
 */
50
 
51
public class FindingsCacheServlet extends HttpServlet {
52
 
53
	private static final long serialVersionUID = 4041338473949999960L;
54
	private final static Logger logger = Logger
55
			.getLogger(FindingsCacheServlet.class);
56
	private final List<File> configfiles = new ArrayList<File>();
57
	private final ObjectFactory FACTORY = new ObjectFactory();
58
 
59
	@Override
60
	public void init() throws ServletException {
61
		super.init();
62
		ExecutorService executor = (ExecutorService) getServletContext()
63
				.getAttribute("CACHE_EXECUTOR");
64
 
65
		if(! this.initConfigList()) return;
66
		if(! this.initConfigFindings()) return;
67
 
68
		int i = 1;
69
		for(File f: configfiles) {
70
			//Initialise configuration bean using default values
71
			FindingsConfigBean cbean = new FindingsConfigBean(i, Logger.getLogger("worker-"+i+ "." + this.getClass().getName()));
72
			i++;
73
 
74
 
75
 
76
			//Read cvs-cache-interval parameter
77
			try {
78
				int cacheinterval = Integer.parseInt(getServletContext()
79
						.getInitParameter("cvs-cache-interval"));
80
				cbean.setCacheinterval(cacheinterval);
81
				logger.info("FindingsCacheServlet set to "+ cacheinterval + " minutes interval.");
82
			} catch (NumberFormatException e) {
83
				 logger.warn("Could not read parameter 'cvs-cache-interval' from web.xml. Using default value '"
84
						+ cbean.getCacheinterval()+ "' minutes");
85
			}
86
 
87
			PropertiesConfiguration config = null;
88
			try {
89
				config = new PropertiesConfiguration(f);
90
			} catch (ConfigurationException e) {
91
				logger.error("Could not read parameter file at '"+f.getAbsolutePath()+"'");
92
				return;
93
			}
94
 
95
 
96
			File cvsconfig = new File(config.getString("CVSROOTCONFIGFILE"));
97
			cbean.setCvsconfig(cvsconfig);
98
			FindingsCacheServlet.logger.debug("Fetching list of files using '"
99
					+ cvsconfig.getAbsolutePath() + "' config file");
100
 
101
 
102
			List<Object> filepatterns = config.getList("FILESEARCH");
103
			cbean.setFilepatterns(filepatterns);
104
			FindingsCacheServlet.logger.debug("Checking '"
105
					+ filepatterns.size()
106
					+ "' patterns for file name and path matching.");
107
 
108
 
109
			List<Object> contentpatterns = config.getList("CONTENTSEARCH");
110
			cbean.setContentpatterns(contentpatterns);
111
			FindingsCacheServlet.logger.debug("Checking '"
112
					+ contentpatterns.size()
113
					+ "' patterns for content matching");
114
 
115
 
116
 
117
 
118
			executor.submit(new ThisRunnable(cbean));
119
		}
120
		logger.info("FindingsCacheServlet has been initialized.");
121
 
122
	}
123
 
124
	/*
125
	 * Initialise CVS findings configuration
126
	 */
127
	private boolean initConfigFindings() {
128
		String filename = getServletContext().getInitParameter(
129
				"cvs-findings-configuration");
130
		if (filename == null) {
131
			logger.warn("'cvs-findings-configuration' init parameter is not specified.");
132
			return false;
133
		}
134
		final File findingsconfig = new File(filename);
135
		logger.info("CVS findings configuration file found at '"
136
				+ findingsconfig.getAbsolutePath() + "'");
137
		if ((!findingsconfig.canRead()) || (findingsconfig.isDirectory())) {
138
			logger.info("CVS findings configuration file '"
139
					+ findingsconfig.getAbsolutePath() + "' does not exist.");
140
			return false;
141
		}
142
		return true;
143
	}
144
 
145
	/*
146
	 * Add all specified CVS configuration files to the list. Parameter pattern
147
	 * is "cvs-config-XX".
148
	 */
149
	private boolean initConfigList() {
150
		Enumeration<String> attributes = getServletContext()
151
				.getInitParameterNames();
152
		while (attributes.hasMoreElements()) {
153
			String name = (String) attributes.nextElement();
154
			if (name.startsWith("cvs-config-")) {
155
				String configfile = getServletContext().getInitParameter(name);
156
				logger.info("Adding CVS configuration file: " + configfile);
157
				this.configfiles.add(new File(configfile));
158
			}
159
		}
160
		/*
161
		 * Verify, that all configuration files do exists and are readable.
162
		 */
163
		List<File> removelist = new ArrayList<File>();
164
		for (File f : configfiles) {
165
			if (!f.exists()) {
166
				logger.warn("CVS configuration file '"
167
						+ f.getAbsolutePath()
168
						+ "' is specified, but does not exist. Removing from list.");
169
				removelist.add(f);
170
			} else if (!f.canRead()) {
171
				logger.warn("CVS configuration file '"
172
						+ f.getAbsolutePath()
173
						+ "' does exist, but is not readable. Removing from list.");
174
				removelist.add(f);
175
			}
176
		}
177
		configfiles.removeAll(removelist);
178
		return true;
179
	}
180
 
181
 
182
 
183
 
184
	class ThisRunnable implements Runnable {
185
		boolean isInterrupted = false;
186
		FindingsConfigBean configuration;
187
 
188
		public ThisRunnable(FindingsConfigBean configuration) {
189
			this.configuration = configuration;
190
		}
191
 
192
		public void run() {
193
			CVSInfoImpl instance = new CVSInfoImpl();
194
 
195
			ItemListType fileslist = (ItemListType) instance
196
					.getRepositoryFiles(null, configuration.getCvsconfig(), "", true, true, true)
197
					.getEntity();
198
			ObjectFactory FACTORY = new ObjectFactory();
199
			FindingsListType findingsList = FACTORY.createFindingsListType();
200
 
201
			FindingsCacheServlet.logger.info("Processing '"
202
					+ fileslist.getItems().size() + "' files and directories.");
203
 
204
			while (!this.isInterrupted) {
205
				Pattern p;
206
				for (ItemType i : fileslist.getItems()) {
207
					if (this.isInterrupted)
208
						break;
209
					Iterator<Object> iterF = configuration.getFilepatterns().iterator();
210
					while(iterF.hasNext()) {
211
						Object o = iterF.next();
212
						if (this.isInterrupted)
213
							break;
214
						FindingsCacheServlet.logger.debug("Scanning filename '"
215
								+ i.getFullname() + "' for pattern '"
216
								+ (String) o + "'");
217
						p = Pattern.compile((String) o);
218
						Matcher m = p.matcher(i.getFullname());
219
						if (m.find()) {
220
							FindingType finding = FACTORY.createFindingType();
221
							finding.setFilesearch(p.toString());
222
							ItemType it = (ItemType) instance.getFileContent(
223
									null, configuration.getCvsconfig(), i.getFullname(), true)
224
									.getEntity();
225
							finding.setContent(it.getContent());
226
							finding.setData(it.getData());
227
							finding = copyDetails(finding, i);
228
							findingsList.getFindings().add(finding);
229
							FindingsCacheServlet.logger
230
									.debug("Match found for '"
231
											+ i.getFullname() + "'");
232
							break;
233
						}
234
						FindingsCacheServlet.logger
235
								.debug("No match found for '" + i.getFullname()
236
										+ "'");
237
					}
238
				}
239
				FindingsCacheServlet.logger
240
						.debug("Processing file content for '"
241
								+ findingsList.getFindings().size()
242
								+ "' entries in the list.");
243
 
244
				for (FindingType t : findingsList.getFindings()) {
245
					if (this.isInterrupted)
246
						break;
247
					boolean isFound = false;
248
					Matcher m;
249
					Iterator<Object> iter = configuration.getContentpatterns().iterator();
250
					while (iter.hasNext()) {
251
 
252
						Object o = iter.next();
253
						if (this.isInterrupted)
254
							break;
255
						FindingsCacheServlet.logger
256
								.debug("Scanning file content for file '"
257
										+ t.getFullname() + "' for pattern '"
258
										+ (String) o + "'");
259
 
260
						Pattern p1 = Pattern.compile((String) o);
261
						m = p1.matcher(t.getContent());
262
						t.setContentsearch(p1.toString());
263
 
264
						isFound = true;
265
						int s = m.start();
266
						int e = m.end();
267
						String c = m.group();
268
 
269
						FindingDetailsType fd = FACTORY
270
								.createFindingDetailsType();
271
						GroupMatchListType gm = FACTORY
272
								.createGroupMatchListType();
273
						gm.setMatchAtIndex(s);
274
						gm.setMatchToIndex(e);
275
						gm.setMatchString(c);
276
						gm.setMatchGroup(0);
277
						fd.setFullmatch(gm);
278
						for (int i = 1; i <= m.groupCount(); i++) {
279
							GroupMatchListType gmg = FACTORY
280
									.createGroupMatchListType();
281
							s = m.start(i);
282
							e = m.end(i);
283
							c = m.group(i);
284
							gmg.setMatchAtIndex(s);
285
							gmg.setMatchToIndex(e);
286
							gmg.setMatchString(c);
287
							gmg.setMatchGroup(i);
288
							fd.getMatchLists().add(gmg);
289
						}
290
						t.getFindingLists().add(fd);
291
						FindingsCacheServlet.logger
292
								.debug("Found matching content at index '" + s
293
										+ "' in file '" + t.getFullname()
294
										+ "' with pattern '" + p1.toString()
295
										+ "'");
296
					}
297
 
298
					if (!isFound) {
299
						findingsList.getFindings().remove(t);
300
						FindingsCacheServlet.logger
301
								.debug("Found matching filename for '"
302
										+ t.getFullname()
303
										+ "' but content didn't match. Removing.");
304
					}
305
 
306
					try {
307
						instance.getCacheInstance().put(
308
								"FINDINGS-" + t.getROOT(), findingsList);
309
						FindingsCacheServlet.logger
310
								.info("FINDINGS for CVSROOT '" + t.getROOT()
311
										+ "' have been updated in cache.");
312
					} catch (CacheException e) {
313
						FindingsCacheServlet.logger.error(e.getMessage(), e);
314
					}
315
				}
316
				try {
317
					int cacheinterval = configuration.getCacheinterval();
318
					FindingsCacheServlet.logger.debug("Now sleeping for '"
319
							+ cacheinterval + "' minutes");
320
					Thread.currentThread();
321
					Thread.sleep(cacheinterval * 60000);
322
					FindingsCacheServlet.logger.debug("Waking up after '"
323
							+ cacheinterval + "' minutes of sleep");
324
				} catch (InterruptedException e) {
325
					this.isInterrupted = true;
326
					FindingsCacheServlet.logger
327
							.warn("FindingsCacheServlet cache was interrupted. Shutting down.");
328
				}
329
			}
330
 
331
		}
332
 
333
		private FindingType copyDetails(FindingType finding, ItemType item) {
334
			finding.setDescription(item.getDescription());
335
			finding.setFullname(item.getFullname());
336
			finding.setName(item.getName());
337
			finding.setPath(item.getPath());
338
			finding.setRemotefullname(item.getRemotefullname());
339
			finding.setRemotename(item.getRemotename());
340
			finding.setRemotepath(item.getRemotepath());
341
			finding.setROOT(item.getROOT());
342
			return finding;
343
		}
344
 
345
	}
346
}