Bug 428173 - Output file not closed with separate RunTask and RenderTask
Summary: Output file not closed with separate RunTask and RenderTask
Status: VERIFIED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: BIRT (show other bugs)
Version: 4.3.0   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: 4.4.1   Edit
Assignee: ShiHeng Guan CLA
QA Contact: Wei Yan CLA
URL:
Whiteboard:
Keywords: greatbug, plan
Depends on:
Blocks:
 
Reported: 2014-02-14 04:48 EST by Henning von Bargen CLA
Modified: 2014-09-16 20:45 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Henning von Bargen CLA 2014-02-14 04:48:10 EST
See also http://www.eclipse.org/forums/index.php/t/459265/

With the following code, I cannot rename the PDF file after the RenderTask:

	IReportDocument rptdoc = engine.openReportDocument(destName + ".rptdocument");
	renderTask = engine.createRenderTask(rptdoc);
	try {
		IRenderOption options = new RenderOption();
		options.setOutputFileName(destName);
		options.setOutputFormat(destFormatMapped);
		PDFRenderOption pdfOptions = new PDFRenderOption(options);
		pdfOptions.setOption(IPDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.OUTPUT_TO_MULTIPLE_PAGES);
		pdfOptions.setOption(IPDFRenderOption.PDF_TEXT_WRAPPING, true);
		pdfOptions.setOption(IPDFRenderOption.PDF_HYPHENATION, true);
		options.setOption(IRenderOption.CLOSE_OUTPUTSTREAM_ON_EXIT, true);
		renderTask.setRenderOption(options);
		renderTask.render();
	} catch (EngineException ex) {
		throw new CancelWithMessage("BIRT RenderTask failed", ex);
	}
	finally {
		try {
			for (Object ex: renderTask.getErrors()) {
				log.error("BIRT RenderTask error", (Exception)ex);
			}
			renderTask.close();
			renderTask = null;
			rptdoc.close();
		} catch (Exception e) {
			log.error("Error closing BIRT RenderTask", e);
		}
	}

	// Next, try to rename the file.
	...

Looking at the BIRT source code, it looks like when you use the IRenderOption.setOutputFileName API, the resulting PDF file is NOT closed by birt when the RenderTask is finished, even if you explicitly set the CLOSE_OUTPUTSTREAM_ON_EXIT option; this option is only considered when passing an OutputStream instead of a filename.

The funny thing is:

I am not able to reproduce the error with RunAndRenderTask.
I am not able to reproduce the error with BIRT 4.2.1
I am able to reproduce the error with BIRT 4.3.0 and separate RunTask/RenderTask.

Please also note that the iText API example code (in chapter 1) expliticly close the FileOutputStream after calling iText (fos.close()).
I was not able to find something similar in the BIRT source code.
Comment 1 Henning von Bargen CLA 2014-02-14 04:59:00 EST
As a workaround, one can use the following code:

	FileOutputStream fos = null;
	try {
		IRenderOption options = new RenderOption();
		fos = new FileOutputStream(destName);
		final int BUFSIZE = 32768;
		options.setOutputStream(new BufferedOutputStream(fos, BUFSIZE));
		options.setOutputFormat("PDF");
		...
	}
	finally {
		try {
			for (Object ex: renderTask.getErrors()) {
				log.error("BIRT RenderTask error", (Exception)ex);
			}
			renderTask.close();
			renderTask = null;
			log.info("renderTask closed.");
			rptdoc.close();
			log.info("rptdoc closed.");
			fos.close();
			log.info("fos closed");
		} catch (Exception e) {
			log.error("Error closing BIRT RenderTask", e);
		}
	}

That is, use a FileOutputStream instead and take care of closing it.
Comment 2 ShiHeng Guan CLA 2014-09-05 19:04:21 EDT
the issue is not on RenderTask.  The outputstream is not close correctly on the PDFRender.  Run the same code using html and it works perfectly.  The work around still fails some times.
Comment 3 ShiHeng Guan CLA 2014-09-08 14:03:25 EDT
there were more than one outputstream so it only close one, fix on Luna

commit a86abe749fbbac32a7cabc56c081d2347c2110a1

Signed-off-by: sguan-actuate <sguan@actuate.com>
Comment 4 Wei Yan CLA 2014-09-16 20:45:39 EDT
v