/xservices/trunk/src/java/net/brutex/xservices/util/cache/CacheExecutorService.java |
---|
0,0 → 1,56 |
/* |
* Copyright 2012 Brian Rosenberger (Brutex Network) |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); |
* you may not use this file except in compliance with the License. |
* You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software |
* distributed under the License is distributed on an "AS IS" BASIS, |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* See the License for the specific language governing permissions and |
* limitations under the License. |
*/ |
package net.brutex.xservices.util.cache; |
import java.util.concurrent.ExecutorService; |
import java.util.concurrent.Executors; |
import java.util.concurrent.ThreadFactory; |
import javax.servlet.ServletContext; |
import javax.servlet.ServletContextEvent; |
import javax.servlet.ServletContextListener; |
/** |
* @author Brian Rosenberger, bru(at)brutex.de |
* @since 0.5.0-20120825 |
*/ |
public class CacheExecutorService implements ServletContextListener { |
static final String EXECUTOR_NAME = "CACHE_EXECUTOR"; |
private ExecutorService executor; |
public void contextInitialized(ServletContextEvent arg0) { |
ServletContext context = arg0.getServletContext(); |
int nr_executors = 1; |
ThreadFactory daemonFactory = new DaemonThreadFactory(); |
try { |
nr_executors = Integer.parseInt(context.getInitParameter("cache:thread-count")); |
} catch (NumberFormatException ignore ) {} |
if(nr_executors <= 1) { |
executor = Executors.newSingleThreadExecutor(daemonFactory); |
} else { |
executor = Executors.newFixedThreadPool(nr_executors,daemonFactory); |
} |
context.setAttribute(EXECUTOR_NAME, executor); |
} |
public void contextDestroyed(ServletContextEvent arg0) { |
ServletContext context = arg0.getServletContext(); |
executor.shutdownNow(); // or process/wait until all pending jobs are done |
} |
} |
Property changes: |
Added: svn:mime-type |
+text/plain |
\ No newline at end of property |
/xservices/trunk/src/java/net/brutex/xservices/util/cache/DaemonThreadFactory.java |
---|
0,0 → 1,52 |
/* |
* Copyright 2012 Brian Rosenberger (Brutex Network) |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); |
* you may not use this file except in compliance with the License. |
* You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software |
* distributed under the License is distributed on an "AS IS" BASIS, |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* See the License for the specific language governing permissions and |
* limitations under the License. |
*/ |
package net.brutex.xservices.util.cache; |
import java.util.concurrent.Executors; |
import java.util.concurrent.ThreadFactory; |
public class DaemonThreadFactory implements ThreadFactory { |
private final ThreadFactory factory; |
/** |
* Construct a ThreadFactory with setDeamon(true) using |
* Executors.defaultThreadFactory() |
*/ |
public DaemonThreadFactory() { |
this(Executors.defaultThreadFactory()); |
} |
/** |
* Construct a ThreadFactory with setDeamon(true) wrapping the given factory |
* |
* @param thread |
* factory to wrap |
*/ |
public DaemonThreadFactory(ThreadFactory factory) { |
if (factory == null) |
throw new NullPointerException("factory cannot be null"); |
this.factory = factory; |
} |
public Thread newThread(Runnable r) { |
final Thread t = factory.newThread(r); |
t.setDaemon(true); |
return t; |
} |
} |
Property changes: |
Added: svn:mime-type |
+text/plain |
\ No newline at end of property |
/xservices/trunk/src/java/net/brutex/xservices/util/cache/CacheServlet.java |
---|
0,0 → 1,122 |
/* |
* Copyright 2012 Brian Rosenberger (Brutex Network) |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); |
* you may not use this file except in compliance with the License. |
* You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software |
* distributed under the License is distributed on an "AS IS" BASIS, |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* See the License for the specific language governing permissions and |
* limitations under the License. |
*/ |
package net.brutex.xservices.util.cache; |
import java.io.File; |
import java.util.ArrayList; |
import java.util.Enumeration; |
import java.util.List; |
import java.util.concurrent.ExecutorService; |
import javax.servlet.ServletException; |
import javax.servlet.http.HttpServlet; |
import javax.ws.rs.core.GenericEntity; |
import javax.ws.rs.core.Response; |
import org.apache.log4j.Logger; |
import net.brutex.xservices.types.scm.ModuleType; |
import net.brutex.xservices.ws.rs.CVSInfoImpl; |
/** |
* Perform Caching actions on the CVS Info actions |
* |
* @author Brian Rosenberger, bru(at)brutex.de |
* @since 0.5.0-200120825 |
* |
*/ |
public class CacheServlet extends HttpServlet { |
private final Logger logger = Logger.getLogger(CacheServlet.class); |
List<File> configfiles = new ArrayList<File>();; |
int cacheinterval; |
/* |
* (non-Javadoc) |
* |
* @see javax.servlet.GenericServlet#init() |
*/ |
@Override |
public void init() throws ServletException { |
super.init(); |
ExecutorService executor = (ExecutorService) getServletContext() |
.getAttribute(CacheExecutorService.EXECUTOR_NAME); |
Enumeration<String> attributes = getServletContext() |
.getInitParameterNames(); |
while (attributes.hasMoreElements()) { |
String name = attributes.nextElement(); |
if (name.startsWith("cvs-config-")) { |
String configfile = (String) getServletContext() |
.getInitParameter(name); |
logger.info("CVS configuration file: " + configfile); |
this.configfiles.add(new File(configfile)); |
} |
} |
cacheinterval = 15; |
try { |
cacheinterval = Integer.parseInt((String) getServletContext() |
.getInitParameter("cvs-cache-interval")); |
} catch (NumberFormatException e) { |
logger.debug("Could not read parameter 'cvs-cache-interval' from web.xml. Using default value '"+cacheinterval+"' minutes"); |
} |
logger.info("CacheServlet set to " + cacheinterval + " minutes interval."); |
executor.submit(new Runnable() { |
boolean isInterrupted = false; |
@Override |
public void run() { |
while (!isInterrupted) { |
for (File configfile : configfiles) { |
CVSInfoImpl instance = new CVSInfoImpl(); |
logger.info("Caching modules from " + configfile.toURI().toString()); |
Response response = instance.getModules(null, |
configfile, true); |
List<ModuleType> list = (List<ModuleType>) ((GenericEntity) response |
.getEntity()).getEntity(); |
if (list.size() == 0) |
list.add(new ModuleType("", "", "", "")); |
for (ModuleType t : list) { |
try { |
//Extra sleep |
Thread.currentThread().sleep(5000); |
} catch (InterruptedException e) { |
isInterrupted = true; |
break; |
} |
logger.info("Caching module '" + t.getName()+"'"); |
instance.getRepositoryFiles(null, configfile, t.getName(), true, true); |
} |
} |
try { |
logger.debug("Now sleeping for '"+cacheinterval+"' minutes"); |
Thread.currentThread().sleep(cacheinterval * 60000); |
logger.debug("Waking up after '"+cacheinterval+"' minutes of sleep"); |
} catch (InterruptedException e) { |
isInterrupted = true; |
break; |
} |
} |
} |
}); |
} |
} |
Property changes: |
Added: svn:mime-type |
+text/plain |
\ No newline at end of property |