Subversion Repositories XServices

Rev

Rev 198 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 198 Rev 199
Line 28... Line 28...
28
import java.util.regex.Pattern;
28
import java.util.regex.Pattern;
29
import javax.servlet.ServletContext;
29
import javax.servlet.ServletContext;
30
import javax.servlet.ServletException;
30
import javax.servlet.ServletException;
31
import javax.servlet.http.HttpServlet;
31
import javax.servlet.http.HttpServlet;
32
import javax.ws.rs.core.Response;
32
import javax.ws.rs.core.Response;
-
 
33
 
-
 
34
import lombok.extern.slf4j.Slf4j;
33
import net.brutex.xservices.types.scm.ItemListType;
35
import net.brutex.xservices.types.scm.ItemListType;
34
import net.brutex.xservices.types.scm.ItemType;
36
import net.brutex.xservices.types.scm.ItemType;
35
import net.brutex.xservices.types.scmfindings.FindingDetailsType;
37
import net.brutex.xservices.types.scmfindings.FindingDetailsType;
36
import net.brutex.xservices.types.scmfindings.FindingType;
38
import net.brutex.xservices.types.scmfindings.FindingType;
37
import net.brutex.xservices.types.scmfindings.FindingsListType;
39
import net.brutex.xservices.types.scmfindings.FindingsListType;
Line 41... Line 43...
41
 
43
 
42
import org.apache.commons.configuration2.PropertiesConfiguration;
44
import org.apache.commons.configuration2.PropertiesConfiguration;
43
import org.apache.commons.configuration2.builder.fluent.Configurations;
45
import org.apache.commons.configuration2.builder.fluent.Configurations;
44
import org.apache.commons.configuration2.ex.ConfigurationException;
46
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.commons.jcs.access.exception.CacheException;
Line 47... Line 48...
47
import org.apache.logging.log4j.Logger;
48
 
48
 
49
 
49
/**
50
/**
50
 * @author Brian Rosenberger, bru(at)brutex.de
51
 * @author Brian Rosenberger, bru(at)brutex.de
51
 * 
52
 * 
52
 */
53
 */
Line 53... Line 54...
53
 
54
@Slf4j
54
public class FindingsCacheServlet extends HttpServlet {
-
 
55
 
55
public class FindingsCacheServlet extends HttpServlet {
56
	private static final long serialVersionUID = 4041338473949999960L;
56
 
57
	private static final Logger logger = LogManager.getLogger();
57
	private static final long serialVersionUID = 4041338473949999960L;
Line 58... Line 58...
58
	private final List<File> configfiles = new ArrayList<File>();
58
	private final List<File> configfiles = new ArrayList<File>();
Line 69... Line 69...
69
		if(! this.initConfigFindings()) return;
69
		if(! this.initConfigFindings()) return;
Line 70... Line 70...
70
 
70
 
71
		int i = 1;
71
		int i = 1;
72
		for(File f: configfiles) {
72
		for(File f: configfiles) {
73
			//Initialise configuration bean using default values
73
			//Initialise configuration bean using default values
74
			FindingsConfigBean cbean = new FindingsConfigBean(i, LogManager.getLogger("worker-"+i+ "." + this.getClass().getName()));
74
			FindingsConfigBean cbean = new FindingsConfigBean(i);
Line 75... Line 75...
75
			i++;
75
			i++;
76
			
76
			
77
			
77
			
78
			
78
			
79
			//Read cvs-cache-interval parameter
79
			//Read cvs-cache-interval parameter
80
			try {
80
			try {
81
				int cacheinterval = Integer.parseInt(getServletContext()
81
				int cacheinterval = Integer.parseInt(getServletContext()
82
						.getInitParameter("cvs-cache-interval"));
82
						.getInitParameter("cvs-cache-interval"));
83
				cbean.setCacheinterval(cacheinterval);
83
				cbean.setCacheinterval(cacheinterval);
84
				logger.info("FindingsCacheServlet set to "+ cacheinterval + " minutes interval.");
84
				log.info("FindingsCacheServlet set to "+ cacheinterval + " minutes interval.");
Line 85... Line 85...
85
			} catch (NumberFormatException e) {
85
			} catch (NumberFormatException e) {
86
				 logger.warn("Could not read parameter 'cvs-cache-interval' from web.xml. Using default value '"
86
				 log.warn("Could not read parameter 'cvs-cache-interval' from web.xml. Using default value '"
87
						+ cbean.getCacheinterval()+ "' minutes");
87
						+ cbean.getCacheinterval()+ "' minutes");
88
			}
88
			}
89
			
89
			
90
			Configurations configs = new Configurations();
90
			Configurations configs = new Configurations();
91
			PropertiesConfiguration config = null;
91
			PropertiesConfiguration config = null;
92
			try {
92
			try {
Line 93... Line 93...
93
				config = configs.properties(f);
93
				config = configs.properties(f);
94
			} catch (ConfigurationException e) {
94
			} catch (ConfigurationException e) {
95
				logger.error("Could not read parameter file at '"+f.getAbsolutePath()+"'");
95
				log.error("Could not read parameter file at '"+f.getAbsolutePath()+"'");
96
				return;
96
				return;
Line 97... Line 97...
97
			}
97
			}
98
			
98
			
99
					
99
					
100
			File cvsconfig = new File(config.getString("CVSROOTCONFIGFILE"));
100
			File cvsconfig = new File(config.getString("CVSROOTCONFIGFILE"));
101
			cbean.setCvsconfig(cvsconfig);
101
			cbean.setCvsconfig(cvsconfig);
Line 102... Line 102...
102
			FindingsCacheServlet.logger.debug("Fetching list of files using '"
102
			FindingsCacheServlet.log.debug("Fetching list of files using '"
103
					+ cvsconfig.getAbsolutePath() + "' config file");
103
					+ cvsconfig.getAbsolutePath() + "' config file");
104
			
104
			
105
			
105
			
106
			List<Object> filepatterns = config.getList("FILESEARCH");
106
			List<Object> filepatterns = config.getList("FILESEARCH");
Line 107... Line 107...
107
			cbean.setFilepatterns(filepatterns);
107
			cbean.setFilepatterns(filepatterns);
Line 108... Line 108...
108
			FindingsCacheServlet.logger.debug("Checking '"
108
			FindingsCacheServlet.log.debug("Checking '"
109
					+ filepatterns.size()
109
					+ filepatterns.size()
Line 110... Line 110...
110
					+ "' patterns for file name and path matching.");
110
					+ "' patterns for file name and path matching.");
Line 111... Line 111...
111
			
111
			
112
			
112
			
113
			List<Object> contentpatterns = config.getList("CONTENTSEARCH");
113
			List<Object> contentpatterns = config.getList("CONTENTSEARCH");
114
			cbean.setContentpatterns(contentpatterns);
114
			cbean.setContentpatterns(contentpatterns);
115
			FindingsCacheServlet.logger.debug("Checking '"
115
			FindingsCacheServlet.log.debug("Checking '"
116
					+ contentpatterns.size()
116
					+ contentpatterns.size()
117
					+ "' patterns for content matching");
117
					+ "' patterns for content matching");
118
			
118
			
119
 
119
 
120
			
120
			
121
			
121
			
122
			executor.submit(new ThisRunnable(cbean));
122
			executor.submit(new ThisRunnable(cbean));
123
			
123
			
124
		}
124
		}
125
		logger.info("FindingsCacheServlet has been initialized.");
125
		log.info("FindingsCacheServlet has been initialized.");
126
 
126
 
127
	}
127
	}
128
	
128
	
129
	/*
129
	/*
130
	 * Initialise CVS findings configuration
130
	 * Initialise CVS findings configuration
Line 156... Line 156...
156
				.getInitParameterNames();
156
				.getInitParameterNames();
157
		while (attributes.hasMoreElements()) {
157
		while (attributes.hasMoreElements()) {
158
			String name = (String) attributes.nextElement();
158
			String name = (String) attributes.nextElement();
159
			if (name.startsWith("cvs-config-")) {
159
			if (name.startsWith("cvs-config-")) {
160
				String configfile = getServletContext().getInitParameter(name);
160
				String configfile = getServletContext().getInitParameter(name);
161
				logger.info("Adding CVS configuration file: " + configfile);
161
				log.info("Adding CVS configuration file: " + configfile);
162
				this.configfiles.add(new File(configfile));
162
				this.configfiles.add(new File(configfile));
163
			}
163
			}
164
		}
164
		}
165
		/*
165
		/*
166
		 * Verify, that all configuration files do exists and are readable.
166
		 * Verify, that all configuration files do exists and are readable.
167
		 */
167
		 */
168
		List<File> removelist = new ArrayList<File>();
168
		List<File> removelist = new ArrayList<File>();
169
		for (File f : configfiles) {
169
		for (File f : configfiles) {
170
			if (!f.exists()) {
170
			if (!f.exists()) {
171
				logger.warn("CVS configuration file '"
171
				log.warn("CVS configuration file '"
172
						+ f.getAbsolutePath()
172
						+ f.getAbsolutePath()
173
						+ "' is specified, but does not exist. Removing from list.");
173
						+ "' is specified, but does not exist. Removing from list.");
174
				removelist.add(f);
174
				removelist.add(f);
175
			} else if (!f.canRead()) {
175
			} else if (!f.canRead()) {
176
				logger.warn("CVS configuration file '"
176
				log.warn("CVS configuration file '"
177
						+ f.getAbsolutePath()
177
						+ f.getAbsolutePath()
178
						+ "' does exist, but is not readable. Removing from list.");
178
						+ "' does exist, but is not readable. Removing from list.");
179
				removelist.add(f);
179
				removelist.add(f);
180
			}
180
			}
181
		}
181
		}
Line 201... Line 201...
201
					.getRepositoryFiles(null, configuration.getCvsconfig(), "", true, true, true)
201
					.getRepositoryFiles(null, configuration.getCvsconfig(), "", true, true, true)
202
					.getEntity();
202
					.getEntity();
203
			ObjectFactory FACTORY = new ObjectFactory();
203
			ObjectFactory FACTORY = new ObjectFactory();
204
			FindingsListType findingsList = FACTORY.createFindingsListType();
204
			FindingsListType findingsList = FACTORY.createFindingsListType();
Line 205... Line 205...
205
 
205
 
206
			FindingsCacheServlet.logger.info("Processing '"
206
			FindingsCacheServlet.log.info("Processing '"
Line 207... Line 207...
207
					+ fileslist.getItems().size() + "' files and directories.");
207
					+ fileslist.getItems().size() + "' files and directories.");
208
 
208
 
209
			while (!this.isInterrupted) {
209
			while (!this.isInterrupted) {
Line 214... Line 214...
214
					Iterator<Object> iterF = configuration.getFilepatterns().iterator();
214
					Iterator<Object> iterF = configuration.getFilepatterns().iterator();
215
					while(iterF.hasNext()) {
215
					while(iterF.hasNext()) {
216
						Object o = iterF.next();
216
						Object o = iterF.next();
217
						if (this.isInterrupted)
217
						if (this.isInterrupted)
218
							break;
218
							break;
219
						FindingsCacheServlet.logger.debug("Scanning filename '"
219
						FindingsCacheServlet.log.debug("Scanning filename '"
220
								+ i.getFullname() + "' for pattern '"
220
								+ i.getFullname() + "' for pattern '"
221
								+ (String) o + "'");
221
								+ (String) o + "'");
222
						p = Pattern.compile((String) o);
222
						p = Pattern.compile((String) o);
223
						Matcher m = p.matcher(i.getFullname());
223
						Matcher m = p.matcher(i.getFullname());
224
						if (m.find()) {
224
						if (m.find()) {
Line 229... Line 229...
229
									.getEntity();
229
									.getEntity();
230
							finding.setContent(it.getContent());
230
							finding.setContent(it.getContent());
231
							finding.setData(it.getData());
231
							finding.setData(it.getData());
232
							finding = copyDetails(finding, i);
232
							finding = copyDetails(finding, i);
233
							findingsList.getFindings().add(finding);
233
							findingsList.getFindings().add(finding);
234
							FindingsCacheServlet.logger
234
							FindingsCacheServlet.log
235
									.debug("Match found for '"
235
									.debug("Match found for '"
236
											+ i.getFullname() + "'");
236
											+ i.getFullname() + "'");
237
							break;
237
							break;
238
						}
238
						}
239
						FindingsCacheServlet.logger
239
						FindingsCacheServlet.log
240
								.debug("No match found for '" + i.getFullname()
240
								.debug("No match found for '" + i.getFullname()
241
										+ "'");
241
										+ "'");
242
					}
242
					}
243
				}
243
				}
244
				FindingsCacheServlet.logger
244
				FindingsCacheServlet.log
245
						.debug("Processing file content for '"
245
						.debug("Processing file content for '"
246
								+ findingsList.getFindings().size()
246
								+ findingsList.getFindings().size()
247
								+ "' entries in the list.");
247
								+ "' entries in the list.");
Line 248... Line 248...
248
 
248
 
Line 255... Line 255...
255
					while (iter.hasNext()) {
255
					while (iter.hasNext()) {
Line 256... Line 256...
256
 
256
 
257
						Object o = iter.next();
257
						Object o = iter.next();
258
						if (this.isInterrupted)
258
						if (this.isInterrupted)
259
							break;
259
							break;
260
						FindingsCacheServlet.logger
260
						FindingsCacheServlet.log
261
								.debug("Scanning file content for file '"
261
								.debug("Scanning file content for file '"
262
										+ t.getFullname() + "' for pattern '"
262
										+ t.getFullname() + "' for pattern '"
Line 263... Line 263...
263
										+ (String) o + "'");
263
										+ (String) o + "'");
Line 291... Line 291...
291
							gmg.setMatchString(c);
291
							gmg.setMatchString(c);
292
							gmg.setMatchGroup(i);
292
							gmg.setMatchGroup(i);
293
							fd.getMatchLists().add(gmg);
293
							fd.getMatchLists().add(gmg);
294
						}
294
						}
295
						t.getFindingLists().add(fd);
295
						t.getFindingLists().add(fd);
296
						FindingsCacheServlet.logger
296
						FindingsCacheServlet.log
297
								.debug("Found matching content at index '" + s
297
								.debug("Found matching content at index '" + s
298
										+ "' in file '" + t.getFullname()
298
										+ "' in file '" + t.getFullname()
299
										+ "' with pattern '" + p1.toString()
299
										+ "' with pattern '" + p1.toString()
300
										+ "'");
300
										+ "'");
301
					}
301
					}
Line 302... Line 302...
302
 
302
 
303
					if (!isFound) {
303
					if (!isFound) {
304
						findingsList.getFindings().remove(t);
304
						findingsList.getFindings().remove(t);
305
						FindingsCacheServlet.logger
305
						FindingsCacheServlet.log
306
								.debug("Found matching filename for '"
306
								.debug("Found matching filename for '"
307
										+ t.getFullname()
307
										+ t.getFullname()
308
										+ "' but content didn't match. Removing.");
308
										+ "' but content didn't match. Removing.");
Line 309... Line 309...
309
					}
309
					}
310
 
310
 
311
					try {
311
					try {
312
						instance.getCacheInstance().put(
312
						instance.getCacheInstance().put(
313
								"FINDINGS-" + t.getROOT(), findingsList);
313
								"FINDINGS-" + t.getROOT(), findingsList);
314
						FindingsCacheServlet.logger
314
						FindingsCacheServlet.log
315
								.info("FINDINGS for CVSROOT '" + t.getROOT()
315
								.info("FINDINGS for CVSROOT '" + t.getROOT()
316
										+ "' have been updated in cache.");
316
										+ "' have been updated in cache.");
317
					} catch (CacheException e) {
317
					} catch (CacheException e) {
318
						FindingsCacheServlet.logger.error(e.getMessage(), e);
318
						FindingsCacheServlet.log.error(e.getMessage(), e);
319
					}
319
					}
320
				}
320
				}
321
				try {
321
				try {
322
					int cacheinterval = configuration.getCacheinterval();
322
					int cacheinterval = configuration.getCacheinterval();
323
					FindingsCacheServlet.logger.debug("Now sleeping for '"
323
					FindingsCacheServlet.log.debug("Now sleeping for '"
324
							+ cacheinterval + "' minutes");
324
							+ cacheinterval + "' minutes");
325
					Thread.currentThread();
325
					Thread.currentThread();
326
					Thread.sleep(cacheinterval * 60000);
326
					Thread.sleep(cacheinterval * 60000);
327
					FindingsCacheServlet.logger.debug("Waking up after '"
327
					FindingsCacheServlet.log.debug("Waking up after '"
328
							+ cacheinterval + "' minutes of sleep");
328
							+ cacheinterval + "' minutes of sleep");
329
				} catch (InterruptedException e) {
329
				} catch (InterruptedException e) {
330
					this.isInterrupted = true;
330
					this.isInterrupted = true;
331
					FindingsCacheServlet.logger
331
					FindingsCacheServlet.log
332
							.warn("FindingsCacheServlet cache was interrupted. Shutting down.");
332
							.warn("FindingsCacheServlet cache was interrupted. Shutting down.");
Line 333... Line 333...
333
				}
333
				}
Line 359... Line 359...
359
	public void destroy() {
359
	public void destroy() {
360
		// TODO Auto-generated method stub
360
		// TODO Auto-generated method stub
361
		executor.shutdown();
361
		executor.shutdown();
362
		try {
362
		try {
363
			executor.awaitTermination(3, TimeUnit.SECONDS);
363
			executor.awaitTermination(3, TimeUnit.SECONDS);
364
			logger.info("Cache Worker Threads have shut down.");
364
			log.info("Cache Worker Threads have shut down.");
365
		} catch (InterruptedException e) {
365
		} catch (InterruptedException e) {
366
			logger.error("Cache Worker Threads did not terminate within timeout.", e);
366
			log.error("Cache Worker Threads did not terminate within timeout.", e);
367
		}
367
		}
368
		super.destroy();
368
		super.destroy();
369
	}
369
	}
Line 370... Line 370...
370
	
370
	
371
}
371
}