Changeset 13643 in josm for trunk


Ignore:
Timestamp:
2018-04-16T23:00:30+02:00 (6 years ago)
Author:
wiktorn
Message:

In case of any IOExceptions when creating disk cache return memory-only cache

Closes: #16193

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/JCSCacheManager.java

    r13204 r13643  
    101101
    102102    @SuppressWarnings("resource")
    103     private static void initialize() throws IOException {
     103    private static void initialize() {
    104104        File cacheDir = new File(Config.getDirs().getCacheDirectory(true), "jcs");
    105105
    106         if (!cacheDir.exists() && !cacheDir.mkdirs())
    107             throw new IOException("Cannot access cache directory");
    108 
    109         File cacheDirLockPath = new File(cacheDir, ".lock");
    110         if (!cacheDirLockPath.exists() && !cacheDirLockPath.createNewFile()) {
    111             Logging.warn("Cannot create cache dir lock file");
    112         }
    113         cacheDirLock = FileChannel.open(cacheDirLockPath.toPath(), StandardOpenOption.WRITE).tryLock();
    114 
    115         if (cacheDirLock == null)
    116             Logging.warn("Cannot lock cache directory. Will not use disk cache");
    117 
     106        if (!cacheDir.exists() && !cacheDir.mkdirs()) {
     107            Logging.warn("Cache directory " + cacheDir.toString() + " does not exists and could not create it");
     108        } else {
     109            File cacheDirLockPath = new File(cacheDir, ".lock");
     110            try {
     111                if (!cacheDirLockPath.exists() && !cacheDirLockPath.createNewFile()) {
     112                    Logging.warn("Cannot create cache dir lock file");
     113                }
     114                cacheDirLock = FileChannel.open(cacheDirLockPath.toPath(), StandardOpenOption.WRITE).tryLock();
     115
     116                if (cacheDirLock == null)
     117                    Logging.warn("Cannot lock cache directory. Will not use disk cache");
     118            } catch (IOException e) {
     119                Logging.warn("Cannot create cache dir \"" + cacheDirLockPath.toString() + "\" lock file: " + e.toString());
     120                Logging.warn("Will not use disk cache");
     121            }
     122        }
    118123        // this could be moved to external file
    119124        Properties props = new Properties();
     
    142147     * @param cacheName region name
    143148     * @return cache access object
    144      * @throws IOException if directory is not found
    145      */
    146     public static <K, V> CacheAccess<K, V> getCache(String cacheName) throws IOException {
     149     */
     150    public static <K, V> CacheAccess<K, V> getCache(String cacheName) {
    147151        return getCache(cacheName, DEFAULT_MAX_OBJECTS_IN_MEMORY.get().intValue(), 0, null);
    148152    }
     
    157161     * @param cachePath         path to disk cache. if null, no disk cache will be created
    158162     * @return cache access object
    159      * @throws IOException if directory is not found
    160      */
    161     public static <K, V> CacheAccess<K, V> getCache(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath)
    162             throws IOException {
     163     */
     164    public static <K, V> CacheAccess<K, V> getCache(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) {
    163165        if (cacheManager != null)
    164166            return getCacheInner(cacheName, maxMemoryObjects, maxDiskObjects, cachePath);
     
    172174
    173175    @SuppressWarnings("unchecked")
    174     private static <K, V> CacheAccess<K, V> getCacheInner(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath)
    175             throws IOException {
     176    private static <K, V> CacheAccess<K, V> getCacheInner(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) {
    176177        CompositeCache<K, V> cc = cacheManager.getCache(cacheName, getCacheAttributes(maxMemoryObjects));
    177178
     
    183184                            diskAttributes, cacheManager, null, new StandardSerializer())});
    184185                }
    185             } catch (IOException e) {
    186                 throw e;
    187             } catch (Exception e) { // NOPMD
    188                 throw new IOException(e);
     186            } catch (Exception e) {
     187                // in case any error in setting auxiliary cache, do not use disk cache at all - only memory
     188                cc.setAuxCaches(new AuxiliaryCache[0]);
    189189            }
    190190        }
  • trunk/src/org/openstreetmap/josm/gui/layer/AbstractCachedTileSourceLayer.java

    r12620 r13643  
    22package org.openstreetmap.josm.gui.layer;
    33
    4 import java.io.IOException;
    54import java.util.Map;
    65import java.util.concurrent.ConcurrentHashMap;
     
    1615import org.openstreetmap.josm.data.imagery.TileLoaderFactory;
    1716import org.openstreetmap.josm.data.preferences.IntegerProperty;
    18 import org.openstreetmap.josm.tools.Logging;
    1917
    2018/**
     
    8482            return cache;
    8583        }
    86         try {
    87             cache = JCSCacheManager.getCache(getCacheName(),
    88                     0,
    89                     getDiskCacheSize(),
    90                     CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
    91             return cache;
    92         } catch (IOException e) {
    93             Logging.warn(e);
    94             return null;
    95         }
     84        cache = JCSCacheManager.getCache(getCacheName(),
     85                0,
     86                getDiskCacheSize(),
     87                CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
     88        return cache;
    9689    }
    9790
     
    125118     */
    126119    public static CacheAccess<String, BufferedImageCacheEntry> getCache(String name) {
    127             try {
    128                 return JCSCacheManager.getCache(name,
    129                         0,
    130                         MAX_DISK_CACHE_SIZE.get() * 1024, // MAX_DISK_CACHE_SIZE is in MB, needs to by in sync with getDiskCacheSize
    131                         CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
    132             } catch (IOException e) {
    133                 Logging.warn(e);
    134                 return null;
    135             }
     120            return JCSCacheManager.getCache(name,
     121                    0,
     122                    MAX_DISK_CACHE_SIZE.get() * 1024, // MAX_DISK_CACHE_SIZE is in MB, needs to by in sync with getDiskCacheSize
     123                    CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
    136124    }
    137125
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ThumbsLoader.java

    r13127 r13643  
    6868    private void initCache() {
    6969        if (!cacheOff) {
    70             try {
    71                 cache = JCSCacheManager.getCache("geoimage-thumbnails", 0, 120,
    72                         Config.getDirs().getCacheDirectory(true).getPath() + File.separator + "geoimage-thumbnails");
    73             } catch (IOException e) {
    74                 Logging.warn("Failed to initialize cache for geoimage-thumbnails");
    75                 Logging.warn(e);
    76             }
     70            cache = JCSCacheManager.getCache("geoimage-thumbnails", 0, 120,
     71                    Config.getDirs().getCacheDirectory(true).getPath() + File.separator + "geoimage-thumbnails");
    7772        }
    7873    }
Note: See TracChangeset for help on using the changeset viewer.