--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2009/09/29 15:49:19 353 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/02/05 13:57:39 584 @@ -5,10 +5,18 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.logging.Logger; -import com.gargoylesoftware.htmlunit.*; -import com.gargoylesoftware.htmlunit.html.*; +import com.gargoylesoftware.htmlunit.Page; +import com.gargoylesoftware.htmlunit.RefreshHandler; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.DomNodeList; +import com.gargoylesoftware.htmlunit.html.HtmlElement; +import com.gargoylesoftware.htmlunit.html.HtmlPage; + +import dk.thoerup.circuitbreaker.CircuitBreaker; +import dk.thoerup.circuitbreaker.CircuitBreakerManager; public class TimetableFetcher { @@ -17,24 +25,60 @@ } } + + Map> cache; + Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); - + + private boolean useTempSite; + + public TimetableFetcher(boolean tmpSite, int cacheTimeout) { + useTempSite = tmpSite; + + cache = new TimeoutMap>(cacheTimeout); + } + + + List cachedLookupTimetable(String trainID, String type) throws Exception { + String key = trainID+type; + List list = cache.get(key); + + if (list == null) { + list = lookupTimetable(trainID,type); + cache.put(key, list); + } else { + logger.info("Timetable: Cache hit " + trainID); + } + return list; + } + List lookupTimetable(String trainID, String type) throws Exception { + if (useTempSite == false ){ + return lookupTimetableRealSite(trainID, type); + } else { + return new ArrayList(); // no timetable data on temp site + } + } + + List lookupTimetableRealSite(String trainID, String type) throws Exception { List timetableList = new ArrayList(); String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; final WebClient webClient = new WebClient(); - webClient.setTimeout(1000); + webClient.setTimeout(2500); webClient.setJavaScriptEnabled(false); webClient.setRefreshHandler( new NullRefreshHandler() ); webClient.setCssEnabled(false); - final HtmlPage page = webClient.getPage(url); + BanedkInvocation wrapper = new BanedkInvocation(webClient, url); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); + HtmlPage page = (HtmlPage) breaker.invoke(wrapper); + boolean currentStation = false; boolean currentStationSaved = false;