source: josm/trunk/src/org/openstreetmap/josm/data/imagery/CachedAttributionBingAerialTileSource.java@ 11288

Last change on this file since 11288 was 11288, checked in by simon04, 7 years ago

see #13376 - Use TimeUnit instead of combinations of 1000/60/60/24

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import java.io.IOException;
5import java.io.StringReader;
6import java.net.URL;
7import java.util.List;
8import java.util.concurrent.Callable;
9import java.util.concurrent.TimeUnit;
10
11import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
12import org.openstreetmap.gui.jmapviewer.tilesources.TileSourceInfo;
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.gui.util.GuiHelper;
15import org.openstreetmap.josm.io.CacheCustomContent;
16import org.openstreetmap.josm.tools.HttpClient;
17import org.xml.sax.InputSource;
18
19/**
20 * Bing TileSource with cached attribution
21 *
22 * @author Wiktor Niesiobędzki
23 * @since 8526
24 */
25public class CachedAttributionBingAerialTileSource extends BingAerialTileSource {
26 private Runnable attributionDownloadedTask;
27
28 /**
29 * Creates tile source
30 * @param info ImageryInfo description of this tile source
31 */
32 public CachedAttributionBingAerialTileSource(ImageryInfo info) {
33 super(info);
34 }
35
36 /**
37 * Creates tile source
38 * @param info ImageryInfo description of this tile source
39 * @param attributionDownloadedTask runnable to be executed once attribution is loaded
40 */
41
42 public CachedAttributionBingAerialTileSource(TileSourceInfo info, Runnable attributionDownloadedTask) {
43 super(info);
44 this.attributionDownloadedTask = attributionDownloadedTask;
45 }
46
47 class BingAttributionData extends CacheCustomContent<IOException> {
48
49 BingAttributionData() {
50 super("bing.attribution.xml", CacheCustomContent.INTERVAL_HOURLY);
51 }
52
53 @Override
54 protected byte[] updateData() throws IOException {
55 URL u = getAttributionUrl();
56 final String r = HttpClient.create(u).connect().fetchContent();
57 Main.info("Successfully loaded Bing attribution data.");
58 return r.getBytes("UTF-8");
59 }
60 }
61
62 @Override
63 protected Callable<List<Attribution>> getAttributionLoaderCallable() {
64 return () -> {
65 BingAttributionData attributionLoader = new BingAttributionData();
66 int waitTimeSec = 1;
67 while (true) {
68 try {
69 String xml = attributionLoader.updateIfRequiredString();
70 List<Attribution> ret = parseAttributionText(new InputSource(new StringReader(xml)));
71 if (attributionDownloadedTask != null) {
72 GuiHelper.runInEDT(attributionDownloadedTask);
73 attributionDownloadedTask = null;
74 }
75 return ret;
76 } catch (IOException ex) {
77 Main.warn(ex, "Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds.");
78 Thread.sleep(TimeUnit.SECONDS.toMillis(waitTimeSec));
79 waitTimeSec *= 2;
80 }
81 }
82 };
83 }
84}
Note: See TracBrowser for help on using the repository browser.