2 |
|
|
3 |
|
|
4 |
import java.net.URL; |
import java.net.URL; |
5 |
|
import java.util.HashMap; |
6 |
import java.util.Map; |
import java.util.Map; |
7 |
|
import java.util.logging.Level; |
8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
9 |
|
|
10 |
|
import net.sf.jsr107cache.Cache; |
11 |
|
import net.sf.jsr107cache.CacheException; |
12 |
|
import net.sf.jsr107cache.CacheManager; |
13 |
|
|
14 |
import org.jsoup.nodes.Document; |
import org.jsoup.nodes.Document; |
15 |
import org.jsoup.nodes.Element; |
import org.jsoup.nodes.Element; |
16 |
import org.jsoup.select.Elements; |
import org.jsoup.select.Elements; |
17 |
|
|
18 |
|
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory; |
19 |
|
|
20 |
import dk.thoerup.android.traininfo.common.MetroBean; |
import dk.thoerup.android.traininfo.common.MetroBean; |
21 |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
|
22 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
23 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
24 |
import dk.thoerup.traininfoservice.StationDAO; |
import dk.thoerup.traininfoservice.StationDAO; |
25 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
26 |
|
import dk.thoerup.traininfoservice.jdo.JdoStationBean; |
27 |
|
|
28 |
|
|
29 |
|
|
32 |
Logger logger = Logger.getLogger(MetroFetcher.class.getName()); |
Logger logger = Logger.getLogger(MetroFetcher.class.getName()); |
33 |
StationDAO stationDAO = new StationDAO(); |
StationDAO stationDAO = new StationDAO(); |
34 |
|
|
35 |
Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(60000); //TODO: make metro cache timeout configurable |
//Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(60000); //TODO: make metro cache timeout configurable |
36 |
|
Cache cache; |
37 |
|
|
38 |
|
public MetroFetcher() { |
39 |
|
Map props = new HashMap(); |
40 |
|
props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, 60000); |
41 |
|
|
42 |
|
try { |
43 |
|
cache = CacheManager.getInstance().getCacheFactory().createCache(props); |
44 |
|
} catch (CacheException e) { |
45 |
|
logger.log(Level.WARNING, "error creating cache", e); |
46 |
|
} |
47 |
|
} |
48 |
|
|
49 |
public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception { |
public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception { |
50 |
final String key = "metro:" + stationID; |
final String key = "metro:" + stationID; |
51 |
|
|
52 |
MetroBean metroBean = cache.get(key); |
MetroBean metroBean = (MetroBean) cache.get(key); |
53 |
|
|
54 |
if (metroBean == null) { |
if (metroBean == null) { |
55 |
metroBean = lookupMetroDepartures(stationID); |
metroBean = lookupMetroDepartures(stationID); |
63 |
|
|
64 |
public MetroBean lookupMetroDepartures(int stationID) throws Exception { |
public MetroBean lookupMetroDepartures(int stationID) throws Exception { |
65 |
|
|
66 |
StationEntry station = stationDAO.getById(stationID) ; |
JdoStationBean station = stationDAO.getById(stationID) ; |
67 |
|
|
68 |
|
|
69 |
URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true"); |
URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true"); |