4 |
import java.net.URL; |
import java.net.URL; |
5 |
import java.net.URLEncoder; |
import java.net.URLEncoder; |
6 |
import java.util.Collections; |
import java.util.Collections; |
7 |
|
import java.util.HashMap; |
8 |
import java.util.Map; |
import java.util.Map; |
9 |
|
import java.util.logging.Level; |
10 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
11 |
|
|
12 |
|
import net.sf.jsr107cache.Cache; |
13 |
|
import net.sf.jsr107cache.CacheException; |
14 |
|
import net.sf.jsr107cache.CacheManager; |
15 |
|
|
16 |
import org.jsoup.nodes.Document; |
import org.jsoup.nodes.Document; |
17 |
import org.jsoup.nodes.Element; |
import org.jsoup.nodes.Element; |
18 |
import org.jsoup.select.Elements; |
import org.jsoup.select.Elements; |
19 |
|
|
20 |
|
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory; |
21 |
|
|
22 |
import dk.thoerup.android.traininfo.common.DepartureBean; |
import dk.thoerup.android.traininfo.common.DepartureBean; |
23 |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
24 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
33 |
STOG, |
STOG, |
34 |
REGIONAL |
REGIONAL |
35 |
} |
} |
36 |
|
Cache cache; |
37 |
|
|
38 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
|
|
|
|
Map<String, DepartureBean> cache; |
|
39 |
|
|
40 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
41 |
|
|
42 |
private boolean useAzureSite; |
private boolean useAzureSite; |
43 |
private int replyTimeout; |
private int replyTimeout; |
44 |
|
|
45 |
|
@SuppressWarnings("unchecked") |
46 |
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
47 |
this.replyTimeout = replyTimeout; |
this.replyTimeout = replyTimeout; |
48 |
useAzureSite = azureSite; |
useAzureSite = azureSite; |
49 |
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
|
50 |
|
Map props = new HashMap(); |
51 |
|
props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, cacheTimeout); |
52 |
|
|
53 |
|
try { |
54 |
|
cache = CacheManager.getInstance().getCacheFactory().createCache(props); |
55 |
|
} catch (CacheException e) { |
56 |
|
logger.log(Level.WARNING, "error creating cache", e); |
57 |
|
} |
58 |
|
|
59 |
} |
} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
65 |
final String key = "" + stationID + ":" + arrival; |
final String key = "departure:" + stationID + ":" + arrival; |
66 |
|
|
67 |
DepartureBean departureBean = cache.get(key); |
DepartureBean departureBean = (DepartureBean) cache.get(key); |
|
|
|
68 |
|
|
69 |
if (departureBean == null) { |
if (departureBean == null) { |
70 |
departureBean = lookupDepartures(stationID,arrival); |
departureBean = lookupDepartures(stationID,arrival); |
71 |
cache.put(key, departureBean); |
cache.put(key, departureBean); |
72 |
|
logger.info("Departure: Cache miss " + key + " !!! "); //remove before production |
73 |
} else { |
} else { |
74 |
Statistics.getInstance().incrementDepartureCacheHits(); |
Statistics.getInstance().incrementDepartureCacheHits(); |
75 |
logger.info("Departure: Cache hit " + key); //remove before production |
logger.info("Departure: Cache hit " + key); |
76 |
} |
} |
77 |
|
|
78 |
return departureBean; |
return departureBean; |
79 |
} |
} |
80 |
|
|
83 |
|
|
84 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
85 |
|
|
86 |
StationEntry station = new StationEntry(); // stationDao.getById(stationID); |
StationEntry station = stationDao.getById(stationID); |
87 |
station.setId(82); |
|
|
station.setName("Test Station"); |
|
|
station.setRegional("HS"); |
|
88 |
|
|
89 |
departureBean.stationName = station.getName(); |
departureBean.stationName = station.getName(); |
90 |
|
|