Subversion Repositories XServices

Rev

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

Rev Author Line No. Line
94 brianR 1
/*
2
 *   Copyright 2012 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.util.ArrayList;
21
import java.util.Enumeration;
22
import java.util.List;
23
import java.util.concurrent.ExecutorService;
24
 
25
import javax.servlet.ServletException;
26
import javax.servlet.http.HttpServlet;
27
import javax.ws.rs.core.GenericEntity;
28
import javax.ws.rs.core.Response;
29
 
30
import org.apache.log4j.Logger;
31
 
32
import net.brutex.xservices.types.scm.ModuleType;
33
import net.brutex.xservices.ws.rs.CVSInfoImpl;
34
 
35
/**
36
 * Perform Caching actions on the CVS Info actions
37
 *
38
 * @author Brian Rosenberger, bru(at)brutex.de
39
 * @since 0.5.0-200120825
40
 *
41
 */
42
public class CacheServlet extends HttpServlet {
43
 
44
	private final Logger logger = Logger.getLogger(CacheServlet.class);
45
	List<File> configfiles = new ArrayList<File>();;
46
	int cacheinterval;
47
 
48
	/*
49
	 * (non-Javadoc)
50
	 *
51
	 * @see javax.servlet.GenericServlet#init()
52
	 */
53
	@Override
54
	public void init() throws ServletException {
55
		super.init();
56
		ExecutorService executor = (ExecutorService) getServletContext()
57
				.getAttribute(CacheExecutorService.EXECUTOR_NAME);
58
 
59
		Enumeration<String> attributes = getServletContext()
60
				.getInitParameterNames();
61
		while (attributes.hasMoreElements()) {
62
			String name = attributes.nextElement();
63
			if (name.startsWith("cvs-config-")) {
64
				String configfile = (String) getServletContext()
65
						.getInitParameter(name);
66
				logger.info("CVS configuration file: " + configfile);
67
				this.configfiles.add(new File(configfile));
68
			}
69
		}
70
		cacheinterval = 15;
71
		try {
72
			cacheinterval = Integer.parseInt((String) getServletContext()
73
					.getInitParameter("cvs-cache-interval"));
74
		} catch (NumberFormatException e) {
75
			logger.debug("Could not read parameter 'cvs-cache-interval' from web.xml. Using default value '"+cacheinterval+"' minutes");
76
		}
77
		logger.info("CacheServlet set to " + cacheinterval + " minutes interval.");
78
 
79
		executor.submit(new Runnable() {
80
			boolean isInterrupted = false;
81
 
82
			@Override
83
			public void run() {
84
				while (!isInterrupted) {
85
					for (File configfile : configfiles) {
86
						CVSInfoImpl instance = new CVSInfoImpl();
87
						logger.info("Caching modules from "	+ configfile.toURI().toString());
88
						Response response = instance.getModules(null,
89
								configfile, true);
90
						List<ModuleType> list = (List<ModuleType>) ((GenericEntity) response
91
								.getEntity()).getEntity();
92
						if (list.size() == 0)
93
							list.add(new ModuleType("", "", "", ""));
94
						for (ModuleType t : list) {
95
							try {
96
								//Extra sleep
97
								Thread.currentThread().sleep(5000);
98
							} catch (InterruptedException e) {
99
								isInterrupted = true;
100
								break;
101
							}
102
							logger.info("Caching module '" + t.getName()+"'");
103
							instance.getRepositoryFiles(null, configfile, t.getName(), true, true);
104
 
105
						}
106
					}
107
					try {
108
						logger.debug("Now sleeping for '"+cacheinterval+"' minutes");
109
						Thread.currentThread().sleep(cacheinterval * 60000);
110
						logger.debug("Waking up after '"+cacheinterval+"' minutes of sleep");
111
					} catch (InterruptedException e) {
112
						isInterrupted = true;
113
						break;
114
					}
115
				}
116
 
117
			}
118
		});
119
 
120
	}
121
 
122
}