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