package org.sf.mustru.ui;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.sf.mustru.utils.Constants;
import org.sf.mustru.utils.IndexTools;
/**
* Thread to run the summary task from IndexTools
*/
public class RunSummary implements IRunnableWithProgress
{
private int TIMELIMIT = 30000; //*-- time limit in seconds to perform the repair operation
private String tmpFile = Constants.MUSTRU_HOME + File.separator + "tmp" + File.separator + "summary.txt";
private boolean jobRan = true;
private int[] timeIncrement = { 10, 5, 4, 3, 2};
public RunSummary() { super(); }
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
{
monitor.beginTask("Summarizing the Index", 100);
IndexTools itools = new IndexTools(tmpFile);
itools.setRunRepair(false); itools.setRunSummary(true);
itools.start();
boolean done = false; long scanStart = new Date().getTime(); int times = 0;
while (!done)
{ done = true;
//*-- wait a second
try { Thread.sleep(1000); } catch (InterruptedException e) { }
long end = new Date().getTime();
//*-- check if the thread is taking too much time or was manually cancelled
if (itools.isRunning()) done = false;
long secs = end - scanStart; secs /= 1000;
if ( ( (secs > TIMELIMIT) && itools.isAlive() ) ||
(monitor.isCanceled()) )
{ itools.setRunning(false); done = true; monitor.done(); setJobRan(false); }
monitor.worked( (times < 5) ? timeIncrement[times]: 1 );
times++;
} //*-- end of while
monitor.done();
}
public boolean isJobRan()
{ return jobRan; }
public void setJobRan(boolean jobRan)
{ this.jobRan = jobRan; }
}