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