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