source: josm/trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJob.java@ 12809

Last change on this file since 12809 was 12765, checked in by wiktorn, 7 years ago

Use tools.Logging instead of FeatureAdapter.getLogger

See: #15229

  • Property svn:eol-style set to native
File size: 11.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.ByteArrayInputStream;
7import java.io.IOException;
8import java.net.URL;
9import java.util.HashSet;
10import java.util.List;
11import java.util.Map;
12import java.util.Map.Entry;
13import java.util.Optional;
14import java.util.Set;
15import java.util.concurrent.ConcurrentHashMap;
16import java.util.concurrent.ConcurrentMap;
17import java.util.concurrent.ThreadPoolExecutor;
18import java.util.concurrent.TimeUnit;
19
20import org.apache.commons.jcs.access.behavior.ICacheAccess;
21import org.openstreetmap.gui.jmapviewer.Tile;
22import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
23import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
24import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
25import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
26import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
27import org.openstreetmap.josm.data.cache.CacheEntry;
28import org.openstreetmap.josm.data.cache.CacheEntryAttributes;
29import org.openstreetmap.josm.data.cache.ICachedLoaderListener;
30import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob;
31import org.openstreetmap.josm.data.preferences.LongProperty;
32import org.openstreetmap.josm.tools.HttpClient;
33import org.openstreetmap.josm.tools.Logging;
34
35/**
36 * Class bridging TMS requests to JCS cache requests
37 *
38 * @author Wiktor Niesiobędzki
39 * @since 8168
40 */
41public class TMSCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, BufferedImageCacheEntry> implements TileJob, ICachedLoaderListener {
42 private static final LongProperty MAXIMUM_EXPIRES = new LongProperty("imagery.generic.maximum_expires", TimeUnit.DAYS.toMillis(30));
43 private static final LongProperty MINIMUM_EXPIRES = new LongProperty("imagery.generic.minimum_expires", TimeUnit.HOURS.toMillis(1));
44 protected final Tile tile;
45 private volatile URL url;
46
47 // we need another deduplication of Tile Loader listeners, as for each submit, new TMSCachedTileLoaderJob was created
48 // that way, we reduce calls to tileLoadingFinished, and general CPU load due to surplus Map repaints
49 private static final ConcurrentMap<String, Set<TileLoaderListener>> inProgress = new ConcurrentHashMap<>();
50
51 /**
52 * Constructor for creating a job, to get a specific tile from cache
53 * @param listener Tile loader listener
54 * @param tile to be fetched from cache
55 * @param cache object
56 * @param connectTimeout when connecting to remote resource
57 * @param readTimeout when connecting to remote resource
58 * @param headers HTTP headers to be sent together with request
59 * @param downloadExecutor that will be executing the jobs
60 */
61 public TMSCachedTileLoaderJob(TileLoaderListener listener, Tile tile,
62 ICacheAccess<String, BufferedImageCacheEntry> cache,
63 int connectTimeout, int readTimeout, Map<String, String> headers,
64 ThreadPoolExecutor downloadExecutor) {
65 super(cache, connectTimeout, readTimeout, headers, downloadExecutor);
66 this.tile = tile;
67 if (listener != null) {
68 String deduplicationKey = getCacheKey();
69 synchronized (inProgress) {
70 Set<TileLoaderListener> newListeners = inProgress.get(deduplicationKey);
71 if (newListeners == null) {
72 newListeners = new HashSet<>();
73 inProgress.put(deduplicationKey, newListeners);
74 }
75 newListeners.add(listener);
76 }
77 }
78 }
79
80 @Override
81 public String getCacheKey() {
82 if (tile != null) {
83 TileSource tileSource = tile.getTileSource();
84 return Optional.ofNullable(tileSource.getName()).orElse("").replace(':', '_') + ':'
85 + tileSource.getTileId(tile.getZoom(), tile.getXtile(), tile.getYtile());
86 }
87 return null;
88 }
89
90 /*
91 * this doesn't needs to be synchronized, as it's not that costly to keep only one execution
92 * in parallel, but URL creation and Tile.getUrl() are costly and are not needed when fetching
93 * data from cache, that's why URL creation is postponed until it's needed
94 *
95 * We need to have static url value for TileLoaderJob, as for some TileSources we might get different
96 * URL's each call we made (servers switching), and URL's are used below as a key for duplicate detection
97 *
98 */
99 @Override
100 public URL getUrl() throws IOException {
101 if (url == null) {
102 synchronized (this) {
103 if (url == null) {
104 String sUrl = tile.getUrl();
105 if (!"".equals(sUrl)) {
106 url = new URL(sUrl);
107 }
108 }
109 }
110 }
111 return url;
112 }
113
114 @Override
115 public boolean isObjectLoadable() {
116 if (cacheData != null) {
117 byte[] content = cacheData.getContent();
118 try {
119 return content.length > 0 || cacheData.getImage() != null || isNoTileAtZoom();
120 } catch (IOException e) {
121 Logging.logWithStackTrace(Logging.LEVEL_WARN, e, "JCS TMS - error loading from cache for tile {0}: {1}",
122 new Object[] {tile.getKey(), e.getMessage()}
123 );
124 }
125 }
126 return false;
127 }
128
129 @Override
130 protected boolean isResponseLoadable(Map<String, List<String>> headers, int statusCode, byte[] content) {
131 attributes.setMetadata(tile.getTileSource().getMetadata(headers));
132 if (tile.getTileSource().isNoTileAtZoom(headers, statusCode, content)) {
133 attributes.setNoTileAtZoom(true);
134 return false; // do no try to load data from no-tile at zoom, cache empty object instead
135 }
136 return super.isResponseLoadable(headers, statusCode, content);
137 }
138
139 @Override
140 protected boolean cacheAsEmpty() {
141 return isNoTileAtZoom() || super.cacheAsEmpty();
142 }
143
144 @Override
145 public void submit(boolean force) {
146 tile.initLoading();
147 try {
148 super.submit(this, force);
149 } catch (IOException | IllegalArgumentException e) {
150 // if we fail to submit the job, mark tile as loaded and set error message
151 Logging.log(Logging.LEVEL_WARN, e);
152 tile.finishLoading();
153 tile.setError(e.getMessage());
154 }
155 }
156
157 @Override
158 public void loadingFinished(CacheEntry object, CacheEntryAttributes attributes, LoadResult result) {
159 this.attributes = attributes; // as we might get notification from other object than our selfs, pass attributes along
160 Set<TileLoaderListener> listeners;
161 synchronized (inProgress) {
162 listeners = inProgress.remove(getCacheKey());
163 }
164 boolean status = result.equals(LoadResult.SUCCESS);
165
166 try {
167 tile.finishLoading(); // whatever happened set that loading has finished
168 // set tile metadata
169 if (this.attributes != null) {
170 for (Entry<String, String> e: this.attributes.getMetadata().entrySet()) {
171 tile.putValue(e.getKey(), e.getValue());
172 }
173 }
174
175 switch(result) {
176 case SUCCESS:
177 handleNoTileAtZoom();
178 if (attributes != null) {
179 int httpStatusCode = attributes.getResponseCode();
180 if (httpStatusCode >= 400 && !isNoTileAtZoom()) {
181 if (attributes.getErrorMessage() == null) {
182 tile.setError(tr("HTTP error {0} when loading tiles", httpStatusCode));
183 } else {
184 tile.setError(tr("Error downloading tiles: {0}", attributes.getErrorMessage()));
185 }
186 status = false;
187 }
188 }
189 status &= tryLoadTileImage(object); //try to keep returned image as background
190 break;
191 case FAILURE:
192 tile.setError("Problem loading tile");
193 tryLoadTileImage(object);
194 break;
195 case CANCELED:
196 tile.loadingCanceled();
197 // do nothing
198 }
199
200 // always check, if there is some listener interested in fact, that tile has finished loading
201 if (listeners != null) { // listeners might be null, if some other thread notified already about success
202 for (TileLoaderListener l: listeners) {
203 l.tileLoadingFinished(tile, status);
204 }
205 }
206 } catch (IOException e) {
207 Logging.warn("JCS TMS - error loading object for tile {0}: {1}", new Object[] {tile.getKey(), e.getMessage()});
208 tile.setError(e);
209 tile.setLoaded(false);
210 if (listeners != null) { // listeners might be null, if some other thread notified already about success
211 for (TileLoaderListener l: listeners) {
212 l.tileLoadingFinished(tile, false);
213 }
214 }
215 }
216 }
217
218 /**
219 * For TMS use BaseURL as settings discovery, so for different paths, we will have different settings (useful for developer servers)
220 *
221 * @return base URL of TMS or server url as defined in super class
222 */
223 @Override
224 protected String getServerKey() {
225 TileSource ts = tile.getSource();
226 if (ts instanceof AbstractTMSTileSource) {
227 return ((AbstractTMSTileSource) ts).getBaseUrl();
228 }
229 return super.getServerKey();
230 }
231
232 @Override
233 protected BufferedImageCacheEntry createCacheEntry(byte[] content) {
234 return new BufferedImageCacheEntry(content);
235 }
236
237 @Override
238 public void submit() {
239 submit(false);
240 }
241
242 @Override
243 protected CacheEntryAttributes parseHeaders(HttpClient.Response urlConn) {
244 CacheEntryAttributes ret = super.parseHeaders(urlConn);
245 // keep the expiration time between MINIMUM_EXPIRES and MAXIMUM_EXPIRES, so we will cache the tiles
246 // at least for some short period of time, but not too long
247 if (ret.getExpirationTime() < now + MINIMUM_EXPIRES.get()) {
248 ret.setExpirationTime(now + MINIMUM_EXPIRES.get());
249 }
250 if (ret.getExpirationTime() > now + MAXIMUM_EXPIRES.get()) {
251 ret.setExpirationTime(now + MAXIMUM_EXPIRES.get());
252 }
253 return ret;
254 }
255
256 private boolean handleNoTileAtZoom() {
257 if (isNoTileAtZoom()) {
258 Logging.debug("JCS TMS - Tile valid, but no file, as no tiles at this level {0}", tile);
259 tile.setError("No tile at this zoom level");
260 tile.putValue("tile-info", "no-tile");
261 return true;
262 }
263 return false;
264 }
265
266 private boolean isNoTileAtZoom() {
267 if (attributes == null) {
268 Logging.warn("Cache attributes are null");
269 }
270 return attributes != null && attributes.isNoTileAtZoom();
271 }
272
273 private boolean tryLoadTileImage(CacheEntry object) throws IOException {
274 if (object != null) {
275 byte[] content = object.getContent();
276 if (content.length > 0) {
277 tile.loadImage(new ByteArrayInputStream(content));
278 if (tile.getImage() == null) {
279 tile.setError(tr("Could not load image from tile server"));
280 return false;
281 }
282 }
283 }
284 return true;
285 }
286}
Note: See TracBrowser for help on using the repository browser.