Changeset 30849 in osm for applications/viewer/jmapviewer/src
- Timestamp:
- 2014-12-14T23:20:23+01:00 (10 years ago)
- Location:
- applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.java
r30847 r30849 43 43 public class OsmFileCacheTileLoader extends OsmTileLoader implements CachedTileLoader { 44 44 45 private static final Logger log = Logger.getLogger(OsmFileCacheTileLoader.class.getName()); 46 47 private static final String ETAG_FILE_EXT = ".etag"; 45 private static final Logger log = FeatureAdapter.getLogger(OsmFileCacheTileLoader.class.getName()); 46 48 47 private static final String TAGS_FILE_EXT = ".tags"; 49 48 … … 57 56 protected final Map<TileSource, File> sourceCacheDirMap; 58 57 59 protected long maxCacheFileAge = FILE_AGE_ONE_WEEK;60 protected long recheckAfter = FILE_AGE_ONE_ DAY;58 protected long maxCacheFileAge = Long.MAX_VALUE; // max. age not limited 59 protected long recheckAfter = FILE_AGE_ONE_WEEK; 61 60 62 61 public static File getDefaultCacheDir() throws SecurityException { … … 134 133 File tileCacheDir; 135 134 File tileFile = null; 136 long fileAge = 0; 137 boolean fileTilePainted = false; 135 Long fileAge = null; 138 136 139 137 public FileLoadJob(Tile tile) { … … 156 154 } 157 155 tileCacheDir = getSourceCacheDir(tile.getSource()); 158 if (loadTileFromFile()) { 156 157 if (loadTileFromFile(recheckAfter)) { 158 log.log(Level.FINEST, "TMS - found in tile cache: {0}", tile); 159 tile.setLoaded(true); 160 listener.tileLoadingFinished(tile, true); 159 161 return; 160 162 } 161 if (fileTilePainted) { 162 TileJob job = new TileJob() { 163 164 @Override 165 public void run() { 166 loadOrUpdateTile(); 167 } 168 @Override 169 public Tile getTile() { 170 return tile; 171 } 172 }; 173 JobDispatcher.getInstance().addJob(job); 174 } else { 175 loadOrUpdateTile(); 176 } 177 } 178 179 protected void loadOrUpdateTile() { 163 TileJob job = new TileJob() { 164 165 @Override 166 public void run() { 167 if (loadOrUpdateTile()) { 168 tile.setLoaded(true); 169 listener.tileLoadingFinished(tile, true); 170 } else { 171 // failed to download - use old cache file if available 172 if (loadTileFromFile(maxCacheFileAge)) { 173 tile.setLoaded(true); 174 tile.error = false; 175 listener.tileLoadingFinished(tile, true); 176 log.log(Level.FINEST, "TMS - found stale tile in cache: {0}", tile); 177 } else { 178 // failed completely 179 tile.setLoaded(true); 180 listener.tileLoadingFinished(tile, false); 181 } 182 } 183 } 184 @Override 185 public Tile getTile() { 186 return tile; 187 } 188 }; 189 JobDispatcher.getInstance().addJob(job); 190 } 191 192 protected boolean loadOrUpdateTile() { 180 193 try { 181 194 URLConnection urlConn = loadTileFromOsm(tile); 182 if ( tileFile != null) {195 if (fileAge != null) { 183 196 switch (tile.getSource().getTileUpdate()) { 184 197 case IfModifiedSince: … … 187 200 case LastModified: 188 201 if (!isOsmTileNewer(fileAge)) { 189 log.finest("LastModified test: local version is up to date: " + tile); 190 tile.setLoaded(true); 191 tileFile.setLastModified(System.currentTimeMillis() - maxCacheFileAge + recheckAfter); 192 return; 202 log.log(Level.FINEST, "TMS - LastModified test: local version is up to date: {0}", tile); 203 tileFile.setLastModified(System.currentTimeMillis()); 204 return true; 193 205 } 194 206 break; … … 204 216 case ETag: 205 217 if (hasOsmTileETag(fileETag)) { 206 tile.setLoaded(true); 207 tileFile.setLastModified(System.currentTimeMillis() - maxCacheFileAge 208 + recheckAfter); 209 return; 218 log.log(Level.FINEST, "TMS - ETag test: local version is up to date: {0}", tile); 219 tileFile.setLastModified(System.currentTimeMillis()); 220 return true; 210 221 } 211 222 } … … 216 227 // If we are isModifiedSince or If-None-Match has been set 217 228 // and the server answers with a HTTP 304 = "Not Modified" 218 log.finest("ETag test: local version is up to date: " + tile); 219 tile.setLoaded(true); 220 tileFile.setLastModified(System.currentTimeMillis() - maxCacheFileAge + recheckAfter); 221 return; 229 switch (tile.getSource().getTileUpdate()) { 230 case IfModifiedSince: 231 log.log(Level.FINEST, "TMS - IfModifiedSince test: local version is up to date: {0}", tile); 232 break; 233 case IfNoneMatch: 234 log.log(Level.FINEST, "TMS - IfNoneMatch test: local version is up to date: {0}", tile); 235 break; 236 } 237 if (loadTileFromFile(maxCacheFileAge)) { 238 tileFile.setLastModified(System.currentTimeMillis()); 239 return true; 240 } 222 241 } 223 242 … … 227 246 if ("no-tile".equals(tile.getValue("tile-info"))) 228 247 { 248 log.log(Level.FINEST, "TMS - No tile: tile-info=no-tile: {0}", tile); 229 249 tile.setError("No tile at this zoom level"); 230 listener.tileLoadingFinished(tile, true);250 return true; 231 251 } else { 232 for (int i = 0; i < 5; ++i) {252 for (int i = 0; i < 5; ++i) { 233 253 if (urlConn instanceof HttpURLConnection && ((HttpURLConnection)urlConn).getResponseCode() == 503) { 234 254 Thread.sleep(5000+(new Random()).nextInt(5000)); … … 238 258 if (buffer != null) { 239 259 tile.loadImage(new ByteArrayInputStream(buffer)); 240 tile.setLoaded(true);241 listener.tileLoadingFinished(tile, true);242 260 saveTileToFile(buffer); 243 break; 261 log.log(Level.FINEST, "TMS - downloaded tile from server: {0}", tile.getUrl()); 262 return true; 244 263 } 245 264 } … … 247 266 } catch (Exception e) { 248 267 tile.setError(e.getMessage()); 249 listener.tileLoadingFinished(tile, false);250 268 if (input == null) { 251 269 try { 252 System.err.println("Failed loading " + tile.getUrl() +": " + e.getMessage()); 270 log.log(Level.WARNING, "TMS - Failed downloading {0}: {1}", new Object[]{tile.getUrl(), e.getMessage()}); 271 return false; 253 272 } catch(IOException i) { 254 273 } 255 274 } 256 } finally { 257 tile.loading = false; 258 tile.setLoaded(true); 259 } 260 } 261 262 protected boolean loadTileFromFile() { 275 } 276 log.log(Level.WARNING, "TMS - Failed downloading tile: {0}", tile); 277 return false; 278 } 279 280 protected boolean loadTileFromFile(long maxAge) { 263 281 try { 264 282 tileFile = getTileFile(); 265 283 if (!tileFile.exists()) 266 284 return false; 267 268 285 loadTagsFromFile(); 286 287 fileAge = tileFile.lastModified(); 288 if (System.currentTimeMillis() - fileAge > maxAge) 289 return false; 290 269 291 if ("no-tile".equals(tile.getValue("tile-info"))) { 270 292 tile.setError("No tile at this zoom level"); … … 272 294 tileFile.delete(); 273 295 } 274 tileFile = getTagsFile();296 tileFile = null; 275 297 } else { 276 298 try (FileInputStream fin = new FileInputStream(tileFile)) { … … 280 302 } 281 303 } 282 283 fileAge = tileFile.lastModified(); 284 boolean oldTile = System.currentTimeMillis() - fileAge > maxCacheFileAge; 285 if (!oldTile) { 286 tile.setLoaded(true); 287 listener.tileLoadingFinished(tile, true); 288 fileTilePainted = true; 289 return true; 290 } 291 listener.tileLoadingFinished(tile, true); 292 fileTilePainted = true; 304 return true; 305 293 306 } catch (Exception e) { 307 log.log(Level.WARNING, "TMS - Error while loading image from tile cache: {0}; {1}", new Object[]{e.getMessage(), tile}); 294 308 tileFile.delete(); 295 309 tileFile = null; 296 fileAge = 0;310 fileAge = null; 297 311 } 298 312 return false; -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/TMSTileSource.java
r30223 r30849 28 28 } 29 29 30 @Override 30 31 public TileUpdate getTileUpdate() { 31 32 return TileUpdate.IfNoneMatch;
Note:
See TracChangeset
for help on using the changeset viewer.