Ticket #11216: jcs-tms-v08.patch

File jcs-tms-v08.patch, 92.9 KB (added by wiktorn, 9 years ago)
  • src/org/apache/commons

  • build.xml

    Property changes on: src/org/apache/commons
    ___________________________________________________________________
    Added: svn:externals
    ## -0,0 +1,2 ##
    +http://svn.apache.org/repos/asf/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs jcs
    +http://svn.apache.org/repos/asf/commons/proper/logging/trunk/src/main/java/org/apache/commons/logging loggingIndex: build.xmlIndex: build.xml
     
    220220            destdir="build" target="1.7" source="1.7" debug="on" includeantruntime="false" createMissingPackageInfoClass="false" encoding="iso-8859-1">
    221221            <!-- get rid of "internal proprietary API" warning -->
    222222            <compilerarg value="-XDignore.symbol.file"/>
     223                <exclude name="org/apache/commons/jcs/admin/**"/>
     224                <exclude name="org/apache/commons/jcs/auxiliary/disk/jdbc/**"/>
     225                <exclude name="org/apache/commons/jcs/auxiliary/remote/**"/>
     226                <exclude name="org/apache/commons/jcs/utils/servlet/**"/>
     227                <exclude name="org/apache/commons/logging/impl/AvalonLogger.java"/>
     228                <exclude name="org/apache/commons/logging/impl/Log4JLogger.java"/>
     229                <exclude name="org/apache/commons/logging/impl/LogKitLogger.java"/>
     230                <exclude name="org/apache/commons/logging/impl/ServletContextCleaner.java"/>
    223231        </javac>
    224232        <!-- JMapViewer/JOSM -->
    225233        <javac srcdir="${src.dir}" excludes="com/**,oauth/**,org/apache/commons/**,org/glassfish/**,org/openstreetmap/gui/jmapviewer/Demo.java"
  • src/org/openstreetmap/josm/Main.java

     
    6767import org.openstreetmap.josm.data.ProjectionBounds;
    6868import org.openstreetmap.josm.data.UndoRedoHandler;
    6969import org.openstreetmap.josm.data.ViewportData;
     70import org.openstreetmap.josm.data.cache.JCSCacheManager;
    7071import org.openstreetmap.josm.data.coor.CoordinateFormat;
    7172import org.openstreetmap.josm.data.coor.LatLon;
    7273import org.openstreetmap.josm.data.osm.DataSet;
     
    10881089     * @since 3378
    10891090     */
    10901091    public static boolean exitJosm(boolean exit, int exitCode) {
     1092        JCSCacheManager.shutdown();
    10911093        if (Main.saveUnsavedModifications()) {
    10921094            geometry.remember("gui.geometry");
    10931095            if (map != null) {
  • src/org/openstreetmap/josm/data/cache/JCSCacheManager.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.cache;
     3
     4import java.io.File;
     5import java.io.IOException;
     6import java.text.MessageFormat;
     7import java.util.Properties;
     8import java.util.logging.Handler;
     9import java.util.logging.Level;
     10import java.util.logging.LogRecord;
     11import java.util.logging.Logger;
     12
     13import org.apache.commons.jcs.access.CacheAccess;
     14import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
     15import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCache;
     16import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes;
     17import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheManager;
     18import org.apache.commons.jcs.engine.CompositeCacheAttributes;
     19import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes.DiskUsagePattern;
     20import org.apache.commons.jcs.engine.control.CompositeCache;
     21import org.apache.commons.jcs.engine.control.CompositeCacheManager;
     22import org.apache.commons.jcs.utils.serialization.StandardSerializer;
     23import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
     24import org.openstreetmap.josm.Main;
     25import org.openstreetmap.josm.data.preferences.IntegerProperty;
     26
     27
     28/**
     29 * @author Wiktor Niesiobędzki
     30 *
     31 * Wrapper class for JCS Cache. Sets some sane environment and returns instances of cache objects.
     32 * Static configuration for now assumes some small LRU cache in memory and larger LRU cache on disk
     33 *
     34 */
     35public class JCSCacheManager {
     36    private static final Logger log = FeatureAdapter.getLogger(JCSCacheManager.class.getCanonicalName());
     37
     38    private static volatile CompositeCacheManager cacheManager = null;
     39    private static long maxObjectTTL        = Long.MAX_VALUE;
     40    private final static String PREFERENCE_PREFIX = "jcs.cache";
     41
     42    /**
     43     * default objects to be held in memory by JCS caches (per region)
     44     */
     45    public static final IntegerProperty DEFAULT_MAX_OBJECTS_IN_MEMORY  = new IntegerProperty(PREFERENCE_PREFIX + ".max_objects_in_memory", 1000);
     46
     47    private static void initialize() throws IOException {
     48        File cacheDir = new File(Main.pref.getCacheDirectory(), "jcs");
     49
     50        if ((!cacheDir.exists() && !cacheDir.mkdirs()))
     51            throw new IOException("Cannot access cache directory");
     52
     53        // raising logging level gives ~500x performance gain
     54        // http://westsworld.dk/blog/2008/01/jcs-and-performance/
     55        Logger jcsLog = Logger.getLogger("org.apache.commons.jcs");
     56        jcsLog.setLevel(Level.INFO);
     57        jcsLog.setUseParentHandlers(false);
     58        // we need a separate handler from Main's, as we  downgrade LEVEL.INFO to DEBUG level
     59        jcsLog.addHandler(new Handler() {
     60            @Override
     61            public void publish(LogRecord record) {
     62                String msg = MessageFormat.format(record.getMessage(), record.getParameters());
     63                if (record.getLevel().intValue() >= Level.SEVERE.intValue()) {
     64                    Main.error(msg);
     65                } else if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
     66                    Main.warn(msg);
     67                    // downgrade INFO level to debug, as JCS is too verbose at INFO level
     68                } else if (record.getLevel().intValue() >= Level.INFO.intValue()) {
     69                    Main.debug(msg);
     70                } else {
     71                    Main.trace(msg);
     72                }
     73            }
     74
     75            @Override
     76            public void flush() {
     77            }
     78
     79            @Override
     80            public void close() throws SecurityException {
     81            }
     82        });
     83
     84
     85        CompositeCacheManager cm  = CompositeCacheManager.getUnconfiguredInstance();
     86        // this could be moved to external file
     87        Properties props = new Properties();
     88        // these are default common to all cache regions
     89        // use of auxiliary cache and sizing of the caches is done with giving proper geCache(...) params
     90        props.setProperty("jcs.default.cacheattributes",                            org.apache.commons.jcs.engine.CompositeCacheAttributes.class.getCanonicalName());
     91        props.setProperty("jcs.default.cacheattributes.MaxObjects",                 DEFAULT_MAX_OBJECTS_IN_MEMORY.get().toString());
     92        props.setProperty("jcs.default.cacheattributes.UseMemoryShrinker",          "true");
     93        props.setProperty("jcs.default.cacheattributes.DiskUsagePatternName",       "UPDATE"); // store elements on disk on put
     94        props.setProperty("jcs.default.elementattributes",                          CacheEntryAttributes.class.getCanonicalName());
     95        props.setProperty("jcs.default.elementattributes.IsEternal",                "false");
     96        props.setProperty("jcs.default.elementattributes.MaxLife",                  Long.toString(maxObjectTTL));
     97        props.setProperty("jcs.default.elementattributes.IdleTime",                 Long.toString(maxObjectTTL));
     98        props.setProperty("jcs.default.elementattributes.IsSpool",                  "true");
     99        cm.configure(props);
     100        cacheManager = cm;
     101
     102    }
     103
     104    /**
     105     * Returns configured cache object for named cache region
     106     * @param cacheName region name
     107     * @return cache access object
     108     * @throws IOException if directory is not found
     109     */
     110    public static <K,V> CacheAccess<K, V> getCache(String cacheName) throws IOException {
     111        return getCache(cacheName, DEFAULT_MAX_OBJECTS_IN_MEMORY.get().intValue(), 0, null);
     112    }
     113
     114    /**
     115     * Returns configured cache object with defined limits of memory cache and disk cache
     116     * @param cacheName         region name
     117     * @param maxMemoryObjects  number of objects to keep in memory
     118     * @param maxDiskObjects    number of objects to keep on disk (if cachePath provided)
     119     * @param cachePath         path to disk cache. if null, no disk cache will be created
     120     * @return cache access object
     121     * @throws IOException if directory is not found
     122     */
     123    public static <K,V> CacheAccess<K, V> getCache(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) throws IOException {
     124        if (cacheManager != null)
     125            return getCacheInner(cacheName, maxMemoryObjects, maxDiskObjects, cachePath);
     126
     127        synchronized (JCSCacheManager.class) {
     128            if (cacheManager == null)
     129                initialize();
     130            return getCacheInner(cacheName, maxMemoryObjects, maxDiskObjects, cachePath);
     131        }
     132    }
     133
     134
     135    @SuppressWarnings("unchecked")
     136    private static <K,V> CacheAccess<K, V> getCacheInner(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) {
     137        CompositeCache<K, V> cc = cacheManager.getCache(cacheName, getCacheAttributes(maxMemoryObjects));
     138
     139        if (cachePath != null) {
     140            IndexedDiskCacheAttributes diskAttributes = getDiskCacheAttributes(maxDiskObjects, cachePath);
     141            diskAttributes.setCacheName(cacheName);
     142            IndexedDiskCache<K, V> diskCache = IndexedDiskCacheManager.getInstance(null, null, new StandardSerializer()).getCache(diskAttributes);
     143            cc.setAuxCaches(new AuxiliaryCache[]{diskCache});
     144        }
     145        return new CacheAccess<K, V>(cc);
     146    }
     147
     148    /**
     149     * Close all files to ensure, that all indexes and data are properly written
     150     */
     151    public static void shutdown() {
     152        // use volatile semantics to get consistent object
     153        CompositeCacheManager localCacheManager = cacheManager;
     154        if (localCacheManager != null) {
     155            localCacheManager.shutDown();
     156        }
     157    }
     158
     159    private static IndexedDiskCacheAttributes getDiskCacheAttributes(int maxDiskObjects, String cachePath) {
     160        IndexedDiskCacheAttributes ret = new IndexedDiskCacheAttributes();
     161        ret.setMaxKeySize(maxDiskObjects);
     162        if (cachePath != null) {
     163            File path = new File(cachePath);
     164            if (!path.exists() && !path.mkdirs()) {
     165                log.log(Level.WARNING, "Failed to create cache path: {0}", cachePath);
     166            } else {
     167                ret.setDiskPath(path);
     168            }
     169        }
     170        return ret;
     171    }
     172
     173    private static CompositeCacheAttributes getCacheAttributes(int maxMemoryElements) {
     174        CompositeCacheAttributes ret = new CompositeCacheAttributes();
     175        ret.setMaxObjects(maxMemoryElements);
     176        ret.setDiskUsagePattern(DiskUsagePattern.UPDATE);
     177        return ret;
     178    }
     179}
  • src/org/openstreetmap/josm/data/cache/CacheEntry.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.cache;
     3
     4import java.io.Serializable;
     5
     6/**
     7 * @author Wiktor Niesiobędzki
     8 *
     9 * Class that will hold JCS cache entries
     10 *
     11 */
     12public class CacheEntry implements Serializable {
     13    private static final long serialVersionUID = 1L; //version
     14    private byte[] content;
     15
     16    /**
     17     * @param content of the cache entry
     18     */
     19    public CacheEntry(byte[] content) {
     20        this.content = content;
     21    }
     22
     23    /**
     24     * @return cache entry content
     25     */
     26    public byte[] getContent() {
     27        return content;
     28    }
     29}
  • src/org/openstreetmap/josm/data/cache/ICachedLoaderListener.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.cache;
     3
     4public interface ICachedLoaderListener {
     5    /**
     6     * Will be called when K object was successfully downloaded
     7     *
     8     * @param object
     9     * @param success
     10     */
     11    public void loadingFinished(byte[] object, boolean success);
     12
     13}
  • src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.cache;
     3
     4import java.io.ByteArrayOutputStream;
     5import java.io.FileNotFoundException;
     6import java.io.IOException;
     7import java.io.InputStream;
     8import java.net.HttpURLConnection;
     9import java.net.MalformedURLException;
     10import java.net.URLConnection;
     11import java.util.HashSet;
     12import java.util.Map;
     13import java.util.Random;
     14import java.util.Set;
     15import java.util.concurrent.ConcurrentHashMap;
     16import java.util.concurrent.ConcurrentMap;
     17import java.util.concurrent.Executor;
     18import java.util.concurrent.LinkedBlockingDeque;
     19import java.util.concurrent.RejectedExecutionException;
     20import java.util.concurrent.ThreadPoolExecutor;
     21import java.util.concurrent.TimeUnit;
     22import java.util.logging.Level;
     23import java.util.logging.Logger;
     24
     25import org.apache.commons.jcs.access.behavior.ICacheAccess;
     26import org.apache.commons.jcs.engine.behavior.ICacheElement;
     27import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
     28import org.openstreetmap.josm.data.preferences.IntegerProperty;
     29
     30/**
     31 * @author Wiktor Niesiobędzki
     32 *
     33 * @param <K> cache entry key type
     34 *
     35 * Generic loader for HTTP based tiles. Uses custom attribute, to check, if entry has expired
     36 * according to HTTP headers sent with tile. If so, it tries to verify using Etags
     37 * or If-Modified-Since / Last-Modified.
     38 *
     39 * If the tile is not valid, it will try to download it from remote service and put it
     40 * to cache. If remote server will fail it will try to use stale entry.
     41 *
     42 * This class will keep only one Job running for specified tile. All others will just finish, but
     43 * listeners will be gathered and notified, once download job will be finished
     44 */
     45public abstract class JCSCachedTileLoaderJob<K> implements ICachedLoaderJob<K>, Runnable {
     46    private static final Logger log = FeatureAdapter.getLogger(JCSCachedTileLoaderJob.class.getCanonicalName());
     47    protected static final long DEFAULT_EXPIRE_TIME = 1000L * 60 * 60 * 24 * 7; // 7 days
     48    // Limit for the max-age value send by the server.
     49    protected static final long EXPIRE_TIME_SERVER_LIMIT = 1000L * 60 * 60 * 24 * 28; // 4 weeks
     50    // Absolute expire time limit. Cached tiles that are older will not be used,
     51    // even if the refresh from the server fails.
     52    protected static final long ABSOLUTE_EXPIRE_TIME_LIMIT = Long.MAX_VALUE; // unlimited
     53
     54    /**
     55     * maximum download threads that will be started
     56     */
     57    public static IntegerProperty THREAD_LIMIT = new IntegerProperty("cache.jcs.max_threads", 10);
     58    private static Executor DOWNLOAD_JOB_DISPATCHER = new ThreadPoolExecutor(
     59            2, // we have a small queue, so threads will be quickly started (threads are started only, when queue is full)
     60            THREAD_LIMIT.get().intValue(), // do not this number of threads
     61            30, // keepalive for thread
     62            TimeUnit.SECONDS,
     63            // make queue of LIFO type - so recently requested tiles will be loaded first (assuming that these are which user is waiting to see)
     64            new LinkedBlockingDeque<Runnable>(5) {
     65                /* keep the queue size fairly small, we do not want to
     66                 download a lot of tiles, that user is not seeing anyway */
     67                @Override
     68                public boolean offer(Runnable t) {
     69                    return super.offerFirst(t);
     70                }
     71
     72                @Override
     73                public Runnable remove() {
     74                    return super.removeFirst();
     75                }
     76            }
     77            );
     78
     79    private ICacheAccess<K, CacheEntry> cache;
     80    private long now;
     81    private ICacheElement<K,CacheEntry> cacheElement;
     82    private int connectTimeout;
     83    private int readTimeout;
     84    private Map<String, String> headers;
     85    private static ConcurrentMap<String,Set<ICachedLoaderListener>> inProgress = new ConcurrentHashMap<>();
     86
     87    protected CacheEntryAttributes attributes = null;
     88    protected byte[] data = null;
     89    private static ConcurrentMap<String, Boolean> useHead = new ConcurrentHashMap<>();
     90
     91
     92
     93    /**
     94     * @param cache cache instance that we will work on
     95     * @param headers
     96     * @param readTimeout
     97     * @param connectTimeout
     98     */
     99    public JCSCachedTileLoaderJob(ICacheAccess<K,CacheEntry> cache,
     100            int connectTimeout, int readTimeout,
     101            Map<String, String> headers) {
     102
     103        this.cache = cache;
     104        this.now = System.currentTimeMillis();
     105        this.connectTimeout = connectTimeout;
     106        this.readTimeout = readTimeout;
     107        this.headers = headers;
     108    }
     109
     110    private void ensureCacheElement() {
     111        if (cacheElement == null && getCacheKey() != null) {
     112            cacheElement = cache.getCacheElement(getCacheKey());
     113            if (cacheElement != null) {
     114                attributes = (CacheEntryAttributes) cacheElement.getElementAttributes();
     115                data = cacheElement.getVal().getContent();
     116            }
     117        }
     118    }
     119
     120    public byte[] get() {
     121        ensureCacheElement();
     122        return data;
     123    }
     124
     125    @Override
     126    public void submit(ICachedLoaderListener listener) {
     127        boolean first = false;
     128        String url = getUrl().toString();
     129        if (url == null) {
     130            log.log(Level.WARNING, "No url returned for: {0}, skipping", getCacheKey());
     131            return;
     132        }
     133        synchronized (inProgress) {
     134            Set<ICachedLoaderListener> newListeners = inProgress.get(url);
     135            if (newListeners == null) {
     136                newListeners = new HashSet<>();
     137                inProgress.put(url, newListeners);
     138                first = true;
     139            }
     140            newListeners.add(listener);
     141        }
     142
     143        if (first) {
     144            ensureCacheElement();
     145            if (cacheElement != null && isCacheElementValid() && (isObjectLoadable())) {
     146                // we got something in cache, and it's valid, so lets return it
     147                log.log(Level.FINE, "JCS - Returning object from cache: {0}", getCacheKey());
     148                finishLoading(true);
     149                return;
     150            }
     151            // object not in cache, so submit work to separate thread
     152            try {
     153                // use getter method, so subclasses may override executors, to get separate thread pool
     154                getDownloadExecutor().execute(JCSCachedTileLoaderJob.this);
     155            } catch (RejectedExecutionException e) {
     156                // queue was full, try again later
     157                log.log(Level.FINE, "JCS - rejected job for: {0}", getCacheKey());
     158                finishLoading(false);
     159            }
     160        }
     161    }
     162
     163    /**
     164     *
     165     * @return checks if object from cache has sufficient data to be returned
     166     */
     167    protected boolean isObjectLoadable() {
     168        return data != null && data.length > 0;
     169    }
     170
     171    /**
     172     *
     173     * @return cache object as empty, regardless of what remote resource has returned (ex. based on headers)
     174     */
     175    protected boolean cacheAsEmpty() {
     176        return false;
     177    }
     178
     179    /**
     180     * @return key under which discovered server settings will be kept
     181     */
     182    protected String getServerKey() {
     183        return getUrl().getHost();
     184    }
     185
     186    /**
     187     * this needs to be non-static, so it can be overridden by subclasses
     188     */
     189    protected Executor getDownloadExecutor() {
     190        return DOWNLOAD_JOB_DISPATCHER;
     191    }
     192
     193
     194    public void run() {
     195        final Thread currentThread = Thread.currentThread();
     196        final String oldName = currentThread.getName();
     197        currentThread.setName("JCS Downloading: " + getUrl());
     198        try {
     199            // try to load object from remote resource
     200            if (loadObject()) {
     201                finishLoading(true);
     202            } else {
     203                // if loading failed - check if we can return stale entry
     204                if (isObjectLoadable()) {
     205                    // try to get stale entry in cache
     206                    finishLoading(true);
     207                    log.log(Level.FINE, "JCS - found stale object in cache: {0}", getUrl());
     208                } else {
     209                    // failed completely
     210                    finishLoading(false);
     211                }
     212            }
     213        } finally {
     214            currentThread.setName(oldName);
     215        }
     216    }
     217
     218
     219    private void finishLoading(boolean success) {
     220        Set<ICachedLoaderListener> listeners = null;
     221        synchronized (inProgress) {
     222            listeners = inProgress.remove(getUrl().toString());
     223        }
     224        if (listeners == null) {
     225            log.log(Level.WARNING, "Listener not found for URL: {0}. Listener not notified!", getUrl());
     226            return;
     227        }
     228        try {
     229            for (ICachedLoaderListener l: listeners) {
     230                l.loadingFinished(data, success);
     231            }
     232        } catch (Exception e) {
     233            log.log(Level.WARNING, "JCS - Error while loading object from cache: {0}; {1}", new Object[]{e.getMessage(), getUrl()});
     234            log.log(Level.FINE, "Stacktrace", e);
     235            for (ICachedLoaderListener l: listeners) {
     236                l.loadingFinished(data, false);
     237            }
     238
     239        }
     240
     241    }
     242
     243    private boolean isCacheElementValid() {
     244        long expires = attributes.getExpirationTime();
     245
     246        // check by expire date set by server
     247        if (expires != 0L) {
     248            // put a limit to the expire time (some servers send a value
     249            // that is too large)
     250            expires = Math.min(expires, attributes.getCreateTime() + EXPIRE_TIME_SERVER_LIMIT);
     251            if (now > expires) {
     252                log.log(Level.FINE, "JCS - Object {0} has expired -> valid to {1}, now is: {2}", new Object[]{getUrl(), Long.toString(expires), Long.toString(now)});
     253                return false;
     254            }
     255        } else {
     256            // check by file modification date
     257            if (now - attributes.getLastModification() > DEFAULT_EXPIRE_TIME) {
     258                log.log(Level.FINE, "JCS - Object has expired, maximum file age reached {0}", getUrl());
     259                return false;
     260            }
     261        }
     262        return true;
     263    }
     264
     265    private boolean loadObject() {
     266        try {
     267            // if we have object in cache, and host doesn't support If-Modified-Since nor If-None-Match
     268            // then just use HEAD request and check returned values
     269            if (isObjectLoadable() &&
     270                    Boolean.TRUE.equals(useHead.get(getServerKey())) &&
     271                    isCacheValidUsingHead()) {
     272                log.log(Level.FINE, "JCS - cache entry verified using HEAD request: {0}", getUrl());
     273                return true;
     274            }
     275            URLConnection urlConn = getURLConnection();
     276
     277            if (isObjectLoadable()  &&
     278                    (now - attributes.getLastModification()) <= ABSOLUTE_EXPIRE_TIME_LIMIT) {
     279                urlConn.setIfModifiedSince(attributes.getLastModification());
     280            }
     281            if (isObjectLoadable() && attributes.getEtag() != null) {
     282                urlConn.addRequestProperty("If-None-Match", attributes.getEtag());
     283            }
     284            if (urlConn instanceof HttpURLConnection && ((HttpURLConnection)urlConn).getResponseCode() == 304) {
     285                // If isModifiedSince or If-None-Match has been set
     286                // and the server answers with a HTTP 304 = "Not Modified"
     287                log.log(Level.FINE, "JCS - IfModifiedSince/Etag test: local version is up to date: {0}", getUrl());
     288                return true;
     289            } else if (isObjectLoadable()) {
     290                // we have an object in cache, but we haven't received 304 resposne code
     291                // check if we should use HEAD request to verify
     292                if((attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||
     293                        attributes.getLastModification() == urlConn.getLastModified()) {
     294                    // we sent ETag or If-Modified-Since, but didn't get 304 response code
     295                    // for further requests - use HEAD
     296                    String serverKey = getServerKey();
     297                    log.log(Level.INFO, "JCS - Host: {0} found not to return 304 codes for If-Modifed-Since or If-None-Match headers", serverKey);
     298                    useHead.put(serverKey, Boolean.TRUE);
     299                }
     300            }
     301
     302            attributes = parseHeaders(urlConn);
     303
     304            for (int i = 0; i < 5; ++i) {
     305                if (urlConn instanceof HttpURLConnection && ((HttpURLConnection)urlConn).getResponseCode() == 503) {
     306                    Thread.sleep(5000+(new Random()).nextInt(5000));
     307                    continue;
     308                }
     309                data = read(urlConn);
     310                synchronized (cache) { // FIXME: does it protect against corrupted cache files?
     311                    if (!cacheAsEmpty() && data != null && data.length > 0) {
     312                        cache.put(getCacheKey(), new CacheEntry(data), attributes);
     313                        log.log(Level.FINE, "JCS - downloaded key: {0}, length: {1}, url: {2}",
     314                                new Object[] {getCacheKey(), data.length, getUrl()});
     315                        return true;
     316                    } else {
     317                        log.log(Level.FINE, "JCS - Caching empty object {0}", getUrl());
     318                        cache.put(getCacheKey(), new CacheEntry(new byte[]{}), attributes);
     319                        return true;
     320                    }
     321                }
     322            }
     323        } catch (FileNotFoundException e) {
     324            log.log(Level.FINE, "JCS - Caching empty object as server returned 404 for: {0}", getUrl());
     325            cache.put(getCacheKey(), new CacheEntry(new byte[]{}), attributes);
     326            handleNotFound();
     327            return true;
     328        } catch (Exception e) {
     329            log.log(Level.WARNING, "JCS - Exception during download " + getUrl(), e);
     330        }
     331        log.log(Level.WARNING, "JCS - Silent failure during download: {0}", getUrl());
     332        return false;
     333
     334    }
     335
     336    protected abstract void handleNotFound();
     337
     338    private CacheEntryAttributes parseHeaders(URLConnection urlConn) {
     339        CacheEntryAttributes ret = new CacheEntryAttributes();
     340        ret.setNoTileAtZoom("no-tile".equals(urlConn.getHeaderField("X-VE-Tile-Info")));
     341
     342        Long lng = urlConn.getExpiration();
     343        if (lng.equals(0L)) {
     344            try {
     345                String str = urlConn.getHeaderField("Cache-Control");
     346                if (str != null) {
     347                    for (String token: str.split(",")) {
     348                        if (token.startsWith("max-age=")) {
     349                            lng = Long.parseLong(token.substring(8)) * 1000 +
     350                                    System.currentTimeMillis();
     351                        }
     352                    }
     353                }
     354            } catch (NumberFormatException e) {} //ignore malformed Cache-Control headers
     355        }
     356
     357        ret.setExpirationTime(lng);
     358        ret.setLastModification(now);
     359        ret.setEtag(urlConn.getHeaderField("ETag"));
     360        return ret;
     361    }
     362
     363    private HttpURLConnection getURLConnection() throws IOException, MalformedURLException {
     364        HttpURLConnection urlConn = (HttpURLConnection) getUrl().openConnection();
     365        urlConn.setRequestProperty("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
     366        urlConn.setReadTimeout(readTimeout); // 30 seconds read timeout
     367        urlConn.setConnectTimeout(connectTimeout);
     368        for(Map.Entry<String, String> e: headers.entrySet()) {
     369            urlConn.setRequestProperty(e.getKey(), e.getValue());
     370        }
     371        return urlConn;
     372    }
     373
     374    private boolean isCacheValidUsingHead() throws IOException {
     375        HttpURLConnection urlConn = (HttpURLConnection) getUrl().openConnection();
     376        urlConn.setRequestMethod("HEAD");
     377        long lastModified = urlConn.getLastModified();
     378        return (
     379                (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getRequestProperty("ETag"))) ||
     380                (lastModified != 0 && lastModified <= attributes.getLastModification())
     381                );
     382    }
     383
     384    private static byte[] read(URLConnection urlConn) throws IOException {
     385        InputStream input = urlConn.getInputStream();
     386        try {
     387            ByteArrayOutputStream bout = new ByteArrayOutputStream(input.available());
     388            byte[] buffer = new byte[2048];
     389            boolean finished = false;
     390            do {
     391                int read = input.read(buffer);
     392                if (read >= 0) {
     393                    bout.write(buffer, 0, read);
     394                } else {
     395                    finished = true;
     396                }
     397            } while (!finished);
     398            if (bout.size() == 0)
     399                return null;
     400            return bout.toByteArray();
     401        } finally {
     402            input.close();
     403        }
     404    }
     405}
  • src/org/openstreetmap/josm/data/cache/CacheEntryAttributes.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.cache;
     3
     4import java.util.HashMap;
     5import java.util.Map;
     6
     7import org.apache.commons.jcs.engine.ElementAttributes;
     8
     9/**
     10 * Class that contains attirubtes for JCS cache entries. Parameters are used to properly handle HTTP caching
     11 *
     12 * @author Wiktor Niesiobędzki
     13 *
     14 */
     15public class CacheEntryAttributes extends ElementAttributes {
     16    private static final long serialVersionUID = 1L; //version
     17    private Map<String, String> attrs = new HashMap<String, String>();
     18    private final static String NO_TILE_AT_ZOOM = "noTileAtZoom";
     19    private final static String ETAG = "Etag";
     20    private final static String LAST_MODIFICATION = "lastModification";
     21    private final static String EXPIRATION_TIME = "expirationTime";
     22
     23    public CacheEntryAttributes() {
     24        super();
     25        attrs.put(NO_TILE_AT_ZOOM, "false");
     26        attrs.put(ETAG, null);
     27        attrs.put(LAST_MODIFICATION, "0");
     28        attrs.put(EXPIRATION_TIME, "0");
     29    }
     30
     31    public boolean isNoTileAtZoom() {
     32        return Boolean.toString(true).equals(attrs.get(NO_TILE_AT_ZOOM));
     33    }
     34    public void setNoTileAtZoom(boolean noTileAtZoom) {
     35        attrs.put(NO_TILE_AT_ZOOM, Boolean.toString(noTileAtZoom));
     36    }
     37    public String getEtag() {
     38        return attrs.get(ETAG);
     39    }
     40    public void setEtag(String etag) {
     41        attrs.put(ETAG, etag);
     42    }
     43
     44    private long getLongAttr(String key) {
     45        try {
     46            return Long.parseLong(attrs.get(key));
     47        } catch (NumberFormatException e) {
     48            attrs.put(key, "0");
     49            return 0;
     50        }
     51    }
     52
     53    public long getLastModification() {
     54        return getLongAttr(LAST_MODIFICATION);
     55    }
     56    public void setLastModification(long lastModification) {
     57        attrs.put(LAST_MODIFICATION, Long.toString(lastModification));
     58    }
     59    public long getExpirationTime() {
     60        return getLongAttr(EXPIRATION_TIME);
     61    }
     62    public void setExpirationTime(long expirationTime) {
     63        attrs.put(EXPIRATION_TIME, Long.toString(expirationTime));
     64    }
     65
     66}
  • src/org/openstreetmap/josm/data/cache/ICachedLoaderJob.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.cache;
     3
     4import java.net.URL;
     5
     6
     7/**
     8 *
     9 * @author Wiktor Niesiobędzki
     10 *
     11 * @param <K> cache key type
     12 */
     13public interface ICachedLoaderJob<K> {
     14    /**
     15     * returns cache entry key
     16     *
     17     * @param tile
     18     * @return cache key for tile
     19     */
     20    public K getCacheKey();
     21
     22    /**
     23     * method to get download URL for Job
     24     * @return URL that should be fetched
     25     *
     26     */
     27    public URL getUrl();
     28    /**
     29     * implements the main algorithm for fetching
     30     */
     31    public void run();
     32
     33    /**
     34     * fetches object from cache, or returns null when object is not found
     35     *
     36     * @return filled tile with data or null when no cache entry found
     37     */
     38    public byte[] get();
     39
     40    /**
     41     * Submit job for background fetch, and listener will be
     42     * fed with value object
     43     *
     44     * @param listener
     45     */
     46    public void submit(ICachedLoaderListener listener);
     47}
  • src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoader.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.imagery;
     3
     4import java.io.IOException;
     5import java.util.Map;
     6
     7import org.apache.commons.jcs.access.behavior.ICacheAccess;
     8import org.openstreetmap.gui.jmapviewer.Tile;
     9import org.openstreetmap.gui.jmapviewer.interfaces.CachedTileLoader;
     10import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
     11import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
     12import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
     13import org.openstreetmap.josm.data.cache.CacheEntry;
     14import org.openstreetmap.josm.data.cache.JCSCacheManager;
     15import org.openstreetmap.josm.data.preferences.IntegerProperty;
     16
     17/**
     18 * @author Wiktor Niesiobędzki
     19 *
     20 * Wrapper class that bridges between JCS cache and Tile Loaders
     21 *
     22 */
     23public class TMSCachedTileLoader implements TileLoader, CachedTileLoader {
     24
     25    private ICacheAccess<String, CacheEntry> cache;
     26    private int connectTimeout;
     27    private int readTimeout;
     28    private Map<String, String> headers;
     29    private TileLoaderListener listener;
     30    public static final String PREFERENCE_PREFIX   = "imagery.tms.cache.";
     31    // average tile size is about 20kb
     32    public static IntegerProperty MAX_OBJECTS_ON_DISK = new IntegerProperty(PREFERENCE_PREFIX + "max_objects_disk", 25000); // 25000 is around 500MB under this assumptions
     33
     34
     35    /**
     36     * Constructor
     37     * @param listener          called when tile loading has finished
     38     * @param name              of the cache
     39     * @param connectTimeout    to remote resource
     40     * @param readTimeout       to remote resource
     41     * @param headers           to be sent along with request
     42     * @param cacheDir          where cache file shall reside
     43     * @throws IOException      when cache initialization fails
     44     */
     45    public TMSCachedTileLoader(TileLoaderListener listener, String name, int connectTimeout, int readTimeout, Map<String, String> headers, String cacheDir) throws IOException {
     46        this.cache = JCSCacheManager.getCache(name,
     47                0, // don't use memory cache - we have already cache with BufferedImage
     48                MAX_OBJECTS_ON_DISK.get(),
     49                cacheDir);
     50        this.connectTimeout = connectTimeout;
     51        this.readTimeout = readTimeout;
     52        this.headers = headers;
     53        this.listener = listener;
     54    }
     55
     56    @Override
     57    public TileJob createTileLoaderJob(Tile tile) {
     58        return new TMSCachedTileLoaderJob(listener, tile, cache, connectTimeout, readTimeout, headers);
     59    }
     60
     61    @Override
     62    public void clearCache() {
     63        this.cache.clear();
     64    }
     65}
  • src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJob.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.data.imagery;
     3
     4import java.io.ByteArrayInputStream;
     5import java.io.IOException;
     6import java.net.URL;
     7import java.util.Map;
     8import java.util.concurrent.Executor;
     9import java.util.concurrent.LinkedBlockingDeque;
     10import java.util.concurrent.ThreadPoolExecutor;
     11import java.util.concurrent.TimeUnit;
     12import java.util.logging.Level;
     13import java.util.logging.Logger;
     14
     15import org.apache.commons.jcs.access.behavior.ICacheAccess;
     16import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
     17import org.openstreetmap.gui.jmapviewer.Tile;
     18import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
     19import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
     20import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
     21import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
     22import org.openstreetmap.josm.data.cache.CacheEntry;
     23import org.openstreetmap.josm.data.cache.ICachedLoaderListener;
     24import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob;
     25import org.openstreetmap.josm.data.preferences.IntegerProperty;
     26
     27/**
     28 * @author Wiktor Niesiobędzki
     29 *
     30 * Class bridging TMS requests to JCS cache requests
     31 *
     32 */
     33public class TMSCachedTileLoaderJob extends JCSCachedTileLoaderJob<String> implements TileJob, ICachedLoaderListener  {
     34    private static final Logger log = FeatureAdapter.getLogger(TMSCachedTileLoaderJob.class.getCanonicalName());
     35    private Tile tile;
     36    private TileLoaderListener listener;
     37    private URL url;
     38
     39    /**
     40     * overrides the THREAD_LIMIT in superclass, as we want to have separate limit and pool for TMS
     41     */
     42    public static IntegerProperty THREAD_LIMIT = new IntegerProperty("imagery.tms.tmsloader.maxjobs", 25);
     43    /**
     44     * separate from JCS thread pool for TMS loader, so we can have different thread pools for default JCS
     45     * and for TMS imagery
     46     */
     47    private static ThreadPoolExecutor DOWNLOAD_JOB_DISPATCHER = new ThreadPoolExecutor(
     48            THREAD_LIMIT.get().intValue(), // keep the thread number constant
     49            THREAD_LIMIT.get().intValue(), // do not this number of threads
     50            30, // keepalive for thread
     51            TimeUnit.SECONDS,
     52            // make queue of LIFO type - so recently requested tiles will be loaded first (assuming that these are which user is waiting to see)
     53            new LinkedBlockingDeque<Runnable>(5) {
     54                /* keep the queue size fairly small, we do not want to
     55                 download a lot of tiles, that user is not seeing anyway */
     56                @Override
     57                public boolean offer(Runnable t) {
     58                    return super.offerFirst(t);
     59                }
     60
     61                @Override
     62                public Runnable remove() {
     63                    return super.removeFirst();
     64                }
     65            }
     66            );
     67
     68    /**
     69     * Constructor for creating a job, to get a specific tile from cache
     70     * @param listener
     71     * @param tile to be fetched from cache
     72     * @param cache object
     73     * @param connectTimeout when connecting to remote resource
     74     * @param readTimeout when connecting to remote resource
     75     * @param headers to be sent together with request
     76     */
     77    public TMSCachedTileLoaderJob(TileLoaderListener listener, Tile tile, ICacheAccess<String, CacheEntry> cache, int connectTimeout, int readTimeout,
     78            Map<String, String> headers) {
     79        super(cache, connectTimeout, readTimeout, headers);
     80        this.tile = tile;
     81        this.listener = listener;
     82        // URLs tend to change for some tile providers. Make a static reference here, so the tile URL might be used as a key
     83        // for request deduplication
     84        try {
     85            this.url = new URL(tile.getUrl());
     86        } catch (IOException e) {
     87            log.log(Level.WARNING, "JCS TMS Cache - error creating URL for tile {0}: {1}", new Object[] {tile.getKey(), e.getMessage()});
     88        }
     89
     90    }
     91
     92    @Override
     93    public Tile getTile() {
     94        return tile;
     95    }
     96
     97    @Override
     98    public String getCacheKey() {
     99        if (tile != null)
     100            return tile.getKey();
     101        return null;
     102    }
     103
     104    @Override
     105    public URL getUrl() {
     106        return url;
     107    }
     108
     109    @Override
     110    public boolean isObjectLoadable() {
     111        return (data != null && data.length > 0) || cacheAsEmpty();
     112    }
     113
     114    @Override
     115    protected boolean cacheAsEmpty() {
     116        if (attributes != null && attributes.isNoTileAtZoom()) {
     117            // do not remove file - keep the information, that there is no tile, for further requests
     118            // the code above will check, if this information is still valid
     119            log.log(Level.FINE, "JCS TMS - Tile valid, but no file, as no tiles at this level {0}", tile);
     120            tile.setError("No tile at this zoom level");
     121            tile.putValue("tile-info", "no-tile");
     122            return true;
     123        }
     124        return false;
     125    }
     126
     127    @Override
     128    protected Executor getDownloadExecutor() {
     129        return DOWNLOAD_JOB_DISPATCHER;
     130    }
     131
     132    public void submit() {
     133        tile.initLoading();
     134        super.submit(this);
     135    }
     136
     137    @Override
     138    public void loadingFinished(byte[] object, boolean success) {
     139        try {
     140            tile.finishLoading();
     141            if (object != null && object.length > 0) {
     142                tile.loadImage(new ByteArrayInputStream(object));
     143            }
     144            if (listener != null) {
     145                listener.tileLoadingFinished(tile, success);
     146            }
     147        } catch (IOException e) {
     148            log.log(Level.WARNING, "JCS TMS - error loading object for tile {0}: {1}", new Object[] {tile.getKey(), e.getMessage()});
     149            tile.setError(e.getMessage());
     150            tile.setLoaded(false);
     151            if (listener != null) {
     152                listener.tileLoadingFinished(tile, false);
     153            }
     154        }
     155    }
     156
     157    /**
     158     * Method for getting the tile from cache only, without trying to reach remote resource
     159     * @return tile or null, if nothing (useful) was found in cache
     160     */
     161    public Tile getCachedTile() {
     162        byte[] data = super.get();
     163        if (isObjectLoadable()) {
     164            loadingFinished(data, true);
     165            return tile;
     166        } else {
     167            return null;
     168        }
     169    }
     170
     171    @Override
     172    protected void handleNotFound() {
     173        tile.setError("No tile at this zoom level");
     174        tile.putValue("tile-info", "no-tile");
     175    }
     176
     177    @Override
     178    protected String getServerKey() {
     179        TileSource ts = tile.getSource();
     180        if (ts instanceof AbstractTMSTileSource) {
     181            return ((AbstractTMSTileSource) ts).getBaseUrl();
     182        }
     183        return super.getServerKey();
     184    }
     185}
  • src/org/openstreetmap/josm/gui/preferences/imagery/TMSSettingsPanel.java

     
    1111import javax.swing.JSpinner;
    1212import javax.swing.SpinnerNumberModel;
    1313
     14import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
     15import org.openstreetmap.josm.data.imagery.TMSCachedTileLoaderJob;
    1416import org.openstreetmap.josm.gui.layer.TMSLayer;
    15 import org.openstreetmap.josm.tools.GBC;
    1617import org.openstreetmap.josm.gui.widgets.JosmTextField;
     18import org.openstreetmap.josm.tools.GBC;
    1719
    1820/**
    1921 * {@code JPanel} giving access to TMS settings.
     
    2830    private final JSpinner maxZoomLvl;
    2931    private final JCheckBox addToSlippyMapChosser = new JCheckBox();
    3032    private final JosmTextField tilecacheDir = new JosmTextField();
     33    private final JSpinner maxElementsOnDisk;
     34    private final JSpinner maxConcurrentDownloads;
     35
    3136
    3237    /**
    3338     * Constructs a new {@code TMSSettingsPanel}.
     
    3641        super(new GridBagLayout());
    3742        minZoomLvl = new JSpinner(new SpinnerNumberModel(TMSLayer.DEFAULT_MIN_ZOOM, TMSLayer.MIN_ZOOM, TMSLayer.MAX_ZOOM, 1));
    3843        maxZoomLvl = new JSpinner(new SpinnerNumberModel(TMSLayer.DEFAULT_MAX_ZOOM, TMSLayer.MIN_ZOOM, TMSLayer.MAX_ZOOM, 1));
     44        maxElementsOnDisk = new JSpinner(new SpinnerNumberModel(TMSCachedTileLoader.MAX_OBJECTS_ON_DISK.get().intValue(), 0, Integer.MAX_VALUE, 1));
     45        maxConcurrentDownloads = new JSpinner(new SpinnerNumberModel(TMSCachedTileLoaderJob.THREAD_LIMIT.get().intValue(), 0, Integer.MAX_VALUE, 1));
    3946
    4047        add(new JLabel(tr("Auto zoom by default: ")), GBC.std());
    4148        add(GBC.glue(5, 0), GBC.std());
     
    6067        add(new JLabel(tr("Tile cache directory: ")), GBC.std());
    6168        add(GBC.glue(5, 0), GBC.std());
    6269        add(tilecacheDir, GBC.eol().fill(GBC.HORIZONTAL));
     70
     71        add(new JLabel(tr("Maximum concurrent downloads: ")), GBC.std());
     72        add(GBC.glue(5, 0), GBC.std());
     73        add(maxConcurrentDownloads, GBC.eol());
     74
     75        add(new JLabel(tr("Maximum elements in disk cache: ")), GBC.std());
     76        add(GBC.glue(5, 0), GBC.std());
     77        add(this.maxElementsOnDisk, GBC.eol());
     78
    6379    }
    64    
     80
    6581    /**
    6682     * Loads the TMS settings.
    6783     */
     
    7288        this.maxZoomLvl.setValue(TMSLayer.getMaxZoomLvl(null));
    7389        this.minZoomLvl.setValue(TMSLayer.getMinZoomLvl(null));
    7490        this.tilecacheDir.setText(TMSLayer.PROP_TILECACHE_DIR.get());
     91        this.maxElementsOnDisk.setValue(TMSCachedTileLoader.MAX_OBJECTS_ON_DISK.get());
     92        this.maxConcurrentDownloads.setValue(TMSCachedTileLoaderJob.THREAD_LIMIT.get());
    7593    }
    76    
     94
    7795    /**
    7896     * Saves the TMS settings.
    7997     * @return true when restart is required
    8098     */
    8199    public boolean saveSettings() {
    82100        boolean restartRequired = false;
    83        
     101
    84102        if (TMSLayer.PROP_ADD_TO_SLIPPYMAP_CHOOSER.get() != this.addToSlippyMapChosser.isSelected()) {
    85103            restartRequired = true;
    86104        }
     
    89107        TMSLayer.PROP_DEFAULT_AUTOLOAD.put(this.autoloadTiles.isSelected());
    90108        TMSLayer.setMaxZoomLvl((Integer)this.maxZoomLvl.getValue());
    91109        TMSLayer.setMinZoomLvl((Integer)this.minZoomLvl.getValue());
    92         TMSLayer.PROP_TILECACHE_DIR.put(this.tilecacheDir.getText());
    93        
     110
     111        TMSCachedTileLoader.MAX_OBJECTS_ON_DISK.put((Integer) this.maxElementsOnDisk.getValue());
     112
     113        if (!TMSCachedTileLoaderJob.THREAD_LIMIT.get().equals(this.maxConcurrentDownloads.getValue())) {
     114            restartRequired = true;
     115            TMSCachedTileLoaderJob.THREAD_LIMIT.put((Integer) this.maxConcurrentDownloads.getValue());
     116        }
     117
     118        if (!TMSLayer.PROP_TILECACHE_DIR.get().equals(this.tilecacheDir.getText())) {
     119            restartRequired = true;
     120            TMSLayer.PROP_TILECACHE_DIR.put(this.tilecacheDir.getText());
     121        }
     122
    94123        return restartRequired;
    95124    }
    96125}
  • src/org/openstreetmap/josm/gui/layer/TMSLayer.java

     
    2121import java.net.URL;
    2222import java.util.ArrayList;
    2323import java.util.Collections;
    24 import java.util.HashSet;
     24import java.util.HashMap;
    2525import java.util.LinkedList;
    2626import java.util.List;
    2727import java.util.Map;
    28 import java.util.Map.Entry;
    2928import java.util.Scanner;
    30 import java.util.Set;
    3129import java.util.concurrent.Callable;
    3230import java.util.regex.Matcher;
    3331import java.util.regex.Pattern;
     
    4139
    4240import org.openstreetmap.gui.jmapviewer.AttributionSupport;
    4341import org.openstreetmap.gui.jmapviewer.Coordinate;
    44 import org.openstreetmap.gui.jmapviewer.JobDispatcher;
    4542import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
    46 import org.openstreetmap.gui.jmapviewer.OsmFileCacheTileLoader;
    4743import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
    48 import org.openstreetmap.gui.jmapviewer.TMSFileCacheTileLoader;
    4944import org.openstreetmap.gui.jmapviewer.Tile;
    5045import org.openstreetmap.gui.jmapviewer.interfaces.CachedTileLoader;
    51 import org.openstreetmap.gui.jmapviewer.interfaces.TileClearController;
     46import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
    5247import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
    5348import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
    5449import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
     
    6358import org.openstreetmap.josm.data.coor.LatLon;
    6459import org.openstreetmap.josm.data.imagery.ImageryInfo;
    6560import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
     61import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
    6662import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    6763import org.openstreetmap.josm.data.preferences.BooleanProperty;
    6864import org.openstreetmap.josm.data.preferences.IntegerProperty;
     
    7571import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    7672import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
    7773import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    78 import org.openstreetmap.josm.gui.progress.ProgressMonitor.CancelListener;
    7974import org.openstreetmap.josm.io.CacheCustomContent;
    8075import org.openstreetmap.josm.io.OsmTransferException;
    8176import org.openstreetmap.josm.io.UTFInputStreamReader;
     
    108103    public static final IntegerProperty PROP_MAX_ZOOM_LVL = new IntegerProperty(PREFERENCE_PREFIX + ".max_zoom_lvl", DEFAULT_MAX_ZOOM);
    109104    //public static final BooleanProperty PROP_DRAW_DEBUG = new BooleanProperty(PREFERENCE_PREFIX + ".draw_debug", false);
    110105    public static final BooleanProperty PROP_ADD_TO_SLIPPYMAP_CHOOSER = new BooleanProperty(PREFERENCE_PREFIX + ".add_to_slippymap_chooser", true);
    111     public static final IntegerProperty PROP_TMS_JOBS = new IntegerProperty("tmsloader.maxjobs", 25);
    112106    public static final StringProperty PROP_TILECACHE_DIR;
    113107
    114108    static {
     
    122116    }
    123117
    124118    public interface TileLoaderFactory {
    125         OsmTileLoader makeTileLoader(TileLoaderListener listener);
     119        TileLoader makeTileLoader(TileLoaderListener listener);
     120        TileLoader makeTileLoader(TileLoaderListener listener, Map<String, String> headers);
    126121    }
    127122
     123    // MemoryTileCache caches rendered tiles, to reduce latency during map panning
     124    // ImageIO.read() takes a lot of time, so we can't use JCS cache
    128125    protected MemoryTileCache tileCache;
    129126    protected TileSource tileSource;
    130     protected OsmTileLoader tileLoader;
     127    protected TileLoader tileLoader;
     128
    131129
    132130    public static TileLoaderFactory loaderFactory = new TileLoaderFactory() {
    133131        @Override
    134         public OsmTileLoader makeTileLoader(TileLoaderListener listener) {
    135             String cachePath = TMSLayer.PROP_TILECACHE_DIR.get();
    136             if (cachePath != null && !cachePath.isEmpty()) {
    137                 try {
    138                     OsmFileCacheTileLoader loader;
    139                     loader = new TMSFileCacheTileLoader(listener, new File(cachePath));
    140                     loader.headers.put("User-Agent", Version.getInstance().getFullAgentString());
    141                     return loader;
    142                 } catch (IOException e) {
    143                     Main.warn(e);
    144                 }
     132        public TileLoader makeTileLoader(TileLoaderListener listener, Map<String, String> inputHeaders) {
     133            Map<String, String> headers = new HashMap<>();
     134            headers.put("User-Agent", Version.getInstance().getFullAgentString());
     135            headers.put("Accept", "text/html, image/png, image/jpeg, image/gif, */*");
     136            if (inputHeaders != null)
     137                headers.putAll(inputHeaders);
     138
     139            try {
     140                return new TMSCachedTileLoader(listener, "TMS",
     141                        Main.pref.getInteger("socket.timeout.connect",15) * 1000,
     142                        Main.pref.getInteger("socket.timeout.read", 30) * 1000,
     143                        headers,
     144                        PROP_TILECACHE_DIR.get());
     145            } catch (IOException e) {
     146                Main.warn(e);
    145147            }
    146148            return null;
    147149        }
     150
     151        @Override
     152        public TileLoader makeTileLoader(TileLoaderListener listener) {
     153            return makeTileLoader(listener, null);
     154        }
    148155    };
    149156
    150157    /**
    151158     * Plugins that wish to set custom tile loader should call this method
    152159     */
     160
    153161    public static void setCustomTileLoaderFactory(TileLoaderFactory loaderFactory) {
    154162        TMSLayer.loaderFactory = loaderFactory;
    155163    }
    156164
    157     private Set<Tile> tileRequestsOutstanding = new HashSet<>();
    158 
    159165    @Override
    160166    public synchronized void tileLoadingFinished(Tile tile, boolean success) {
    161167        if (tile.hasError()) {
     
    165171        if (sharpenLevel != 0 && success) {
    166172            tile.setImage(sharpenImage(tile.getImage()));
    167173        }
    168         tile.setLoaded(true);
     174        tile.setLoaded(success);
    169175        needRedraw = true;
    170176        if (Main.map != null) {
    171177            Main.map.repaint(100);
    172178        }
    173         tileRequestsOutstanding.remove(tile);
    174179        if (Main.isDebugEnabled()) {
    175180            Main.debug("tileLoadingFinished() tile: " + tile + " success: " + success);
    176181        }
    177182    }
    178183
    179     private static class TmsTileClearController implements TileClearController, CancelListener {
    180 
    181         private final ProgressMonitor monitor;
    182         private boolean cancel = false;
    183 
    184         public TmsTileClearController(ProgressMonitor monitor) {
    185             this.monitor = monitor;
    186             this.monitor.addCancelListener(this);
    187         }
    188 
    189         @Override
    190         public void initClearDir(File dir) {
    191         }
    192 
    193         @Override
    194         public void initClearFiles(File[] files) {
    195             monitor.setTicksCount(files.length);
    196             monitor.setTicks(0);
    197         }
    198 
    199         @Override
    200         public boolean cancel() {
    201             return cancel;
    202         }
    203 
    204         @Override
    205         public void fileDeleted(File file) {
    206             monitor.setTicks(monitor.getTicks()+1);
    207         }
    208 
    209         @Override
    210         public void clearFinished() {
    211             monitor.finishTask();
    212         }
    213 
    214         @Override
    215         public void operationCanceled() {
    216             cancel = true;
    217         }
    218     }
    219 
    220184    /**
    221185     * Clears the tile cache.
    222186     *
     
    231195    void clearTileCache(ProgressMonitor monitor) {
    232196        tileCache.clear();
    233197        if (tileLoader instanceof CachedTileLoader) {
    234             ((CachedTileLoader)tileLoader).clearCache(tileSource, new TmsTileClearController(monitor));
     198            ((CachedTileLoader)tileLoader).clearCache();
    235199        }
    236200    }
    237201
     
    415379
    416380        currentZoomLevel = getBestZoom();
    417381
    418         tileCache = new MemoryTileCache();
    419 
    420         tileLoader = loaderFactory.makeTileLoader(this);
    421         if (tileLoader == null) {
    422             tileLoader = new OsmTileLoader(this);
    423         }
    424         tileLoader.timeoutConnect = Main.pref.getInteger("socket.timeout.connect",15) * 1000;
    425         tileLoader.timeoutRead = Main.pref.getInteger("socket.timeout.read", 30) * 1000;
     382        Map<String, String> headers = null;
    426383        if (tileSource instanceof TemplatedTMSTileSource) {
    427             for(Entry<String, String> e : ((TemplatedTMSTileSource)tileSource).getHeaders().entrySet()) {
    428                 tileLoader.headers.put(e.getKey(), e.getValue());
    429             }
     384            headers = (((TemplatedTMSTileSource)tileSource).getHeaders());
    430385        }
    431         tileLoader.headers.put("User-Agent", Version.getInstance().getFullAgentString());
    432     }
    433386
    434     @Override
    435     public void setOffset(double dx, double dy) {
    436         super.setOffset(dx, dy);
    437         needRedraw = true;
     387        tileCache = new MemoryTileCache();
     388        tileLoader = loaderFactory.makeTileLoader(this, headers);
     389        if (tileLoader == null)
     390            tileLoader = new OsmTileLoader(this);
    438391    }
    439392
    440393    /**
     
    471424        return intResult;
    472425    }
    473426
    474     /**
    475      * Function to set the maximum number of workers for tile loading to the value defined
    476      * in preferences.
    477      */
    478     public static void setMaxWorkers() {
    479         JobDispatcher.setMaxWorkers(PROP_TMS_JOBS.get());
    480         JobDispatcher.getInstance().setLIFO(true);
    481     }
    482 
    483427    @SuppressWarnings("serial")
    484428    public TMSLayer(ImageryInfo info) {
    485429        super(info);
    486430
    487         setMaxWorkers();
    488431        if(!isProjectionSupported(Main.getProjection())) {
    489432            JOptionPane.showMessageDialog(Main.parent,
    490                 tr("TMS layers do not support the projection {0}.\n{1}\n"
    491                 + "Change the projection or remove the layer.",
    492                 Main.getProjection().toCode(), nameSupportedProjections()),
    493                 tr("Warning"),
    494                 JOptionPane.WARNING_MESSAGE);
     433                    tr("TMS layers do not support the projection {0}.\n{1}\n"
     434                            + "Change the projection or remove the layer.",
     435                            Main.getProjection().toCode(), nameSupportedProjections()),
     436                            tr("Warning"),
     437                            JOptionPane.WARNING_MESSAGE);
    495438        }
    496439
    497440        setBackgroundLayer(true);
     
    684627            Main.debug("zoomChanged(): " + currentZoomLevel);
    685628        }
    686629        needRedraw = true;
    687         JobDispatcher.getInstance().cancelOutstandingJobs();
    688         tileRequestsOutstanding.clear();
    689630    }
    690631
    691632    int getMaxZoomLvl() {
     
    770711     * are temporary only and intentionally not inserted
    771712     * into the tileCache.
    772713     */
    773     synchronized Tile tempCornerTile(Tile t) {
     714    Tile tempCornerTile(Tile t) {
    774715        int x = t.getXtile() + 1;
    775716        int y = t.getYtile() + 1;
    776717        int zoom = t.getZoom();
     
    780721        return new Tile(tileSource, x, y, zoom);
    781722    }
    782723
    783     synchronized Tile getOrCreateTile(int x, int y, int zoom) {
     724    Tile getOrCreateTile(int x, int y, int zoom) {
    784725        Tile tile = getTile(x, y, zoom);
    785726        if (tile == null) {
    786727            tile = new Tile(tileSource, x, y, zoom);
     
    794735     * This can and will return null for tiles that are not
    795736     * already in the cache.
    796737     */
    797     synchronized Tile getTile(int x, int y, int zoom) {
     738    Tile getTile(int x, int y, int zoom) {
    798739        int max = (1 << zoom);
    799740        if (x < 0 || x >= max || y < 0 || y >= max)
    800741            return null;
    801742        return tileCache.getTile(tileSource, x, y, zoom);
    802743    }
    803744
    804     synchronized boolean loadTile(Tile tile, boolean force) {
     745    boolean loadTile(Tile tile, boolean force) {
    805746        if (tile == null)
    806747            return false;
    807         if (!force && (tile.hasError() || tile.isLoaded()))
     748        if (!force && (tile.isLoaded() || tile.hasError()))
    808749            return false;
    809750        if (tile.isLoading())
    810751            return false;
    811         if (tileRequestsOutstanding.contains(tile))
    812             return false;
    813         tileRequestsOutstanding.add(tile);
    814         JobDispatcher.getInstance().addJob(tileLoader.createTileLoaderJob(tile));
     752        tileLoader.createTileLoaderJob(tile).submit();
    815753        return true;
    816754    }
    817755
     
    12681206        public TileSet getTileSet(int zoom) {
    12691207            if (zoom < minZoom)
    12701208                return nullTileSet;
    1271             TileSet ts = tileSets[zoom-minZoom];
    1272             if (ts == null) {
    1273                 ts = new TileSet(topLeft, botRight, zoom);
    1274                 tileSets[zoom-minZoom] = ts;
     1209            synchronized (tileSets) {
     1210                TileSet ts = tileSets[zoom-minZoom];
     1211                if (ts == null) {
     1212                    ts = new TileSet(topLeft, botRight, zoom);
     1213                    tileSets[zoom-minZoom] = ts;
     1214                }
     1215                return ts;
    12751216            }
    1276             return ts;
    12771217        }
     1218
    12781219        public TileSetInfo getTileSetInfo(int zoom) {
    12791220            if (zoom < minZoom)
    12801221                return new TileSetInfo();
    1281             TileSetInfo tsi = tileSetInfos[zoom-minZoom];
    1282             if (tsi == null) {
    1283                 tsi = TMSLayer.getTileSetInfo(getTileSet(zoom));
    1284                 tileSetInfos[zoom-minZoom] = tsi;
     1222            synchronized (tileSetInfos) {
     1223                TileSetInfo tsi = tileSetInfos[zoom-minZoom];
     1224                if (tsi == null) {
     1225                    tsi = TMSLayer.getTileSetInfo(getTileSet(zoom));
     1226                    tileSetInfos[zoom-minZoom] = tsi;
     1227                }
     1228                return tsi;
    12851229            }
    1286             return tsi;
    12871230        }
    12881231    }
    12891232
    12901233    @Override
    12911234    public void paint(Graphics2D g, MapView mv, Bounds bounds) {
    1292         //long start = System.currentTimeMillis();
    12931235        EastNorth topLeft = mv.getEastNorth(0, 0);
    12941236        EastNorth botRight = mv.getEastNorth(mv.getWidth(), mv.getHeight());
    12951237
  • src/org/openstreetmap/josm/gui/bbox/SlippyMapBBoxChooser.java

     
    1111import java.util.ArrayList;
    1212import java.util.Arrays;
    1313import java.util.Collections;
     14import java.util.HashMap;
    1415import java.util.HashSet;
    1516import java.util.List;
     17import java.util.Map;
    1618import java.util.Set;
    1719import java.util.concurrent.CopyOnWriteArrayList;
    1820
     
    2527import org.openstreetmap.gui.jmapviewer.MemoryTileCache;
    2628import org.openstreetmap.gui.jmapviewer.OsmTileLoader;
    2729import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
     30import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
    2831import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
    2932import org.openstreetmap.gui.jmapviewer.tilesources.MapQuestOpenAerialTileSource;
    3033import org.openstreetmap.gui.jmapviewer.tilesources.MapQuestOsmTileSource;
     
    113116    private static final StringProperty PROP_MAPSTYLE = new StringProperty("slippy_map_chooser.mapstyle", "Mapnik");
    114117    public static final String RESIZE_PROP = SlippyMapBBoxChooser.class.getName() + ".resize";
    115118
    116     private OsmTileLoader cachedLoader;
     119    private TileLoader cachedLoader;
    117120    private OsmTileLoader uncachedLoader;
    118121
    119122    private final SizeButton iSizeButton;
     
    131134        debug = Main.isDebugEnabled();
    132135        SpringLayout springLayout = new SpringLayout();
    133136        setLayout(springLayout);
    134         TMSLayer.setMaxWorkers();
    135         cachedLoader = TMSLayer.loaderFactory.makeTileLoader(this);
     137
     138        Map<String, String> headers = new HashMap<>();
     139        headers.put("User-Agent", Version.getInstance().getFullAgentString());
     140
     141        cachedLoader = TMSLayer.loaderFactory.makeTileLoader(this, headers);
    136142
    137143        uncachedLoader = new OsmTileLoader(this);
    138         uncachedLoader.headers.put("User-Agent", Version.getInstance().getFullAgentString());
     144        uncachedLoader.headers.putAll(headers);
    139145        setZoomContolsVisible(Main.pref.getBoolean("slippy_map_chooser.zoomcontrols",false));
    140146        setMapMarkerVisible(false);
    141147        setMinimumSize(new Dimension(350, 350 / 2));
  • src/org/openstreetmap/gui/jmapviewer/TMSFileCacheTileLoader.java

     
    1 // License: GPL. For details, see Readme.txt file.
    2 package org.openstreetmap.gui.jmapviewer;
    3 
    4 import java.io.File;
    5 import java.io.IOException;
    6 import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
    7 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
    8 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
    9 
    10 /**
    11  * Reworked version of the OsmFileCacheTileLoader.
    12  *
    13  * When class OsmFileCacheTileLoader is no longer needed, it can be integrated
    14  * here and removed.
    15  */
    16 public class TMSFileCacheTileLoader extends OsmFileCacheTileLoader {
    17 
    18     public TMSFileCacheTileLoader(TileLoaderListener map, File cacheDir) throws IOException {
    19         super(map, cacheDir);
    20     }
    21 
    22     @Override
    23     public TileJob createTileLoaderJob(final Tile tile) {
    24         return new TMSFileLoadJob(tile);
    25     }
    26 
    27     protected class TMSFileLoadJob extends FileLoadJob {
    28 
    29         public TMSFileLoadJob(Tile tile) {
    30             super(tile);
    31         }
    32 
    33         @Override
    34         protected File getTileFile() {
    35             return getDataFile(tile.getSource().getTileType());
    36         }
    37 
    38         @Override
    39         protected File getTagsFile() {
    40             return getDataFile(TAGS_FILE_EXT);
    41         }
    42 
    43         protected File getDataFile(String ext) {
    44             int nDigits = (int) Math.ceil(Math.log10(1 << tile.getZoom()));
    45             String x = String.format("%0" + nDigits + "d", tile.getXtile());
    46             String y = String.format("%0" + nDigits + "d", tile.getYtile());
    47             File path = new File(tileCacheDir, "z" + tile.getZoom());
    48             for (int i=0; i<nDigits; i++) {
    49                 String component = "x" + x.substring(i, i+1) + "y" + y.substring(i, i+1);
    50                 if (i == nDigits -1 ) {
    51                     component += "." + ext;
    52                 }
    53                 path = new File(path, component);
    54             }
    55             return path;
    56         }
    57     }
    58 
    59     @Override
    60     protected File getSourceCacheDir(TileSource source) {
    61         File dir = sourceCacheDirMap.get(source);
    62         if (dir == null) {
    63             String id = source.getId();
    64             if (id != null) {
    65                 dir = new File(cacheDirBase, id);
    66             } else {
    67                 dir = new File(cacheDirBase, source.getName().replaceAll("[\\\\/:*?\"<>|]", "_"));
    68             }
    69             if (!dir.exists()) {
    70                 dir.mkdirs();
    71             }
    72         }
    73         return dir;
    74     }
    75 
    76 }
  • src/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.java

     
    1 // License: GPL. For details, see Readme.txt file.
    2 package org.openstreetmap.gui.jmapviewer;
    3 
    4 import java.io.BufferedReader;
    5 import java.io.ByteArrayInputStream;
    6 import java.io.ByteArrayOutputStream;
    7 import java.io.File;
    8 import java.io.FileInputStream;
    9 import java.io.FileNotFoundException;
    10 import java.io.FileOutputStream;
    11 import java.io.IOException;
    12 import java.io.InputStream;
    13 import java.io.InputStreamReader;
    14 import java.io.OutputStreamWriter;
    15 import java.io.PrintWriter;
    16 import java.net.HttpURLConnection;
    17 import java.net.URL;
    18 import java.net.URLConnection;
    19 import java.nio.charset.Charset;
    20 import java.util.HashMap;
    21 import java.util.Map;
    22 import java.util.Map.Entry;
    23 import java.util.Random;
    24 import java.util.logging.Level;
    25 import java.util.logging.Logger;
    26 
    27 import org.openstreetmap.gui.jmapviewer.interfaces.CachedTileLoader;
    28 import org.openstreetmap.gui.jmapviewer.interfaces.TileClearController;
    29 import org.openstreetmap.gui.jmapviewer.interfaces.TileJob;
    30 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
    31 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
    32 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
    33 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource.TileUpdate;
    34 
    35 /**
    36  * A {@link TileLoader} implementation that loads tiles from OSM via HTTP and
    37  * saves all loaded files in a directory located in the temporary directory.
    38  * If a tile is present in this file cache it will not be loaded from OSM again.
    39  *
    40  * @author Jan Peter Stotz
    41  * @author Stefan Zeller
    42  */
    43 public class OsmFileCacheTileLoader extends OsmTileLoader implements CachedTileLoader {
    44 
    45     private static final Logger log = FeatureAdapter.getLogger(OsmFileCacheTileLoader.class.getName());
    46 
    47     protected static final String TAGS_FILE_EXT = "tags";
    48 
    49     private static final Charset TAGS_CHARSET = Charset.forName("UTF-8");
    50 
    51     // Default expire time (i.e. maximum age of cached tile before refresh).
    52     // Used when the server does not send an expires or max-age value in the http header.
    53     protected static final long DEFAULT_EXPIRE_TIME = 1000L * 60 * 60 * 24 * 7; // 7 days
    54     // Limit for the max-age value send by the server.
    55     protected static final long EXPIRE_TIME_SERVER_LIMIT = 1000L * 60 * 60 * 24 * 28; // 4 weeks
    56     // Absolute expire time limit. Cached tiles that are older will not be used,
    57     // even if the refresh from the server fails.
    58     protected static final long ABSOLUTE_EXPIRE_TIME_LIMIT = Long.MAX_VALUE; // unlimited
    59 
    60     protected String cacheDirBase;
    61 
    62     protected final Map<TileSource, File> sourceCacheDirMap;
    63 
    64 
    65     public static File getDefaultCacheDir() throws SecurityException {
    66         String tempDir = null;
    67         String userName = System.getProperty("user.name");
    68         try {
    69             tempDir = System.getProperty("java.io.tmpdir");
    70         } catch (SecurityException e) {
    71             log.log(Level.WARNING,
    72                     "Failed to access system property ''java.io.tmpdir'' for security reasons. Exception was: "
    73                         + e.toString());
    74             throw e; // rethrow
    75         }
    76         try {
    77             if (tempDir == null)
    78                 throw new IOException("No temp directory set");
    79             String subDirName = "JMapViewerTiles";
    80             // On Linux/Unix systems we do not have a per user tmp directory.
    81             // Therefore we add the user name for getting a unique dir name.
    82             if (userName != null && userName.length() > 0) {
    83                 subDirName += "_" + userName;
    84             }
    85             File cacheDir = new File(tempDir, subDirName);
    86             return cacheDir;
    87         } catch (Exception e) {
    88         }
    89         return null;
    90     }
    91 
    92     /**
    93      * Create a OSMFileCacheTileLoader with given cache directory.
    94      * If cacheDir is not set or invalid, IOException will be thrown.
    95      * @param map the listener checking for tile load events (usually the map for display)
    96      * @param cacheDir directory to store cached tiles
    97      */
    98     public OsmFileCacheTileLoader(TileLoaderListener map, File cacheDir) throws IOException  {
    99         super(map);
    100         if (cacheDir == null || (!cacheDir.exists() && !cacheDir.mkdirs()))
    101             throw new IOException("Cannot access cache directory");
    102 
    103         log.finest("Tile cache directory: " + cacheDir);
    104         cacheDirBase = cacheDir.getAbsolutePath();
    105         sourceCacheDirMap = new HashMap<>();
    106     }
    107 
    108     /**
    109      * Create a OSMFileCacheTileLoader with system property temp dir.
    110      * If not set an IOException will be thrown.
    111      * @param map the listener checking for tile load events (usually the map for display)
    112      */
    113     public OsmFileCacheTileLoader(TileLoaderListener map) throws SecurityException, IOException {
    114         this(map, getDefaultCacheDir());
    115     }
    116 
    117     @Override
    118     public TileJob createTileLoaderJob(final Tile tile) {
    119         return new FileLoadJob(tile);
    120     }
    121 
    122     protected File getSourceCacheDir(TileSource source) {
    123         File dir = sourceCacheDirMap.get(source);
    124         if (dir == null) {
    125             dir = new File(cacheDirBase, source.getName().replaceAll("[\\\\/:*?\"<>|]", "_"));
    126             if (!dir.exists()) {
    127                 dir.mkdirs();
    128             }
    129         }
    130         return dir;
    131     }
    132 
    133     protected class FileLoadJob implements TileJob {
    134         InputStream input = null;
    135 
    136         Tile tile;
    137         File tileCacheDir;
    138         File tileFile = null;
    139         File tagsFile = null;
    140         Long fileMtime = null;
    141         Long now = null; // current time in milliseconds (keep consistent value for the whole run)
    142 
    143         public FileLoadJob(Tile tile) {
    144             this.tile = tile;
    145         }
    146 
    147         @Override
    148         public Tile getTile() {
    149             return tile;
    150         }
    151 
    152         @Override
    153         public void run() {
    154             synchronized (tile) {
    155                 if ((tile.isLoaded() && !tile.hasError()) || tile.isLoading())
    156                     return;
    157                 tile.loaded = false;
    158                 tile.error = false;
    159                 tile.loading = true;
    160             }
    161             now = System.currentTimeMillis();
    162             tileCacheDir = getSourceCacheDir(tile.getSource());
    163             tileFile = getTileFile();
    164             tagsFile = getTagsFile();
    165 
    166             loadTagsFromFile();
    167 
    168             if (isCacheValid() && (isNoTileAtZoom() || loadTileFromFile())) {
    169                 log.log(Level.FINE, "TMS - found in tile cache: {0}", tile);
    170                 tile.setLoaded(true);
    171                 listener.tileLoadingFinished(tile, true);
    172                 return;
    173             }
    174 
    175             TileJob job = new TileJob() {
    176 
    177                 @Override
    178                 public void run() {
    179                     if (loadOrUpdateTile()) {
    180                         tile.setLoaded(true);
    181                         listener.tileLoadingFinished(tile, true);
    182                     } else {
    183                         // failed to download - use old cache file if available
    184                         if (isNoTileAtZoom() || loadTileFromFile()) {
    185                             tile.setLoaded(true);
    186                             tile.error = false;
    187                             listener.tileLoadingFinished(tile, true);
    188                             log.log(Level.FINE, "TMS - found stale tile in cache: {0}", tile);
    189                         } else {
    190                             // failed completely
    191                             tile.setLoaded(true);
    192                             listener.tileLoadingFinished(tile, false);
    193                         }
    194                     }
    195                 }
    196                 @Override
    197                 public Tile getTile() {
    198                     return tile;
    199                 }
    200             };
    201             JobDispatcher.getInstance().addJob(job);
    202         }
    203 
    204         protected boolean loadOrUpdateTile() {
    205             try {
    206                 URLConnection urlConn = loadTileFromOsm(tile);
    207                 if (fileMtime != null && now - fileMtime <= ABSOLUTE_EXPIRE_TIME_LIMIT) {
    208                     switch (tile.getSource().getTileUpdate()) {
    209                     case IfModifiedSince:
    210                         urlConn.setIfModifiedSince(fileMtime);
    211                         break;
    212                     case LastModified:
    213                         if (!isOsmTileNewer(fileMtime)) {
    214                             log.log(Level.FINE, "TMS - LastModified test: local version is up to date: {0}", tile);
    215                             tileFile.setLastModified(now);
    216                             return true;
    217                         }
    218                         break;
    219                     default:
    220                         break;
    221                     }
    222                 }
    223                 if (tile.getSource().getTileUpdate() == TileUpdate.ETag || tile.getSource().getTileUpdate() == TileUpdate.IfNoneMatch) {
    224                     String fileETag = tile.getValue("etag");
    225                     if (fileETag != null) {
    226                         switch (tile.getSource().getTileUpdate()) {
    227                         case IfNoneMatch:
    228                             urlConn.addRequestProperty("If-None-Match", fileETag);
    229                             break;
    230                         case ETag:
    231                             if (hasOsmTileETag(fileETag)) {
    232                                 log.log(Level.FINE, "TMS - ETag test: local version is up to date: {0}", tile);
    233                                 tileFile.setLastModified(now);
    234                                 return true;
    235                             }
    236                         default:
    237                             break;
    238                         }
    239                     }
    240                     tile.putValue("etag", urlConn.getHeaderField("ETag"));
    241                 }
    242                 if (urlConn instanceof HttpURLConnection && ((HttpURLConnection)urlConn).getResponseCode() == 304) {
    243                     // If isModifiedSince or If-None-Match has been set
    244                     // and the server answers with a HTTP 304 = "Not Modified"
    245                     switch (tile.getSource().getTileUpdate()) {
    246                     case IfModifiedSince:
    247                         log.log(Level.FINE, "TMS - IfModifiedSince test: local version is up to date: {0}", tile);
    248                         break;
    249                     case IfNoneMatch:
    250                         log.log(Level.FINE, "TMS - IfNoneMatch test: local version is up to date: {0}", tile);
    251                         break;
    252                     default:
    253                         break;
    254                     }
    255                     loadTileFromFile();
    256                     tileFile.setLastModified(now);
    257                     return true;
    258                 }
    259 
    260                 loadTileMetadata(tile, urlConn);
    261                 saveTagsToFile();
    262 
    263                 if ("no-tile".equals(tile.getValue("tile-info")))
    264                 {
    265                     log.log(Level.FINE, "TMS - No tile: tile-info=no-tile: {0}", tile);
    266                     tile.setError("No tile at this zoom level");
    267                     return true;
    268                 } else {
    269                     for (int i = 0; i < 5; ++i) {
    270                         if (urlConn instanceof HttpURLConnection && ((HttpURLConnection)urlConn).getResponseCode() == 503) {
    271                             Thread.sleep(5000+(new Random()).nextInt(5000));
    272                             continue;
    273                         }
    274                         byte[] buffer = loadTileInBuffer(urlConn);
    275                         if (buffer != null) {
    276                             tile.loadImage(new ByteArrayInputStream(buffer));
    277                             saveTileToFile(buffer);
    278                             log.log(Level.FINE, "TMS - downloaded tile from server: {0}", tile.getUrl());
    279                             return true;
    280                         }
    281                     }
    282                 }
    283             } catch (Exception e) {
    284                 tile.setError(e.getMessage());
    285                 if (input == null) {
    286                     try {
    287                         log.log(Level.WARNING, "TMS - Failed downloading {0}: {1}", new Object[]{tile.getUrl(), e.getMessage()});
    288                         return false;
    289                     } catch(IOException i) {
    290                     }
    291                 }
    292             }
    293             log.log(Level.WARNING, "TMS - Failed downloading tile: {0}", tile);
    294             return false;
    295         }
    296 
    297         protected boolean isCacheValid() {
    298             Long expires = null;
    299             if (tileFile.exists()) {
    300                 fileMtime = tileFile.lastModified();
    301             } else if (tagsFile.exists()) {
    302                 fileMtime = tagsFile.lastModified();
    303             } else
    304                 return false;
    305 
    306             try {
    307                 expires = Long.parseLong(tile.getValue("expires"));
    308             } catch (NumberFormatException e) {}
    309 
    310             // check by expire date set by server
    311             if (expires != null && !expires.equals(0L)) {
    312                 // put a limit to the expire time (some servers send a value
    313                 // that is too large)
    314                 expires = Math.min(expires, fileMtime + EXPIRE_TIME_SERVER_LIMIT);
    315                 if (now > expires) {
    316                     log.log(Level.FINE, "TMS - Tile has expired -> not valid {0}", tile);
    317                     return false;
    318                 }
    319             } else {
    320                 // check by file modification date
    321                 if (now - fileMtime > DEFAULT_EXPIRE_TIME) {
    322                     log.log(Level.FINE, "TMS - Tile has expired, maximum file age reached {0}", tile);
    323                     return false;
    324                 }
    325             }
    326             return true;
    327         }
    328 
    329         protected boolean isNoTileAtZoom() {
    330             if ("no-tile".equals(tile.getValue("tile-info"))) {
    331                 // do not remove file - keep the information, that there is no tile, for further requests
    332                 // the code above will check, if this information is still valid
    333                 log.log(Level.FINE, "TMS - Tile valid, but no file, as no tiles at this level {0}", tile);
    334                 tile.setError("No tile at this zoom level");
    335                 return true;
    336             }
    337             return false;
    338         }
    339 
    340         protected boolean loadTileFromFile() {
    341             if (!tileFile.exists())
    342                 return false;
    343 
    344             try (FileInputStream fin = new FileInputStream(tileFile)) {
    345                 if (fin.available() == 0)
    346                     throw new IOException("File empty");
    347                 tile.loadImage(fin);
    348                 return true;
    349             } catch (Exception e) {
    350                 log.log(Level.WARNING, "TMS - Error while loading image from tile cache: {0}; {1}", new Object[]{e.getMessage(), tile});
    351                 tileFile.delete();
    352                 if (tagsFile.exists()) {
    353                     tagsFile.delete();
    354                 }
    355                 tileFile = null;
    356                 fileMtime = null;
    357             }
    358             return false;
    359         }
    360 
    361         protected byte[] loadTileInBuffer(URLConnection urlConn) throws IOException {
    362             input = urlConn.getInputStream();
    363             try {
    364                 ByteArrayOutputStream bout = new ByteArrayOutputStream(input.available());
    365                 byte[] buffer = new byte[2048];
    366                 boolean finished = false;
    367                 do {
    368                     int read = input.read(buffer);
    369                     if (read >= 0) {
    370                         bout.write(buffer, 0, read);
    371                     } else {
    372                         finished = true;
    373                     }
    374                 } while (!finished);
    375                 if (bout.size() == 0)
    376                     return null;
    377                 return bout.toByteArray();
    378             } finally {
    379                 input.close();
    380                 input = null;
    381             }
    382         }
    383 
    384         /**
    385          * Performs a <code>HEAD</code> request for retrieving the
    386          * <code>LastModified</code> header value.
    387          *
    388          * Note: This does only work with servers providing the
    389          * <code>LastModified</code> header:
    390          * <ul>
    391          * <li>{@link org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource.CycleMap} - supported</li>
    392          * <li>{@link org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource.Mapnik} - not supported</li>
    393          * </ul>
    394          *
    395          * @param fileAge time of the
    396          * @return <code>true</code> if the tile on the server is newer than the
    397          *         file
    398          * @throws IOException
    399          */
    400         protected boolean isOsmTileNewer(long fileAge) throws IOException {
    401             URL url;
    402             url = new URL(tile.getUrl());
    403             HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    404             prepareHttpUrlConnection(urlConn);
    405             urlConn.setRequestMethod("HEAD");
    406             urlConn.setReadTimeout(30000); // 30 seconds read timeout
    407             // System.out.println("Tile age: " + new
    408             // Date(urlConn.getLastModified()) + " / "
    409             // + new Date(fileMtime));
    410             long lastModified = urlConn.getLastModified();
    411             if (lastModified == 0)
    412                 return true; // no LastModified time returned
    413             return (lastModified > fileAge);
    414         }
    415 
    416         protected boolean hasOsmTileETag(String eTag) throws IOException {
    417             URL url;
    418             url = new URL(tile.getUrl());
    419             HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    420             prepareHttpUrlConnection(urlConn);
    421             urlConn.setRequestMethod("HEAD");
    422             urlConn.setReadTimeout(30000); // 30 seconds read timeout
    423             // System.out.println("Tile age: " + new
    424             // Date(urlConn.getLastModified()) + " / "
    425             // + new Date(fileMtime));
    426             String osmETag = urlConn.getHeaderField("ETag");
    427             if (osmETag == null)
    428                 return true;
    429             return (osmETag.equals(eTag));
    430         }
    431 
    432         protected File getTileFile() {
    433             return new File(tileCacheDir + "/" + tile.getZoom() + "_" + tile.getXtile() + "_" + tile.getYtile() + "."
    434                     + tile.getSource().getTileType());
    435         }
    436 
    437         protected File getTagsFile() {
    438             return new File(tileCacheDir + "/" + tile.getZoom() + "_" + tile.getXtile() + "_" + tile.getYtile() + "."
    439                     + TAGS_FILE_EXT);
    440         }
    441 
    442         protected void saveTileToFile(byte[] rawData) {
    443             File file = getTileFile();
    444             file.getParentFile().mkdirs();
    445             try (FileOutputStream f = new FileOutputStream(file)) {
    446                 f.write(rawData);
    447             } catch (Exception e) {
    448                 log.log(Level.SEVERE, "Failed to save tile content: {0}", e.getLocalizedMessage());
    449             }
    450         }
    451 
    452         protected void saveTagsToFile() {
    453             File tagsFile = getTagsFile();
    454             tagsFile.getParentFile().mkdirs();
    455             if (tile.getMetadata() == null) {
    456                 tagsFile.delete();
    457                 return;
    458             }
    459             try (PrintWriter f = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tagsFile), TAGS_CHARSET))) {
    460                 for (Entry<String, String> entry : tile.getMetadata().entrySet()) {
    461                     f.println(entry.getKey() + "=" + entry.getValue());
    462                 }
    463             } catch (Exception e) {
    464                 System.err.println("Failed to save tile tags: " + e.getLocalizedMessage());
    465             }
    466         }
    467 
    468         protected boolean loadTagsFromFile() {
    469             File tagsFile = getTagsFile();
    470             try (BufferedReader f = new BufferedReader(new InputStreamReader(new FileInputStream(tagsFile), TAGS_CHARSET))) {
    471                 for (String line = f.readLine(); line != null; line = f.readLine()) {
    472                     final int i = line.indexOf('=');
    473                     if (i == -1 || i == 0) {
    474                         System.err.println("Malformed tile tag in file '" + tagsFile.getName() + "':" + line);
    475                         continue;
    476                     }
    477                     tile.putValue(line.substring(0,i),line.substring(i+1));
    478                 }
    479             } catch (FileNotFoundException e) {
    480             } catch (Exception e) {
    481                 System.err.println("Failed to load tile tags: " + e.getLocalizedMessage());
    482             }
    483 
    484             return true;
    485         }
    486     }
    487 
    488     public String getCacheDirBase() {
    489         return cacheDirBase;
    490     }
    491 
    492     public void setTileCacheDir(String tileCacheDir) {
    493         File dir = new File(tileCacheDir);
    494         dir.mkdirs();
    495         this.cacheDirBase = dir.getAbsolutePath();
    496     }
    497 
    498     @Override
    499     public void clearCache(TileSource source) {
    500         clearCache(source, null);
    501     }
    502 
    503     @Override
    504     public void clearCache(TileSource source, TileClearController controller) {
    505         File dir = getSourceCacheDir(source);
    506         if (dir != null) {
    507             if (controller != null) controller.initClearDir(dir);
    508             if (dir.isDirectory()) {
    509                 File[] files = dir.listFiles();
    510                 if (controller != null) controller.initClearFiles(files);
    511                 for (File file : files) {
    512                     if (controller != null && controller.cancel()) return;
    513                     file.delete();
    514                     if (controller != null) controller.fileDeleted(file);
    515                 }
    516             }
    517             dir.delete();
    518         }
    519         if (controller != null) controller.clearFinished();
    520     }
    521 }
  • src/org/openstreetmap/gui/jmapviewer/interfaces/TileJob.java

     
    1717     * @return {@link Tile} to be handled
    1818     */
    1919    public Tile getTile();
     20
     21    /**
     22     * submits download job to backend.
     23     */
     24    void submit();
    2025}
  • src/org/openstreetmap/gui/jmapviewer/interfaces/CachedTileLoader.java

     
    55 * Interface that allow cleaning the tile cache without specifying exact type of loader
    66 */
    77public interface CachedTileLoader {
    8     public void clearCache(TileSource source);
    9     public void clearCache(TileSource source, TileClearController controller);
     8    public void clearCache();
    109}
  • src/org/openstreetmap/gui/jmapviewer/Demo.java

     
    1010import java.awt.event.ItemListener;
    1111import java.awt.event.MouseAdapter;
    1212import java.awt.event.MouseEvent;
    13 import java.io.IOException;
    1413
    1514import javax.swing.JButton;
    1615import javax.swing.JCheckBox;
     
    101100            }
    102101        });
    103102        JComboBox<TileLoader> tileLoaderSelector;
    104         try {
    105             tileLoaderSelector = new JComboBox<>(new TileLoader[] { new OsmFileCacheTileLoader(map()), new OsmTileLoader(map()) });
    106         } catch (IOException e) {
    107             tileLoaderSelector = new JComboBox<>(new TileLoader[] { new OsmTileLoader(map()) });
    108         }
     103        tileLoaderSelector = new JComboBox<>(new TileLoader[] { new OsmTileLoader(map()) });
    109104        tileLoaderSelector.addItemListener(new ItemListener() {
    110105            public void itemStateChanged(ItemEvent e) {
    111106                map().setTileLoader((TileLoader) e.getItem());
  • src/org/openstreetmap/gui/jmapviewer/TileController.java

     
    4545            tile.loadPlaceholderFromCache(tileCache);
    4646        }
    4747        if (!tile.isLoaded()) {
    48             jobDispatcher.addJob(tileLoader.createTileLoaderJob(tile));
     48            tileLoader.createTileLoaderJob(tile).submit();
    4949        }
    5050        return tile;
    5151    }
  • src/org/openstreetmap/gui/jmapviewer/JMapViewer.java

     
    985985    }
    986986
    987987    public void tileLoadingFinished(Tile tile, boolean success) {
     988        tile.setLoaded(success);
    988989        repaint();
    989990    }
    990991
  • src/org/openstreetmap/gui/jmapviewer/OsmTileLoader.java

     
    8585            public Tile getTile() {
    8686                return tile;
    8787            }
     88
     89            @Override
     90            public void submit() {
     91                run();
     92
     93            }
    8894        };
    8995    }
    9096