8 |
import java.util.Map; |
import java.util.Map; |
9 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
10 |
|
|
11 |
|
import com.gargoylesoftware.htmlunit.BrowserVersion; |
12 |
import com.gargoylesoftware.htmlunit.Page; |
import com.gargoylesoftware.htmlunit.Page; |
13 |
import com.gargoylesoftware.htmlunit.RefreshHandler; |
import com.gargoylesoftware.htmlunit.RefreshHandler; |
14 |
import com.gargoylesoftware.htmlunit.WebClient; |
import com.gargoylesoftware.htmlunit.WebClient; |
16 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
17 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
18 |
|
|
19 |
import dk.thoerup.curcuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
20 |
import dk.thoerup.curcuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
21 |
|
|
22 |
public class TimetableFetcher { |
public class TimetableFetcher { |
23 |
|
|
27 |
|
|
28 |
} |
} |
29 |
|
|
30 |
Map<String, List<TimetableBean>> cache = new TimeoutMap<String,List<TimetableBean>>(120 * 1000); |
Map<String, List<TimetableBean>> cache; |
31 |
|
|
32 |
|
|
33 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
34 |
|
|
35 |
|
private boolean useTempSite; |
36 |
|
|
37 |
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Throwable { |
public TimetableFetcher(boolean tmpSite, int cacheTimeout) { |
38 |
|
useTempSite = tmpSite; |
39 |
|
|
40 |
|
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
41 |
|
} |
42 |
|
|
43 |
|
|
44 |
|
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception { |
45 |
String key = trainID+type; |
String key = trainID+type; |
46 |
List<TimetableBean> list = cache.get(key); |
List<TimetableBean> list = cache.get(key); |
47 |
|
|
53 |
} |
} |
54 |
return list; |
return list; |
55 |
} |
} |
56 |
|
|
57 |
|
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
58 |
|
if (useTempSite == false ){ |
59 |
|
return lookupTimetableRealSite(trainID, type); |
60 |
|
} else { |
61 |
|
return new ArrayList<TimetableBean>(); // no timetable data on temp site |
62 |
|
} |
63 |
|
} |
64 |
|
|
65 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Throwable { |
List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception { |
66 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
67 |
|
|
68 |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
69 |
|
|
70 |
|
|
71 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
72 |
webClient.setTimeout(2500); |
webClient.setTimeout(2500); |
73 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
74 |
webClient.setRefreshHandler( new NullRefreshHandler() ); |
webClient.setRefreshHandler( new NullRefreshHandler() ); |
119 |
} else { |
} else { |
120 |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
121 |
} |
} |
122 |
|
webClient.closeAllWindows(); |
123 |
|
|
124 |
return timetableList; |
return timetableList; |
125 |
} |
} |