Class JCSCachedTileLoaderJob<K,​V extends CacheEntry>

  • Type Parameters:
    K - cache entry key type
    V - cache value type
    All Implemented Interfaces:
    java.lang.Runnable, ICachedLoaderJob<K>
    Direct Known Subclasses:
    TMSCachedTileLoaderJob

    public abstract class JCSCachedTileLoaderJob<K,​V extends CacheEntry>
    extends java.lang.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
    • Constructor Detail

      • JCSCachedTileLoaderJob

        protected JCSCachedTileLoaderJob​(org.apache.commons.jcs3.access.behavior.ICacheAccess<K,​V> cache,
                                         TileJobOptions options,
                                         java.util.concurrent.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 Detail

      • 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 java.io.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:
        java.io.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​(java.util.Map<java.lang.String,​java.util.List<java.lang.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 java.lang.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 java.lang.Runnable
      • 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​(java.net.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

        public java.lang.String detectErrorMessage​(java.lang.String data)
        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​(java.util.Map<java.lang.String,​java.util.List<java.lang.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
      • getRequest

        private HttpClient getRequest​(java.lang.String requestMethod)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • isCacheValidUsingHead

        private boolean isCacheValidUsingHead()
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • cancelOutstandingTasks

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

        public void setFinishedTask​(java.lang.Runnable runnable)
        Sets a job, that will be run, when job will finish execution
        Parameters:
        runnable - that will be executed