001package org.kuali.ole.sys.context;
002
003import org.kuali.ole.OleLocationsXMLPollerServiceImpl;
004import org.kuali.ole.OleXmlPollerService;
005import org.kuali.rice.core.api.config.property.ConfigContext;
006import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
007import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
008
009import java.util.ArrayList;
010import java.util.Iterator;
011import java.util.List;
012import java.util.concurrent.*;
013
014/**
015 * Created by pvsubrah on 12/6/13.
016 */
017public class OlePollingStandaloneLifeCycle extends BaseLifecycle {
018    protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePollingStandaloneLifeCycle.class);
019
020    private ScheduledExecutorService scheduledExecutor;
021    private List<OleXmlPollerService> xmlPollingServices;
022    private List<ScheduledFuture> futures = new ArrayList<ScheduledFuture>();
023
024    public void start() throws Exception {
025        LOG.info("Configuring XML ingestion pipeline...");
026        scheduledExecutor = Executors.newScheduledThreadPool(2);
027        if (!ConfigContext.getCurrentContextConfig().getDevMode()) {
028            for (Iterator<OleXmlPollerService> iterator = xmlPollingServices.iterator(); iterator.hasNext(); ) {
029                OleXmlPollerService oleXmlPollerService = iterator.next();
030                LOG.info("Starting XML data loader for " + oleXmlPollerService.getClass().getName()  +"Polling at " + oleXmlPollerService.getPollIntervalSecs() + "-second intervals");
031                futures.add(scheduledExecutor.scheduleWithFixedDelay(oleXmlPollerService, oleXmlPollerService.getInitialDelaySecs(), oleXmlPollerService.getPollIntervalSecs(), TimeUnit.SECONDS));
032            }
033            super.start();
034        }
035    }
036
037    public void stop() throws Exception {
038        if (isStarted()) {
039            LOG.warn("Shutting down XML file polling timer");
040            try {
041                if (futures != null) {
042                    for (Iterator<ScheduledFuture> iterator = futures.iterator(); iterator.hasNext(); ) {
043                        ScheduledFuture future = iterator.next();
044                        if (!future.cancel(false)) {
045                            LOG.warn("Failed to cancel the XML Poller service.");
046                        }
047                        future = null;
048                    }
049                }
050                if (scheduledExecutor != null) {
051                    scheduledExecutor.shutdownNow();
052                    scheduledExecutor = null;
053                }
054            } finally {
055                super.stop();
056            }
057        }
058    }
059
060    public void setXmlPollingServices(List xmlPollingServices) {
061        this.xmlPollingServices = xmlPollingServices;
062    }
063
064    public List getXmlPollingServices() {
065        return xmlPollingServices;
066    }
067}