2 * BioJava development code
4 * This code may be freely distributed and modified under the
5 * terms of the GNU Lesser General Public Licence. This should
6 * be distributed with the code. If you do not have a copy,
9 * http://www.gnu.org/copyleft/lesser.html
11 * Copyright for this code is held jointly by the individual
12 * authors. These should be listed in @author doc comments.
14 * For more information on the BioJava project and its aims,
15 * or to join the biojava-l mailing list, visit the home page
18 * http://www.biojava.org/
20 * Created on 21.09.2004
21 * @author Andreas Prlic
25 package org.biojava.dasobert.das;
29 import java.util.logging.*;
31 import org.biojava.dasobert.dasregistry.*;
32 import org.biojava.dasobert.eventmodel.*;
35 * a thread that connects to a DAS - Feature service and gets the features
37 * @author Andreas Prlic
40 public class FeatureThread implements Runnable
44 * number of times the client tries to reconnect to the server if a "come back
45 * later" is returned. the server should provide a reasonable estimation how
46 * long it will take him to create results. if this number of requests is
47 * still not successfull, give up.
49 public static int MAX_COME_BACK_ITERATIONS = 5;
51 public static int MAX_NR_FEATURES = 300;
53 static Logger logger = Logger.getLogger("org.biojava.spice");
59 List featureListeners;
63 public FeatureThread(String accessionCode, Das1Source dasSource)
65 this.dasSource = dasSource;
66 this.ac = accessionCode;
67 featureListeners = new ArrayList();
70 public void addFeatureListener(FeatureListener li)
72 featureListeners.add(li);
75 public void clearFeatureListeners()
77 featureListeners.clear();
80 public synchronized void stop()
88 Thread me = Thread.currentThread();
91 String url = dasSource.getUrl();
92 String queryString = url + "features?segment=" + ac;
96 cmd = new URL(queryString);
97 } catch (MalformedURLException e)
99 logger.warning("got MalformedURL from das source " + dasSource);
104 logger.info("requesting features from " + cmd);
105 DAS_FeatureRetrieve ftmp = new DAS_FeatureRetrieve(cmd);
107 int comeBackLater = ftmp.getComeBackLater();
108 int securityCounter = 0;
109 while ((thread == me) && (comeBackLater > 0))
112 if (securityCounter >= MAX_COME_BACK_ITERATIONS)
118 notifyComeBackLater(comeBackLater);
119 // server is still calculating - asks us to come back later
123 } catch (InterruptedException e)
130 comeBackLater = ftmp.getComeBackLater();
138 List features = ftmp.get_features();
139 String version = ftmp.getVersion();
141 // a fallback mechanism to prevent DAS sources from bringing down spice
142 if (features.size() > MAX_NR_FEATURES)
145 .warning("DAS source returned more than " + MAX_NR_FEATURES
147 + " throwing away excess features at " + cmd);
148 features = features.subList(0, MAX_NR_FEATURES);
151 // notify FeatureListeners
152 Map[] feats = (Map[]) features.toArray(new Map[features.size()]);
153 notifyFeatureListeners(feats, version);
164 thread = new Thread(this);
168 private void notifyFeatureListeners(Map[] feats, String version)
170 logger.finest("FeatureThread found " + feats.length + " features");
171 FeatureEvent fevent = new FeatureEvent(feats, dasSource, version);
172 Iterator fiter = featureListeners.iterator();
173 while (fiter.hasNext())
175 FeatureListener fi = (FeatureListener) fiter.next();
176 fi.newFeatures(fevent);
181 * the Annotation server requested to be queried again in a while
183 * @param comeBackLater
185 private void notifyComeBackLater(int comeBackLater)
187 FeatureEvent event = new FeatureEvent(new HashMap[0], dasSource, "");
188 event.setComeBackLater(comeBackLater);
189 Iterator fiter = featureListeners.iterator();
190 while (fiter.hasNext())
192 FeatureListener fi = (FeatureListener) fiter.next();
193 fi.comeBackLater(event);