001package org.kuali.ole.docstore.process;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006/**
007 * Created by IntelliJ IDEA.
008 * User: pvsubrah
009 * Date: 1/11/12
010 * Time: 6:40 AM
011 * To change this template use File | Settings | File Templates.
012 */
013public class BulkLoadHandler {
014    private static Logger logger = LoggerFactory.getLogger(BulkLoadHandler.class);
015
016    private BulkIngestNIndexRouteBuilder bulkRoute = null;
017    private static BulkLoadHandler loadHandler = new BulkLoadHandler();
018
019    private BulkLoadHandler() {
020    }
021
022    public static BulkLoadHandler getInstance() {
023        return loadHandler;
024    }
025
026    public synchronized void loadBulk(String folder, String user, String action) throws Exception {
027        if (bulkRoute == null) {
028            bulkRoute = new BulkIngestNIndexRouteBuilder(folder, user, action,
029                    DocStoreCamelContext.getInstance().getCamelContext());
030            bulkRoute.setErrorHandlerBuilder(DocStoreCamelContext.getInstance().getErrorHandler());
031            DocStoreCamelContext.getInstance().getCamelContext().addRoutes(bulkRoute);
032        } else {
033            logger.warn("Bulk Ingest Process is Already Running @ " + folder);
034            throw new Exception("Bulk Ingest Process is Already Running @ " + folder);
035        }
036    }
037
038    public synchronized void start() {
039        try {
040            bulkRoute.getFileEndPoint().start();
041        } catch (Exception e) {
042            logger.error("Unable to Stop Bulk Ingest Process : ", e);
043        }
044    }
045
046    public synchronized void stop() {
047        try {
048            if (bulkRoute != null && bulkRoute.getFileEndPoint() != null) {
049                bulkRoute.getFileEndPoint().stop();
050            }
051        } catch (Exception e) {
052            logger.error("Unable to Stop Bulk Ingest Process : ", e);
053        }
054    }
055
056    public synchronized void remove() {
057        try {
058            bulkRoute.getFileEndPoint().shutdown();
059            bulkRoute = null;
060        } catch (Exception e) {
061            logger.error("Unable to Stop Bulk Ingest Process : ", e);
062        }
063    }
064
065    public String getStatus() {
066        if (bulkRoute != null) {
067            return bulkRoute.getFileEndPoint().getStatus().name();
068        } else {
069            return null;
070        }
071    }
072
073    public BulkIngestNIndexRouteBuilder getBulkRoute() {
074        return bulkRoute;
075    }
076
077    public void setBulkRoute(BulkIngestNIndexRouteBuilder bulkRoute) {
078        this.bulkRoute = bulkRoute;
079    }
080}