Class JCSCachedTileLoaderJob<K,V extends CacheEntry>

java.lang.Object
org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob<K,V>
Type Parameters:
K - cache entry key type
V - cache value type
All Implemented Interfaces:
Runnable, ICachedLoaderJob<K>
Direct Known Subclasses:
TMSCachedTileLoaderJob

public abstract class JCSCachedTileLoaderJob<K,V extends CacheEntry> extends Object implements ICachedLoaderJob<K>
Generic loader for HTTP based tiles. Uses custom attribute, to check, if entry has expired according to HTTP headers sent with tile. If so, it tries to verify using Etags or If-Modified-Since / Last-Modified. If the tile is not valid, it will try to download it from remote service and put it to cache. If remote server will fail it will try to use stale entry. This class will keep only one Job running for specified tile. All others will just finish, but listeners will be gathered and notified, once download job will be finished
Since:
8168
  • Field Details

  • Constructor Details

    • JCSCachedTileLoaderJob

      protected JCSCachedTileLoaderJob(org.apache.commons.jcs3.access.behavior.ICacheAccess<K,V> cache, TileJobOptions options, ThreadPoolExecutor downloadJobExecutor)
      Parameters:
      cache - cache instance that we will work on
      options - options of the request
      downloadJobExecutor - that will be executing the jobs
    • JCSCachedTileLoaderJob

      protected JCSCachedTileLoaderJob(org.apache.commons.jcs3.access.behavior.ICacheAccess<K,V> cache, TileJobOptions options)
      Parameters:
      cache - cache instance that we will work on
      options - of the request
  • Method Details

    • ensureCacheElement

      private void ensureCacheElement()
    • get

      public V get()
      Description copied from interface: ICachedLoaderJob
      fetches object from cache, or returns null when object is not found
      Specified by:
      get in interface ICachedLoaderJob<K>
      Returns:
      filled tile with data or null when no cache entry found
    • submit

      public void submit(ICachedLoaderListener listener, boolean force) throws IOException
      Description copied from interface: ICachedLoaderJob
      Submit job for background fetch, and listener will be fed with value object
      Specified by:
      submit in interface ICachedLoaderJob<K>
      Parameters:
      listener - cache loader listener
      force - true if the load should skip all the caches (local & remote)
      Throws:
      IOException - on failure from getUrl() call
    • executionFinished

      protected void executionFinished()
      This method is run when job has finished
    • isObjectLoadable

      protected boolean isObjectLoadable()
      Checks if object from cache has sufficient data to be returned.
      Returns:
      true if object from cache has sufficient data to be returned
    • cacheAsEmpty

      protected boolean cacheAsEmpty(Map<String,List<String>> headerFields, int responseCode)
      Simple implementation. All errors should be cached as empty. Though some JDK (JDK8 on Windows for example) doesn't return 4xx error codes, instead they do throw an FileNotFoundException or IOException
      Parameters:
      headerFields - headers sent by server
      responseCode - http status code
      Returns:
      true if we should put empty object into cache, regardless of what remote resource has returned
    • getServerKey

      protected String getServerKey()
      Returns key under which discovered server settings will be kept.
      Returns:
      key under which discovered server settings will be kept
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • finishLoading

    • isCacheElementValid

      protected boolean isCacheElementValid()
    • loadObject

      protected boolean loadObject()
      Load an cache object
      Returns:
      true if object was successfully downloaded, false, if there was a loading failure
      Since:
      18831
    • loadObjectFile

      private boolean loadObjectFile(URL url)
    • loadObjectHttp

      private boolean loadObjectHttp()
      Load an cache object via HTTP
      Returns:
      true if object was successfully downloaded via http, false, if there was a loading failure
    • detectErrorMessage

      Tries do detect an error message from given string.
      Parameters:
      data - string to analyze
      Returns:
      error message if detected, or null
      Since:
      14535
    • isResponseLoadable

      protected boolean isResponseLoadable(Map<String,List<String>> headerFields, int responseCode, byte[] raw)
      Check if the object is loadable. This means, if the data will be parsed, and if this response will finish as successful retrieve.

      This simple implementation doesn't load empty response, nor client (4xx) and server (5xx) errors

      Parameters:
      headerFields - headers sent by server
      responseCode - http status code
      raw - data read from server
      Returns:
      true if object should be cached and returned to listener
    • createCacheEntry

      protected abstract V createCacheEntry(byte[] content)
    • parseHeaders

    • getRequest

      private HttpClient getRequest(String requestMethod) throws IOException
      Throws:
      IOException
    • isCacheValidUsingHead

      private boolean isCacheValidUsingHead() throws IOException
      Throws:
      IOException
    • updateAttributes

      private void updateAttributes(HttpClient.Response urlConn)
      Updates the cache attributes after the server confirmed that the cached object is still valid.

      The "no tile at this zoom level" marker is kept: it was found from the response which delivered the object, and a confirmation does not repeat that. Without it, an empty no-tile entry would look like a loaded tile without an image (see #24893).

      Parameters:
      urlConn - the response which confirmed the cached object
    • cancelOutstandingTasks

      public void cancelOutstandingTasks()
      TODO: move to JobFactory cancels all outstanding tasks in the queue.
    • setFinishedTask

      public void setFinishedTask(Runnable runnable)
      Sets a job, that will be run, when job will finish execution
      Parameters:
      runnable - that will be executed
    • handleJobCancellation

      public void handleJobCancellation()
      Marks this job as canceled
    • getUrlNoException