Class JCSCachedTileLoaderJob<K,V extends CacheEntry>
java.lang.Object
org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob<K,V>
- Type Parameters:
K- cache entry key typeV- 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 Summary
FieldsModifier and TypeFieldDescriptionprotected static final longprotected CacheEntryAttributesprotected Vprivate final intprivate static final ThreadPoolExecutorprotected static final longprivate final ThreadPoolExecutorprotected static final longprivate Runnableprivate booleanprivate static final ConcurrentMap<String,Set<ICachedLoaderListener>> private final longprotected final longprivate final intstatic final IntegerPropertymaximum download threads that will be startedprivate static final ConcurrentMap<String,Boolean> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedJCSCachedTileLoaderJob(org.apache.commons.jcs3.access.behavior.ICacheAccess<K, V> cache, TileJobOptions options) protectedJCSCachedTileLoaderJob(org.apache.commons.jcs3.access.behavior.ICacheAccess<K, V> cache, TileJobOptions options, ThreadPoolExecutor downloadJobExecutor) -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancacheAsEmpty(Map<String, List<String>> headerFields, int responseCode) Simple implementation.voidTODO: move to JobFactory cancels all outstanding tasks in the queue.protected abstract VcreateCacheEntry(byte[] content) detectErrorMessage(String data) Tries do detect an error message from given string.private voidprotected voidThis method is run when job has finishedprivate voidget()fetches object from cache, or returns null when object is not foundprivate HttpClientgetRequest(String requestMethod) protected StringReturns key under which discovered server settings will be kept.private URLvoidMarks this job as canceledprotected booleanprivate booleanprotected booleanChecks if object from cache has sufficient data to be returned.protected booleanisResponseLoadable(Map<String, List<String>> headerFields, int responseCode, byte[] raw) Check if the object is loadable.protected booleanLoad an cache objectprivate booleanloadObjectFile(URL url) private booleanLoad an cache object via HTTPprotected CacheEntryAttributesparseHeaders(HttpClient.Response urlConn) voidrun()voidsetFinishedTask(Runnable runnable) Sets a job, that will be run, when job will finish executionvoidsubmit(ICachedLoaderListener listener, boolean force) Submit job for background fetch, and listener will be fed with value objectprivate voidupdateAttributes(HttpClient.Response urlConn) Updates the cache attributes after the server confirmed that the cached object is still valid.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.openstreetmap.josm.data.cache.ICachedLoaderJob
getCacheKey, getUrl
-
Field Details
-
DEFAULT_EXPIRE_TIME
-
EXPIRE_TIME_SERVER_LIMIT
-
ABSOLUTE_EXPIRE_TIME_LIMIT
-
THREAD_LIMIT
maximum download threads that will be started -
DEFAULT_DOWNLOAD_JOB_DISPATCHER
-
inProgress
-
useHead
-
now
-
cache
-
cacheElement
-
cacheData
-
attributes
-
connectTimeout
-
readTimeout
-
headers
-
downloadJobExecutor
-
finishTask
-
force
-
minimumExpiryTime
-
-
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 onoptions- options of the requestdownloadJobExecutor- 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 onoptions- of the request
-
-
Method Details
-
ensureCacheElement
-
get
Description copied from interface:ICachedLoaderJobfetches object from cache, or returns null when object is not found- Specified by:
getin interfaceICachedLoaderJob<K>- Returns:
- filled tile with data or null when no cache entry found
-
submit
Description copied from interface:ICachedLoaderJobSubmit job for background fetch, and listener will be fed with value object- Specified by:
submitin interfaceICachedLoaderJob<K>- Parameters:
listener- cache loader listenerforce- true if the load should skip all the caches (local & remote)- Throws:
IOException- on failure from getUrl() call
-
executionFinished
This method is run when job has finished -
isObjectLoadable
Checks if object from cache has sufficient data to be returned.- Returns:
trueif object from cache has sufficient data to be returned
-
cacheAsEmpty
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 serverresponseCode- http status code- Returns:
- true if we should put empty object into cache, regardless of what remote resource has returned
-
getServerKey
Returns key under which discovered server settings will be kept.- Returns:
- key under which discovered server settings will be kept
-
run
-
finishLoading
-
isCacheElementValid
-
loadObject
Load an cache object- Returns:
trueif object was successfully downloaded, false, if there was a loading failure- Since:
- 18831
-
loadObjectFile
-
loadObjectHttp
Load an cache object via HTTP- Returns:
trueif 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 serverresponseCode- http status coderaw- data read from server- Returns:
- true if object should be cached and returned to listener
-
createCacheEntry
-
parseHeaders
-
getRequest
- Throws:
IOException
-
isCacheValidUsingHead
- Throws:
IOException
-
updateAttributes
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
TODO: move to JobFactory cancels all outstanding tasks in the queue. -
setFinishedTask
Sets a job, that will be run, when job will finish execution- Parameters:
runnable- that will be executed
-
handleJobCancellation
Marks this job as canceled -
getUrlNoException
-