1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.concurrent.ConcurrentHashMap;
|
---|
7 |
|
---|
8 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
9 | import org.apache.commons.jcs.access.behavior.ICacheAccess;
|
---|
10 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
---|
11 | import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource;
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
|
---|
14 | import org.openstreetmap.josm.data.cache.JCSCacheManager;
|
---|
15 | import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
|
---|
16 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
17 | import org.openstreetmap.josm.data.imagery.TileLoaderFactory;
|
---|
18 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | *
|
---|
22 | * Class providing cache to other layers
|
---|
23 | *
|
---|
24 | * @author Wiktor Niesiobędzki
|
---|
25 | * @param <T> Tile Source class used by this Imagery Layer
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public abstract class AbstractCachedTileSourceLayer<T extends AbstractTMSTileSource> extends AbstractTileSourceLayer<T> {
|
---|
29 | /** loader factory responsible for loading tiles for all layers */
|
---|
30 | private static Map<String, TileLoaderFactory> loaderFactories = new ConcurrentHashMap<>();
|
---|
31 |
|
---|
32 | private static final String PREFERENCE_PREFIX = "imagery.cache.";
|
---|
33 |
|
---|
34 | private static volatile TileLoaderFactory loaderFactoryOverride;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * how many object on disk should be stored for TMS region in MB. 500 MB is default value
|
---|
38 | */
|
---|
39 | public static final IntegerProperty MAX_DISK_CACHE_SIZE = new IntegerProperty(PREFERENCE_PREFIX + "max_disk_size", 512);
|
---|
40 |
|
---|
41 | private ICacheAccess<String, BufferedImageCacheEntry> cache;
|
---|
42 | private volatile TileLoaderFactory loaderFactory;
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Creates an instance of class based on InageryInfo
|
---|
47 | *
|
---|
48 | * @param info ImageryInfo describing the layer
|
---|
49 | */
|
---|
50 | public AbstractCachedTileSourceLayer(ImageryInfo info) {
|
---|
51 | super(info);
|
---|
52 |
|
---|
53 | if (loaderFactoryOverride != null) {
|
---|
54 | loaderFactory = loaderFactoryOverride;
|
---|
55 | } else {
|
---|
56 | String key = this.getClass().getCanonicalName();
|
---|
57 | loaderFactory = loaderFactories.get(key);
|
---|
58 | if (loaderFactory == null) {
|
---|
59 | synchronized (AbstractCachedTileSourceLayer.class) {
|
---|
60 | // check again, maybe another thread initialized factory
|
---|
61 | loaderFactory = loaderFactories.get(key);
|
---|
62 | if (loaderFactory == null) {
|
---|
63 | loaderFactory = new CachedTileLoaderFactory(getCache(), getTileLoaderClass());
|
---|
64 | loaderFactories.put(key, loaderFactory);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | protected synchronized TileLoaderFactory getTileLoaderFactory() {
|
---|
73 | if (loaderFactory == null) {
|
---|
74 | loaderFactory = new CachedTileLoaderFactory(getCache(), getTileLoaderClass());
|
---|
75 | }
|
---|
76 | return loaderFactory;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * @return cache used by this layer
|
---|
81 | */
|
---|
82 | private synchronized ICacheAccess<String, BufferedImageCacheEntry> getCache() {
|
---|
83 | if (cache != null) {
|
---|
84 | return cache;
|
---|
85 | }
|
---|
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 | Main.warn(e);
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Plugins that wish to set custom tile loader should call this method
|
---|
101 | * @param newLoaderFactory that will be used to load tiles
|
---|
102 | */
|
---|
103 |
|
---|
104 | public static synchronized void setTileLoaderFactory(TileLoaderFactory newLoaderFactory) {
|
---|
105 | loaderFactoryOverride = newLoaderFactory;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Returns tile loader factory for cache region and specified TileLoader class
|
---|
110 | * @param name of the cache region
|
---|
111 | * @param klazz type of the TileLoader
|
---|
112 | * @return factory returning cached tile loaders using specified cache and TileLoaders
|
---|
113 | */
|
---|
114 | public static TileLoaderFactory getTileLoaderFactory(String name, Class<? extends TileLoader> klazz) {
|
---|
115 | return new CachedTileLoaderFactory(getCache(name), klazz);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * @param name of cache region
|
---|
120 | * @return cache configured object for specified cache region
|
---|
121 | */
|
---|
122 | public static CacheAccess<String, BufferedImageCacheEntry> getCache(String name) {
|
---|
123 | try {
|
---|
124 | return JCSCacheManager.getCache(name,
|
---|
125 | 0,
|
---|
126 | MAX_DISK_CACHE_SIZE.get() * 1024, // MAX_DISK_CACHE_SIZE is in MB, needs to by in sync with getDiskCacheSize
|
---|
127 | CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
|
---|
128 | } catch (IOException e) {
|
---|
129 | Main.warn(e);
|
---|
130 | return null;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected abstract Class<? extends TileLoader> getTileLoaderClass();
|
---|
135 |
|
---|
136 | protected int getDiskCacheSize() {
|
---|
137 | return MAX_DISK_CACHE_SIZE.get() * 1024;
|
---|
138 | }
|
---|
139 |
|
---|
140 | protected abstract String getCacheName();
|
---|
141 | }
|
---|