/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/MetroFetcher.java
ViewVC logotype

Annotation of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/MetroFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1048 - (hide annotations) (download)
Tue Sep 14 06:10:30 2010 UTC (13 years, 8 months ago) by torben
File size: 3261 byte(s)
Re-add Debug log statements but with loglevel=fine
1 torben 1042 package dk.thoerup.traininfoservice.banedk;
2    
3    
4     import java.net.URL;
5    
6     import java.util.ArrayList;
7     import java.util.List;
8     import java.util.Map;
9     import java.util.logging.Logger;
10    
11    
12     import org.jsoup.nodes.Document;
13     import org.jsoup.nodes.Element;
14     import org.jsoup.select.Elements;
15    
16     import dk.thoerup.circuitbreaker.CircuitBreaker;
17     import dk.thoerup.circuitbreaker.CircuitBreakerManager;
18     import dk.thoerup.traininfoservice.StationBean;
19     import dk.thoerup.traininfoservice.StationDAO;
20     import dk.thoerup.traininfoservice.Statistics;
21    
22    
23    
24     public class MetroFetcher {
25     public static class MetroEntry {
26     String metro;
27     String destination;
28     String minutes;
29     }
30    
31     public static class MetroBean {
32     String head;
33     String operationInfo; //driftsinfo
34     String plan; // koereplan
35    
36     List<MetroEntry> entries = new ArrayList<MetroEntry>();
37     }
38    
39     Logger logger = Logger.getLogger(MetroFetcher.class.getName());
40     StationDAO stationDAO = new StationDAO();
41    
42 torben 1043 Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(60000); //TODO: make metro cache timeout configurable
43 torben 1042
44     public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception {
45     final String key = "metro:" + stationID;
46    
47     MetroBean metroBean = cache.get(key);
48    
49     if (metroBean == null) {
50     metroBean = lookupMetroDepartures(stationID);
51     cache.put(key, metroBean);
52     } else {
53     Statistics.getInstance().incrementDepartureCacheHits();
54     logger.info("Metro: Cache hit " + key); //remove before production
55     }
56     return metroBean;
57     }
58    
59     public MetroBean lookupMetroDepartures(int stationID) throws Exception {
60    
61     StationBean station = stationDAO.getById(stationID) ;
62    
63    
64     URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true");
65 torben 1048 logger.fine("URL:" + url);
66 torben 1042
67     JsoupInvocation wrapper = new JsoupInvocation( url, 3000);
68     CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro");
69    
70     Document doc = (Document) breaker.invoke(wrapper);
71    
72     MetroBean bean = new MetroBean();
73    
74     bean.head = doc.getElementsByClass("metro-widget-headline").get(0).text();
75    
76     Element content = doc.getElementsByClass("metro-widget-content").get(0);
77     Elements contentElems = content.children();
78    
79     Element departureTable = content.getElementsByTag("table").get(0);
80    
81     Elements rows = departureTable.getElementsByTag("tr");
82    
83     for (int i=1; i<rows.size(); i++) {
84     Element row = rows.get(i);
85     Elements fields = row.children();
86    
87     String metro = fields.get(0).text();
88     String destination = fields.get(1).text();
89     Elements minuteFields = fields.get(2).children();
90     String minutes ="";
91    
92     for (Element elm : minuteFields) {
93     minutes = minutes + elm.text() + " ";
94     }
95    
96     MetroEntry entry = new MetroEntry();
97     entry.metro = metro;
98     entry.destination = destination;
99     entry.minutes = minutes;
100    
101     bean.entries.add(entry);
102     }
103    
104 torben 1043 bean.operationInfo = contentElems.get(3).text();
105 torben 1042
106 torben 1043 int planIndex;
107     if (contentElems.get(4).text().trim().equalsIgnoreCase("stationsinformation")) {
108     planIndex = 7;
109     } else {
110     planIndex = 5;
111     }
112    
113     bean.plan = contentElems.get(planIndex).text();
114    
115 torben 1042 return bean;
116     }
117    
118     }

  ViewVC Help
Powered by ViewVC 1.1.20