cz/vutbr/fit/dudka/SGVis/Visual/LookupWorker.java

Go to the documentation of this file.
00001 package cz.vutbr.fit.dudka.SGVis.Visual;
00002 
00003 import java.net.URL;
00004 import java.util.HashSet;
00005 
00006 import javax.swing.SwingWorker;
00007 
00008 import cz.vutbr.fit.dudka.SGVis.Config;
00009 import cz.vutbr.fit.dudka.SGVis.Data.RelationStorage;
00010 import cz.vutbr.fit.dudka.SGVis.Lookup.Lookup;
00011 import cz.vutbr.fit.dudka.SGVis.Lookup.Request;
00012 import cz.vutbr.fit.dudka.SGVis.Lookup.Response;
00013 
00017 public class LookupWorker extends SwingWorker<Void, Void> {
00018   private URL url;
00019   private RelationStorage storage;
00020   private boolean downloadOk;
00021   private GraphView caller;
00022 
00023   private static HashSet<LookupWorker> set = new HashSet<LookupWorker>();
00024   private static int cntTotal = 0;
00025   private static int cntSuccess = 0;
00026   private static int cntFailed = 0;
00027   
00033   public static synchronized void run(URL url, GraphView caller) {
00034     LookupWorker worker = new LookupWorker(url, caller);
00035     set.add(worker);
00036     cntTotal++;
00037     worker.execute();
00038   }
00039   
00043   public static synchronized void killAll() {
00044     HashSet<LookupWorker> setClone = new HashSet<LookupWorker>(set);
00045     for (LookupWorker worker: setClone)
00046       worker.cancel(true);
00047     set.clear();
00048   }
00049   
00053   public static synchronized int getTotalCount() {
00054     return cntTotal;
00055   }
00056   
00060   public static synchronized int getActiveCount() {
00061     return set.size();
00062   }
00063   
00067   public static synchronized int getSuccessCount() {
00068     return cntSuccess; 
00069   }
00070   
00074   public static synchronized int getFailedCount() {
00075     return cntFailed;
00076   }
00077 
00078   private static synchronized void removeFromSet(LookupWorker worker) {
00079     set.remove(worker);
00080   }
00081 
00082   private LookupWorker(URL url, GraphView caller) {
00083     super();
00084     this.url = url;
00085     this.storage = new RelationStorage();
00086     this.caller = caller;
00087     this.downloadOk = false;
00088   }
00089 
00093   @Override
00094   protected Void doInBackground() throws Exception {
00095     try {
00096       Lookup lookup = Config.createLookup();
00097       Request rq = Config.createRequest(url.toString());
00098       String responseText = lookup.lookup(rq);
00099       Response response = new Response(responseText);
00100       response.addTo(this.storage);
00101       this.downloadOk = true;
00102     }
00103     catch (Exception e) {
00104       this.downloadOk = false;
00105     }
00106     return null;
00107   }
00108 
00112   @Override
00113   protected void done() {
00114     if (downloadOk) {
00115       caller.handleResponse(this.storage, url);
00116       cntSuccess++;
00117     } else {
00118       caller.lookupError(url);
00119       cntFailed++;
00120     }
00121     removeFromSet(this);
00122   }
00123 }

Generated on Sat May 3 22:56:09 2008 for SGVis by  doxygen 1.5.4