--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2009/10/08 12:19:42 421 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/05/05 20:11:03 711 @@ -5,8 +5,10 @@ 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.BrowserVersion; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.RefreshHandler; import com.gargoylesoftware.htmlunit.WebClient; @@ -14,8 +16,9 @@ import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlPage; -import dk.thoerup.curcuitbreaker.CircuitBreaker; -import dk.thoerup.curcuitbreaker.CircuitBreakerManager; +import dk.thoerup.circuitbreaker.CircuitBreaker; +import dk.thoerup.circuitbreaker.CircuitBreakerManager; +import dk.thoerup.traininfoservice.Statistics; public class TimetableFetcher { @@ -25,13 +28,21 @@ } - TimeoutCache> cache = new TimeoutCache>(120 * 1000); + Map> cache; Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); + private boolean useTempSite; - List cachedLookupTimetable(String trainID, String type) throws Throwable { + 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); @@ -39,18 +50,27 @@ list = lookupTimetable(trainID,type); cache.put(key, list); } else { + Statistics.getInstance().incrementTimetableCacheHits(); 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 lookupTimetable(String trainID, String type) throws Throwable { + 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(); + final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); webClient.setTimeout(2500); webClient.setJavaScriptEnabled(false); webClient.setRefreshHandler( new NullRefreshHandler() ); @@ -101,6 +121,7 @@ } else { logger.warning("No time table found, trainID=" + trainID + " type=" + type); } + webClient.closeAllWindows(); return timetableList; }