Changeset 8602 in josm
- Timestamp:
- 2015-07-14T21:58:01+02:00 (9 years ago)
- Location:
- trunk
- Files:
-
- 75 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java
r8568 r8602 14 14 import java.util.concurrent.ConcurrentHashMap; 15 15 import java.util.concurrent.ConcurrentMap; 16 import java.util.concurrent.Executors;17 16 import java.util.concurrent.LinkedBlockingDeque; 18 import java.util.concurrent.ThreadFactory;19 17 import java.util.concurrent.ThreadPoolExecutor; 20 18 import java.util.concurrent.TimeUnit; … … 79 77 // make queue of LIFO type - so recently requested tiles will be loaded first (assuming that these are which user is waiting to see) 80 78 new LinkedBlockingDeque<Runnable>(), 81 getNamedThreadFactory("JCS downloader")79 Utils.getNamedThreadFactory("JCS downloader") 82 80 ); 83 81 84 public static ThreadFactory getNamedThreadFactory(final String name) { 85 return new ThreadFactory() { 86 @Override 87 public Thread newThread(Runnable r) { 88 Thread t = Executors.defaultThreadFactory().newThread(r); 89 t.setName(name); 90 return t; 91 } 92 }; 93 } 82 94 83 95 84 private static ConcurrentMap<String, Set<ICachedLoaderListener>> inProgress = new ConcurrentHashMap<>(); … … 312 301 } 313 302 314 URLConnection urlConn = getURLConnection();303 HttpURLConnection urlConn = getURLConnection(); 315 304 316 305 if (isObjectLoadable() && … … 321 310 urlConn.addRequestProperty("If-None-Match", attributes.getEtag()); 322 311 } 323 if ( responseCode(urlConn) == 304) {312 if (urlConn.getResponseCode() == 304) { 324 313 // If isModifiedSince or If-None-Match has been set 325 314 // and the server answers with a HTTP 304 = "Not Modified" 326 315 log.log(Level.FINE, "JCS - IfModifiedSince/Etag test: local version is up to date: {0}", getUrl()); 327 316 return true; 328 } else if (isObjectLoadable() ) {329 // we have an object in cache, but we haven't received 304 resposne code330 // check if we should use HEAD request to verify331 if ((attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||332 attributes.getLastModification() == urlConn.getLastModified()) {333 334 335 336 337 338 339 340 } 317 } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 resposne code 318 && ( 319 (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) || 320 attributes.getLastModification() == urlConn.getLastModified()) 321 ) { 322 // we sent ETag or If-Modified-Since, but didn't get 304 response code 323 // for further requests - use HEAD 324 String serverKey = getServerKey(); 325 log.log(Level.INFO, "JCS - Host: {0} found not to return 304 codes for If-Modifed-Since or If-None-Match headers", 326 serverKey); 327 useHead.put(serverKey, Boolean.TRUE); 328 } 329 341 330 342 331 attributes = parseHeaders(urlConn); 343 332 344 333 for (int i = 0; i < 5; ++i) { 345 if ( responseCode(urlConn) == 503) {334 if (urlConn.getResponseCode() == 503) { 346 335 Thread.sleep(5000+(new Random()).nextInt(5000)); 347 336 continue; 348 337 } 349 338 350 attributes.setResponseCode( responseCode(urlConn));339 attributes.setResponseCode(urlConn.getResponseCode()); 351 340 byte[] raw = Utils.readBytesFromStream(urlConn.getInputStream()); 352 341 353 if (isResponseLoadable(urlConn.getHeaderFields(), responseCode(urlConn), raw)) {342 if (isResponseLoadable(urlConn.getHeaderFields(), urlConn.getResponseCode(), raw)) { 354 343 // we need to check cacheEmpty, so for cases, when data is returned, but we want to store 355 344 // as empty (eg. empty tile images) to save some space … … 446 435 } 447 436 448 private URLConnection getURLConnection() throws IOException {449 URLConnection urlConn =getUrl().openConnection();437 private HttpURLConnection getURLConnection() throws IOException { 438 HttpURLConnection urlConn = (HttpURLConnection) getUrl().openConnection(); 450 439 urlConn.setRequestProperty("Accept", "text/html, image/png, image/jpeg, image/gif, */*"); 451 440 urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout … … 461 450 462 451 private boolean isCacheValidUsingHead() throws IOException { 463 URLConnection urlConn = getUrl().openConnection(); 464 if (urlConn instanceof HttpURLConnection) { 465 ((HttpURLConnection) urlConn).setRequestMethod("HEAD"); 466 long lastModified = urlConn.getLastModified(); 467 return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) || 468 (lastModified != 0 && lastModified <= attributes.getLastModification()); 469 } 470 // for other URL connections, do not use HEAD requests for cache validation 471 return false; 452 HttpURLConnection urlConn = getURLConnection(); 453 urlConn.setRequestMethod("HEAD"); 454 long lastModified = urlConn.getLastModified(); 455 return (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) || 456 (lastModified != 0 && lastModified <= attributes.getLastModification()); 472 457 } 473 458 … … 499 484 finishLoading(LoadResult.CANCELED); 500 485 } 501 502 /*503 * Temporary fix for file URLs. Returns response code for HttpURLConnections or 200 for all other504 */505 private int responseCode(URLConnection urlConn) throws IOException {506 if (urlConn instanceof HttpURLConnection) {507 return ((HttpURLConnection) urlConn).getResponseCode();508 } else {509 return 200;510 }511 }512 486 } -
trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoader.java
r8598 r8602 17 17 import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry; 18 18 import org.openstreetmap.josm.data.cache.HostLimitQueue; 19 import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob;20 19 import org.openstreetmap.josm.data.preferences.IntegerProperty; 20 import org.openstreetmap.josm.tools.Utils; 21 21 22 22 /** … … 85 85 TimeUnit.SECONDS, 86 86 new HostLimitQueue(HOST_LIMIT.get().intValue()), 87 JCSCachedTileLoaderJob.getNamedThreadFactory(name)87 Utils.getNamedThreadFactory(name) 88 88 ); 89 89 } -
trunk/src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java
r8601 r8602 20 20 import java.io.File; 21 21 import java.io.IOException; 22 import java.net.MalformedURLException; 23 import java.net.URL; 22 24 import java.text.SimpleDateFormat; 23 25 import java.util.ArrayList; … … 56 58 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener; 57 59 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 60 import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource; 58 61 import org.openstreetmap.josm.Main; 59 62 import org.openstreetmap.josm.actions.RenameLayerAction; … … 129 132 130 133 protected TileCache tileCache; 131 protected TileSource tileSource;134 protected AbstractTMSTileSource tileSource; 132 135 protected TileLoader tileLoader; 133 136 … … 151 154 * @throws IllegalArgumentException when Imagery is not supported by layer 152 155 */ 153 protected abstract TileSource getTileSource(ImageryInfo info) throws IllegalArgumentException;156 protected abstract AbstractTMSTileSource getTileSource(ImageryInfo info) throws IllegalArgumentException; 154 157 155 158 protected Map<String, String> getHeaders(TileSource tileSource) { … … 160 163 } 161 164 162 protected void initTileSource( TileSource tileSource) {165 protected void initTileSource(AbstractTMSTileSource tileSource) { 163 166 attribution.initialize(tileSource); 164 167 … … 173 176 tileCache = new MemoryTileCache(); 174 177 } 178 179 try { 180 if ("file".equalsIgnoreCase(new URL(tileSource.getBaseUrl()).getProtocol())) { 181 tileLoader = new OsmTileLoader(this); 182 tileCache = new MemoryTileCache(); 183 } 184 } catch (MalformedURLException e) { 185 // ignore, assume that this is not a file 186 } 187 188 175 189 if (tileLoader == null) 176 190 tileLoader = new OsmTileLoader(this); -
trunk/src/org/openstreetmap/josm/gui/layer/TMSLayer.java
r8598 r8602 6 6 import org.apache.commons.jcs.access.CacheAccess; 7 7 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; 8 import org.openstreetmap.gui.jmapviewer. interfaces.TileSource;8 import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource; 9 9 import org.openstreetmap.gui.jmapviewer.tilesources.ScanexTileSource; 10 10 import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource; … … 66 66 */ 67 67 @Override 68 protected TileSource getTileSource(ImageryInfo info) throws IllegalArgumentException {68 protected AbstractTMSTileSource getTileSource(ImageryInfo info) throws IllegalArgumentException { 69 69 return getTileSourceStatic(info); 70 70 } … … 96 96 * @throws IllegalArgumentException if url from imagery info is null or invalid 97 97 */ 98 public static TileSource getTileSourceStatic(ImageryInfo info) throws IllegalArgumentException {98 public static AbstractTMSTileSource getTileSourceStatic(ImageryInfo info) throws IllegalArgumentException { 99 99 if (info.getImageryType() == ImageryType.TMS) { 100 100 TemplatedTMSTileSource.checkUrl(info.getUrl()); -
trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java
r8598 r8602 18 18 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; 19 19 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 20 import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource; 20 21 import org.openstreetmap.josm.Main; 21 22 import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry; … … 73 74 74 75 @Override 75 protected TileSource getTileSource(ImageryInfo info) {76 protected AbstractTMSTileSource getTileSource(ImageryInfo info) { 76 77 if (info.getImageryType() == ImageryType.WMS && info.getUrl() != null) { 77 78 TemplatedWMSTileSource.checkUrl(info.getUrl()); -
trunk/src/org/openstreetmap/josm/gui/layer/WMTSLayer.java
r8598 r8602 8 8 import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate; 9 9 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; 10 import org.openstreetmap.gui.jmapviewer. interfaces.TileSource;10 import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource; 11 11 import org.openstreetmap.josm.Main; 12 12 import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry; … … 48 48 49 49 @Override 50 protected TileSource getTileSource(ImageryInfo info) {50 protected AbstractTMSTileSource getTileSource(ImageryInfo info) { 51 51 try { 52 52 if (info.getImageryType() == ImageryType.WMTS && info.getUrl() != null) { -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r8574 r8602 46 46 import java.util.concurrent.ExecutorService; 47 47 import java.util.concurrent.Executors; 48 import java.util.concurrent.ThreadFactory; 48 49 import java.util.regex.Matcher; 49 50 import java.util.regex.Pattern; … … 1370 1371 * Returns the initial capacity to pass to the HashMap / HashSet constructor 1371 1372 * when it is initialized with a known number of entries. 1372 * 1373 * 1373 1374 * When a HashMap is filled with entries, the underlying array is copied over 1374 1375 * to a larger one multiple times. To avoid this process when the number of … … 1387 1388 * Returns the initial capacity to pass to the HashMap / HashSet constructor 1388 1389 * when it is initialized with a known number of entries. 1389 * 1390 * 1390 1391 * When a HashMap is filled with entries, the underlying array is copied over 1391 1392 * to a larger one multiple times. To avoid this process when the number of … … 1393 1394 * given to the HashMap constructor. This method returns a suitable value 1394 1395 * that avoids rehashing but doesn't waste memory. 1395 * 1396 * 1396 1397 * Assumes default load factor (0.75). 1397 1398 * @param nEntries the number of entries expected … … 1401 1402 return hashMapInitialCapacity(nEntries, 0.75f); 1402 1403 } 1404 1405 /** 1406 * @param name to be set for the threads 1407 * @return Thread Factory returning named threads 1408 */ 1409 public static ThreadFactory getNamedThreadFactory(final String name) { 1410 return new ThreadFactory() { 1411 @Override 1412 public Thread newThread(Runnable r) { 1413 Thread t = Executors.defaultThreadFactory().newThread(r); 1414 t.setName(name); 1415 return t; 1416 } 1417 }; 1418 } 1403 1419 }
Note:
See TracChangeset
for help on using the changeset viewer.