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 longABSOLUTE_EXPIRE_TIME_LIMITprotected CacheEntryAttributesattributesprotected org.apache.commons.jcs3.access.behavior.ICacheAccess<K,V>cacheprotected VcacheDataprivate org.apache.commons.jcs3.engine.behavior.ICacheElement<K,V>cacheElementprivate intconnectTimeoutprivate static java.util.concurrent.ThreadPoolExecutorDEFAULT_DOWNLOAD_JOB_DISPATCHERprotected static longDEFAULT_EXPIRE_TIMEprivate java.util.concurrent.ThreadPoolExecutordownloadJobExecutorprotected static longEXPIRE_TIME_SERVER_LIMITprivate java.lang.RunnablefinishTaskprivate booleanforceprivate java.util.Map<java.lang.String,java.lang.String>headersprivate static java.util.concurrent.ConcurrentMap<java.lang.String,java.util.Set<ICachedLoaderListener>>inProgressprivate longminimumExpiryTimeprotected longnowprivate intreadTimeoutstatic IntegerPropertyTHREAD_LIMITmaximum download threads that will be startedprivate static java.util.concurrent.ConcurrentMap<java.lang.String,java.lang.Boolean>useHead
-
Constructor Summary
Constructors Modifier Constructor Description protectedJCSCachedTileLoaderJob(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, java.util.concurrent.ThreadPoolExecutor downloadJobExecutor)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected booleancacheAsEmpty(java.util.Map<java.lang.String,java.util.List<java.lang.String>> headerFields, int responseCode)Simple implementation.voidcancelOutstandingTasks()TODO: move to JobFactory cancels all outstanding tasks in the queue.protected abstract VcreateCacheEntry(byte[] content)java.lang.StringdetectErrorMessage(java.lang.String data)Tries do detect an error message from given string.private voidensureCacheElement()protected voidexecutionFinished()This method is run when job has finishedprivate voidfinishLoading(ICachedLoaderListener.LoadResult result)Vget()fetches object from cache, or returns null when object is not foundprivate HttpClientgetRequest(java.lang.String requestMethod)protected java.lang.StringgetServerKey()Returns key under which discovered server settings will be kept.private java.net.URLgetUrlNoException()voidhandleJobCancellation()Marks this job as canceledprotected booleanisCacheElementValid()private booleanisCacheValidUsingHead()protected booleanisObjectLoadable()Checks if object from cache has sufficient data to be returned.protected booleanisResponseLoadable(java.util.Map<java.lang.String,java.util.List<java.lang.String>> headerFields, int responseCode, byte[] raw)Check if the object is loadable.protected booleanloadObject()Load an cache objectprivate booleanloadObjectFile(java.net.URL url)private booleanloadObjectHttp()Load an cache object via HTTPprotected CacheEntryAttributesparseHeaders(HttpClient.Response urlConn)voidrun()voidsetFinishedTask(java.lang.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 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: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
public void submit(ICachedLoaderListener listener, boolean force) throws java.io.IOException
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:
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:
trueif 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:
runin interfacejava.lang.Runnable
-
finishLoading
private void finishLoading(ICachedLoaderListener.LoadResult result)
-
isCacheElementValid
protected boolean isCacheElementValid()
-
loadObject
protected boolean loadObject()
Load an cache object- Returns:
trueif 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:
trueif 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()
-
-