Changeset 3695 in josm


Ignore:
Timestamp:
2010-12-03T19:11:39+01:00 (14 years ago)
Author:
bastiK
Message:

fixed #5692 - users of imagery plugin don't see bing menu entry, because it uses old cache file from wms plugin
(Now the preference key to store the age and location of cache file contains not only the url, but the destination directory as well. Previously it could happen, that 2 plugins access the same url and try to cache it in their private plugin folder, but both plugins will share a single cache file. This used to be ok, but in this case it is no good, simply because we want a fresh download when the new plugin is installed. On top of that, in case both plugins had different caching times, the sharing wouldn't respect that either.)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java

    r3321 r3695  
    2222 * Mirrors a file to a local file.
    2323 * <p>
    24  * The file mirrored is only downloaded if it has been more than one day since last download
     24 * The file mirrored is only downloaded if it has been more than 7 days since last download
    2525 */
    2626public class MirroredInputStream extends InputStream {
     
    4040    }
    4141
     42    /**
     43     * Get an inputstream from a given filename, url or internal resource.
     44     * @param name can be
     45     *  - relative or absolute file name
     46     *  - file:///SOME/FILE the same as above
     47     *  - resource://SOME/FILE file from the classpath (usually in the current *.jar)
     48     *  - http://... a url. It will be cached on disk.
     49     * @param destDir the destination directory for the cache file. only applies for urls.
     50     * @param maxTime the maximum age of the cache file (in seconds)
     51     * @throws IOException when the resource with the given name could not be retrieved
     52     */
    4253    public MirroredInputStream(String name, String destDir, long maxTime) throws IOException {
    4354        URL url;
     
    126137            if (!url.getProtocol().equals("file"))
    127138            {
    128                 String localPath = Main.pref.get("mirror." + url);
     139                String prefKey = getPrefKey(url, destDir);
     140                String localPath = Main.pref.get(prefKey);
    129141                if (localPath != null && localPath.length() > 0)
    130142                {
     
    135147                    }
    136148                }
    137                 Main.pref.put("mirror." + url, null);
     149                Main.pref.put(prefKey, null);
    138150            }
    139151        } catch (java.net.MalformedURLException e) {}
    140152    }
    141153
     154    /**
     155     * get preference key to store the location and age of the cached file.
     156     * 2 resources that point to the same url, but that are to be stored in different
     157     * directories will not share a cache file.
     158     */
     159    private static String getPrefKey(URL url, String destDir) {
     160        StringBuilder prefKey = new StringBuilder("mirror.");
     161        if (destDir != null) {
     162            String prefDir = Main.pref.getPreferencesDir();
     163            if (destDir.startsWith(prefDir)) {
     164                destDir = destDir.substring(prefDir.length());
     165            }
     166            prefKey.append(destDir);
     167            prefKey.append(".");
     168        }
     169        prefKey.append(url.toString());
     170        return prefKey.toString();
     171    }
     172
    142173    private File checkLocal(URL url, String destDir, long maxTime) throws IOException {
    143         String localPath = Main.pref.get("mirror." + url);
     174        String prefKey = getPrefKey(url, destDir);
     175        String localPath = Main.pref.get(prefKey);
    144176        File file = null;
    145177        if (localPath != null && localPath.length() > 0) {
     
    196228            file = new File(destDir, localPath);
    197229            destDirFile.renameTo(file);
    198             Main.pref.put("mirror." + url, System.currentTimeMillis() + ";" + file);
     230            Main.pref.put(prefKey, System.currentTimeMillis() + ";" + file);
    199231        }
    200232
Note: See TracChangeset for help on using the changeset viewer.