diff --git .gitignore .gitignore
new file mode 100644
index 0000000..2384283
-
|
+
|
|
| 1 | /bin/ |
| 2 | /build/ |
| 3 | /dist/ |
diff --git src/org/openstreetmap/josm/data/cache/ICachedLoaderListener.java src/org/openstreetmap/josm/data/cache/ICachedLoaderListener.java
index 32fa35a..fe26c09 100644
|
|
|
2 | 2 | package org.openstreetmap.josm.data.cache; |
3 | 3 | |
4 | 4 | public interface ICachedLoaderListener { |
| 5 | |
| 6 | /** |
| 7 | * Result of download |
| 8 | * |
| 9 | */ |
| 10 | enum LoadResult { |
| 11 | SUCCESS, |
| 12 | FAILURE, |
| 13 | REJECTED |
| 14 | } |
5 | 15 | /** |
6 | | * Will be called when K object was successfully downloaded |
7 | | * |
| 16 | * Will be called when K object processed. The result might be: |
| 17 | * LoadResult.SUCCESS when object was fetched |
| 18 | * LoadResult.FAILURE when there was a failure during download |
| 19 | * LoadResult.REJECTED when job was rejected because of full queue |
| 20 | * |
8 | 21 | * @param data |
9 | | * @param success |
| 22 | * @param result |
10 | 23 | */ |
11 | | public void loadingFinished(CacheEntry data, boolean success); |
| 24 | public void loadingFinished(CacheEntry data, LoadResult result); |
12 | 25 | |
13 | 26 | } |
diff --git src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java
index d636de4..478004b 100644
|
|
import java.util.logging.Logger; |
26 | 26 | import org.apache.commons.jcs.access.behavior.ICacheAccess; |
27 | 27 | import org.apache.commons.jcs.engine.behavior.ICacheElement; |
28 | 28 | import org.openstreetmap.gui.jmapviewer.FeatureAdapter; |
| 29 | import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult; |
29 | 30 | import org.openstreetmap.josm.data.preferences.IntegerProperty; |
30 | 31 | |
31 | 32 | /** |
… |
… |
public abstract class JCSCachedTileLoaderJob<K, V extends CacheEntry> implements |
151 | 152 | if (cacheElement != null && isCacheElementValid() && (isObjectLoadable())) { |
152 | 153 | // we got something in cache, and it's valid, so lets return it |
153 | 154 | log.log(Level.FINE, "JCS - Returning object from cache: {0}", getCacheKey()); |
154 | | finishLoading(true); |
| 155 | finishLoading(LoadResult.SUCCESS); |
155 | 156 | return; |
156 | 157 | } |
157 | 158 | // object not in cache, so submit work to separate thread |
… |
… |
public abstract class JCSCachedTileLoaderJob<K, V extends CacheEntry> implements |
161 | 162 | } catch (RejectedExecutionException e) { |
162 | 163 | // queue was full, try again later |
163 | 164 | log.log(Level.FINE, "JCS - rejected job for: {0}", getCacheKey()); |
164 | | finishLoading(false); |
| 165 | finishLoading(LoadResult.REJECTED); |
165 | 166 | } |
166 | 167 | } |
167 | 168 | } |
… |
… |
public abstract class JCSCachedTileLoaderJob<K, V extends CacheEntry> implements |
205 | 206 | try { |
206 | 207 | // try to load object from remote resource |
207 | 208 | if (loadObject()) { |
208 | | finishLoading(true); |
| 209 | finishLoading(LoadResult.SUCCESS); |
209 | 210 | } else { |
210 | 211 | // if loading failed - check if we can return stale entry |
211 | 212 | if (isObjectLoadable()) { |
212 | 213 | // try to get stale entry in cache |
213 | | finishLoading(true); |
| 214 | finishLoading(LoadResult.SUCCESS); |
214 | 215 | log.log(Level.FINE, "JCS - found stale object in cache: {0}", getUrl()); |
215 | 216 | } else { |
216 | 217 | // failed completely |
217 | | finishLoading(false); |
| 218 | finishLoading(LoadResult.FAILURE); |
218 | 219 | } |
219 | 220 | } |
220 | 221 | } finally { |
… |
… |
public abstract class JCSCachedTileLoaderJob<K, V extends CacheEntry> implements |
223 | 224 | } |
224 | 225 | |
225 | 226 | |
226 | | private void finishLoading(boolean success) { |
| 227 | private void finishLoading(LoadResult result) { |
227 | 228 | Set<ICachedLoaderListener> listeners = null; |
228 | 229 | synchronized (inProgress) { |
229 | 230 | listeners = inProgress.remove(getUrl().toString()); |
… |
… |
public abstract class JCSCachedTileLoaderJob<K, V extends CacheEntry> implements |
234 | 235 | } |
235 | 236 | try { |
236 | 237 | for (ICachedLoaderListener l: listeners) { |
237 | | l.loadingFinished(cacheData, success); |
| 238 | l.loadingFinished(cacheData, result); |
238 | 239 | } |
239 | 240 | } catch (Exception e) { |
240 | 241 | log.log(Level.WARNING, "JCS - Error while loading object from cache: {0}; {1}", new Object[]{e.getMessage(), getUrl()}); |
241 | 242 | log.log(Level.FINE, "Stacktrace", e); |
242 | 243 | for (ICachedLoaderListener l: listeners) { |
243 | | l.loadingFinished(cacheData, false); |
| 244 | l.loadingFinished(cacheData, LoadResult.FAILURE); |
244 | 245 | } |
245 | 246 | |
246 | 247 | } |
diff --git src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJob.java src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJob.java
index cb7754f..44e8467 100644
|
|
public class TMSCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, Buffe |
132 | 132 | return false; |
133 | 133 | } |
134 | 134 | |
| 135 | private boolean isNoTileAtZoom() { |
| 136 | return attributes != null && attributes.isNoTileAtZoom(); |
| 137 | } |
| 138 | |
135 | 139 | @Override |
136 | 140 | protected boolean cacheAsEmpty() { |
137 | | if (attributes != null && attributes.isNoTileAtZoom()) { |
138 | | // do not remove file - keep the information, that there is no tile, for further requests |
139 | | // the code above will check, if this information is still valid |
| 141 | return isNoTileAtZoom(); |
| 142 | } |
| 143 | |
| 144 | private boolean handleNoTileAtZoom() { |
| 145 | if (isNoTileAtZoom()) { |
140 | 146 | log.log(Level.FINE, "JCS TMS - Tile valid, but no file, as no tiles at this level {0}", tile); |
141 | 147 | tile.setError("No tile at this zoom level"); |
142 | 148 | tile.putValue("tile-info", "no-tile"); |
143 | 149 | return true; |
144 | 150 | } |
145 | | return false; // as there is no other cache to cache the Tile, also cache other empty requests |
| 151 | return false; |
146 | 152 | } |
147 | 153 | |
148 | 154 | @Override |
… |
… |
public class TMSCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, Buffe |
156 | 162 | } |
157 | 163 | |
158 | 164 | @Override |
159 | | public void loadingFinished(CacheEntry object, boolean success) { |
| 165 | public void loadingFinished(CacheEntry object, LoadResult result) { |
160 | 166 | try { |
161 | | loadTile(object, success); |
| 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 | } |
162 | 182 | if (listener != null) { |
163 | | listener.tileLoadingFinished(tile, success); |
| 183 | listener.tileLoadingFinished(tile, result.equals(LoadResult.SUCCESS)); |
164 | 184 | } |
165 | 185 | } catch (IOException e) { |
166 | 186 | log.log(Level.WARNING, "JCS TMS - error loading object for tile {0}: {1}", new Object[] {tile.getKey(), e.getMessage()}); |
… |
… |
public class TMSCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, Buffe |
180 | 200 | BufferedImageCacheEntry data = get(); |
181 | 201 | if (isObjectLoadable()) { |
182 | 202 | try { |
183 | | loadTile(data); |
| 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 | } |
184 | 211 | return tile; |
185 | 212 | } catch (IOException e) { |
186 | 213 | log.log(Level.WARNING, "JCS TMS - error loading object for tile {0}: {1}", new Object[] {tile.getKey(), e.getMessage()}); |
… |
… |
public class TMSCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, Buffe |
188 | 215 | } |
189 | 216 | |
190 | 217 | } else { |
191 | | return null; |
192 | | } |
193 | | } |
194 | | |
195 | | // loads tile when calling back from cache |
196 | | private void loadTile(CacheEntry object, boolean success) throws IOException { |
197 | | tile.finishLoading(); |
198 | | if (object != null) { |
199 | | byte[] content = object.getContent(); |
200 | | if (content != null && content.length > 0) { |
201 | | tile.loadImage(new ByteArrayInputStream(content)); |
202 | | } |
203 | | } |
204 | | if (!success) { |
205 | | tile.setError("Problem loading tile"); |
206 | | } |
207 | | } |
208 | | |
209 | | // loads tile when geting stright from cache |
210 | | private void loadTile(BufferedImageCacheEntry object) throws IOException { |
211 | | tile.finishLoading(); |
212 | | if (cacheAsEmpty() || object != null) { // if cache as empty object, do not try to load image |
213 | | if (object.getImage() != null) { |
214 | | tile.setImage(object.getImage()); |
215 | | } |
| 218 | return tile; |
216 | 219 | } |
217 | 220 | } |
218 | 221 | |