package com.waldura.servlet;
import java.io.File;
/**
* This class does the asynchronous generation of a report.
* It is used by {@link HandleGenReportRequest}
* to generate the report asynchronously.
*
* @see HandleGenReportRequest
* @author Renaud Waldura, 7/21/2001
*/
public class ReportGeneration
implements Runnable
{
/**
* The name of the thread generating the report.
*/
public static final String REPORT_GENERATION_THREAD_NAME = "ReportGenerator-";
// used only for the thread name
private static int n = 0;
private static synchronized int threadNumber() { return ++n; }
private Thread t;
private Exception e;
private GenReport reportEngine;
/**
* Constructor.
*/
public ReportGeneration(GenReport reportEngine)
{
this.reportEngine = reportEngine;
t = new Thread(this, REPORT_GENERATION_THREAD_NAME + threadNumber());
}
/*
* The file this report is written to.
*/
private String reportFile;
public void setReportFile(String s) { reportFile = s; }
public String getReportFile() { return reportFile; }
/*
* The URL mapping to that file above.
*/
private String reportURL;
public void setReportURL(String s) { reportURL = s; }
public String getReportURL() { return reportURL; }
/**
* Returns the error that may have occurred while generating
* the report.
*/
public Exception getException()
{
return e;
}
/**
* Start report generation.
*/
public void start()
{
t.start();
}
/**
* Wait for report generation to complete.
* This method times out and returns if report generation hasn't completed
* in the imparted delay.
* {@link #isFinished()} then returns false,
* indicating that this method timed-out -- the report isn't ready yet.
*
* @param delay how long to wait for the thread to complete,
* in seconds.
*/
public void waitForCompletion(int delay)
throws InterruptedException
{
t.join(delay * 1001);
}
/**
* Returns whether {@link #waitForCompletion(int)}
* timed-out or completed normally -- false when it timed-out,
* indicating that the report generation thread is still running.
*/
public boolean isFinished()
{
return !t.isAlive();
}
/**
* Generates the report by calling
* {@link GenReport#execute(File)}. Any errors
* are caught and saved; use
* {@link #getException()} to retrieve the error.
*/
public void run()
{
try
{
reportEngine.execute( new File(reportFile) );
/*
* THIS FOR TESTING ONLY: make the report generation take a long time.
*
try
{
Thread.sleep(3 * 60 * 1000);
}
catch (InterruptedException ie) {}
*/
}
catch (Exception e)
{
this.e = e;
}
}
}