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

Last change on this file since 9970 was 9970, checked in by Don-vip, 8 years ago

sonar - remove useless parentheses

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