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