Subversion Repositories XServices

Rev

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

/*
 *   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;
                                        }
                                }

                        }
                });

        }

}