1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import java.io.ByteArrayInputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.net.URL;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.concurrent.Executor;
|
---|
9 | import java.util.concurrent.LinkedBlockingDeque;
|
---|
10 | import java.util.concurrent.ThreadPoolExecutor;
|
---|
11 | import java.util.concurrent.TimeUnit;
|
---|
12 | import java.util.logging.Level;
|
---|
13 | import java.util.logging.Logger;
|
---|
14 |
|
---|
15 | import org.apache.commons.jcs.access.behavior.ICacheAccess;
|
---|
16 | import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
|
---|
17 | import org.openstreetmap.gui.jmapviewer.Tile;
|
---|
18 | import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
|
---|
19 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
|
---|
20 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
21 | import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
|
---|
22 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
23 | import org.openstreetmap.josm.data.cache.CacheEntry;
|
---|
24 | import org.openstreetmap.josm.data.cache.ICachedLoaderListener;
|
---|
25 | import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob;
|
---|
26 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * @author Wiktor Niesiobędzki
|
---|
30 | *
|
---|
31 | * Class bridging TMS requests to JCS cache requests
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public class TMSCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, BufferedImageCacheEntry> implements TileJob, ICachedLoaderListener {
|
---|
35 | private static final Logger log = FeatureAdapter.getLogger(TMSCachedTileLoaderJob.class.getCanonicalName());
|
---|
36 | private Tile tile;
|
---|
37 | private TileLoaderListener listener;
|
---|
38 | private volatile URL url;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * overrides the THREAD_LIMIT in superclass, as we want to have separate limit and pool for TMS
|
---|
42 | */
|
---|
43 | public static final IntegerProperty THREAD_LIMIT = new IntegerProperty("imagery.tms.tmsloader.maxjobs", 25);
|
---|
44 | /**
|
---|
45 | * separate from JCS thread pool for TMS loader, so we can have different thread pools for default JCS
|
---|
46 | * and for TMS imagery
|
---|
47 | */
|
---|
48 | private static ThreadPoolExecutor DOWNLOAD_JOB_DISPATCHER = new ThreadPoolExecutor(
|
---|
49 | THREAD_LIMIT.get().intValue(), // keep the thread number constant
|
---|
50 | THREAD_LIMIT.get().intValue(), // do not this number of threads
|
---|
51 | 30, // keepalive for thread
|
---|
52 | TimeUnit.SECONDS,
|
---|
53 | // make queue of LIFO type - so recently requested tiles will be loaded first (assuming that these are which user is waiting to see)
|
---|
54 | new LinkedBlockingDeque<Runnable>(5) {
|
---|
55 | /* keep the queue size fairly small, we do not want to
|
---|
56 | download a lot of tiles, that user is not seeing anyway */
|
---|
57 | @Override
|
---|
58 | public boolean offer(Runnable t) {
|
---|
59 | return super.offerFirst(t);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public Runnable remove() {
|
---|
64 | return super.removeFirst();
|
---|
65 | }
|
---|
66 | }
|
---|
67 | );
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Constructor for creating a job, to get a specific tile from cache
|
---|
71 | * @param listener
|
---|
72 | * @param tile to be fetched from cache
|
---|
73 | * @param cache object
|
---|
74 | * @param connectTimeout when connecting to remote resource
|
---|
75 | * @param readTimeout when connecting to remote resource
|
---|
76 | * @param headers to be sent together with request
|
---|
77 | */
|
---|
78 | public TMSCachedTileLoaderJob(TileLoaderListener listener, Tile tile, ICacheAccess<String, BufferedImageCacheEntry> cache, int connectTimeout, int readTimeout,
|
---|
79 | Map<String, String> headers) {
|
---|
80 | super(cache, connectTimeout, readTimeout, headers);
|
---|
81 | this.tile = tile;
|
---|
82 | this.listener = listener;
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public Tile getTile() {
|
---|
87 | return getCachedTile();
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public String getCacheKey() {
|
---|
92 | if (tile != null)
|
---|
93 | return tile.getKey();
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * this doesn't needs to be synchronized, as it's not that costly to keep only one execution
|
---|
99 | * in parallel, but URL creation and Tile.getUrl() are costly and are not needed when fetching
|
---|
100 | * data from cache
|
---|
101 | *
|
---|
102 | * We need to have static url value for TileLoaderJob, as for some TileSources we might get different
|
---|
103 | * URL's each call we made (servers switching), and URL's are used below as a key for duplicate detection
|
---|
104 | *
|
---|
105 | */
|
---|
106 | @Override
|
---|
107 | public URL getUrl() {
|
---|
108 | if (url == null) {
|
---|
109 | try {
|
---|
110 | synchronized (this) {
|
---|
111 | if (url == null)
|
---|
112 | url = new URL(tile.getUrl());
|
---|
113 | }
|
---|
114 | } catch (IOException e) {
|
---|
115 | log.log(Level.WARNING, "JCS TMS Cache - error creating URL for tile {0}: {1}", new Object[] {tile.getKey(), e.getMessage()});
|
---|
116 | log.log(Level.INFO, "Exception: ", e);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | return url;
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public boolean isObjectLoadable() {
|
---|
124 | if (cacheData != null) {
|
---|
125 | byte[] content = cacheData.getContent();
|
---|
126 | try {
|
---|
127 | return content != null || cacheData.getImage() != null || cacheAsEmpty();
|
---|
128 | } catch (IOException e) {
|
---|
129 | log.log(Level.WARNING, "JCS TMS - error loading from cache for tile {0}: {1}", new Object[] {tile.getKey(), e.getMessage()});
|
---|
130 | }
|
---|
131 | }
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 |
|
---|
135 | private boolean isNoTileAtZoom() {
|
---|
136 | return attributes != null && attributes.isNoTileAtZoom();
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | protected boolean cacheAsEmpty() {
|
---|
141 | return isNoTileAtZoom();
|
---|
142 | }
|
---|
143 |
|
---|
144 | private boolean handleNoTileAtZoom() {
|
---|
145 | if (isNoTileAtZoom()) {
|
---|
146 | log.log(Level.FINE, "JCS TMS - Tile valid, but no file, as no tiles at this level {0}", tile);
|
---|
147 | tile.setError("No tile at this zoom level");
|
---|
148 | tile.putValue("tile-info", "no-tile");
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | protected Executor getDownloadExecutor() {
|
---|
156 | return DOWNLOAD_JOB_DISPATCHER;
|
---|
157 | }
|
---|
158 |
|
---|
159 | public void submit() {
|
---|
160 | tile.initLoading();
|
---|
161 | super.submit(this);
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | public void loadingFinished(CacheEntry object, LoadResult result) {
|
---|
166 | try {
|
---|
167 | tile.finishLoading(); // whatever happened set that loading has finished
|
---|
168 | switch(result){
|
---|
169 | case FAILURE:
|
---|
170 | tile.setError("Problem loading tile");
|
---|
171 | case SUCCESS:
|
---|
172 | handleNoTileAtZoom();
|
---|
173 | if (object != null) {
|
---|
174 | byte[] content = object.getContent();
|
---|
175 | if (content != null && content.length > 0) {
|
---|
176 | tile.loadImage(new ByteArrayInputStream(content));
|
---|
177 | }
|
---|
178 | }
|
---|
179 | case REJECTED:
|
---|
180 | // do not set anything here, leave waiting sign
|
---|
181 | }
|
---|
182 | if (listener != null) {
|
---|
183 | listener.tileLoadingFinished(tile, result.equals(LoadResult.SUCCESS));
|
---|
184 | }
|
---|
185 | } catch (IOException e) {
|
---|
186 | log.log(Level.WARNING, "JCS TMS - error loading object for tile {0}: {1}", new Object[] {tile.getKey(), e.getMessage()});
|
---|
187 | tile.setError(e.getMessage());
|
---|
188 | tile.setLoaded(false);
|
---|
189 | if (listener != null) {
|
---|
190 | listener.tileLoadingFinished(tile, false);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Method for getting the tile from cache only, without trying to reach remote resource
|
---|
197 | * @return tile or null, if nothing (useful) was found in cache
|
---|
198 | */
|
---|
199 | public Tile getCachedTile() {
|
---|
200 | BufferedImageCacheEntry data = get();
|
---|
201 | if (isObjectLoadable()) {
|
---|
202 | try {
|
---|
203 | if (data != null && data.getImage() != null) {
|
---|
204 | tile.setImage(data.getImage());
|
---|
205 | tile.finishLoading();
|
---|
206 | }
|
---|
207 | if (isNoTileAtZoom()) {
|
---|
208 | handleNoTileAtZoom();
|
---|
209 | tile.finishLoading();
|
---|
210 | }
|
---|
211 | return tile;
|
---|
212 | } catch (IOException e) {
|
---|
213 | log.log(Level.WARNING, "JCS TMS - error loading object for tile {0}: {1}", new Object[] {tile.getKey(), e.getMessage()});
|
---|
214 | return null;
|
---|
215 | }
|
---|
216 |
|
---|
217 | } else {
|
---|
218 | return tile;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | @Override
|
---|
223 | protected boolean handleNotFound() {
|
---|
224 | tile.setError("No tile at this zoom level");
|
---|
225 | tile.putValue("tile-info", "no-tile");
|
---|
226 | return true;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * For TMS use BaseURL as settings discovery, so for different paths, we will have different settings (useful for developer servers)
|
---|
231 | *
|
---|
232 | * @return base URL of TMS or server url as defined in super class
|
---|
233 | */
|
---|
234 | @Override
|
---|
235 | protected String getServerKey() {
|
---|
236 | TileSource ts = tile.getSource();
|
---|
237 | if (ts instanceof AbstractTMSTileSource) {
|
---|
238 | return ((AbstractTMSTileSource) ts).getBaseUrl();
|
---|
239 | }
|
---|
240 | return super.getServerKey();
|
---|
241 | }
|
---|
242 |
|
---|
243 | @Override
|
---|
244 | protected BufferedImageCacheEntry createCacheEntry(byte[] content) {
|
---|
245 | return new BufferedImageCacheEntry(content);
|
---|
246 | }
|
---|
247 | }
|
---|