public void fetch(String ids, boolean isAsync)
{
- isAsync &= Platform.isJS();
if (ids == null)
{
ids = textArea.getText();
private class AsyncFetchTask extends AsyncSwingWorker
{
- private boolean addToLast = false;
+ protected boolean addToLast = false;
- private List<String> aresultq = new ArrayList<>();
+ protected List<String> aresultq = new ArrayList<>();
- private List<String> presultTitle = new ArrayList<>();
+ protected List<String> presultTitle = new ArrayList<>();
- private List<AlignmentI> presult = new ArrayList<>();
+ protected List<AlignmentI> presult = new ArrayList<>();
- private List<AlignmentI> aresult = new ArrayList<>();
+ protected List<AlignmentI> aresult = new ArrayList<>();
- private FeatureSettingsModelI preferredFeatureColours = null;
+ protected FeatureSettingsModelI preferredFeatureColours = null;
- private List<DbSourceProxy> sources;
+ protected List<DbSourceProxy> sources;
- private Iterator<DbSourceProxy> sourceIterator;
+ protected Iterator<DbSourceProxy> sourceIterator;
- private String[] fetchArray;
+ protected String[] fetchArray;
- private List<String> fetchList;
+ protected List<String> fetchList;
- private Iterator<String> fetchIterator;
+ protected Iterator<String> fetchIterator;
- private int fetchCount;
+ protected int fetchCount;
- private DbSourceProxy source;
+ protected DbSourceProxy source;
- private String ids;
+ protected String ids;
+
+ protected long myID;
+
+ protected boolean isAsync;
+
+ protected Throwable taskError;
public AsyncFetchTask(String ids, Component owner, String title,
int delayMillis,
{
super(owner, title, delayMillis, min, max);
this.ids = ids;
+ isAsync = (delayMillis != 0);
+ myID = Thread.currentThread().getId();
}
@Override
private final static int STATE_DONE = 100;
+ protected static final int STATE_TASK_ERROR = 99;
+
@Override
public int doInBackgroundAsync(int progress)
{
+ System.out.println("SequenceFetcher.AsyncFetchTask " + isAsync + " "
+ + progress + " "
+ + taskError);
switch (progress)
{
case STATE_INIT:
case STATE_FETCH_SINGLE:
if (fetchIterator.hasNext())
{
- // source only handles one accession id at a time
- try
+ return runSubtask(new Runnable()
{
- if (delayMillis == 0)
- {
- // for CrossRef2xmlTest only
- Thread.sleep(5);
- }
- String accession = fetchIterator.next();
- if (!fetchSingleAccession(source, accession, aresultq, aresult))
- {
- fetchList.add(accession);
- }
- } catch (Throwable e)
- {
- if (!showError(e))
+
+ @Override
+ public void run()
{
- return STATE_DONE;
+ // source only handles one accession id at a time
+ try
+ {
+ if (!isAsync)
+ {
+ // for CrossRef2xmlTest only; to "allow the server to breathe"
+ // (but we are doing that already when isAsync is true)
+ Thread.sleep(5);
+ }
+ String notFound = fetchSingleAccession(source,
+ fetchIterator.next(), aresultq, aresult);
+ if (notFound != null)
+ {
+ fetchList.add(notFound);
+ }
+ } catch (Throwable e)
+ {
+ taskError = e;
+ }
}
- }
- return STATE_FETCH_SINGLE;
+ });
}
return STATE_PROCESS;
case STATE_FETCH_MULTIPLE:
// proxy can fetch multiple accessions at one time
- try
+ setProgressAsync(STATE_PROCESS);
+ return runSubtask(new Runnable()
{
- fetchMultipleAccessions(source, fetchIterator, aresultq, aresult,
- fetchList);
- } catch (Throwable e)
- {
- if (!showError(e))
+
+ @Override
+ public void run()
{
- return STATE_DONE;
+ try
+ {
+ fetchMultipleAccessions(source, fetchIterator, aresultq,
+ aresult, fetchList);
+ } catch (Throwable e)
+ {
+ taskError = e;
+ }
}
- }
- return STATE_PROCESS;
+
+ });
case STATE_PROCESS:
// Stack results ready for opening in alignment windows
if (aresult != null && aresult.size() > 0)
preferredFeatureColours);
}
break;
+ case STATE_TASK_ERROR:
+ showProgress(null);
+ super.cancelAsync();
+ return 0; // arbitrary; ignored
}
return STATE_DONE;
}
- private void showProgress(String msg)
+ protected void resume()
{
- guiWindow.setProgressBar(msg, Thread.currentThread().hashCode());
+ if (!isAsync)
+ {
+ return;
+ }
+ super.setPaused(false);
+ super.stateLoop();
+ }
+
+ private int runSubtask(Runnable subtask)
+ {
+
+ Runnable r = new Runnable()
+ {
+
+ @Override
+ public void run()
+ {
+ try
+ {
+ subtask.run();
+ } catch (Throwable e)
+ {
+ taskError = e;
+ }
+ if (checkError(taskError))
+ {
+ setProgressAsync(STATE_TASK_ERROR);
+ }
+ else
+ {
+ taskError = null;
+ }
+ if (isAsync)
+ {
+ resume();
+ }
+ }
+ };
+
+ if (isAsync)
+ {
+ super.setPaused(true);
+ new Thread(r).start();
+ return 0; // arbitrary return -- ignored because we are paused.
+ }
+ r.run();
+ return super.getProgressAsync();
}
@Override
resetDialog();
}
- private boolean showError(Throwable e)
+ /**
+ *
+ * @param e
+ * @return true if there was an error and we need to STOP
+ */
+ protected boolean checkError(Throwable e)
{
+ if (e == null)
+ {
+ return false;
+ }
String problem = "retrieving " + ids + " from "
+ database.getSelectedItem();
if (e instanceof Exception)
return true;
}
+ private void showProgress(String msg)
+ {
+ guiWindow.setProgressBar(msg, myID);
+ }
+
}
/**
* a list of successful queries to add to
* @param aresult
* a list of retrieved alignments to add to
- * @return true if the fetch was successful, else false
+ * @return null if the fetch was successful; the accession if not
*/
- boolean fetchSingleAccession(DbSourceProxy proxy, String accession,
- List<String> aresultq, List<AlignmentI> aresult)
+ String fetchSingleAccession(DbSourceProxy proxy, String accession,
+ List<String> aresultq, List<AlignmentI> aresult) throws Throwable
{
- boolean success = false;
- try
- {
- // BH no longer necessary; we are doing 5-ms asynchronous delays all along
- // if (aresult != null)
- // {
- // try
- // {
- // // give the server a chance to breathe
- // Thread.sleep(5);
- // } catch (Exception e)
- // {
- // //
- // }
- // }
- //
- AlignmentI indres = null;
- try
- {
- indres = proxy.getSequenceRecords(accession);
- } catch (OutOfMemoryError oome)
- {
- new OOMWarning(
- "fetching " + accession + " from " + proxy.getDbName(),
- oome, this);
- }
- if (indres != null)
- {
- aresultq.add(accession);
- aresult.add(indres);
- success = true;
- }
- } catch (Exception e)
+ AlignmentI indres = proxy.getSequenceRecords(accession);
+ if (indres == null)
{
- Cache.log.info("Error retrieving " + accession + " from "
- + proxy.getDbName(), e);
+ return accession;
}
- return success;
+ aresultq.add(accession);
+ aresult.add(indres);
+ return null;
}
/**