All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wadpam.open.io.Scheduler Maven / Gradle / Ivy

/*
 * INSERT COPYRIGHT HERE
 */

package com.wadpam.open.io;

import java.io.OutputStream;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author sosandstrom
 */
public class Scheduler {
    
    protected static final Logger LOG = LoggerFactory.getLogger(Scheduler.class);
    
    static final String KEY_PRE_EXPORT = "Exporter.Scheduler.preExport";
    static final String KEY_PRE_DAO = "Exporter.Scheduler.preDao";
    static final Integer STATE_PENDING = 0;
    static final Integer STATE_RUNNING = 1;
    static final Integer STATE_DONE = 2;
    
    protected final HashMap CACHE = new HashMap();

    protected static Exporter exporter = null;
    
    /**
     * Override to do processing in different thread.
     * This implementation simply calls {@link Exporter#exportDao(java.lang.Object, int, int, int) }.
     * @param preExport
     * @param daoIndex
     * @param offset
     * @param limit 
     */
    public void scheduleExportDao(OutputStream out, int daoIndex, int offset, int limit) {
        LOG.debug("scheduling for dao #{}, {}/{} on {}", new Object[] {
            daoIndex, offset, limit, out
        });
        exporter.exportDao(out, daoIndex, offset, limit);
    }
    
    /**
     * Override to do postExport in different thread.
     */
    protected void schedulePostExport(OutputStream out, Object arg) {
        LOG.debug("scheduling for postExport on {}", out);
        Object preExport = getCached(KEY_PRE_EXPORT);
        exporter.postExport(out, arg, preExport);
    }

    public Object getCached(Object key) {
        return CACHE.get(key);
    }

    public void putCached(Object key, Object value) {
        CACHE.put(key, value);
    }

    public void removeCached(Object key) {
        CACHE.remove(key);
    }

    public static String getDaoKey(int daoIndex) {
        return String.format("Exporter.Scheduler.daoKey.%d", daoIndex);        
    }

    /**
     * @param daoIndex
     * @return 201 Created when all done, 204 No Content 
     */
    public int onDone(OutputStream out, Object arg, int daoIndex) {
        putCached(getDaoKey(daoIndex), STATE_DONE);
        
        int i = 0;
        Object state = null;
        String cacheKey;
        
        // are all Daos exported?
        do {
            cacheKey = getDaoKey(i);
            state = getCached(cacheKey);
            LOG.debug("onDone({}) #{} has state {}", new Object[] 
                {daoIndex, i, state});
            i++;
        } while (STATE_DONE.equals(state));
        
        // done?
        if (null == state) {
            schedulePostExport(out, arg);
        }
        
        // Created when all done, No Content 
        return null == state ? 201 : 204;
    }
    
    public void preExport(Object arg) {
        
    }

    public void setExporter(Exporter exporter) {
        Scheduler.exporter = exporter;
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy