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

File:
1 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        }
Note: See TracChangeset for help on using the changeset viewer.