Search in sources :

Example 1 with CleanupCallback

use of au.com.vaadinutils.jasper.ui.CleanupCallback in project VaadinUtils by rlsutton1.

the class JasperManager method run.

@Override
public void run() {
    JRSwapFileVirtualizer fileVirtualizer = null;
    CleanupCallback cleanupCallback = null;
    boolean initialized = false;
    try {
        logger.warn("{} permits are available", concurrentLimit.availablePermits());
        concurrentLimit.acquire();
        initialized = true;
        inQueue = false;
        queueEntry.setStatus("Gathering report data phase 1");
        reportProperties.initDBConnection();
        cleanupCallback = reportProperties.getCleanupCallback();
        List<ReportParameter<?>> extraParams = reportProperties.prepareData(params, reportProperties.getReportFileName(), cleanupCallback);
        compileReport();
        if (reportProperties.getCustomReportParameterMap() != null) {
            boundParams.putAll(reportProperties.getCustomReportParameterMap());
        }
        if (extraParams != null) {
            params.removeAll(extraParams);
            params.addAll(extraParams);
        }
        logger.info("Running report " + reportProperties.getReportFileName());
        for (ReportParameter<?> param : params) {
            for (String parameterName : param.getParameterNames()) {
                bindParameter(param, parameterName);
                if (param.displayInreport()) {
                    // populate dynamically added parameters to display user
                    // friendly parameters on the report
                    boundParams.put("ParamDisplay-" + parameterName, param.getDisplayValue(parameterName));
                }
                logger.info(parameterName + " " + param.getValue(parameterName));
            }
        }
        reportProperties.prepareForOutputFormat(exportMethod);
        queueEntry.setStatus("Gathering report data phase 2");
        // use file virtualizer to prevent out of heap
        String fileName = "/tmp";
        JRSwapFile file = new JRSwapFile(fileName, 100, 10);
        fileVirtualizer = new JRSwapFileVirtualizer(500, file);
        boundParams.put(JRParameter.REPORT_VIRTUALIZER, fileVirtualizer);
        if (stop) {
            return;
        }
        if (exportMethod == OutputFormat.CSV) {
            boundParams.put(JRParameter.IS_IGNORE_PAGINATION, true);
        }
        JasperPrint jasper_print = fillReport(exportMethod);
        queueEntry.setStatus("Waiting for browser to start streaming");
        if (stop) {
            return;
        }
        logger.warn("Output stream is ready");
        progressListener.outputStreamReady();
        logger.warn("Waiting for reader stream");
        if (readerReady.await(10, TimeUnit.SECONDS)) {
            outputStream = new PipedOutputStream(inputStream);
            writerReady.countDown();
            exportMethod.export(jasper_print, outputStream, getPageMonitorConfig(exportMethod.getConfig()), images);
            imagesrcs = (images.size() <= 0) ? null : new DataSource[images.size()];
            if (imagesrcs != null) {
                int xi = 0;
                for (Map.Entry<String, byte[]> entry : images.entrySet()) {
                    ByteArrayDataSource image = new ByteArrayDataSource(entry.getValue(), "image/gif");
                    image.setName(entry.getKey());
                    imagesrcs[xi++] = image;
                }
            }
        } else {
            logger.error("Couldn't attach to reader stream");
        }
        Thread.sleep(750);
        queueEntry.setStatus("Cleaning up");
    } catch (Exception e) {
        logger.error(e, e);
    } finally {
        if (queueEntry != null) {
            jobQueue.remove(queueEntry);
            queueEntry = null;
        }
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            logger.error(e, e);
        }
        if (cleanupCallback != null) {
            try {
                cleanupCallback.cleanup();
            } catch (Exception e) {
                logger.error(e, e);
            }
        }
        if (fileVirtualizer != null) {
            try {
                fileVirtualizer.cleanup();
            } catch (Exception e) {
                logger.error(e, e);
            }
        }
        if (initialized) {
            concurrentLimit.release();
            try {
                reportProperties.closeDBConnection();
            } catch (Exception e) {
                logger.error(e, e);
            }
        }
        completeBarrier.countDown();
        if (progressListener != null) {
            progressListener.completed();
        }
    }
}
Also used : ReportParameter(au.com.vaadinutils.jasper.parameter.ReportParameter) JRSwapFileVirtualizer(net.sf.jasperreports.engine.fill.JRSwapFileVirtualizer) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRSwapFile(net.sf.jasperreports.engine.util.JRSwapFile) PipedOutputStream(java.io.PipedOutputStream) IOException(java.io.IOException) JasperPrint(net.sf.jasperreports.engine.JasperPrint) FileNotFoundException(java.io.FileNotFoundException) JRException(net.sf.jasperreports.engine.JRException) IOException(java.io.IOException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) CleanupCallback(au.com.vaadinutils.jasper.ui.CleanupCallback) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Aggregations

ReportParameter (au.com.vaadinutils.jasper.parameter.ReportParameter)1 CleanupCallback (au.com.vaadinutils.jasper.ui.CleanupCallback)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PipedOutputStream (java.io.PipedOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DataSource (javax.activation.DataSource)1 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)1 JRException (net.sf.jasperreports.engine.JRException)1 JasperPrint (net.sf.jasperreports.engine.JasperPrint)1 JRSwapFileVirtualizer (net.sf.jasperreports.engine.fill.JRSwapFileVirtualizer)1 JRSwapFile (net.sf.jasperreports.engine.util.JRSwapFile)1