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:
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
-
-
Field Summary
Fields Modifier and Type Field Description protected static long
ABSOLUTE_EXPIRE_TIME_LIMIT
protected CacheEntryAttributes
attributes
protected org.apache.commons.jcs3.access.behavior.ICacheAccess<K,V>
cache
protected V
cacheData
private org.apache.commons.jcs3.engine.behavior.ICacheElement<K,V>
cacheElement
private int
connectTimeout
private static java.util.concurrent.ThreadPoolExecutor
DEFAULT_DOWNLOAD_JOB_DISPATCHER
protected static long
DEFAULT_EXPIRE_TIME
private java.util.concurrent.ThreadPoolExecutor
downloadJobExecutor
protected static long
EXPIRE_TIME_SERVER_LIMIT
private java.lang.Runnable
finishTask
private boolean
force
private java.util.Map<java.lang.String,java.lang.String>
headers
private static java.util.concurrent.ConcurrentMap<java.lang.String,java.util.Set<ICachedLoaderListener>>
inProgress
private long
minimumExpiryTime
protected long
now
private int
readTimeout
static IntegerProperty
THREAD_LIMIT
maximum download threads that will be startedprivate static java.util.concurrent.ConcurrentMap<java.lang.String,java.lang.Boolean>
useHead
-
Constructor Summary
Constructors Modifier Constructor Description protected
JCSCachedTileLoaderJob(org.apache.commons.jcs3.access.behavior.ICacheAccess<K,V> cache, TileJobOptions options)
protected
JCSCachedTileLoaderJob(org.apache.commons.jcs3.access.behavior.ICacheAccess<K,V> cache, TileJobOptions options, java.util.concurrent.ThreadPoolExecutor downloadJobExecutor)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected boolean
cacheAsEmpty(java.util.Map<java.lang.String,java.util.List<java.lang.String>> headerFields, int responseCode)
Simple implementation.void
cancelOutstandingTasks()
TODO: move to JobFactory cancels all outstanding tasks in the queue.protected abstract V
createCacheEntry(byte[] content)
java.lang.String
detectErrorMessage(java.lang.String data)
Tries do detect an error message from given string.private void
ensureCacheElement()
protected void
executionFinished()
This method is run when job has finishedprivate void
finishLoading(ICachedLoaderListener.LoadResult result)
V
get()
fetches object from cache, or returns null when object is not foundprivate HttpClient
getRequest(java.lang.String requestMethod)
protected java.lang.String
getServerKey()
Returns key under which discovered server settings will be kept.private java.net.URL
getUrlNoException()
void
handleJobCancellation()
Marks this job as canceledprotected boolean
isCacheElementValid()
private boolean
isCacheValidUsingHead()
protected boolean
isObjectLoadable()
Checks if object from cache has sufficient data to be returned.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.protected boolean
loadObject()
Load an cache objectprivate boolean
loadObjectFile(java.net.URL url)
private boolean
loadObjectHttp()
Load an cache object via HTTPprotected CacheEntryAttributes
parseHeaders(HttpClient.Response urlConn)
void
run()
void
setFinishedTask(java.lang.Runnable runnable)
Sets a job, that will be run, when job will finish executionvoid
submit(ICachedLoaderListener listener, boolean force)
Submit job for background fetch, and listener will be fed with value object-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.openstreetmap.josm.data.cache.ICachedLoaderJob
getCacheKey, getUrl
-
-
-
-
Field Detail
-
DEFAULT_EXPIRE_TIME
protected static final long DEFAULT_EXPIRE_TIME
-
EXPIRE_TIME_SERVER_LIMIT
protected static final long EXPIRE_TIME_SERVER_LIMIT
-
ABSOLUTE_EXPIRE_TIME_LIMIT
protected static final long ABSOLUTE_EXPIRE_TIME_LIMIT
-
THREAD_LIMIT
public static final IntegerProperty THREAD_LIMIT
maximum download threads that will be started
-
DEFAULT_DOWNLOAD_JOB_DISPATCHER
private static final java.util.concurrent.ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER
-
inProgress
private static final java.util.concurrent.ConcurrentMap<java.lang.String,java.util.Set<ICachedLoaderListener>> inProgress
-
useHead
private static final java.util.concurrent.ConcurrentMap<java.lang.String,java.lang.Boolean> useHead
-
now
protected final long now
-
cache
protected final org.apache.commons.jcs3.access.behavior.ICacheAccess<K,V extends CacheEntry> cache
-
cacheElement
private org.apache.commons.jcs3.engine.behavior.ICacheElement<K,V extends CacheEntry> cacheElement
-
cacheData
protected V extends CacheEntry cacheData
-
attributes
protected CacheEntryAttributes attributes
-
connectTimeout
private final int connectTimeout
-
readTimeout
private final int readTimeout
-
headers
private final java.util.Map<java.lang.String,java.lang.String> headers
-
downloadJobExecutor
private final java.util.concurrent.ThreadPoolExecutor downloadJobExecutor
-
finishTask
private java.lang.Runnable finishTask
-
force
private boolean force
-
minimumExpiryTime
private final long minimumExpiryTime
-
-
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 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 Detail
-
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 interfaceICachedLoaderJob<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 interfaceICachedLoaderJob<K>
- Parameters:
listener
- cache loader listenerforce
- 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 serverresponseCode
- 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 interfacejava.lang.Runnable
-
finishLoading
private void finishLoading(ICachedLoaderListener.LoadResult result)
-
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(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 serverresponseCode
- http status coderaw
- data read from server- Returns:
- true if object should be cached and returned to listener
-
createCacheEntry
protected abstract V createCacheEntry(byte[] content)
-
parseHeaders
protected CacheEntryAttributes parseHeaders(HttpClient.Response urlConn)
-
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
-
handleJobCancellation
public void handleJobCancellation()
Marks this job as canceled
-
getUrlNoException
private java.net.URL getUrlNoException()
-
-